Why this so !!!!

C

code break

Hi all.

Can you tell me the reason .

1)why compile is not giving error ?
2)if not error then the value of x should be 10 but it is printing junk
value.

The code is :-
include <stdio.h>

int x = 10;

static int y;

int main() {

int x = x;

static int y;

printf("%d, %d\n",x,y);

return 0;

}


i'm using microsoft visual studio compiler ...

i hope you guys get back to me with reason....

this help me lot....
 
Z

Zero

First of all you should decide whether you want to have local or global
variables.
Remember, if you have a local and a global variable with the same name,
as your int x in main, then, this local x is higher prior and the
global x is ignored.

Therefore: In main you define int x and initialize it with the value in
x. But as you have not put a value in x before there will be a random
value in it. In y there should be Zero in accordance to the definition
of a static variable.
 
B

bert

code said:
Hi all.

Can you tell me the reason .

1)why compile is not giving error ?
2)if not error then the value of x should be 10 but it is printing junk
value.

The code is :-
include <stdio.h>

int x = 10;

static int y;

int main() {

int x = x;

static int y;

printf("%d, %d\n",x,y);

return 0;

}


i'm using microsoft visual studio compiler ...

i hope you guys get back to me with reason....

this help me lot....

Your inner x and y are distinct from your outer x and y,
and they have made the outer ones not visible. What
you have written in main() is equivalent to:

int xx = xx; static int yy;
printf("%d, %d\n",xx,yy);

which of course prints uninitialised junk values. Some
compilers would give the warning that xx is being used
before it is being assigned.
--
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

code said:
Hi all.

Can you tell me the reason .

1)why compile is not giving error ?

The compiler is not reporting an error because you do not have an error.
2)if not error then the value of x should be 10 but it is printing junk
value.

You are wrong. In the case you are concerned about, x is not
initialized, so junk is one of the possible outputs.
The code is :-
include <stdio.h>

int x = 10;

static int y;

OK, you've defined two global variables: x and y
*Unless otherwise overridden* these two variables will be available to
every function in your program.
int main() {

int x = x;

Here, you override the global variable x and define a local variable x
You assign this local variable x the value given to the /local variable/ x.

What you've done is roughly equivalent to:
int x; /* define local variable x (/
x = x; /* initialize local variable x from local variable x */

Since you didn't initialize x with anything, the initializer is "junk"
(as you put it), and later uses of this value of x will reproduce "junk".

static int y;

Here, you override the global variable y and define a local variable y
printf("%d, %d\n",x,y);

Of course, you didn't provide initial values for either x or y, so this
printf will print "junk" for each.
return 0;

}


i'm using microsoft visual studio compiler ...

I'm sorry. I hope you soon can afford a real C compiler :-(
i hope you guys get back to me with reason....

this help me lot....



- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFETMPsagVFX4UWr64RAuQ+AKDaNTIFKdFuVFUauO6YzVDAS8bAiwCaApEj
t800VEjK3SZd/OReyf3ggok=
=vs6N
-----END PGP SIGNATURE-----
 
M

Michael Brennan

Lew said:
Here, you override the global variable y and define a local variable y


Of course, you didn't provide initial values for either x or y, so this
printf will print "junk" for each.

Only the first number will output "junk", y is a static variable
and will automatically be initialized to 0.

/Michael
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael said:
Only the first number will output "junk", y is a static variable
and will automatically be initialized to 0.

Of course. My mistake.


- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFETM+JagVFX4UWr64RArcOAKC+U2atfuELrUgU58xJrQCMUfXH6QCgx2j/
IOFmNWNKWJS07PFQr+m0CDk=
=rmFZ
-----END PGP SIGNATURE-----
 
J

joe.gbox

It's interesting that the second x in 'int x = x;' refers to the first
one, not the global one. The definition of local x hasn't completed
yet at that very place.
 
V

Vladimir Oka

(e-mail address removed) wrote:

Quote context. On Google, click Show Options, then Reply that appears.
It's interesting that the second x in 'int x = x;' refers to the first
one, not the global one. The definition of local x hasn't completed
yet at that very place.

It has started however, and the compiler already knows `x` and its type
(which from that point replace the file scope one). It's just the value
that's indeterminate.
 
A

Andrew Poelstra

It's interesting that the second x in 'int x = x;' refers to the first
one, not the global one. The definition of local x hasn't completed
yet at that very place.

You mean in the same way that your knowledge of how to use Google hasn't
been completed yet? Click 'More Options' and then 'Add Reply' to quote
properly.

And to answer your question: Yes it has.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,177
Messages
2,570,952
Members
47,506
Latest member
tomiy16522

Latest Threads

Top