Vishvendra Singh
Vishvendra Singh

Reputation: 187

Operator value evalution issue in C program

#include<stdio.h>
void main(){

     int i;
     i = i-3;
     printf("%d",i); // output -3
}

Why answer is 3 every time run this c program. but we knows c picks random value for variable which is not initialized. so why it gives -3. I also try it using auto storage class. I am using codeblocks compiler.

Upvotes: 0

Views: 83

Answers (2)

Zolfaghari
Zolfaghari

Reputation: 1323

This code has following Error:

Uninitialized local variable 'i' used.

Incorrect Statments >>>

Xx. we knows c picks random value for variable which is not initialized .xX

Upvotes: 0

user1814023
user1814023

Reputation:

Your code is causing undefined behavior. Anything could happen in the code. Your code might print 3 or 300 or a Mona Lisa picture on the screen.

6.3.2.1p2:

If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.

Upvotes: 3

Related Questions