Default values of int when not initialized in c. why do i get different outputs?

后端 未结 5 1869
余生分开走
余生分开走 2021-01-16 13:49

i was just trying to check the default value of int and char when not initialised in c. my problem is that i get two different outputs when i use clrscr() to clear the scree

相关标签:
5条回答
  • 2021-01-16 14:29

    In case of your code, you will get garbage values, but if the integer varibale was declared globally then its default value will be 0 for sure in C. I mentioned this because question's title was-"Default values of int when not initialized in c".

    0 讨论(0)
  • 2021-01-16 14:37

    Actually zeros were garbage as well. After you add clrscr(); you changed stack so you did to garbage.

    0 讨论(0)
  • 2021-01-16 14:38

    I think it always give some garbage value ..you can't determine the behavior... :-/

    0 讨论(0)
  • 2021-01-16 14:50

    In managed code, there are default values, usually 0 or equivalent.

    In the unmanaged world, there is no default value. When the memory is allocated, the system just tells you "You can write here is you want, but I don't know what mess the previous program let behind".

    This behaviour is seen for some people as bad since their program can be unpredictable for some obscure reasons, but it is also a way to be able to optimize as much as we can memory management, think of large buffer allocation which when allocated would be filled with 0s, and then filled with actual data. You get twice the performance in unmanaged code!

    0 讨论(0)
  • 2021-01-16 14:52

    You shouldn't be expecting anything in particular. The values are uninitialized, i.e.: can be anything.

    Why the garbage value in the first case differs from the garbage value in the second case? Who knows? Different memory map, different execution paths - different garbage.

    0 讨论(0)
提交回复
热议问题