6

I am reading a book on the C language ('Mastering C'), and found the topic on scope resolution operator (::) on page 203, on Google Books here.

But when I run the following code sample (copied from the book), the C compiler gives me an error. I searched on the internet but I am unable to find any reference to a scope resolution operator in C.

#include <stdio.h>
int a = 50;
int main(void)
{
    int a =10;  
    printf("%d",a);
    printf("%d\n", ::a);        
    return 0;
}

So if I want to access a global variable then how could I do that from within the main() function ?

abligh
  • 24,573
  • 4
  • 47
  • 84
munjal007
  • 245
  • 1
  • 2
  • 6
  • 7
    Maybe you're reading a book on _C++_? – ForceBru Feb 03 '15 at 13:37
  • @al-Acme: The second hit is this question... – Thomas Padron-McCarthy Feb 03 '15 at 13:41
  • @ThomasPadron-McCarthy - Look again. :D – Sadique Feb 03 '15 at 13:41
  • 1
    @al-Acme: that's a Wikipedia page that explains the scope resolution operator in 3 languages (including C++). It doesn't say it does not exist in C, or for that matter any other language. Indeed it says *'Other languages ... feature both scope resolution and method access'* - i.e. it leaves open the question of whether it exists in C. – abligh Feb 03 '15 at 13:45
  • 1
    @munjal007: feel free to name and shame the book if it is indeed meant to be a C book. – abligh Feb 03 '15 at 13:48
  • @abligh - So it also leaves the question: Is there a scope resolution operator in `Java`/`Javascript`/`Python`/ `Blah`... anyways SO gives away `10-15` votes on these days on such simple questions while answers requiring effort get nothing. – Sadique Feb 03 '15 at 13:49
  • @al-Acme indeed it does. For instance there is one in perl and that's not listed. My point is that its absence in that page doesn't answer the OP's question. – abligh Feb 03 '15 at 13:51
  • 1
    Not to mention a [duplicate question](http://stackoverflow.com/questions/21201788/does-c-supports-scope-resolution). – Sadique Feb 03 '15 at 13:51
  • 1
    @abligh My point is that this question does not deserve 3 votes and the answer does not deserve 9 upvotes (not that i have anything against you - you did did not do anything wrong by posting the right answer) `For instance there is one in perl and that's not listed` - my point was a simple google search was required for this. – Sadique Feb 03 '15 at 13:53
  • 1
    This question should get more votes but only if the reference of author and title of the book is included. And hopefully then people come here to know an example of a book *not* to read to learn C. – Brandin Feb 03 '15 at 15:05

7 Answers7

17

No. C does not have a scope resolution operator. C++ has one (::). Perhaps you are (or your book is) confusing C with C++.

You asked how you could access the global variable a from within a function (here main) which has its own local variable a. You can't do this in C. It is lexically out of scope. Of course you could take the address of the variable somewhere else and pass that in as a pointer, but that's a different thing entirely. Just rename the variable, i.e. 'don't do that'

abligh
  • 24,573
  • 4
  • 47
  • 84
  • 5
    _`Perhaps your book is confusing C with C++`_ I hope the reader is, and not the book. – Iharob Al Asimi Feb 03 '15 at 13:38
  • @iharob: I make no assumptions :-) I've seen awful books. I'll edit it though. – abligh Feb 03 '15 at 13:42
  • @iharob : the book name is mastering in C. , The questions in that book is good , but the writer did a little mistake. – munjal007 Feb 03 '15 at 14:13
  • 2
    @munjal007 That is not a little mistake, a typo is. But the operator does not exist in c, the concept of a namespace doesn't exist in c either. I found the error on page 64 operators precedence table. – Iharob Al Asimi Feb 03 '15 at 14:24
  • @iharob page 203 of that book (http://books.google.co.uk/books?id=19F8byBjFJ8C&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage&q=scope&f=false) tells you how to use the mysterious non-existent C scope operator. Perhaps there is some (non-standard) C variant that supports it after all? – abligh Feb 03 '15 at 14:40
  • I am surprised the author didn't mention the [goes to](http://stackoverflow.com/q/1642028/1983495) operator. – Iharob Al Asimi Feb 03 '15 at 15:03
  • 4
    @abligh I think the book is probably just not a good reference. If I were reading it I would also be confused. Look at their version and explanation of Hello world on pages 18,19. ``The line `void main()` marks the beginning of the program that gets executed when the program is run. Some traditional compilers may report an error on this line. In such cases, remove the keyword void, chainging the line to `main()`.`` --- funny, I always thought it was int main. – Brandin Feb 03 '15 at 15:03
8

No, namespaces are a feature of C++.

It is, however, possible to refer to global a in your example. You can achieve this by using the extern keyword:

#include <stdio.h>

int a = 50;

int main(void)
{
    int a = 10;
    printf("%d\n",a);

    { // Note the scope
        extern int a; // Uses the global now
        printf("%d\n", a);
    }

    return 0;
}

That's a bit tricky, though. It's bad style. Don't do that.

rubikonx9
  • 1,403
  • 15
  • 27
5

:: operator is available in C++ not C. If you wanted to access the global variable, use

#include <stdio.h>
int a = 50;
int main(void)
{
    int a =10;
    printf("%d",a); //prints 10
    {
        extern int a;
        printf("%d", a); //prints 50
    }
    return 0;
}

Or you could use a pointer which holds the address of the global variable a and then dereference the pointer if you want to print the value of the global variable a.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
3

No (the :: operator is C++ specific). In C, if you use the same identifier in different overlapping scopes (say, file scope a and block scope a), the block scope identifier shadows the file scope identifier and there is no way to refer to the shadowed identifier.

It is generally best programming practice to avoid shadowed variables. Many lint type programs can warn about this situation.

Jens
  • 69,818
  • 15
  • 125
  • 179
0

In plain C, there is no scope resolution. You have to name your variables differently.

That means that all variables a below are different ones:

#include <stdio.h>
int a = 50;
int main(void)
{
    int a = 10;
    {
        int a = 20;
        int i;
        for (i = 0; i < 10; i++) {
            int a = 30;
        }
    }
    return 0;
}
Karel Kubat
  • 1,635
  • 11
  • 12
0

You may use pointers to access and edit global variables in C.

#include <stdio.h>
#include <stdlib.h>

int a;
a=78;
int *ptr=&a;            //pointer for global a
int main()
{
    int a=0;
    printf("%d",a);         //Prints 0 as local variable
    printf("%d",*ptr);
    ptr=30;      //changes the value of global variable through pointer
    printf("%d",*ptr);      //Now it prints 30
    return 0;
}
Rishabh Ryber
  • 446
  • 1
  • 7
  • 21
-1

It entirely depends on what kind of compiler you're using to execute your code.

#include <stdio.h>
int a = 50;
int main(void)
{
   int a =10;  
   printf("%d\n",a);
   printf("%d\n", ::a);        
   return 0;
}

Try running this code in Turbo C compiler, and you will get the results.

Now, regarding the C book that you're following, the examples codes present in the book must be in terms with Turbo C/C++ compiler and not the gcc compiler.