Solved

explain this

Posted on 2007-12-04
10
362 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now