Programming in C

Introduction

Gerald Senarclens de Grancy

The C Programming Language

What is C?

Application Areas

  • Operating systems (eg. Linux, Android)
  • Implementation of programming languages (eg. cpython)
  • High performance computations
  • Application software (eg. GNOME Desktop, GIMP, ...)
  • Embedded systems
  • "Green Computing"

History

1978
Brian Kernighan and Dennis Ritchie (K&R) published
"The C Programming Language"
C99
Still not supported by all common C compilers
C11, C17

Structure of a C Program

#include <stdio.h>

/* Hello World program written in C. */
int main(void) {
printf("Hello, World!\n"); // output some text
return 0; // 0 means success
}

Visualize execution

Preprocessor Directives

Allow to include code declared elsewhere

#include <stdio.h>

/* Hello World program written in C. */
int main(void) {
printf("Hello, World!\n"); // output some text
return 0; // 0 means success
}

Comments

// single line comment
int x = 5; // comments can be added at the end of a line

/*
* multiline comment
* ...
*/

Removed by the preprocessor

Main Function

Serves a special purpose in C programs:
program execution begins with main(.)

#include <stdio.h>

/* Hello World program written in C. */
int main(void) {
printf("Hello, World!\n"); // output some text
return 0; // 0 means success
}

Return Value

Indicate success (0) or a failure code to the calling operating system shell

#include <stdio.h>

/* Hello World program written in C. */
int main(void) {
printf("Hello, World!\n"); // output some text
return 0; // 0 means success
}

Compiling and Executing a C Program

For creating executables, a compiler toolchain is needed.

  • GCC - GNU Compiler Collection, GCC
  • Clang - C language family frontend for LLVM
  • MSVC - Microsoft Visual C and C++ compiler

Creating a C executable is a four step process.

  1. Preprocessor
  2. Compiler
  3. Assembler
  4. Linker

Example

Download hello.c

By default, all four stages (preprocessor, compiler, assembler and linker)
are done

clang hello.c  # or `gcc hello.c`
./a.out

Provide a name for the created executable (instead of the default a.out

clang hello.c -o hello
./hello

Compilation can be done with either clang or gcc.

clang $INFILE -o $PROGRAM_NAME
gcc $INFILE -o $PROGRAM_NAME

1. Preprocessor

  • Essentially a pure text processor
  • Removes comments, perform includes etc.
  • Usually never done separately

Stop after preprocessor and send output to stdout

clang -E hello.c

2. Compiler

  • Translates high level code to a lower level (assembly language)
  • Parses the source code
  • Performs type checking
  • Usually never done separately

Creates assembler files (usually *.s)

clang -S hello.c

3. Assembler

  • Translates assembly language to object code (machine language)
  • Creates *.o files
  • Usually done separately when working with multiple input files
    • Impossible to create executables without main(.)
    • Impossible to create executables with multiple main(.)
clang -c hello.c

4. Linker

  • Combines object files into a unified executable program, resolving all symbols
  • Has to be run separately when working with multiple input files
clang -o hello hello.o

Questions
and feedback...