Sei sulla pagina 1di 12

Programming with C

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

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

Arrays continued: dimensions


int i;
int arr[10]; /* declare an array of one dimension */

arr[1]
int M[2][3]; /* two dimensions */

M[2][1]

M1 MOSIG Programming with C

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 } };

M1 MOSIG Programming with C

V. Marangozova-Martin

C Structures
u

A data structure contains multiple pieces of data

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;

struct mystruct V1;

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

When the code is big


v
v
v
v

it will naturally divide in chunks of "related" code


split it into different files
easier to maintain
possible to compile separately

There are two major file types to do that


v

Function (and some data) declarations are put in


header files
(.h extension)

This is the interface that is made visible to other pieces of


code

Sources (implementations) are put in .c files

M1 MOSIG Programming with C

V. Marangozova-Martin

Using a
Header File

whitespaces.h

#define MYVALUE 300

typedef char *string;


int CountWhiteSpaces(string inputString);
int globalArray[100];

Implement declared functions

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...
}

M1 MOSIG Programming with C

V. Marangozova-Martin

Using a
Header File

#define MYVALUE 300

whitespaces.h

typedef char *string;


int CountWhiteSpaces(string inputString);
int globalArray[100];

Use functions implemented elsewhere


other.c

#include "whitespaces.h"
int main(void) {
globalArray[1] = 1234;
char T[MYVALUE];
scanf("%s", T);
printf("The string T is %s\n",T);
}

M1 MOSIG Programming with C

V. Marangozova-Martin

Using Standard Libraries


u

C programs use predefined C functions


provided in standard libraries

#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;
}

M1 MOSIG Programming with C

V. Marangozova-Martin

Small note on the #include directive


u

When using a standard library


#include <stdio.h>

When using your own header files


#include "whitespace.h

M1 MOSIG Programming with C

V. Marangozova-Martin

10

Interesting Standard Libraries


#include <string.h>
size_t strlen(const char * chaine);
char * strcpy (char *destination, const char *source);
char *strchr(const char *str, int c)

#include <math.h>

#include <stdlib.h>

M1 MOSIG Programming with C

V. Marangozova-Martin

11

Back to Compiling C Programs


u
u

Using a C compiler, usually gcc


Compilation steps
v
v
v

Compilation example
v

>
>
>
>

preprocessor: expand # directives


core compiler: produces machine code (.o file)
the linker: combine multiple .o files and libraries (.so or .a files)

gcc
gcc
gcc
gcc

sources = t1.c t2.c t3.c t1.h common.h


t1.c t2.c t3.c the executable is a.out
-o example t1.c t2.c t3.c the executable is example
-c t1.c the result of the compilation is t1.o
-o final t1.o t2.o t3.o

M1 MOSIG Programming with C

V. Marangozova-Martin

12

Potrebbero piacerti anche