Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-16-2011, 03:21 PM   #1
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
Can not access Global variable in C "expected expression before : token"

I have a simple code and I want to reach my global variable but I can't what is wrong in my code:
#include <stdio.h>

int i=4000; /* Global variable declaration*/
int main()
     int a=10, b=20; /* Local Variable */
     int i=100; /* Local Variable */
     printf("%d %d", a, b);
     printf("\nLocal i : %d",i); /* Accessing Local variable */
     printf("\nGlobal i : %d",::i); /* Accessing Global variable */

     return 0;
it gives me :
"expected expression before : token
printf("\nGlobal i : %d",::i); /* Accessing Global variable */
I am programming C in linux and my IDE is code blocks.
Thanks for any help.
Old 11-16-2011, 03:30 PM   #2
Senior Member
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Originally Posted by golden_boy615 View Post
     printf("\nLocal i : %d",i); /* Accessing Local variable */
     printf("\nGlobal i : %d",::i); /* Accessing Global variable */
the :: operator is C++ specific for addressing a member of a static instance of a class or a global object. It is not valid in plain C.
In C, there is no way of addressing a global variable if it's hidden (masked) by a local variable with the same name.

I haven't tried, but maybe you get where you want if you cheat the compiler by claiming that your source is C++ (that is, by selecting the C++ compiler for that source file explicitly).

[X] Doc CPU

Last edited by Doc CPU; 11-16-2011 at 04:11 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ - "snprintf" inside "for" doesn't work as expected. (int to char*) Repgahroll Programming 14 08-31-2010 09:27 AM
how can i create a global variable called "dogpatch" with a value of "woof" earthdog Linux - Newbie 9 12-15-2009 02:03 AM
C compiling error: expected expression before { token CoderMan Programming 3 03-31-2009 05:31 PM
Small C issue.. "expected expression before.." skuzye Programming 23 01-02-2009 11:34 AM
c++ - "index" is a global scope variable in <string>???? BrianK Programming 2 03-18-2008 09:05 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:42 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration