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

1969-1973
Development at Bell Labs as successor to B
In parallel with the beginnings of the Unix operating system
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

Executable program files are created from readable source code.

[c source icon]
main.c
[c source icon]
executable

For creating executables, a compiler toolchain is needed.

  • GCC - GNU Compiler Collection
  • 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 ` 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 .

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

1. Preprocessor

  • Essentially a pure text processor
  • Removes comments, performs 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
  • Must recognize all identifiers (function declarations etc.)

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
  • Should be run separately when working with multiple input files
clang -o hello hello.o

Questions
and feedback...