Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Of
Data Types:
Built in Data types
Char Integers: Short short int Int Long long int Long long Floating point : Float Double Long double 8 16 32 32 64 32 64 64
This gives us the idea that 32 bit architecture implements all types of data Int , float in 32 bit natural size. Only when we want long long int , or double float we need 64 bit.
The ordering of the bits may be from left to right or right to left The size of the bit field will be in terms of one word or more words. One bit field can not be present across the word boundaries. Enum enum_tag{RED,BLACK,WHITE}
Scopes:
Block Function
Type qualifiers:
Const Volatile
Control Constructs:
Break: Break works in loops and switch block , it does not work in if blocks Continue: In a loop it skips the loop block beyond this statement and goes for the next iteration If struct is defined like Struct fraction{ Int numerator; Int denominator; } to instantiate it you need to use the struct keyword like struct fraction f1;
Directives:
Directives are always introduced by a line that starts with a # character, optionally preceded by white space characters (although it isn't common practice to indent the #). Table 7.1 below is a list of the directives defined in the Standard. Directive
# include # define # undef # if # ifdef # ifndef # elif # else # endif # line # error # pragma #
Meaning include a source file define a macro undefine a macro conditional compilation conditional compilation conditional compilation conditional compilation conditional compilation conditional compilation control error reporting force an error message used for implementation-dependent control
Macros:
#define MAXCOUNT 10000 define replaces the defined symbol with its value parameterized macros can be used i.e macro functions #define ABS(value) ( if value>0 return value; else return value;)
static keyword before function means the function is visible within the current file. The static keyword before a global variable is redundant, every global variable is static. Static keyword before a local variable means the variable is stored in the stack but in the heap and has persistence like global variable.
Operators:
*p++ what does it do ?
The order of evaluation of unary operators is from right to left, so ++ is performed before *. Compund operator X+=y X=x+y; means
p->x++ increments x ++p->x so does this! (++p)->x *(p->y)++ *(p++)->y increments p before getting x so does this uses y as a pointer, then increments p *p->y++ uses y as a pointer, then increments it
The way to remember these is that ->, . (dot), ( ) and [ ] bind very tightly. An expression involving one of these is treated as a unit. p->x, a[i], y.x and f(b) are names exactly as abc is.
Assert
While you are debugging programs, it is often useful to check that the value of an expression is the one that you expected. The assert function provides such a diagnostic aid. In order to use assert you must first include the header file <assert.h>. The function is defined as
#include <assert.h> void assert(int expression)
If the expression evaluates to zero (i.e. false) then assert will write a message about the failing expression, including the name of the source file, the line at which the assertion was made and the expression itself. After this, the abort function is called, which will halt the program.
assert(1 == 2); /* Might result in */ Assertion failed: 1 == 2, file silly.c, line 15 Assert
beware of side effects that the expression may have: when assertions are turned off with NDEBUG, the expression is not evaluated. Thus the following example will behave unexpectedly when debugging is turned off with the #define NDEBUG.
#define NDEBUG #include <assert.h>
What the function does, as one line or so What are the inputparameters What it returns And any comment like warning etc.
Code template using setjmp and longjmp; Struct { Jump_buffer Void * Port Semaphore }state; main(){ if jbuffer; p1,p2 port1 s1
(setjmp(state.jbuffer)==0){ do something
if error some where deep down within any function longjmp(state.jbuffer,1); } } else { //error handling release the resources , take pointers from the state. }
SEH: Structured Exception handling Using TRY ,CATCH blocks // try catch in C can be implemented with macros Based on setjmp and longjmp. CSEH: Cleanup Stack Exception Handling This is a more elegant way, since it does not call for extra code littered in the main code. It restores the state of the system to a graceful recovery. A separate clean up stack is maintained in this method. The resource item and cleanup functions are pushed onto the stack. For recovery they are popped and clean up function executed.
Printf related
How to print a byte in hexadecimal
char c[4]={0xaa,0xbb,0xcc,0xdd}; printf("\n %x \n",(unsigned char)c[1]);