This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 6 years ago.
Here's my code:
int main(){
int age;
int height = 72;
//
double blab = (10/23242^4);
printf("I am %d years old.\n",age);
printf("I am %d inches tall.\n",height);
return 0;
}
Having the blab variable makes the value of age is a random number. When I erased this variable, the value is zero, as I think the value of age was supposed to be.
In C, variables are allocated in one of two places: globally, or on the stack. By definition, global variables are initialised to 0 if there is no assignment; stack variables are not.
int willBeZero;
void Fn(void) {
int uninit;
int four = 4;
} // Fn(void)
So if a stack variable is not initialised, what value does it take? What ever happens to be on the stack at that memory location, which could be 0.
If you want a particular value in a variable, you have to put it there.
Uninitialized local (non-static) variables have an indeterminate (and seemingly random) value. Using them without initialization leads to undefined behavior.
Related
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 3 years ago.
Suppose I have this block of code:
#include <stdio.h>
#include <unistd.h>
int *local_pointer(void)
{
int x = 6;
return &x;
}
void add(void)
{
int a;
a = 4;
a = a + 1;
}
int main()
{
int *result;
result = local_pointer();
printf("int is %d\n", *result);
add();
printf("int is %d\n", *result);
return 0;
}
The output of this code is
int is 6
int is 5
I am confused as to why this is?
If we go through main, the result variable is returned a pointer to x and then when it the pointer is dereferenced the its value is 6 however why does calling the add function make the next printf statement print 5?
Your code has a bug. Your local_pointer function returns a pointer to its x variable. But as soon as that function returns, x no longer exists since it was local. So when you dereference the returned pointer, the results are undefined.
If I had to guess, I'd say that a happens to occupy the memory that x used to occupy. But that's just a guess. The behavior might be due to something else. It might change with compiler options. It might change with a different compiler. Who knows? Fix the bug and the mystery will go away.
The function local_pointer probably (cf. other comments on undefined behaviour) returns a pointer to the stack. So I think you are basically printing the content of the stack. The call to add writes the value 5 to the stack, that's what your second printf is outputing 5.
This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Is reading into uninitialized memory space ALWAYS ill advised?
(6 answers)
Closed 4 years ago.
The output in the following code is really mysterious.
#include <stdio.h>
void funktion(int x) {}
void test() {
int foo;
printf("test = %d\n", foo);
}
int main() {
test();
funktion(5);
test();
return 0;
}
Why will the foo-var initialised with the value of the parameter of funktion()? I don't understand why foo-var is initialised by the function-parameter. I know that I can fix it with a explicit initialisation - but I want to understand the mystery. Thanks!
I've added comments to your main() function that explains why is it so. By the way, you're in undefined behaviour land, and different platforms might yield different results.
int main() {
// no return, no parameter, one local variable of type int.
// This pushes an int on the stack, uninitialized (the local variable).
test();
// Stack pointer is back to where it was.
// no return, one int parameter, no local variables.
// This pushes an int with value=5 on the stack (the parameter).
funktion(5);
// Stack pointer is back to where it was.
// no return, no parameter, one local variable of type int.
// This pushes an int on the stack, uninitialized (the local variable).
// Previous value of that memory comes from funktion() call.
test();
// Stack pointer is back to where it was.
return 0;
}
This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 5 years ago.
#include<stdio.h>
void main(void)
{ char m,n;
printf("%d\n",m);//value of m
printf("%d",n);//value of n
}
in the above snippet value of m is always printed as 0 why?why it doesn't change even after multiple times compilation is it automatically assigned if we do not assign while the value of n always changes so why not both change randomly every time i compile?
Am i missing any concept?
Local variables like yours are automatic variables. They are allocated on stack memory and their value is garbage.
Global variables are implicitly have static storage class and have value 0 by default.
Since m is a local variable, its value needn't always be the same. It is indeterminate. This memory location can be given to other processes.
This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 7 years ago.
int main()
{
int a;
printf("the value is %d", a+'a');
return 0;
}
In the above code a is local variable, And local variable are initialize to garbage value if we don't explicitly give them value . So the output should be some garbage value . But why am I getting output as 97?
In your code,
printf("the value is %d", a+'a');
produces undefined behaviour. The output of UB, is, well, undefined.
You cannot rely upon (or justify) the outcome (if any) for a statement which invokes UB.
Local variables are stack variables. They are not initialized (unlike static variables). So better initialize yourself.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 8 years ago.
What is the problem associated with returning a pointer to a local variable?
And (i don't know if its legal to do this) What is the problem with returning a reference to pointer in main?
ex:
int *p;
p=abc();
where abc would be returning int&
P.S. Sorry if not clear. I am confused too :P
You can syntactically return a pointer to a local variable, but that variable will no longer be valid after the function has returned.
If your code is:
int *abc() {
int n = 3;
return &n;
}
void foo() {
int m = 4;
}
int main() {
int *p;
p=abc();
foo();
printf("%d", *p);
}
then m will probably overwrite n and the result will be 4.
In simple terms, local variables are available only as long as it is in that function is in the function stack, once the function is done, its popped out of the function stack.