Professional Documents
Culture Documents
Basics
Vania Marangozova-Martin@imag.fr
M1 MOSIG
2016-2017
Training in C
u
4 slots of 2 hours
v
v
v
v
V. Marangozova-Martin
Introduction
u
V. Marangozova-Martin
ANSI C
To guarantee that a C program can be recompiled
and executed across different hardware architectures
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
V. Marangozova-Martin
Let us try
int main(void) {
return 0;
}
V. Marangozova-Martin
V. Marangozova-Martin
[~/Code/Enseignement/C_MOSIG] ./ssum
The sum is 25
V. Marangozova-Martin
V. Marangozova-Martin
No booleans in C
0 is false
In addition to const
static,
extern,
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;
V. Marangozova-Martin
11
<,<=,>,>= : comparison
u != : different
u == : equal
u
V. Marangozova-Martin
12
#include <stdio.h>
int sum(int a, int b) {
return a+b;
}
int main(void) {
int x;
int y = 15;
x = 10;
printf("The sum is %d\n",
sum(x,y));
}
V. Marangozova-Martin
13
#include <stdio.h>
int main(void) {
int x;
int y = 15;
x = 10;
int main(void) {
int x;
int y = 15;
x = 10;
V. Marangozova-Martin
14
#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));
V. Marangozova-Martin
15
V. Marangozova-Martin
16
for
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;
}
int i = 0;
do {
printf("%d\n", i);
i = i + 1;
} while (i < 10);
V. Marangozova-Martin
17
'a'
's'
'v'
's'
'3'
char my_array[5];
V. Marangozova-Martin
18
V. Marangozova-Martin
19
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
V. Marangozova-Martin
21
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
V. Marangozova-Martin
1/2
25
V. Marangozova-Martin
26
#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
Without
the o option,
the
executable
has the
default name
a.out
> ./test1
M1 MOSIG Programming with C
V. Marangozova-Martin
28
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
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" );
}
}
V. Marangozova-Martin
30
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;
}
int i = 0;
do {
printf("%d\n", i);
i = i + 1;
} while (i < 10); = i + 1;
V. Marangozova-Martin
31
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
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
V. Marangozova-Martin
34