What will output to print a unrecognized int variable? [duplicate] - c

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.

Related

Not generation of core [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 4 years ago.
#include<stdio.h>
#include <stdlib.h>
int *ip_range ;
int main()
{
ip_range = (int *) malloc(1);
ip_range[0]=2;
ip_range[10]=2;
ip_range[20]=2;
ip_range[33787]=12444;
printf("%d\n", ip_range[33787]);
}
I have malloc just 1 block then why it is accessible till 33787 and generating core on 33788.
You are writing to memory which you do now own i.e. was not handed back by malloc , calloc or realloc. This results in undefined behaviour. Your program can do anything at all, including not producing any error message or core dump.
It is undefined behaviour because you are trying to access memory out of bound. C does not check memory bound.
According to cppreference :
undefined behavior - there are no restrictions on the behavior of the program. Examples of undefined behavior are memory accesses
outside of array bounds, signed integer overflow, null pointer
dereference, modification of the same scalar more than once in an
expression without sequence points, access to an object through a
pointer of a different type, etc.

Need explanation on short code snippet [duplicate]

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.
I just made this short program. Can someone please explain why I am getting 2 as a result here?
Here is the code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int variable;
int a;
a=variable;
a=200;
printf("%d",variable);
return 0;
}
Because you print the value of an uninitialized variable. It will have an indeterminate (and seemingly random) value.
The assignment you make to a just copies the value of variable and then of 200 into a. The value of variable remains unmodified and indeterminate.
I recommend you find a good beginners book or two to read.
At the beginning, you define two variables, a and variable. Note, that those variables at this point are not initialized. Right now, the compiler only knows that there are two variables of type int and their names, nothing more.
You then try to initialize variable a with the not initialized variable variable, the result should be clear: The two variables remain uninitialized.
Then you proceed and initialize a with 200, variable is still only defined, not initialized.
After that, you print the still uninitialized variable variable, which hasn't recieved any "real" value as of yet, only what already was "lying around" in memory when the compiler assigned that memory location to the variable. In your case, that was "2" (or at least, that's what printf could extract from there).
Further reading: C Variables. This explains how variables are defined, declared and initialized.
Assigment operator (a = variable;) does not link the two variables, just assignes whatever value there is in the righthandside expression to the left handside.
You can visualize local variables as boxes where you can put values into.
In c if you don't assign a value to the variable then it stores the garbage value which can be anything.In your code you did not assign the value to variable so it print 2 (which can be anything)

Garbage value retrieval [duplicate]

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

What happens in C when you print a declared, but unassigned variable?

Why does the following snippet cause random numbers to print to the screen with printf, but putchar always outputs 1?
#include <stdio.h>
int main() {
char c;
printf("%d\n", c );
putchar(c);
}
According to C99 standard, this is undefined behavior. Let's see why:
Section 6.7.8.9 says that
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
This applies to your variable c, because it has automatic storage duration, and it is not initialized explicitly.
Section J.2 says that
The behavior is undefined in the following circumstances:
...
The value of an object with automatic storage duration is used while it is
indeterminate
This applies to your code as well, because you read c when you pass it as a parameter to both printf and putchar, and the value of c is still indeterminate, because it has not been assigned.
1) The c variable first has a random value(default/garbage value) to itself as you declared-but-did-not-initialize your char c to any defined letter or value of ur interest(character).
2) Next you tried to printf the %d(digit/decimal/numerical value) of the char c, so now it is giving you a converted value of the garbage which was earlier assigned to c when you declared the char c in the first place.
3) Finally you tried to use putchar(c), which again behaves similarly because your char c is uninitialized and is still being read thereby re-trying to manage with an undetermined value to be printed onto the screen. (since the same un-initialized character variable c is being passed to both kind of printing as a parameter).
Yes these 3 statements are a bit clumsy to understand but they are as layman as it can get to help speed-up some understanding regarding this query of yours.
Pay attention to the 1st comment response to your question by #bluemoon. Those 3 words alone litterally have a huge amount of sensibility and meaningfull-ness to them, to a point that it also tells you what you have done erroneous in your own code(your actions)."UNDEFINED"(try relating the same with UNINITIALIZED).

Local variables Vs Global variable [duplicate]

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.

Resources