View on GitHub

Computer Architecture and Operating Systems

Course taught at Faculty of Computer Science of Higher School of Economics

Lecture 2

C Programming Language

Before Start: Using Linux Ubuntu in the Cloud.

Lecture

Slides (PDF, PPTX).

Outline

Theory

Builtin data types:

char
unsigned char
short
unsigned short
int 
unsigned int
long
unsigned long
float
double
__int8_t
__int16_t
__int32_t
__int64_t
void *
size_t

Structures:

struct point {
    int x;
    int y;
};
struct point {
    int x;
    int y1 : 16;
    int y2 : 16;
};

Size of various data types:

#include <stdio.h>

typedef struct {
    int x;
    int y;
} point_t;

int main() {
    printf("sizeof(char)   = %ld\n", sizeof(char));
    printf("sizeof(int)    = %ld\n", sizeof(int));
    printf("sizeof(long)   = %ld\n", sizeof(long));
    printf("sizeof(float)  = %ld\n", sizeof(float));
    printf("sizeof(double) = %ld\n", sizeof(double));
    printf("sizeof(void *) = %ld\n", sizeof(void *));
    printf("sizeof(point)  = %ld\n", sizeof(point_t));
    return 0;
}   

Input/output:

int x;
scanf("%d", &x);
printf("%d", x);

Functions and function pointers:

#include <stdio.h>

void print(int x, int y) {
    printf("%d %d\n", x, y);
}

typedef void (* func_t)(int, int);

void test(func_t func) {
    (*func)(10, 20);
}

int main() {
    void (* func )(int, int) = &print;
    (*func)(10, 20);
    test(func);
    return 0;
}

Dynamic memory allocation (use malloc and free):

#include <stdio.h>

int main() {
    int i, n;
    printf("Enter array size:\n");
    scanf("%d", &n);

    int* array = malloc(sizeof(int) * n);
    printf("Enter array:\n");
    for (i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }
    printf("Array:\n");
    for (i = 0; i < n; i++) {
        printf("array[%d] = %d\n", i, array[i]);
    }
    free(array);

    return 0;
}

Workshop

Outline

Debugging C programs

Tasks

  1. Write a program in C, which does the following:
    • defines the point structure;
    • inputs an integer value N;
    • allocates an array of N points (assume the coordinates a non-negative);
    • inputs N points;
    • inputs a point;
    • finds the point closest to the specified point in the array of points.

    Notes: use malloc and free to allocate and deallocate the array respectively.

Homework

  1. Write a program in C that inputs two integer values x and y, call function swap that takes the values as arguments and swaps them, prints the values after the swap.

  2. Write a program in C, which does the following:
    • inputs an integer value ā€˜Nā€™;
    • allocates an array of ā€˜Nā€™ integer elements;
    • fills the array with integer values from the standard input;
    • reverses the array;
    • prints the resulting array;
    • dellocates the array.

    Notes: use malloc and free to allocate and deallocate the array respectively.

  3. Write a program in C, which does the following:
    • inputs two integer values N and M;
    • allocates a matrix of size N * M and fills it with values from standard input;
    • transposes the matrix;
    • prints the resulting matrix;
    • deallocate the matrices.

    Note: the matrices must be allocated with malloc and deallocated with free.

  4. Write a program in C, which does the following:
    • creates a singly-linked list;
    • add to the list numbers from the standard input until user inputs 0;
    • reverses the list;
    • prints the resulting list;
    • deallocates the list.

    Note: use malloc and free to allocate and deallocate list entries respectively.

References