473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Why is this code compiling properly??

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

Note : I am using gcc-3.4.3 on i386-redhat-linux

Dec 13 '06 #1
21 1603
onkar wrote:

(Please put your question in the message body, not just
lonely as a cloud in the subject line.)#
Why is this code compiling properly??
#include<stdio.h>
int i;
int i;
int main(){
printf("i=%d\n",i);
return 0;
}

Note : I am using gcc-3.4.3 on i386-redhat-linux
Why shouldn't it?

--
Chris "Perikles triumphant" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

Dec 13 '06 #2

Chris Dollin wrote:
onkar wrote:

(Please put your question in the message body, not just
lonely as a cloud in the subject line.)#
Why is this code compiling properly??
As both the variables are uninitialized, thats why the code is
compiling properly.
If you try the same thing with atleast one of them being initialised
with some value, then surely your code will not compile.

Cheers,
SandeepKsinha.
#include<stdio.h>
int i;
int i;
int main(){
printf("i=%d\n",i);
return 0;
}

Note : I am using gcc-3.4.3 on i386-redhat-linux

Why shouldn't it?

--
Chris "Perikles triumphant" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/
Dec 13 '06 #3
As both the variables are uninitialized, thats why the code is
compiling properly.
If you try the same thing with atleast one of them being initialised
with some value, then surely your code will not compile.

Cheers,
SandeepKsinha.
only one initialize doesn't give any error, both have to be
initialized for the compiler to give the error.

Dec 13 '06 #4
On 13 Dec 2006 03:41:09 -0800, in comp.lang.c , sa*****@yahoo.co.in
wrote:
>
As both the variables are uninitialized, thats why the code is
compiling properly.
.... but it is broken ,since he attempts to print the value of an
uninitialised variable. This is likely to crash or print garbage.
Don't do that...
>If you try the same thing with atleast one of them being initialised
with some value, then surely your code will not compile.
only one initialize doesn't give any error, both have to be
initialized for the compiler to give the error.
The lines
int i;
int i;

at file scope are both declarations. They are also /tentative/
definitions, that is to say, they are potentially going to be used to
define i, if needed.

Since neither is initialised, the compiler doesn't have a /form
definition/ yet. If the compiler doesn't find a firm definition of i
in the same file, it takes one of the declarations and uses it as a
definition. In this case it defines i to be an int with unspecified
value.

If you initialise one of the declarations with a value, it becomes a
definition. Thats ok.

You can have only one definition, so if you initialise them both, it
is an error.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 13 '06 #5
sandy said:
>
Chris Dollin wrote:
>onkar wrote:

(Please put your question in the message body, not just
lonely as a cloud in the subject line.)#
Why is this code compiling properly??
As both the variables are uninitialized, thats why the code is
compiling properly.
Not so. One of them can be initialised and the code will still be fine.
If you try the same thing with atleast one of them being initialised
with some value, then surely your code will not compile.
Not so. One of them can be initialised and the code will still be fine.

Please do not give incorrect advice on comp.lang.c. Thank you.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 13 '06 #6
2006-12-13 <e6********************************@4ax.com>,
Mark McIntyre wrote:
On 13 Dec 2006 03:41:09 -0800, in comp.lang.c , sa*****@yahoo.co.in
wrote:
>>
As both the variables are uninitialized, thats why the code is
compiling properly.

... but it is broken ,since he attempts to print the value of an
uninitialised variable. This is likely to crash or print garbage.
No it's not. Though the use of the term uninitialized is problematic,
you seem to know what he meant by it, and there _is_ a tradition on some
systems of using it to refer to any such data [OT: bss section, zero-bit
floats and NULLs, and all that]
Dec 13 '06 #7
Mark McIntyre wrote:
On 13 Dec 2006 03:41:09 -0800, in comp.lang.c , sa*****@yahoo.co.in
wrote:
>>
As both the variables are uninitialized, thats why the code is
compiling properly.

... but it is broken ,since he attempts to print the value of an
uninitialised variable. This is likely to crash or print garbage.
Don't do that...
(fx:snip)
Since neither is initialised, the compiler doesn't have a /form
definition/ yet. If the compiler doesn't find a firm definition of i
in the same file, it takes one of the declarations and uses it as a
definition. In this case it defines i to be an int with unspecified
value.
Surely zero, since it's a static variable with no explicit initialisation.

["static" as in "not automatic, not mallocated", rather than as in
"scope local to file."]

--
Chris "Perikles triumphant" Dollin
The "good old days" used to be much better.

Dec 13 '06 #8
Richard Heathfield <rj*@see.sig.invalidwrites:
sandy said:
>Chris Dollin wrote:
>>onkar wrote:

(Please put your question in the message body, not just
lonely as a cloud in the subject line.)#

Why is this code compiling properly??
As both the variables are uninitialized, thats why the code is
compiling properly.

Not so. One of them can be initialised and the code will still be fine.
>If you try the same thing with atleast one of them being initialised
with some value, then surely your code will not compile.

Not so. One of them can be initialised and the code will still be fine.

Please do not give incorrect advice on comp.lang.c. Thank you.
To interpret sandy's explanation charitably:

Both variables are uninitialized (more precisely, not explicitly
initialized). That's why the code is legal.

If one of them were uninitialized and the other were explicitly
initialized, *that* would be why the code is legal.

--
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.
Dec 13 '06 #9
Keith Thompson said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>sandy said:
>>Chris Dollin wrote:
onkar wrote:

(Please put your question in the message body, not just
lonely as a cloud in the subject line.)#

Why is this code compiling properly??

As both the variables are uninitialized, thats why the code is
compiling properly.

Not so. One of them can be initialised and the code will still be fine.
>>If you try the same thing with atleast one of them being initialised
with some value, then surely your code will not compile.

Not so. One of them can be initialised and the code will still be fine.

Please do not give incorrect advice on comp.lang.c. Thank you.

To interpret sandy's explanation charitably:

Both variables are uninitialized (more precisely, not explicitly
initialized). That's why the code is legal.

If one of them were uninitialized and the other were explicitly
initialized, *that* would be why the code is legal.
But he said (and the quote remains, above): "If you try the same thing with
atleast one of them being initialised with some value, then surely your
code will not compile." That is simply wrong.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 14 '06 #10
Random832 wrote:
Mark McIntyre wrote:
>sa*****@yahoo.co.in wrote:
>>As both the variables are uninitialized, thats why the code is
compiling properly.

... but it is broken ,since he attempts to print the value of an
uninitialised variable. This is likely to crash or print garbage.

No it's not. Though the use of the term uninitialized is problematic,
you seem to know what he meant by it, and there _is_ a tradition on
some systems of using it to refer to any such data [OT: bss section,
zero-bit floats and NULLs, and all that]
Yes it is. Accessing an uninitialized variable invokes undefined
behaviour, which can often be a crash.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Dec 14 '06 #11
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
[...]
>To interpret sandy's explanation charitably:

Both variables are uninitialized (more precisely, not explicitly
initialized). That's why the code is legal.

If one of them were uninitialized and the other were explicitly
initialized, *that* would be why the code is legal.

But he said (and the quote remains, above): "If you try the same thing with
atleast one of them being initialised with some value, then surely your
code will not compile." That is simply wrong.
You're right; I should have paid more attention.

--
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.
Dec 14 '06 #12
CBFalconer <cb********@yahoo.comwrites:
Random832 wrote:
>Mark McIntyre wrote:
>>sa*****@yahoo.co.in wrote:

As both the variables are uninitialized, thats why the code is
compiling properly.

... but it is broken ,since he attempts to print the value of an
uninitialised variable. This is likely to crash or print garbage.

No it's not. Though the use of the term uninitialized is problematic,
you seem to know what he meant by it, and there _is_ a tradition on
some systems of using it to refer to any such data [OT: bss section,
zero-bit floats and NULLs, and all that]

Yes it is. Accessing an uninitialized variable invokes undefined
behaviour, which can often be a crash.
Yes, but the code in question:

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

