0

Please explain why I am getting output 2 here. My expected o/p is 5 or 7. Please throw some light. Thank you!

#include<stdio.h>

typedef enum {a=3, b, c, d, j}e;

void f(e *e1) {
    printf("%ld", (int)*e1);
}

main(){
    e es;
    f(&es);
}
uss
  • 1,271
  • 1
  • 13
  • 29

1 Answers1

4

You haven't initialized es, so your program is just printing the random value that happens to be on the stack when the program runs.

You need to say something like:

e es = c;

That will give you the 5 output you seek.

Warren Young
  • 40,875
  • 8
  • 85
  • 101
  • so 2 is just only a random value? then why its getting printed always 2? it should take some other random value right? – uss Aug 29 '13 at 10:18
  • 2
    It's not "random" as in "someone is doing the work needed to generate a correct random number which is hard to predict", it's random as in "there will be a value here which is impossible to predict, but it just is whatever happens to be there, nobody tries to affect in in any way". – unwind Aug 29 '13 at 10:19
  • @sree: unwind is right, but also, given different circumstances, your program will print different values. Build it with a different compiler, or on a different OS or CPU type, or run under [ASLR](http://en.wikipedia.org/wiki/ASLR), or, or, or... – Warren Young Aug 29 '13 at 10:23
  • Thank you Warren Young and unwind for your thoughts. – uss Aug 29 '13 at 10:24