Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Basics
Vania Marangozova-Martin@imag.fr
M1 MOSIG
2015-2016
Training in C
u
4 slots of 2 hours
v
v
v
v
V. Marangozova-Martin
arr[1]
int M[2][3]; /* two dimensions */
M[2][1]
V. Marangozova-Martin
Array Initialization
/* POSSIBLE INITIALIZATION */
int arr[5] = { 1 , 5 , 45 , 3 , 9 };
int M[2][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } };
V. Marangozova-Martin
C Structures
u
struct mystruct {
int int_member;
double double_member;
char string_member[25];
};
typedef struct {
int int_member;
double double_member;
char string_member[25];
} mystruct_t;
mystruct_t V1;
V1.int_member = 10;
V1.double_member = 13.7;
V1.string_member = learning;
V1 = (struct mystruct){10, 13.7, learning};
M1 MOSIG Programming with C
V. Marangozova-Martin
Modularity of C Programs
u
V. Marangozova-Martin
Using a
Header File
whitespaces.h
whitespaces.c
#include "whitespaces.h"
int main(void) {
string myString = "hello this is a string";
printf("The string has %d spaces\n",
CountWhiteSpaces(myString));
}
int CountWhiteSpaces(string inputString) {
//... implementation...
}
V. Marangozova-Martin
Using a
Header File
whitespaces.h
#include "whitespaces.h"
int main(void) {
globalArray[1] = 1234;
char T[MYVALUE];
scanf("%s", T);
printf("The string T is %s\n",T);
}
V. Marangozova-Martin
#include <stdio.h>
int add(int a, int b);
int main(){
int num1,num2,sum;
printf("Enters two number to add\n");
scanf("%d %d",&num1,&num2);
sum=add(num1,num2);
printf("sum=%d",sum);
return 0;
}
V. Marangozova-Martin
V. Marangozova-Martin
10
#include <math.h>
#include <stdlib.h>
V. Marangozova-Martin
11
Compilation example
v
>
>
>
>
gcc
gcc
gcc
gcc
V. Marangozova-Martin
12