Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Basic variable declaration in C.

Status
Not open for further replies.

pavans2510

Member level 2
Joined
Jul 25, 2011
Messages
45
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
Bangalore
Activity points
1,562
Hi all,
This is very basic doubt. Below is the code.
int j ;
int k;
printf ( "\n%d\n", j) ;
printf ( "\n%d\n", k) ;

I get output as

0

1245000

Why does variable value change even if the declaration is same. Is it a random value or is there any reasoning behind this output?
 

Your code snippet suggests that the variables are declared inside a function. They are so called "automatic variables" according to the C specification and not initialized. They can change and have arbitrary values on each new entry of the respective function. If you require automatic variables to be initialized to a defined value on function entry, add an initializing statement. Variables intended to keep the value across function calls must be declared with the static storage qualifier.
 

Hi FvM,
Thanks for the reply. Infact the code is inside main program not inside any function. If the values for the variables for the above are arbitrary then why the values same every time. I'm not just not getting the concept right.
 

Infact the code is inside main program not inside any function. If the values for the variables for the above are arbitrary then why the values same every time. I'm not just not getting the concept right.
main() is a function as well, with the difference that it's only entered once.

The important point is that automatic variables are not initialized and that you shouldn't make any assumption about it's initial value. That's the "concept". It doesn't exclude that some variables might take the same value each time for a specific compiler, it's also not forbidden for the compiler to initialize it. From a specification viewpoint, you can say it's arbitrary.
 

FvM is absolutely right (isn't he always:grin:) but please do some tests for yourself to prove it. The obvious fix is to initialize j and k by changing the first two lines to "int j = 0" and "int k = 0" but you could also simply swap them over so k is declared before j to see what happens.

Brian.
 

Hi all,
So I did a simple program test run. Firstly I tried with below code
int j;
int k;
printf ( "\n%d\n", j) ;
printf ( "\n%d\n", k) ;

I get the following output.

0

1245000

Secondly I tried the second code
int k;
int j;
printf ( "\n%d\n", k) ;
printf ( "\n%d\n", j) ;


I get this output

1245000

0

Ideally variables declared should be arbitrary. Any specific reason for this output?

Regards,
PA-1
 

Ideally variables declared should be arbitrary.
No. There's nothing sure about automatic variable contents, except that they are not initialized to a specific value. What are you trying to achieve?

Technically, it can be well explained for a specific C implementation why the variables behave as they do. You need to look into the assembly level code, find out where the variable space is allocated, e.g. on the stack, and which data have populated the respective memory area before. But that's beyond C specification.
 
Initiation of variables depends on the implementation of compilers
 
FVM is correct but there may be differences in compilers as well. When a variable is declared in C a memory location to hold it is allocated but it's contents are not necessarily cleared. So the number you see it holding is just what was in the memory at that time and that depends on what it was last used for, It may even be data left over from a previous program.

Brian.

- - - Updated - - -

FVM is correct but there may be differences in compilers as well. When a variable is declared in C a memory location to hold it is allocated but it's contents are not necessarily cleared. So the number you see it holding is just what was in the memory at that time and that depends on what it was last used for, It may even be data left over from a previous program.

Brian.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top