-5

I'm getting the output as -28762.Why is it not 0(zero), which should be the default value of integer?

#include<stdio.h>
#include<conio.h>

void main(){
   int a;
   clrscr();
   printf("%d",a);
   getch();
}
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
Nitesh
  • 17
  • 2
  • 3
    Undefined Behaviour is Undefined Behaviour. You need to initialise `a` to some value. – Paul R Jun 17 '17 at 08:52
  • 4
    Don't use TurboC. It is implementing an obsolete variant of C (or C++, with TurboC++). Use recent standard conforming compilers (e.g. [GCC](http://gcc.gnu.org/) or [Clang](http://clang.llvm.org/)...) and enable all warnings & debug info (e.g. compile with `gcc -Wall -Wextra -g` which would have warned you). Notice that your program is wrong (since `main` should *not* be declared as `void` function). – Basile Starynkevitch Jun 17 '17 at 09:31

1 Answers1

-2

While you've not initialized the variable, the variable does refer to come location in memory.

This location's value when converted to integer would yield something , and in your case it's -28762

Please note that when you declare any simple data types like int, float etc this happens.

For complex types like User defined types, and structures this will not happen.

The integer variables are not defaulted to zero, unless they are file scope or static. See reference link https://msdn.microsoft.com/en-us/library/y2xtdbay.aspx

If the declaration of z was for an uninitialized static variable or was at file scope, it would receive an initial value of 0, and that value would be unmodifiable.

DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
  • Is -28762 ..the location in memory..????? – Nitesh Jun 17 '17 at 08:54
  • No but when you declare int, it's assigned a 2 byte memory location. There could be any thing stored at that location – DhruvJoshi Jun 17 '17 at 08:56
  • When is the case that...default value of an integer i.e., '0' is assigned to 'a'..???????? – Nitesh Jun 17 '17 at 08:58
  • 2
    @Nitesh: *local* (automatic) variables are never initialised to a default value. Static/global variables *are* initialised to 0 however. – Paul R Jun 17 '17 at 09:00
  • When i use a pointer *p.. such that p=&a; when i print *p ..it prints value of a and use increment ++p; it changes its location by 2.. now..when i print *p...it gives output=0.. why???pls give an explanation..since i'm a beginner.. – Nitesh Jun 17 '17 at 09:02
  • @Nitesh It is Undefined Behavior to print the value of a local non-`static` uninitialized variable. Once you invoke UB, anything could be the result. – Spikatrix Jun 17 '17 at 09:08
  • @alk Doesn't Turbo C use the same single Borland compiler for both C and C++ programs? I agree that the question is tagged C, though – Spikatrix Jun 17 '17 at 09:22
  • @CoolGuy If I remember correctly Turbo C 2.0 became Turbo C++ 1.0 some when around 1990. – alk Jun 17 '17 at 09:27
  • *While you've not initialized the variable, the variable does refer to come location in memory.* Not true. Could be register allocated. Standard doesn't say that all local variables should have a place in memory – Ajay Brahmakshatriya Jun 19 '17 at 13:41