471,093 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,093 software developers and data experts.

scope resolution operator???????

hi ,

can we use scope resolution operator (::) in C???

sushant
Nov 14 '05 #1
16 1666
sushant wrote:

hi ,

can we use scope resolution operator (::) in C???


No, that is a c++ specific kludge.

Erik

--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"To me C++ seems to be a language that has sacrificed orthogonality
and elegance for random expediency." -- Meilir Page-Jones
Nov 14 '05 #2
sushant wrote:
can we use scope resolution operator (::) in C???


The only reason to do so would be to access some member of a namespace
or class - neither of which C has. What application did you have in mind?
--
Derrick Coetzee
Nov 14 '05 #3
th********@rediffmail.com (sushant) writes:
can we use scope resolution operator (::) in C???


No.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
sushant wrote:

hi ,

can we use scope resolution operator (::) in C???


No, C doesn't have that operator.
Nov 14 '05 #5
i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?
Derrick Coetzee wrote:
sushant wrote:
can we use scope resolution operator (::) in C???
The only reason to do so would be to access some member of a

namespace or class - neither of which C has. What application did you have in mind? --
Derrick Coetzee


Nov 14 '05 #6
Derrick Coetzee wrote:
can we use scope resolution operator (::) in C???


The only reason to do so would be to access some member of a namespace
or class - neither of which C has.


It can also be used to access a hidden global name from a local scope.
Theoretically, for this very purpose '::' could be useful in C as well.

I'm not saying though that this is enough to justify the need for '::'
operator in C. It is not.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #7
th********@rediffmail.com writes:
i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}


main()
{
int x = 10;
{
extern int x;
printf ("%d", x);
}
...
}
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
Nov 14 '05 #8
th********@rediffmail.com wrote:
i want to access the value of global variable inside the main function and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?

NO.

Whenever there's a name clash between a local and global variable the
local variable name and value is recognized.
AFAIK there's isn't any way to access global variable with name clashes
with a local vairable. I knew this so I haven't ever used same name in
local scope, my company's coding guidelines suggest we don't do that ;)
, so I never tried accessing the global variable, so I'm not aware even
it exists.

HTH.
Regards,
Taran

Nov 14 '05 #9
th********@rediffmail.com wrote:
i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..


Ah, I didn't think of this. Another reply indicated one way of doing
this. However, this is purely academic, since if this situation ever
arose in practice you should rename one of your variables right away. In
short: don't ever give a global a name that might be reasonably used by
a local variable. If this results in large variable names, you can use
the #define/#undef trick:

int num_furry_bunny_suits = 2;

#define suits num_furry_bunny_suits
void foo(int i) {
suits += i;
return suits;
}
#undef suits

Cleaner but (sometimes) less efficient is a local pointer:

void foo(int i) {
int* suits = &num_furry_bunny_suits;
*suits += i;
return *suits;
}

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #10
th********@rediffmail.com wrote:

i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?


You can't. In fact the above code should be considered a potential
cause of bugs. If you are lucky enough to be using the GNU C compiler
(gcc) you can avoid these bugs by compiling with the -Wshadow
compiler flag. Add -Werror will turn warnings into errors.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"C++ has its place in the history of programming languages. Just as
Caligula has his place in the history of the Roman Empire."
-- Robert Firth
Nov 14 '05 #11
th********@rediffmail.com writes:
i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?


The best way to do it is to change the name.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #12
Ben Pfaff wrote:
th********@rediffmail.com writes:
i want to access the value of global variable inside the main
function and inside the main func the var with the same name is
available..for eg.

int x=20;

main()
{
int x=10;
printf("%d",x);
}


main()
{
int x = 10;
{
extern int x;
printf ("%d", x);
}
...
}


A foul construct indeed. What happens if the local to main x is
declared static? I.e. how does the extern actually get resolved.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #13
Erik de Castro Lopo wrote:
th********@rediffmail.com wrote:
i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?

You can't. In fact the above code should be considered a potential
cause of bugs. If you are lucky enough to be using the GNU C compiler
(gcc) you can avoid these bugs by compiling with the -Wshadow
compiler flag. Add -Werror will turn warnings into errors.

Erik


lcc-win32 features a -shadows feature too, exactly like the one
you mention.

This will be automatically turned on when you ask for a "check"
pass of the code.
Nov 14 '05 #14


CBFalconer wrote:
Ben Pfaff wrote:
th********@rediffmail.com writes:

i want to access the value of global variable inside the main
function and inside the main func the var with the same name is
available..for eg.

int x=20;

main()
{
int x=10;
printf("%d",x);
}


main()
{
int x = 10;
{
extern int x;
printf ("%d", x);
}
...
}

A foul construct indeed. What happens if the local to main x is
declared static? I.e. how does the extern actually get resolved.

Is the one below at fault ?

bash-2.02$ cat t17.c
#include <stdio.h>

int i = 9;

int main ()
{
static int i = 20;
{
extern int i;
printf ("\nvalue of i decalared outside main = %d\n", i);
}
printf ("\nvalue of i decalared in main = %d\n", i);
return 0;
}
bash-2.02$ gcc -Wall -pedantic t17.c
bash-2.02$ ./a.exe

value of i decalared outside main = 9

value of i decalared in main = 20
bash-2.02$

- Ravi

Nov 14 '05 #15
sushant wrote:
hi ,

can we use scope resolution operator (::) in C???

sushant


I can think of one way you can reference that global variable: Use a
getter/setter function for each global variable (kludge).

I've seen code that avoids naming global variables the same as local
variables by using conventions like g_variable_name for global variables
and not using any prefix (at least not g_) for any local variables.

However, this is clearly not ideal. I'd avoid using global variables as
much as possible. Just my two cents.

Regards,
Jonathan.
Nov 14 '05 #16
On Fri, 07 Jan 2005 07:36:22 +0000, CBFalconer wrote:
Ben Pfaff wrote:


....
main()
{
int x = 10;
{
extern int x;
printf ("%d", x);
}
...
}


A foul construct indeed. What happens if the local to main x is
declared static? I.e. how does the extern actually get resolved.


Block scope object identifiers (except declared with extern) have no
linkage, even static ones. So a block scope static int x; is not a
candidate for linking to an extern int x;

If you try to mix internal and external linkage for the same identifier in
the same translation unit you get undefined behaviour.

Lawrence

Nov 14 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Anonymous | last post: by
5 posts views Thread by exits funnel | last post: by
7 posts views Thread by Richard Hayden | last post: by
reply views Thread by Howard Gardner | last post: by
3 posts views Thread by richard pickworth | last post: by
6 posts views Thread by Jack | last post: by
2 posts views Thread by xtrigger303 | last post: by
5 posts views Thread by Shan | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.