C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
int ctr = 0;
printf("Counter is at %d.\n", ++ctr);
printf("Counter is at %d.\n", ++ctr);
printf("Counter is at %d.\n", ++ctr);
printf("Counter is at %d.\n", ++ctr);
printf("Counter is at %d.\n", ++ctr);
printf("Counter is at %d.\n", --ctr);
printf("Counter is at %d.\n", --ctr);
printf("Counter is at %d.\n", --ctr);
printf("Counter is at %d.\n", --ctr);
return 0;
}

Note

To understand the difference between prefix and postfix, move all the increment and
decrement operators to after the ctr variables (ctr++ and ctr--). Can you guess
what will happen? Compile it and see if you are right!

Sizing Up the Situation


You use sizeof() to find the number of memory locations it takes to store values of any data type.
Although most C compilers now use 4 bytes to store integers, not all do. To find out for sure exactly
how much memory integers and floating points are using, you can use sizeof(). The following
statements do just that:


Click here to view code image


i = sizeof(int); // Puts the size of integers into i.
f = sizeof(float); // Puts the size of floats into f

sizeof() works on variables as well as data types. If you need to know how much memory
variables and arrays take, you can apply the sizeof() operator to them. The following section of
code shows you how:


Click here to view code image


char name[] = "Ruth Claire";
int i = 7;
printf("The size of i is %d.\n", sizeof(i));
printf("The size of name is %d.\n", sizeof(name));

Here is one possible output from this code:


The size of i is 4
The size of name is 12

Depending on your computer and C compiler, your output might differ because of the differences in

Free download pdf