does not access an uninitialized variable. Since i has static storage
duration, it's implicitly initialized to zero.

--
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.
Dec 14 '06 #13
2006-12-14 <ln************@nuthaus.mib.org>,
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Random832 wrote:
>>Mark McIntyre wrote:
sa*****@yahoo.co.in wrote:

As both the variables are uninitialized, thats why the code is
compiling properly.

... but it is broken ,since he attempts to print the value of an
uninitialised variable. This is likely to crash or print garbage.

No it's not. Though the use of the term uninitialized is problematic,
you seem to know what he meant by it, and there _is_ a tradition on
some systems of using it to refer to any such data [OT: bss section,
zero-bit floats and NULLs, and all that]

Yes it is. Accessing an uninitialized variable invokes undefined
behaviour, which can often be a crash.

Yes, but the code in question:
[...]
does not access an uninitialized variable. Since i has static storage
duration, it's implicitly initialized to zero.
The term "uninitialized" is often used to refer to variables in this
state. We're all using it. Even he used it elsewhere in the same post.
He's just being disingenuous.
Dec 14 '06 #14
On 13 Dec 2006 02:49:05 -0800, "onkar" <on*******@gmail.comwrote:
>#include<stdio.h>
int i;
int i;
int main(){
printf("i=%d\n",i);
return 0;
}

Note : I am using gcc-3.4.3 on i386-redhat-linux
I think you're missing a compiler option. Try this, assuming the name
of your source file is foo.c:

gcc -W,-runPClint -o foo foo.c

foo.c(3) : Error 31: Redefinition of symbol 'i' compare with line 2

Works for me.

--
jay
Dec 14 '06 #15
jaysome said:
On 13 Dec 2006 02:49:05 -0800, "onkar" <on*******@gmail.comwrote:
>>#include<stdio.h>
int i;
int i;
int main(){
printf("i=%d\n",i);
return 0;
}

Note : I am using gcc-3.4.3 on i386-redhat-linux

I think you're missing a compiler option. Try this, assuming the name
of your source file is foo.c:

gcc -W,-runPClint -o foo foo.c

foo.c(3) : Error 31: Redefinition of symbol 'i' compare with line 2

Works for me.
Fails for you, you mean. Your compiler doesn't understand about tentative
definitions.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 14 '06 #16
Mark McIntyre said:
On 13 Dec 2006 03:41:09 -0800, in comp.lang.c , sa*****@yahoo.co.in
wrote:
>>
As both the variables are uninitialized, thats why the code is
compiling properly.

... but it is broken ,since he attempts to print the value of an
uninitialised variable.
No, he isn't. Whilst there is no *explicit* initialisation, the object is at
file scope and thus takes on the appropriate default static initialiser
value, which in this case is 0.

<snip>
Since neither is initialised, the compiler doesn't have a /form
definition/ yet. If the compiler doesn't find a firm definition of i
in the same file, it takes one of the declarations and uses it as a
definition. In this case it defines i to be an int with unspecified
value.
Wrong.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 14 '06 #17
onkar wrote:
#include<stdio.h>
int i;
int i;
int main(){
printf("i=%d\n",i);
return 0;
}

Note : I am using gcc-3.4.3 on i386-redhat-linux
It takes one as (extern) declaration and the other as definition. When
it doesn't get any definition (i.e. initialized) and all declarations,
it picks one as uninitialized definition.

If you compile the program as gcc -S foo.c (assume your code is foo.c)
you will get the following code (on my red hat linux pentium machine)
======================================
.file "foo.c"
.section .rodata
..LC0:
.string "i=%d\n"
. . . .
/* Some code */
. . . .
..Lfe1:
.size main,.Lfe1-main
.comm i,4,4
.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
======================================

If you check the lines .comm i, 4, 4
i is lodged just once in memory i.e. one of the line is considered as
declaration while other is definition.

So While linking it will look for symbol i in other object files (if
present), if it doesn't find one, it will keep one declaration as
definition.

