-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
  • 119,891
  • 44
  • 235
  • 294
p0s0731
  • 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
  • 133,132
  • 16
  • 183
  • 261
0

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

Paul Praet
  • 1,367
  • 14
  • 25