Solved

explain this

Posted on 2007-12-04
10
379 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FizzBuzz challenge 9 81
Digital Output does not have continuity to ground? 3 92
groupNoAdj 7 96
How to install SVN Command Line Client? 5 67
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

803 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