13
Master on Free Software C language Juan A. Suárez Romero

MSL2009. C language

Embed Size (px)

DESCRIPTION

Presentation about C language done at Master on Free Software 2009 Edition

Citation preview

Page 1: MSL2009. C language

Master on Free Software

C language

Juan A. Suárez Romero

Page 2: MSL2009. C language

Master on Free Software

List of topics

● History● Control structures● Data types● Operators● Preprocessor

Page 3: MSL2009. C language

Master on Free Software

History

● Developed by Dennis Ritchie● Goal: port a SO from PDP-7 to PDP-8● K&R C● Ansi C (C89/C90 - C99)

Page 4: MSL2009. C language

Master on Free Software

Example

#include <stdio.h>

int main(int argc, char *argv[])

{

printf (“Hello, world!\n”);

return 0;

}

Page 5: MSL2009. C language

Master on Free Software

Control structures

while (condition) statement_set;

do statement_set while (condition)

for (initial_cond, final_cond, change_action)

statement_set

if (condition) st_set else st_set2;

Page 6: MSL2009. C language

Master on Free Software

Data Types

● Simple types– char, int, float, double– signed/unsigned

● Complex types– arrays, structures, unions– strings (arrays of char ended in NULL)

● 'Special' types– Pointers– Void

Page 7: MSL2009. C language

Master on Free Software

Data types

#include <stdio.h>

int main(int argc, char *argv[])

{

char letter;

double pi;

float height;

int first_prime_numbers[30];

}

Page 8: MSL2009. C language

Master on Free Software

Data types

struct email {

int sent_date;

char *sender;

char *subject;

char *content;

};

struct email *myemail;

Page 9: MSL2009. C language

Master on Free Software

Arrays / pointers

● Array is actually a pointer to a chunk of memory

● Uses of * and &

   char *a;

   char b;

   *a?? &b?? (a = &b)?? (b = *a)??

● Pointer arithmetic: a[3] == *(a+3)

Page 10: MSL2009. C language

Master on Free Software

Operators

● Arithmetic (+, -, *, /, %)● Logical (&&, ||, ==, !=, >=, <=)● Increment / Decrement● Size of a type● Self assignment (+=, -=, ...)

Page 11: MSL2009. C language

Master on Free Software

Preprocessor

● Definitions– #define, #undef

● Conditional compiling– #if, #ifdef, #ifndef, #else, #endif

● Include other files– #include

Page 12: MSL2009. C language

Master on Free Software

Functions

int multiply (int n1, int n2);

int multiply (int n1, int n2)

{

       return n1*n2;

}

Page 13: MSL2009. C language

Master on Free Software

References

● The C Programming Language (2nd edition)– Brian W. Kernighan– Dennis M. Ritchie