ch2-Uninitialized variables

Chapter_2     Exercise_2-1     enum Leap_year     Exercise_2-2







uninit.c     K&R, p. 40         download


#include <stdio.h> // for printf()
// uninitialized vars
#define SIZE
#define LENGTH 0

int global;
const int cig;
int const icg;

int main()
{
printf("global vars: ");
printf("%d, %d, %d\n", global, cig, icg);
global++;
// cig++; // compile error:
// icg++; // cannot increment read-only vars
global = 1;
// cig = 0; // compile error:
// cig = 1; // cannot assign a value to read-only vars
// icg = 1;

int local;
const int cil;
int const icl;

printf("local vars: ");
printf("%d, %d, %d\n", local, cil, icl);

local++;
// cil++; // error
// icl++;

local = 1;
// cil = 1; // error
// icl = 1;

static int si;
int static is;
const static int csi;
const int static cis;
static int const sic;
static const int sci;
int static const isc;
int const static ics;

printf("static vars: ");
printf("%d, %d, %d, %d, %d, %d, %d, %d\n", si, is, csi, cis, sic, sci, isc, ics);

const char empty[0]; // must have a size
char const msg[] = "hello";

printf("empty[]: %s\n", empty);
printf("msg[]: %s\n", msg);

// empty[0] = '\0'; // compile error:
// msg[0] = 'c'; // cannot assign to read-only vars

enum boolean {FALSE, TRUE};

// FALSE = TRUE; // compile error:
// FALSE++; // (true lies not allowed)
// LENGTH++ // not lvalues
SIZE;
SIZE 100;
LENGTH;
// LENGTH 10; // error
int size = SIZE 20;
printf("size = %d\n", size);

// printf("%d, %d\n", SIZE, LENGTH); // error
printf("SIZE 0: %d, LENGTH: %d\n", SIZE 0, LENGTH);
printf("SIZE 10: %d\n", SIZE 10);

return 0;
}
/*
gcc uninit.c -o uninit
./uninit
global vars: 0, 0, 0
local vars: 0, 0, 379084928 // garbage values
static vars: 0, 0, 0, 0, 0, 0, 0, 0
empty[]: �hello // garbage value
msg[]: hello
size = 20
SIZE 0: 0, LENGTH: 0
SIZE 10: 10
*/









Chapter_2     Exercise_2-1     enum BACK_TO_TOP Leap_year     Exercise_2-2



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo