Solved

explain this

Posted on 2007-12-04
10
397 Views
Last Modified: 2012-08-14
hello
i have a question in my C textbook that ask the following question. Can you explain this to newbie C student
Consider the following program, which consist of two object module
void p2(void);
int main(){
p2();
return 0;
}
# include <stdio.h>

char main;
void p2(){
printf("ox%x\n",main);
}

when this program is compiled it prints string 0x55\n and terminate, even though p2 never initializes variable main. Can you explain this

Thanks
0
Comment
Question by:dminh01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 20403668
The only correct answer is that you can't explain it : the code is not valid C code (you can't re-declare main as a char), so it shouldn't even compile.
0
 

Author Comment

by:dminh01
ID: 20403716
oh that is right. It has to do with some of the linking process that the book mentioned.
Thanks you are always so helpful
Thanks for your time
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20403727
when you have an uninitialized variable, is value is "undefined", so it can print any random value.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 20403775
In C, there are 4 identifier namespaces, and within one namespace, no two symbols may have the same name.

From the standard :

        If more than one declaration of a particular identifier is visible at any point
        in a translation unit, the syntactic context disambiguates uses that refer to
        different entities.
        Thus, there are separate name spaces for various categories of identifiers,
        as follows:
        - label names (disambiguated by the syntax of the label declaration and use);
        - the tags of structures, unions, and enumerations (disambiguated by
          following any of the keywords struct, union, or enum);
        - the members of structures or unions; each structure or union has a
          separate name space for its members (disambiguated by the type of the
          expression used to access the member via the . or -> operator);
        - all other identifiers, called ordinary identifiers (declared in ordinary
          declarators or as enumeration constants).

Both the main function and the main char fall in the last namespace, so that's not allowed.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20403813
>> when you have an uninitialized variable, is value is "undefined", so it can print any random value.

If the char would have had a valid identifier (which is not the case), it would have static storage duration, and would thus be implicitly initialized to 0.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20403973
>>If the char would have had a valid identifier (which is not the case), it would have static storage duration, and would thus be implicitly initialized to 0.

not all C compilers do this, because not all compilers are ISO C compliant. So assuming that the variable would be initialized is a bad practice, specially if you plan to port your application.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20404020
Just remembering this issue...
What happens if the coder decide to move the global variable into a function? He can forget to initialize and will have an unexpected value. Then, why to promote this practice? It will be better and cleaner to initialize every variable as far as possible.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20404062
>> not all C compilers do this, because not all compilers are ISO C compliant. So assuming that the variable would be initialized is a bad practice, specially if you plan to port your application.

I'm not talking about a specific compiler ... I'm talking about the C programming language. If a certain compiler doesn't do this very basic initialization, then it's far from compliant, and you shouldn't use it imo.


>> Then, why to promote this practice? It will be better and cleaner to initialize every variable as far as possible.

I'm not promoting anything ... The question was to explain the behavior of the code, and that was exactly what I did.

For the record : I do agree that it's safe to always initialize variables explicitly, but for variables with static storage duration it's not strictly necessary to do so.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20404202
>> If a certain compiler doesn't do this very basic initialization, then it's far from compliant, and you shouldn't use it imo.
Maybe in a PC-only world. I use VC++ for PC, but also develop for embedded systems, where not all options are ISO/ANSI C compliant, and there are few choices. But I don't care, because I always initialize the variables.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20404246
That's fine, jaime, but my point was that this question comes from a C book, and we should thus only reason about the C standard ... not a specific C compiler.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question