C in short

First C program

Hello, world: On the internet...

Wikipedia - Zentut - Stackoverflow

Hello, world

  • Create a new work directory (on P)

  • Edit a text file hello.c (SourceCode/notepad++)

#include <stdio.h>

int main()
{
  puts("Hello World\n");
  return 0;
}
  • 1 - Call to the library ``Standard Input/Output''

  • 3 - Declaration of the ``main'' function (program entrance)

  • 4-7 - Implementation of the function : block of instructions

  • 5 - Posting to the standard output

  • 6 - Returned value by the program (0, it's ok)

ToDo: Difference between puts and printf ?

Exécutable

Compile with GCC sous GNU Bash

GNU (GNU is Not Unix) an Operating System open created in 1983.

Few commands:

  • ls: list directory contents (ls -la /global/path)

  • cd: change directory (cd relative/path)

  • cp: copy paste (mv to move)

  • rm: remove (rmdir for directories)

  • man: manuals of commands and libraries ('Q' to quit)

And many others: apropos, whereis, clear, egrep, ...

MSYS and MSYS2 or Cygwin offer an alternative under windows.

Compile with GCC

Compile source code to target machine:

Some flags:

  • -std=c99 : Use C99 standard

  • -Wall -Wextra : Verbose compilation with warning messages (all and more)

  • -gdb : unable gdb to interact with the generated program

  • ... : code optimization, compute for a diferent target machine, ...

Link component:

Example :

Language elements

First instructions in C

Example:

Basic types

  • int: standard integer

  • unsigned int: unsigned integer (~ absolut value)

  • short, unsigned short: small integer

  • long, unsigned long: long integer (double of int memory size)

  • long long, unsigned long long: very long integer (double of long)

  • char, unsigned char: character (eq. very small int)

  • float: standard floating point number

  • double: more precise than float

  • long double:very precise floating point number (double of double)

Functions

regroups a block of instructions [return one value]

Tables

Control structures - Logic

Attention: no boolean, only integers are manipulated.

Control structures - switch

You can see it as a succesion of several ``if ( == )''

Control structures - loops

Executing several-time a sucession of instriuctions:

Specific programe variables

  • struct: No classes, but the possibility to group variables together.

  • enumeration: define a finite domain

  • typedef: define new types.

Example:

Example (typedef):

Last updated

Was this helpful?