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.
Related
This question already has answers here:
Are the members of a global structure initialized to zero by default in C?
(5 answers)
Closed 2 years ago.
I didn't find anything online about what happens when objects are created in C: like their value are initialized or they take garbage value.
#include <stdio.h>
struct temp
{
int a;
} s;
int main()
{
printf("%d", s.a);
}
OUTPUT is : 0.
So is 0 a garbage value?? Or is it an undefined behavior?
Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).
You have used a global structure variable. Therefore initialised to default value, i.e., 0.
It depends on the place where the variable is declared. The structure variable s is declared as global. So it initialized to zero by default.
More in this link
This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 3 years ago.
if i run this program:
#include <stdio.h>
int main()
{
int a=32,b=2,c,i;
for(i=0;i<3;i++){
printf("%d\n",c);
c=a/b;
a=c;
}
return 0;
}
the output is:
32765
16
8
In there, I dont define the value of C, Where from came this output 32765.?
even again i run this code more time,it show different values like 32764,32767. Why this different output showing on?
Because c has automatic storage duration (i.e. is a non-static local variable) and is uninitialized, its value is indeterminate. Attempting to print an uninitialized variable that never had its address taken (i.e. was not the subject of the address-of operator &) invokes undefined behavior.
Even if you did take the address of c you could still have undefined behavior if it contains a trap representation. If it does not contain a trap representation (and most implementations don't have them), the the value is unspecified which simply means the printed value can't be predicted.
Automatic variables (a local variables which are allocated and deallocated automatically when program flow enters and leaves the variables's scope) for which there is no explicit initializer have undefined (i.e. garbage) values.
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:
Returning local data from functions in C and C++ via pointer
(13 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 5 years ago.
#include<stdio.h>
#include<conio.h>
int *x()
{
int y=10;
return (&y);
}
void main()
{
int *p;
clrscr();
p=x();
printf("%d",*p); // Output 10
getch();
}
Here when we call x() function, activation record of x is pushed onto the stack. When we come out of that function, the activation record and all the local variables inside it are destroyed.
So, how are we able to access the value of y in main function after coming out of x function ? The output value should be some garbage value as "y" variable is destroyed.
Function x returning a pointer to an automatic local variable and causing undefined behavior. In this case any expected or unexpected result can be seen.
Local variables have a limited lifetime which only extends inside the block {} they are defined. They have a local-scope.
When the control reaches the end of the block all the storage for the variables in it is not guaranteed to not be writable.
That portion of memory can be re-used. Will it be? who knows.
This translates to: undefined behaviour!
Is not like "y" gets destroyed.
Its memory region is labelled as "writable"
So you may either find your "y", as well as a garbage value.
After all, the function is returning a pointer, not the variable itself
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.