Read King Chapters 4, 5 and 6

(expressions, simple statements and loops)

Basic C Data Types (King Chapter 7)
 

See King Page 111, for the range of integer values for 32-bit computers.

Use the sizeof operator to determine space needs of datatypes. King P. 489 and 494 give i/o conversion specifier

Declaration        Space          scanf printf

char c;            1 byte           %c    %c

short int p;       2 bytes          %hd   %hd

int q;             4 bytes          %d    %d

unsigned int r;    4 bytes                %u

long int s;        4 bytes, (8 ?)   %ld   %d

float u;           4 bytes          %f    %f

double v;          8 bytes                %f

long double x;     8 bytes, (16 ?)  %le   %e

Variables can be initialized at declaration:

char c = 'd';

int q = 4;

float u = 13.5;

double v = 7.9e-2;

Thus a declaration has the following general format:

Type VariableName = InitialValue ;

The '= InitialValue' part is optional

A pointer to a data item is declared in the following way

Type* VariableName ;

int* ptr;

The type specifies the kind of item that is refered to, here ptr can only point to an integer object. As things stand this is not particularly useful. Later pointers come into their own when we access arrays and objects indirectly with pointers.

Character Constants

Character constants are enclosed in single quotes, for example,

'a', 'b', 'c'.

There are a number of special characters that must be "escaped" so they can be recognized. 
See King P. 119.

The common ones are:

New Line  '\n'

Tab       '\t'

Backslash '\\'

the \ is also used to provide values for octal and hexadecimal numbers. See King page 111.

String Constants (literals)

Literal strings are seen first in print statements, and are enclosed in double quotes. They may form an output message like

printf ("Hello World");

or may appear as the format string for presenting the value of an expression

or variable

printf ("Here we have a fifteen %d", 3*5);

We will deal with string variables later after we cover arrays and pointers.

See also King, Chapter 13.

Arrays
 

For example

int a[100];

produces an array with room for 100 integers. The first element is

a[0] and the last element is a[99]
 

For example

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int b[10] = {-3, -1, 0, 1, 3, 5, 7, 9, 11, 13};

int c[ ] = {-3, -1, 0, 1, 3, 5, 7, 9};
 

For example, a text string can be declared and initialized in the following way:

char string[25] = "This uses 19 bytes";
 

char data[5] = "WORD";

char data[5];
data[0] = 'W';
data[1] = 'O'; /* this is the letter Oh */
data[2] = 'R';
data[3] = 'D';
data[4] = '\0'; /* this is the digit Zero */