FA-ProjInfo
  • Introduction
  • Plan du module
  • S1 - KickOff
    • NetWorld
    • Intro to C
    • C in short
    • Lib: Raylib
    • Les groupes
    • Go go go!
  • S2 - Introduction to Game Engine
    • What is a game and a game engine ?
    • Star-Up & Shut-Down
    • The Game/Main Loop
    • Programming Input Devices
    • Resources managment
    • NetWorld outline
  • S3 - Collaborative Project
    • Versionner avec GIT
    • Project Management in online GIT plateform
    • Automatiser la construction
    • Documenter
  • S4 - Rendu et Evaluation
    • Process de l'évaluateur
  • Tools
    • FAQ
Powered by GitBook
On this page
  • First C program
  • Hello, world: On the internet...
  • Hello, world
  • Exécutable
  • Compile with GCC sous GNU Bash
  • Compile with GCC
  • Language elements
  • First instructions in C
  • Basic types
  • Functions
  • Tables
  • Control structures - Logic
  • Control structures - switch
  • Control structures - loops
  • Specific programe variables
  • Example:
  • Example (typedef):

Was this helpful?

  1. S1 - KickOff

C in short

PreviousIntro to CNextLib: Raylib

Last updated 4 years ago

Was this helpful?

First C program

Hello, world: On the internet...

- -

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:

gcc -c [FLAGS] file.c

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:

gcc -o program file.o file2.o file3.o ... [LIBS]

Example :

gcc -c -std=c99 -Wall -Wextra hello.c
gcc -o hello hello.o
./hello

Language elements

First instructions in C

// Declaration:
//--------------
Type nomVariable;

// Assignment:
//--------------
nomVariable = expression;

// Call of function:
//--------------
functionName( parameter1, parameter2, ... );
expression operator (expression ... );

Example:

int a;
int b= 3;
a= (b + 6) / 2;
printf("%i + 6 / 2 = %i", b, a);
sleep(3); // Sleep(3000) under Windows

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]

// Declaration :
//--------------
type myFct ( type param1, type param2, ...);

// Instanciation :
//----------------
type myFct ( type param1, type param2, ...)
{
  instruction;
  instruction;
  return expression;
}

// Call :
//--------
[var= ] myFct(var1, var2, ... );

Tables

// Declaration :
//--------------
type tabName [ N ]; // number of N elements

// Declaration and assignment :
//-----------------------------
type nomTab [ N ] = {var1, ..., varN};

// one element :
//--------------
... tabName[i] ... ;

Control structures - Logic

Attention: no boolean, only integers are manipulated.

if ( condition ){
  instructions;
}
else {
  instructions;
}

/*
condition : int/short ... : (0)false or true
condition && condition : and
condition || condition : or
!condition : no
( condition ) : priority
comparaison operators : == < > <= >=
*/

Control structures - switch

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

switch ( expression ){
  case value1 :
    instructions;
    break;

  case value2 :
      ...

  case valueN :
    instructions;
    break;

  default :
    instructions;
    break;
}

Control structures - loops

Executing several-time a sucession of instriuctions:

while ( condition )
{
  instructions;
  instructions;
  ...
}

do
{
  instructions;
  instructions;
  ...
}while ( condition )

for ( initialization ; condition ; iteration )
{
  instructions;
  instructions;
  ...
}

Specific programe variables

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

    struct my_structure{
    type1 name_variable1;
    type2 name_variable2;
    type3 name_variable3;
    ...
    };
  • enumeration: define a finite domain

    enum color {
    blue= 0, red, green
    };
  • typedef: define new types.

    typedef description name_of_the_new_type;

Example:

#include <stdio.h>
#include <math.h>

struct complex{
  double a;
  double b;
};

double modulus( struct complex c );

int main(){
  struct complex aComplex;
  aComplex.a= 2.3;
  aComplex.b= 5.7;
  printf( "module: %lf\n", modulus(aComplex) );
  return 0;
}

double modulus( struct complex c ){
  return sqrt( c.a*c.a + c.b*c.b );
}

Example (typedef):

#include <stdio.h>
#include <math.h>

struct complex{
  double a;
  double b;
};

typedef struct complex Complex;

double modulus( Complex c );

int main()
{
  Complex aComplex;
  aComplex.a= 2.3;
  aComplex.b= 5.7;
  printf( "module: %lf\n", modulus(aComplex) );
  return 0;
}

double modulus( Complex c ){
  return sqrt( c.a*c.a + c.b*c.b );
}
Wikipedia
Zentut
Stackoverflow