You are on page 1of 34

Programming with C

Basics
Vania Marangozova-Martin@imag.fr
M1 MOSIG
2016-2017

Training in C
u

4 slots of 2 hours
v
v
v
v

Tuesday: Basic syntax, arrays, basic compilation


Wednesday: Syntax, structures, modularity
Thursday: Pointers
Friday: Pointers, Programming tools

M1 MOSIG Programming with C

V. Marangozova-Martin

Introduction
u

Help you identify and learn the necessary


elements for your programming work during
the school year

A short introduction to C programming


v
v
v

Some of the syntax


Structure of a C program
Compilation

To be followed by a practical session

M1 MOSIG Programming with C

V. Marangozova-Martin

A (very) short history of C


Invented in 1972 by Dennis Ritchie, Bell Labs
u Need for a language to write the UNIX OS in a
portable way
u

Up till then, OS was written in assembler

C was normalized in 1989 by the


American National Standards Institute (ANSI)
v
v

ANSI C
To guarantee that a C program can be recompiled
and executed across different hardware architectures

M1 MOSIG Programming with C

V. Marangozova-Martin

A first C Program
A special function
u Executed when the program is launched
u

int main(void) {
return 0;
}

Doesnt do anything J

M1 MOSIG Programming with C

V. Marangozova-Martin

Let us try
int main(void) {
return 0;
}

compile and get an executable in first

source code in first.c


Binary
first
run

M1 MOSIG Programming with C

V. Marangozova-Martin

This is why the first program always is


#include <stdio.h>
int main(void) {
printf("Hello World\n");
}

M1 MOSIG Programming with C

V. Marangozova-Martin

Use some variables


to do some computation
#include <stdio.h>
int main(void) {
int x;
int y = 15;
x = 10;
printf("The sum is %d\n", x+y);
}

[~/Code/Enseignement/C_MOSIG] ./ssum
The sum is 25

M1 MOSIG Programming with C

V. Marangozova-Martin

Use variables of different types


#include <stdio.h>
int main(void) {
int x = 10;
char a = 'b';
long z = 2147483646;
float y = 10.0;
const float pi = 3.14;
printf("int value is %d, with size %lu\n", x, sizeof(x));
printf("char value is %c, with size %lu\n", a, sizeof(a));
printf("long value is %ld, with size %lu\n", z, sizeof(z));
printf("float is %f, with size %lu\n", y, sizeof(y));
}
[~/Code/Enseignement/C_MOSIG] ./vars
int value is 10, with size 4
char value is b, with size 1
long value is 2147483646, with size 8
float is 10.000000, with size 4
M1 MOSIG Programming with C

V. Marangozova-Martin

Use variables of different types


#include <stdio.h>
int main(void) {
int x = 10;
char a = 'b';
long z = 2147483646;
float y = 10.0;
const float pi = 3.14;

No booleans in C
0 is false
In addition to const
static,
extern,

printf("int value is %d, with size %lu\n", x, sizeof(x));


printf("char value is %c, with size %lu\n", a, sizeof(a));
printf("long value is %ld, with size %lu\n", z, sizeof(z));
printf("float is %f, with size %lu\n", y, sizeof(y));
}
[~/Code/Enseignement/C_MOSIG] ./vars
int value is 10, with size 4
char value is b, with size 1
long value is 2147483646, with size 8
float is 10.000000, with size 4
M1 MOSIG Programming with C

V. Marangozova-Martin

10

Simple operators
int x = 3;
x = x+4;
x += 3;
x = x+1;
x++;
z = x++;
z = --x;

//z = x; x = x +1;
//x = x-1; z = x;

//this is a single line comment


/* this is a multiple line
comment
*/
M1 MOSIG Programming with C

V. Marangozova-Martin

11

Inside a C program: arithmetic operators


+, -, * : addition, substraction,
multiplication
u / : division
u % : modulo
u

<,<=,>,>= : comparison
u != : different
u == : equal
u

M1 MOSIG Programming with C

V. Marangozova-Martin

12

Multiple functions in a file


#include <stdio.h>
int main(void) {
int x;
int y = 15;
x = 10;

#include <stdio.h>
int sum(int a, int b) {
return a+b;
}

printf("The sum is %d\n",


x+y);
}

int main(void) {
int x;
int y = 15;
x = 10;
printf("The sum is %d\n",
sum(x,y));
}

M1 MOSIG Programming with C

V. Marangozova-Martin

13

Multiple functions in a file :


declaration order
#include <stdio.h>
int sum(int a, int b) {
return a+b;
}

#include <stdio.h>
int main(void) {
int x;
int y = 15;
x = 10;

int main(void) {
int x;
int y = 15;
x = 10;

printf("The sum is %d\n",


sum(x,y));
}

printf("The sum is %d\n",


sum(x,y));
}

