-2
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?

Yu Hao
  • 115,525
  • 42
  • 225
  • 281
priti sharma
  • 167
  • 5

2 Answers2

2

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.

Sourav Ghosh
  • 130,437
  • 16
  • 177
  • 247
0

Local variables are stack variables. They are not initialized (unlike static variables). So better initialize yourself.

Paul Praet
  • 1,287
  • 13
  • 23