Pages

Monday, April 27, 2015

c programming basic documentation

Basic C Concepts


**compiler creates useable programs from C source
**typed variables kind of data that a variable contains
**typed functions the kind of data returned from a function
**header files (.h) declare functions and variables in a separate file
**structs groups of related values
**enums lists of predefined values
**pointers aliases to other variables



compiled to binary program ( faster than scripting ).
need to porting to other os in more complex program.


without pointer C is identically to php




cth program  : hello.c

#include <stdio.h> main () { printf ("I'm a C program\n"); }

$ gcc hello.c -o hello
$ ./hello



=====
** typed variables.


in scripting, var can be declare and change to one or other type freely.

ex: $variable = 2; $variable = 1.618; $variable = 'A';

in c, we must declare the type of data first!

int variable1 = 2; float variable2 = 1.618; char variable3 = 'A';





variables in C :

int          ( positif & negatif)
unsigned int  ( only positive )
float
char


=====
** typed function.

in C we must declare the type of data a function return.

int how_many_dog (){ return 3};
float change(){ return 1.234};
char grade(){ return ‘B’};


void printSomething(){
printf(“hi \n”);      // nothing returned
}


** parameter need type

int sum_of_data (int val1,int val2){
return val1 +val2;
}

float substraction(float var1, float var2){
return var1 - var2;
}


====

**function prototype

function must be declared before main().

but also can be defined with function prototype.


ex:




#include <stdio.h>


int sum ( int x, int y );   // declare without braces

main () {
int theSum = sum (10, 11); printf ( "Sum: %i\n", theSum );
}

int sum ( int x, int y ) { return x + y; }   // implementation of function prototipe



======


formatting string

int num1 = 20; int num2 = 100;

printf (“number1 is %i while number2 is  %i", num1, num2);  // %i = integer. hold the value.



%i // integer
%d // decimal      ->  integer = decimal
%f // float
%c // char
%u   // unsigned



=======

** casting

float x = 1.23141;
int y= (int) x;


=======

math_functions.h   ->  function prototype

math_functions.c   ->  implementation


test3.c  ->

#include <stdio.h>
#include “math_functions.h”

main(){
//use function here;
}



=======

**struct -> almost same as object in object oriented programming


typedef struct {
int how_long;
int grade;
} Test;



Test test1;
test1.how_long = 12123;
test1.grade = 12324;

Test test2;
test2.how_long =1123;
test2.grade = 9823;


=====

**constant

once declared can’t be changed until program restart.

const float phi = 3.14;
const ration = 5;



=====

** enum

a group of related constant.



======

library - >  ends with .h
function & implementation - > end with .c


stdio.h
strings.h
time.h     ->  library

there are many third party library. Some are free, some are not, which provide header file and documentation but hiding the implementation / source code

/usr/include   ->  C header library.

======


No comments:

Post a Comment