Intro to C-programming


Part 1: Getting started

Hello World

#include<stdio.h>

int main(void) {
	printf("Hello world!\n");
	return 0;
}
Compile using:
gcc hello_world.c
which will generate a.out

Run using:

./a.out


You can (and are recommended to) compile with several flags to the compiler:

  • -g = debug information
  • -Wall = All warnings are displayed
  • -O9 = Optimization

Which makes it:

gcc -g -Wall hello_world.c

Variable types

short/long can specify size.

signed (use 1 bit for sign)/unsigned

const int i = 42;

i is not a variable.

You can declare a variable static, and if done in global scope, use it inside functions etc.

#include<stdio.h>

static int i = 0;

int main(void) {
    printf("%i\n", i);
    i = i+1;
    printf("%i\n", i);
    return 0;
}

Prefixing variables with extern makes them globally visible from other files.

Strings and arrays

"Hello"
is the same as
char str[] = {'H','e','l','l','o','\0'}

We will cover much more on strings when we get to pointers.

#include<stdio.h>

int main(void) {
    char str[] = {'H','e','l','l','o','\0'};
    printf("%s - %s\n", str, "Hello");
    return 0;
}

Enums

#include<stdio.h>

int main(void) {
    enum firstmonths{JAN,FEB,MAR,APR};
    enum lastmonths{NOV=10,DEC=11};
    printf("%i\n", JAN);
    printf("%i\n", NOV);
    return 0;
}

All enumerated values must be different, and will start with 0 if not explicitly defined.

Operators

Comparison:

Logic

Bitwise operators

#include<stdio.h>

static unsigned int binary0110 = 6;
static unsigned int binary0010 = 2;

int main(void) {
    printf("%i\n", binary0110 & binary0010 );
    printf("%i\n", binary0110 | binary0010 );
    printf("%i\n", binary0110 ^ binary0010 );
    printf("%i\n", binary0110 << 1 );
    printf("%i\n", binary0110 >> 1 );
    return 0;
}

Printf

Arguments
%i
int/decimal numner
%u
Unsigned number
%c
Single character
%s
Prints a string
%p
Pointer address

There are more arguments to specify adjustment and width, check the book/manual.

If you create a segfault, and you do not use \n with your printf, you are not guaranteed that it is printed!

Increment/decrement

++ and -- can be used both before and after an operator.

i++
Increments i after its value is used
++i
Increments i before its value is used
#include<stdio.h>

int main(void) {
    int i,j,before,after;

    i = 0;
    j = 0;

    before = ++i;
    after = j++;

    printf("Before: %i After: %i i: %i, j: %i\n", before, after, i, j);
    return 0;
}

Control Flow

Looks like java/python etc.
#include<stdio.h>

int main(void) {
    int i;

    for( i = 0; i < 10; i++) {
        printf("i: %i\n", i);
    }

    i = 0;
    while( i < 10 ) {
        printf("i: %i\n", i);
        i++;
    }

    i = 1;
    if( i ) {
        printf("if statement\n");
    } else {
        printf("else statement\n");
    }

    switch( i ) {
        case 0:
            printf("i is 0\n");
            break;
        case 1:
            printf("i is 1\n");
            break;
        default:
            printf("i is not 0 or 1\n");
            break;
    }

    return 0;
}

break can be used in both loops and switches to exit them. continue can only be used in loops, and jumps to the beginning of the loop.

Style comments

A nice c-style guide can be found at http://www.doc.ic.ac.uk/lab/cplus/cstyle.html

Tutorial Problems

  1. Create, compile and run the hello world sample.
  2. Write a program, printing the fahrenheit temperature for all celsius temperatures between 0 and 100 (with an interval of 5). The formula is celcius = 5/9(fahrenheit-32)
  3. Experiment with different arguments for printf, both legal and illegal. What happens, when an int is printed as a char or a string? In which cases is the output correct, even through the format is wrong?.
  4. Write a program that can invert a text string, so 'programming' becomes 'gnimmargorp'. It will be necessary to find the length of the string. If the string is given as:
    char s[] = "Hello world"
    it is stored as an array of chars and the null-termination '\0'. Use this null-termination, to find the length. You are allowed to destroy the original string in your program.