Exforsys
+ Reply to Thread
Results 1 to 2 of 2

Why initialization Essential

This is a discussion on Why initialization Essential within the C and C++ forums, part of the Programming Talk category; I had a quick small doubt when I was doing my programming with C. Is it essential that all global ...

  1. #1
    caradoc is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    122

    Exclamation Why initialization Essential

    I had a quick small doubt when I was doing my programming with C. Is it essential that all global variables in C must be initialized to zero? What is the reason behind this initialization? Say if I code a program as below:

    #include <stdio.h>
    main()
    {
    int i;
    printf("%d",i);
    }

    Here I have not initialized the global variable i. In this case will the output be junk value. Why is it so can someone kindly explain me what happens inside the system in this context.


  2. #2
    sammy is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    144
    It is a must that you must initialize the global variables in C programming language. Now coming to your next question of whether it is to be initialized with zero. The reason is simple. If you are not initializing with zero the memory would have some junk value which would be used in the program when the memory area is accessed. So make it a practice to always initialize global variables in C programming language with zero.


Latest Article

Network Security Risk Assessment and Measurement

Read More...