To ensure this make a more file foo1.c

/* ================ foo1.c =================*/
int i = 2;
/* file ends */

Recompile the program "gcc foo.c foo1.c"
Execute the program "./a.out"

I'm sure output will be "i=2"

Cheers :-) MJ

Dec 14 '06 #18
On Wed, 13 Dec 2006 16:14:57 +0000, in comp.lang.c , Chris Dollin
<ch**********@hp.comwrote:
>
Surely zero, since it's a static variable with no explicit initialisation.
yes, indeed. I overstated the case.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 15 '06 #19
On Thu, 14 Dec 2006 08:06:26 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>Mark McIntyre said:
>On 13 Dec 2006 03:41:09 -0800, in comp.lang.c , sa*****@yahoo.co.in
wrote:
>>>
As both the variables are uninitialized, thats why the code is
compiling properly.

... but it is broken ,since he attempts to print the value of an
uninitialised variable.

No, he isn't. Whilst there is no *explicit* initialisation, the object is at
file scope and thus takes on the appropriate default static initialiser
value, which in this case is 0.
Yes, somehow I missed that it was file-scope when I wrote that bit. .
>
>Since neither is initialised, the compiler doesn't have a /form
definition/ yet. If the compiler doesn't find a firm definition of i
in the same file, it takes one of the declarations and uses it as a
definition. In this case it defines i to be an int with unspecified
value.

Wrong.
Well, aside from the unspecified part, I'm right. Either you need to
correct people usefully, or you need to stop correcting people. Who do
you think you are? Dan Pop? :-)
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 15 '06 #20
Mark McIntyre said:

<snip>
Either you need to
correct people usefully, or you need to stop correcting people.
You are by no means a newbie. If I tell you you're wrong, I will generally
expect that to be sufficient information for you to spot and correct the
error, especially if the reason you're wrong has already been discussed
earlier in the article, as in this case.

But if you want me to write long, expository articles explaining in
nauseating detail precisely why you are wrong, I can do that too, if the
price is right.

Who do you think you are? Dan Pop? :-)
No.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 15 '06 #21
On Fri, 15 Dec 2006 02:45:22 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>But if you want me to write long, expository articles explaining in
nauseating detail precisely why you are wrong, I can do that too, if the
price is right.
one groat. Not a farthing more.
>Who do you think you are? Dan Pop? :-)

No.
I claim my five pounds...
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Dec 15 '06 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: wenmang | last post by:
Hi, We have the 3rd party libraries written in C. And, in every header file from 3rd party, it has the include guarde: #ifdef __cplusplus #define extern "C" { : APIs... : } #endif
2
by: Curt86 | last post by:
I am trying to port some C code from an old VMS system to Windows 2003. Recently, I've been having some problems with unresolved externals: ccm_com error LNK2001: unresolved external symbol...
11
by: Kalle Rutanen | last post by:
Hello Here is a short code snippet which does not compile. Could someone explain why this is ? class A { public: void set(int a) {
13
by: vashwath | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { typedef union { int i;
1
by: MichaelW | last post by:
I've been Python (2.3.4) plus Tkinter happily under MacOX 10.3, having compiled them from scatch. (Tkinter is based on tcl/tk 8.4.1, which were compiled from source via Fink). I then moved my...
1
by: nik | last post by:
hello friends , iam working on finite state machine .i have some problem regarding code that i have executed in borland compiler ..following #include<iostream.h> #include<conio.h>...
64
by: Bayazee | last post by:
hi can we hide a python code ? if i want to write a commercial software can i hide my source code from users access ? we can conver it to pyc but this file can decompiled ... so ...!! do you...
1
by: Ray Buck | last post by:
I've been trying to install Mailman, which requires a newer version of the Python language compiler (p-code generator?) than the one I currently have on my linux webserver/gateway box. It's...
6
by: =?gb2312?B?tdjH8reitq+7+g==?= | last post by:
Who can tell me the result in your compiler? Is it a bug or standard beheiver? class A { template<int> void a(); }; class B:A
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.