CP264 Notes: C Basics


Basic syntax

Comments

//    --  line comment, compiler ignores the line
/*
 *  …
 */    -- block comment, compiler ignores what in between

Keywords

C has 32 reserved words (keywords). Keywords are those words whose meaning is already defined by the compiler.

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Keywords cannot be used as variable names, function names, or constant names.

Program Statement and Block

Program structure

C source code

[pre-processor statements]
[declare global variables and constants]
[declare function prototypes]

int main(int argc, char *args[]) {
    [program statements]
    return 0;
}
[implementation of functions]

Memory map in execution:

Stack:
function local data at runtime


Heap:
dynamic data
Data:
global, static, constant
Text:
Program Functions

Data Types and Variables

Data Type defines how data is stored in memory

Primary data types are defined using keywords
char, int, float, double
Secondary data types
array, pointer, struct, union, enum, etc

Each data type has a size: the number bytes in memory

-

Each addressable memory cell holds 8 bits (1 byte) Primary data types (size of some data type is platform dependent): For example, int can be 2 bytes, 4 bytes, 8 bytes or larger

See C Data Type Sizes

The char type is stored as an integer using ASCII code. E.g. 'A' is coded as 65, 'a' as 97, '+' as 43.

See an ASCII Table

Variables

Declaration

int a;
char c;
float f;

Assign values to variables:

a = 2;       // assignment statement, compiler assign the value 2 to memory space
c = 'a';     // compiler replaces 'a' by  97, then assigns it to the memory space
f = 1.41;

Declaration & initialization:

int a = 2;
int b =  3;
char c = 'a';
float f = 1.41;
int x = 9,  y = 3;

The sizeof Operator

Syntax:

(size_t is an unsigned integer)

Examples:

int n = 0;
sizeof(int): 4
sizeof n: 4
sizeof &n: 4

double d = 0;
sizeof(double): 8
sizeof d: 8
sizeof &d: 4

char c = 'A';
sizeof(char): 1
sizeof c: 1
sizeof &c: 4

int a[10];
sizeof(int [10]): 40
sizeof a: 40
sizeof &a: 4
sizeof a / sizeof a[0]: 10
sizeof a / sizeof *a: 10

typedef struct {
    int x;
    double y;
    char z;
    char *p;
} some_struct;
sizeof(some_struct): 24
sizeof q: 24
sizeof &q: 4

(What is 'strange' about the size of some_struct?)

Constants

Constant value: A fixed value used in program, like 3.1415926

#define PI 3.1415926
float r = 4;
float area = PI*r*r;
float cf = 2*PI*r;

The pre-processor replaces all occurrences of PI in the source code with 3.1415926

Constant variable: Variable with initialized value, but not allowed to change, i.e. read-only variable

const float pi = 3.1415926;
float r = 4;
float area = pi*r*r;
float cf = 2*pi*r;
pi = 3.14;   // this is not allowed by compiler

Type Conversion

int b = 2;
float f1 = 1.5, f2 = 3.5;

f1 = b;          // implicit casting, f1 = 2.0,
f1 = (float) b;  // explicit casting
b = (int) f2;   //  b = 3

Operators and Expressions

The right size of an assignment statement must be an expression. Expressions consists of constants, variables, operators and parentheses. Example:

z = x + y
a = (b + 2) * (c + 4);

Be aware of operation issues

Precedence of Operators

See Operators in C


Flow Control

Sequence

Sequence: linear order. Lines are executed in the order given.

Selection

Repetition (loop)

Example of prompt input

#include <stdio.h>

int main() {

    setbuf(stdout, NULL);

    char c;
    printf("Enter a character ('*' to quit): ");
    c = getchar();

    while (c != '*') {
        getchar(); // throw away enter key

        if (c >= 'a' && c <= 'z') {
            printf("%c %d %c\n", c, c, c - ' ');
        } else {
            printf("Not a lower case letter\n");
        }
        printf("Enter a character ('*' to quit): ");
        c = getchar();
    }
    return (0);
}

continue statement moves to the next iteration of a loop:

/* output the integers between 100 and 200, not divisible by 3*/
#include <stdio.h>

int main(void) {
    int n;
    for (n = 100; n <= 200; n++) {
        if (n % 3 == 0)
            continue;
        printf("%d ", n);
    }
    printf("\n");
}