C Programming HOW-TO

First Steps with C Programming

BitMan
C Programming HOW-TO

1. Data Types in C: char, int, float, double, void.

2. Data Representations: negative numbers are represented using the two's complement approach (reverse all bits excepting sign flag then add 1 and set sign flag to 1).

3. Variable Declaration Space:

3.1 Local variables - inside a function (also referred as automatic variables); stored on the stack. Their content is lost when the block is left.

3.2 Global variables - used by any piece of code (or program). Declared outside of any function or block; maintain the value all execution time.

4. Variable Qualifiers:

4.1 Const - the value is constant.

Ex: const int i = 10;

i = 11; /* ERROR: I can not be changed */

4.2. Volatile - variable's value can be changed indirectly by the program execution.

4.3. Const + Volatile - can be used in this combination (think that a status register of a processor can not be changed by the program (which means it is const), but can be changed by internal status functionality indirectly (which means is changed like a volatile variable).

5. Variable Storage Class: extern, static, register, auto.

6. Assignment Operator:

Ex: int a = 10;

int b = c = 11; /* Multiple Assignments */

7. Priority Operators - from higher to lower:

++ -- - (unary minus) * / % + -

! > >= < == != && ||

8. Difference between dot (.) and arrow (->) Operators

8.1 Dot (.) is used for accessing directly the elements of any block (union, structure,...)

8.2 Arrow (->) is used to access by pointer the elements of any block.

Ex: struct family {

char father_name[20];

char mother_name[20];

int child_age;

};

struct family fam;

struct family *p_fam;

fam.child_age = 3; /* using dot (.) operator */

p_fam->child_age = 5; /* using arrow (->) operator */

9. C Statements

9.1 IF Statement

9.2 IF-ELSE-IF Statement

9.3 ? Statement

9.4 SWITCH Statement

9.5 FOR Statement

9.6 WHILE Statement

9.7 DO-WHILE Statement

9.8 GOTO Statement

9.9 BREAK / CONTINUE / EXIT / RETURN Statement

10. Arrays

int a[10]; /* single dimension array */

int b[10][10]; /* bi-dimensions array */

int c[10] [10] [10]; /* tri-dimensions array */

Tips and Tricks:

int *p_a; /* pointer of type int */

p_a = a; /* equivalent with p_a = a[0]; */

int d[ ][ ]; /* ERROR: at least the second size parameter must be specified */

11. Pointers - variable that holds an address

int *p_int ;

p_int++; /* p_int will be incremented with the sizeof (int) */

Tips and Tricks:

int *a[10]; /* array of 10 integer pointers */

int (*a)[10]; /* pointer to an array of 10 integers*/

void fct( int x, int y); /* function prototype */

void (*p_fc)(int, int); /* pointer function declaration */

p_fc = fct; /* pointer which points to a function */

12. Dynamic Allocation

This memory is obtained from heap. Function for dynamic memory allocation:

12.1 malloc() - allocates a number of bytes and return a void pointer.

12.2 free() - delete the memory allocated with malloc.

char *p_char;

p_char = malloc(100); /* dynamic allocation of 100 bytes from heap */

.....

free(p_char); /* delete the previous memory allocated with malloc() */

Tips and Tricks:

Difference between malloc() and calloc(): malloc() allocates a bytes of memory and calloc allocates blocks of memory. Malloc takes an argument while calloc takes two (number of blocks and size of blocks). Malloc don't initialize memory while calloc do it.

Basically calloc( a, b) malloc(a * b);

13. Structures

Are a collection of variables for keeping related information together.

Important to note:

13.1 Do not initialize the elements of structures inside structure declaration.

13.2. Structures can be declared inside another structures (nested structures)

Published by BitMan

View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.