M1 MOSIG Programming with C

int sum(int a, int b) {


return a+b;
}

V. Marangozova-Martin

14

Multiple functions in a file :


declaration order
#include <stdio.h>

#include <stdio.h>
int sum(int a, int b);
int main(void) {
int x;
int y = 15;
x = 10;

int main(void) {
int x;
int y = 15;
x = 10;
printf("The sum is %d\n",
sum(x,y));

printf("The sum is %d\n",


sum(x,y));

int sum(int a, int b) {


return a+b;
}

int sum(int a, int b) {


return a+b;
}

M1 MOSIG Programming with C

V. Marangozova-Martin

15

Read values from the standard input


#include <stdio.h>
int sum(int a, int b) {
return a+b;
}
int main(void) {
int x, y;
printf("Enter an int value for x:");
scanf("%d", &x);
printf("Enter an int value for y:");
scanf("%d", &y);
printf("The sum of x and y is %d\n", sum(x,y));
}

M1 MOSIG Programming with C

V. Marangozova-Martin

16

Inside a C program: control structures, loops


u

for

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

//i takes the values from 0 to 10

while

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

M1 MOSIG Programming with C

int i = 0;
do {
printf("%d\n", i);
i = i + 1;
} while (i < 10);

V. Marangozova-Martin

17

Data structures: arrays


u

An array represents a sequence of elements


0

'a'

's'

'v'

's'

'3'

This is an array of 5 elements

In C, the elements are numbered starting from 0

The elements have all the same type

Here, all elements are characters

To declare this array, we can do

char my_array[5];

M1 MOSIG Programming with C

V. Marangozova-Martin

18

Data structures: arrays


#include<stdio.h>
int main(void) {
int i;
int tab[10]; /* declare an array of 10 integers */
/* indexes start at 0 */
for (i = 0; i < 10; i++) { //initialise the array
tab[i] = i;
}
/* print the values */
for (i = 0; i< 10; i++)
printf("The %d-th element of the array is: %d\n",
i, tab[i]);
}

M1 MOSIG Programming with C

V. Marangozova-Martin

19

Data structures: arrays


#include<stdio.h>
int main(void) {
int i;
int tab[10]; /* declare an array of 10 integers */
/* indexes start at 0 */
for (i = 0; i < 10; i++) { //initialise the array
tab[i] = i;
[~/Code/Enseignement/C_MOSIG]
}
The 0-th element of the array
The 1-th element of the array
/* print the values */ The 2-th element of the array
for (i = 0; i< 10; i++) The 3-th element of the array
printf("The %d-th element
of the
array of
is:
%d\n",
The 4-th
element
the
array
i, tab[i]); The 5-th element of the array
}
The 6-th element of the array
The 7-th element of the array
The 8-th element of the array
The 9-th element of the array
M1 MOSIG Programming with C

V. Marangozova-Martin

./tab1
is: 0
is: 1
is: 2
is: 3
is: 4
is: 5
is: 6
is: 7
is: 8
is: 9
20

Using arrays : the MIN example


#include <time.h>
#include<stdio.h>
int main(void) {
int i, min;
int tab[10];
srand(time(NULL));
for (i = 0; i < 10; i++) { //initialize with random values
tab[i] = rand();
}
for (i = 0; i< 10; i++)
printf("The %d element of the array is: %d\n", i, tab[i]);
min = tab[0];
for (i = 1; i< 10; i++)
if (min > tab[i]) min = tab[i];
printf("The MIN is %d\n", min);
}

M1 MOSIG Programming with C

V. Marangozova-Martin

21

Initialize an array statically (1)


int i;
int tab[10] = { 1 , 5 , 45 , 3 , 9, 1, 1, 1, 2, 65 };
/* print the values */
for (i = 0; i< 10; i++)
printf("The %d-th element of the arry is: %d\n", i, tab[i]);

M1 MOSIG Programming with C

V. Marangozova-Martin

22

Initialize an array
from the standard input (2)
#include<stdio.h>
int main(void) {
int i, N;
printf("Give the number of elements: ");
scanf("%d", &N);
int T[N];
for (i = 0; i< N; i++) {
printf("Type the %d-th value: ", i);
scanf("%d", &T[i]);
}
for (i = 0; i< N; i++)
printf("The %d-th element of the array is: %d\n", i, T[i]);
}
M1 MOSIG Programming with C

V. Marangozova-Martin

23

Initialize an array
from the standard input (2)
#include<stdio.h>
int main(void) {
int i, N;
printf("Give the number of elements: ");
scanf("%d", &N);
int T[N];
for (i = 0; i< N; i++) {
printf("Type the %d-th value: ", i);
scanf("%d", &T[i]);
[~/Code/Enseignement/C_MOSIG] ./tab2
}
Give the number of elements: 3
for (i = 0; i< N; i++) Type the 0-th value: 456
Type the
1-tharray
value:
printf("The %d-th element
of the
is:98%d\n", i, T[i]);
Type the 2-th value: 34
}
The 0-th element of the array is: 456
The 1-th element of the array is: 98
The 2-th element of the array is: 34
M1 MOSIG Programming with C

V. Marangozova-Martin

24

Initialize an array from a file (3)


#include<stdio.h>
#include <stdlib.h>
int main(void) {
int result, i, N;
FILE *f;
f = fopen("values.txt", "r");
if (f == NULL) {
perror("Opening values.txt");
exit(1);
}
result = fscanf (f, "%d", &N);
if (result != 1) {
perror("Error reading size of array in values.txt");
exit(1);
}
...
M1 MOSIG Programming with C

V. Marangozova-Martin

1/2
25

Initialize an array from a file (3)


...
int T[N];
for (i = 0; i< N; i++)
result = fscanf(f, "%d", &T[i]);
if (result != 1) {
perror("Error reading values in values.txt");
exit(1);
}
}
for (i = 0; i< N; i++)
printf("The %d-th element of the array is: %d\n", i,
2/2
T[i]);
}

M1 MOSIG Programming with C

V. Marangozova-Martin

26

Read values from the command line


u

The main actually is

#include <stdio.h>
int main(int argc, char** argv) {
//argc gives the number (count) of args
//argv is an array of strings and contains
// the values of args
printf("The number of args is %d\n", argc);
for (int i = 0; i< argc; i++)
printf("The %d arg is %s\n", i, argv[i]);
return 0;
[~/Code/Enseignement/C_MOSIG] ./coml 1 24 ggg opaaaa
}
The number of args is 5
The 0 arg is ./coml
The 1 arg is 1
The 2 arg is 24
The 3 arg is ggg
The 4 arg is opaaaa
M1 MOSIG Programming with C

V. Marangozova-Martin

27

Compiling a C Program
u

gcc compiler
v
v
v
v

compile the program in test1.c


activate all warnings
enforce C99 standard
create the test1 executable

> gcc o test1 Wall std=C99 test1.c

Run the program

Without
the o option,
the
executable
has the
default name
a.out

> ./test1
M1 MOSIG Programming with C

V. Marangozova-Martin

28

Inside a C program: control structures, tests


u

if

int main(void)
{
int a, b;
a = 3;

int main(void)
{
int a;
a = 3;
if (a == 5)
a++; //one
else
a--;

if (a != 5) {
//block of instructions

a++;
b= 3*a;
} else {
a--;
b = !a;
}
return 0;

instruction

return 0;
}
M1 MOSIG Programming with C

}
V. Marangozova-Martin

29

Inside a C program: control structures, tests


u

switch

main()
{
char Grade = 'A';
switch( Grade ) {
case 'A' :
printf( "Excellent\n" );
break;
case 'B' :
printf( "Good\n" );
break;
case 'C' :
printf( "OK\n" );
break;
default :
printf( Not good enough\n" );
}
}

M1 MOSIG Programming with C

V. Marangozova-Martin

30

Inside a C program: control structures, loops


u

for

Instructions
break and continue
change the iteration.

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

while

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

M1 MOSIG Programming with C

int i = 0;
do {
printf("%d\n", i);
i = i + 1;
} while (i < 10); = i + 1;

V. Marangozova-Martin

31

Inside a C program: input/output


int main() {
int c;
printf("Enter a number\n");
scanf("%d",&c);
printf("Number=%d",c);
printf(Print a character %c\n, a);
printf(Print a float %f\n, 3.14);
printf(Print a string %s\n, Im fine!);
printf("%3d\n",b); //using 3 digits
printf(%1.2f\n, 3.14159); /* one digit
for whole part,
two digits
after the decimal */
return 0;
}
M1 MOSIG Programming with C

V. Marangozova-Martin

32

What now?
We continue with a lab
u There are plenty of references on the web
u Do not hesitate to ask questions,
whatever the questions J
u

M1 MOSIG Programming with C

V. Marangozova-Martin

33

Useful References
u

Newbies
v

Free C supports
v
v

http://www.cmi.univ-mrs.fr/~contensi/coursC

http://www-verimag.imag.fr/~moy/cours/poly-c/
http://h.garreta.free.fr/polys/PolyC.pdf

Wikipedia book
v

https://fr.wikibooks.org/wiki/Programmation_C/Introduction

M1 MOSIG Programming with C

V. Marangozova-Martin

34

You might also like