Solved

explain this

Posted on 2007-12-04
10
394 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
  • 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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

680 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