SiteBanner

By looking at our program's variables before (then after) initialization, it just makes sense to take care of setting values as we design our code. 

 

  It's OK that this code throws a warning at compile time.


 

(right-click to download)

/* pgm13 source */

#include <stdio.h>
#include <string.h>

#define STRING_SIZE 9

int main(void) {
     char data[STRING_SIZE];
     int intval;
     int x;

     // neither variable 'data[]' or 'intval' have been initialized
     // whatever shows up via 'printf()' is ARBITRARY at this point

     printf("\nintval contains %d\n\n", intval);
     for (x = 0; x < STRING_SIZE; x++)
          printf("data[%d] holds '%c' (hexadecimal: %x)\n", x, data[x], data[x]);

     // now we purposefully initialize our variables
     strcpy(data, "ZEBRA");
     intval = 443322;

     printf("\nintval contains %d\n\n", intval);
     for (x = 0; x < STRING_SIZE; x++)
          printf("data[%d] holds '%c' (hexadecimal: %x)\n", x, data[x], data[x]);

     printf("\n");
     return(0);
     }