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
Solved

explain this

Posted on 2007-12-04
10
388 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
fix34  challenge 9 141
array6 challenfge 6 114
mapBully challenge 6 152
Assigning default value to structure in C for mutithread application 17 71
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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