Sei sulla pagina 1di 109

Who to know whether system uses big endian or little endian format and how to convert among them?

Here is a small code segment to determine the endianess of a system at run time. short int number = 0x1; char *byte = (char*)&number; if ( byte[0 ) !rintf("#ittle endian"); else !rintf("$ig endian"); unsigned int a = 0x01; a = a%%1; if(a)& !rintf("little endian"); 'else& !rintf("big endian"); '

What is big endian and little endian


$ig(endian and little(endian are terms that describe the order in )hich a se*uence of bytes are stored in com!uter memory. $ig(endian is an order in )hich the "big end" (most significant +alue in the se*uence) is stored first (at the lo)est storage address). #ittle(endian is an order in )hich the "little end" (least significant +alue in the se*uence) is stored first. ,or exam!le- in a big(endian com!uter- the t)o bytes re*uired for the hexadecimal number .,/0 )ould be stored as .,/0 in storage (if ., is stored at storage address 1000- for exam!le- /0 )ill be at address 1001). 1n a little(endian system- it )ould be stored as /0., (/0 at address 1000- ., at 1001).
Advantages and disadvantages of using macro and inline functions?

2d+antage3 4acros and 1nline functions are efficient than calling a normal function. 5he times s!end in calling the function is sa+ed in case of macros and inline functions as these are included directly into the code. 4acros are used to maintain the readability and easy maintenance of the code. 6isad+antage3 4acros and inline functions increased the si7e of executable code. 1nline ,unctions +ersus 4acros 2lthough inline functions are similar to macros (because the function code is ex!anded at the !oint of the call at com!ile time)- inline functions are !arsed (analysed by the com!iler- )hereas macros are ex!anded by the !re(!rocessor. 2s a result- there are se+eral im!ortant differences3 1nline functions follo) all the !rotocols of ty!e safety enforced on normal functions. 1nline functions are s!ecified using the same syntax as any other function exce!t that they include the inline 8ey)ord in the function declaration. 9x!ressions !assed as arguments to inline functions are e+aluated once. 1n some cases- ex!ressions !assed as arguments to macros can be e+aluated more than once. 2nother difference is 1nline functions chec8 for the ty!e of !arameter !assed- 4acros don:t chec8 !arameter ty!e- and you can !ass strings to a macro that does some integer arithmetic. 1n 42;<= ((% you cannot ma8e the macro return something )hich is not the result of the last ex!ression in+o8ed inside it.

1n 1>#1>9 ((% you can return any +alue by using 8ey)ord return(); 1n 42;<=? (( % debugging is tough (because they refer to the ex!anded code- rather than the code the !rogrammer ty!ed.) 1>#1>e ((% debugging is easy

What is Preprocessor?
5he !re!rocessor is used to modify your !rogram according to the preprocessor directives in your source code. @re!rocessor directi+es (such as Adefine) gi+e the !re!rocessor s!ecific instructions on ho) to modify your source code. 5he !re!rocessor reads in all of your include files and the source code you are com!iling and creates a !re!rocessed +ersion of your source code. 5his !re!rocessed +ersion has all of its macros and constant symbols replaced by their corresponding code and value assignments. 1f your source code contains any conditional !re!rocessor directi+es (such as Aif)the !re!rocessor e+aluates the condition and modifies your source code accordingly. 5he !re!rocessor contains many features that are !o)erful to use- such as creating macros- !erforming conditional com!ilation- inserting !redefined en+ironment +ariables into your code- and turning com!iler features on and off. ,or the !rofessional !rogrammer- in(de!th 8no)ledge of the features of the !re!rocessor can be one of the 8eys to creating fast- efficient !rograms. 1n computer science- a preprocessor is a program that !rocesses its in!ut data to !roduce out!ut that is used as in!ut to another !rogram. 5he out!ut is said to be a preprocessedform of the in!ut data- )hich is often used by some subse*uent !rograms li8e compilers. 5he amount and 8ind of !rocessing done de!ends on the nature of the !re!rocessor; some !re!rocessors are only ca!able of !erforming relati+ely sim!le textual substitutions and macro ex!ansions- )hile others ha+e the !o)er of fully( fledged programming languages. A common example from computer programming is the processing performed on source code before the next step of compilation. In some computer languages (e.g., C) there is a phase of translation known as preprocessing. #
@re!rocessor directi+es are lines included in the code of our !rograms that are not !rogram statements but directi+es for the !re!rocessor. 5hese lines are al)ays !receded by a hash sign (#). 5he !re!rocessor is executed before the actual com!ilation of code begins- therefore the !re!rocessor digests all these directi+es before any code is generated by the statements. 5hese !re!rocessor directi+es extend only across a single line of code. 2s soon as a ne)line character is found- the !re!rocessor directi+e is considered to end. >o semicolon (;) is ex!ected at the end of a !re!rocessor directi+e. 5he only )ay a !re!rocessor directi+e can extend through more than one line is by !receding the ne)line character at the end of the line by a bac8slash (\

What is the macro?


A macro is a fragment of code which has been given a name. Bhene+er the name is used- it is re!laced by the contents of the macro. 5here are t)o 8inds of macros. 5hey differ mostly in )hat they loo8 li8e )hen they are used. Object-like macros resemble data obCects )hen used- functionlike macros resemble function calls. Dou may define any +alid identifier as a macro- e+en if it is a ; 8ey)ord. 5he !re!rocessor does not 8no) any thing about 8ey)ords. Object-like acros

2n object-like macro is a sim!le identifier )hich )ill be re!laced by a code fragment. 1t is called obCect(li8e because it loo8s li8e a data obCect in code that uses it. 5hey are most commonly used to gi+e symbolic names to numeric constants.

#define BUFFER_SIZE 1024 !unction-like acros 5o define a function(li8e macro- you use the same E#define: directi+e- but you !ut a !air of !arentheses immediately after the macro name. ,or exam!le#define lang_init() lang_init() ==> c_init() c_init()

2 function(li8e macro is only ex!anded if its name a!!ears )ith a !air of !arentheses after it. 1f you )rite Cust the name- it is left alone. 5his can be useful )hen you ha+e a function and a macro of the same name- and you )ish to use the function sometimes. exte n !"id f""(!"id)# #define f""() $% o!timi7ed inline +ersion %$ &&& f""()# f'nc(t = f""# Here the call to f""() )ill use the macro- but the function !ointer )ill get the address of the real function. 1f the macro )ere to be ex!anded- it )ould cause a syntax error.

What is self-referential macro


2 self-referential macro is one )hose name a!!ears in its definition. <ecall that all macro definitions are rescanned for more macros to re!lace. 1f the self(reference )ere considered a use of the macro- it )ould !roduce an infinitely large ex!ansion. 5o !re+ent this- the self(reference is not considered a macro call. 1t is !assed into the !re!rocessor out!ut unchanged. ;onsider an exam!le3 #define f"" (4 ) f"") )here f"" is also a +ariable in your !rogram. ,ollo)ing the ordinary rules- each reference to f"" )ill ex!and into (4 ) f""); then this )ill be rescanned and )ill ex!and into (4 ) (4 ) f"")); and so on until the com!uter runs out of memory.

"onditional inclusions #$ifdef% $ifndef% $if% $endif% $else and $elif&


5hese directi+es allo) to include or discard !art of the code of a !rogram if a certain condition is met. #ifdef allo)s a section of a !rogram to be com!iled only if the macro that is s!ecified as the !arameter has been defined- no matter )hich its +alue is. ,or exam!le3

#ifdef *+B,E_SIZE int ta-le.*+B,E_SIZE/# #endif


1n this case- the line of code int ta-le.*+B,E_SIZE/# is only com!iled if *+B,E_SIZE )as !re+iously defined )ith #define- inde!endently of its +alue. 1f it )as not defined- that line )ill not be included in the !rogram com!ilation.

#ifndef ser+es for the exact o!!osite3 the code bet)een #ifndef and #endif directi+es is only
com!iled if the s!ecified identifier has not been !re+iously defined. ,or exam!le3

#ifndef *+B,E_SIZE #define *+B,E_SIZE 100 #endif

int ta-le.*+B,E_SIZE/#
Can we use variables inside a switch statement?

Des 5he only things that case be used inside a s)itch statement are constants or enums. 2nything else )ill gi+e you a constant ex!ression re*uired error. 5hat is something li8e this is not +alid

01itc2(i) 3 ca0e 14 $$ S"5et2ing# - ea6# ca0e 74 $$ S"5et2ing# - ea6# 8


What is interrupt latency? How can you recduce it?

Bhen an interru!t fires- the micro!rocessor executes an interru!t ser+ice routine (1?<) that has been installed to ser+ice the interru!t. 5he amount of time that ela!ses bet)een a de+ice interru!t re*uest and the first instruction of the corres!onding 1?< is 8no)n as interru!t latency. 1nterru!t latency is the res!once time of interru!t that meeans the time ta8en by the de+ice to recie+e it and then ac8no)ledge after that it )ill transfer to !articular 1?<. Be can reduce it by )riting smaller 1?< routines.

Why cannot arrays be passed by values to functions?


2rrays can:t be !assed by +alues. $ecause - the array name is e+aluated to be a !ointer to the first element of the array. e.g. )hen )e !ass array x- its e*ui+alent to &x[0 i.e. !ointer to the first element. 1ts ty!e is- therefore- int *- and a called function uses this !ointer (!assed as an argument) to indirectly access the elements of the array. e&g & int 5ain() 3 !"id f'ncti"n1(int +./9int n)# int x.10/9i# f" (i=0#i:10#i))) 3 x.i/ = 10# 8 f'ncti"n1(x910)# 8 !"id f'ncti"n1(int +./9int n) 3 &&&&$% 0"5e ( "ce00ing "n t2e a 8

a; %$

1n the abo+e exam!le-!rototy!e sho)s the first formal !arameter as an integer array )ithout s!ecifying the si7e. 1n ;- this syntax is inter!reted as a !ointer +ariable; so array 2 is declared as an int * +ariable.

5his is the uni*ue feature of ; is that array access is al)ays indirect; thus ma8ing it !articularly easy for a called function to indirectly access elements of an array and store or retrie+e +alues. $ecause in ; )hen you say the name of the array it means the address of the first element. exam!le 3 int a./# f'nc (a)# int f'nc(int a./)# In this when you call the function by passing the argument a actually &a[0] (address of first element) gets passed. Hence it is impossible to pass by alue in !.
Can structures be passed to the functions by value?

Des- )e can do it. consider the follo)ing code.. #incl'de:0tdi"&2> #incl'de:c"ni"&2> 0t 'ct e5( 3 int n"# int e5(n"# 8# t;(edef 0t 'ct e5( e5(data# 5ain() 3 e5(data edata# !"id di0(la;( e5(data )# cl 0c ()# edata&n" = 1# edata&e5(n" = 200<# di0(la;( edata )# et' n 0# 8 !"id di0(la;( e5(data (0t ) 3 ( intf( =>d=9 (0t&e5(n" )# 8 Des structures can be !assed to functions by +alue. 5hough !assing by +alue has t)o disad+antages3 1) 5he chages by the calling function are not reflected 0) 1ts slo)er than the !ass by refrence funcion call. Structure program #incl'de :0tdi"&2> #incl'de :c"ni"&2> 0t 'ct 0t'dent 3 int id# char %na5e#

float (e centage# 8 0t'dent19 0t'dent29 0t'dent?# int 5ain() 3 0t 'ct 0t'dent 0t# 0t'dent1&id=1# 0t'dent2&na5e = =+ngelina=# 0t'dent?&(e centage = @0&<# ( intf(= Id i04 >d \n=9 0t'dent1&id)# ( intf(= Aa5e i04 >0 \n=9 0t'dent2&na5e)# ( intf(= Be centage i04 >f \n=9 0t'dent?&(e centage)# getc2()# return 0# 8
What is the differnce between embedded systems and the system in which rtos is running?

if the system needs to ser+e only e+ent se*uencially- there is no need of <5=?. 1f the system demands the !arallel execution of e+ents then )e need <5=?. embedded systems is the software )hich dri+es the hard)are and the tas8s are executed se*uentially )here as rtos is tiny soft)are )hich can be used in embedded systems a!!lications and the tas8 are executed simultaneously )ithin s!ecific time constraints. <5=? in not mandatory for embedded system. 9mbedded system can ha+e a standalone !rocessor. 9mbedded ?ystem3 ?ystem )hich is designed to !erform a single or fe) dedicated functions. ?ystem )ith <5=?3 also can be an embedded system but naturally <5=? )ill be used in realtime system )hich )ill be need to !erform many functions. <eal time ?ystem 3 ?ystem )hich can !ro+ide the out!ut in a definiteF!redicted amout of time.this does not mean the real time systems are faster.

6ifference bet)een both 3 1.normal 9mbedded systems are not <eal 5ime ?ystem 0.?ystems )ith <5=? are real time systems.
Can you have constant volatile variable?

yes )e can ha+e constant +olatile +ariable. 1n this current context of code )ill not change the +alue of the +ariable but out side of the !rogram i.e. hard)are registers can change it.

DMA deals with which address (physical/virtual addresses) ?


642 deals )ith @hysical addresses. =nly )hen ;@G accesses addresses it refers to 44G(4emory 4anagement Gnit) and 44G con+erts the @hysical address to Hirtual address. $ut- 642 controller is a de+ice )hich directly dri+es the data and address bus during data transfer. ?o- it is !urely @hysical address. (1t ne+er needs to go through 44G & Hirtual addresses). 5hat is )hy )hen )riting the de+ice dri+ers- the !hysical address of the data buffer has to be assigned to the 642.

What are the different rtos available? What is the difference beetween R !" ans !"
=rdinary =? doesnIt 8ee! any time constraints n deadlines )hereas <5=? -real time =? 8ee! time constrains for all !rocesses that it 8ee!s trac8 of time consumed by each e+ent n therefore maintains to finish tas8s in s!ecified time. <5=? are designed n installed in systems )here oF! shud obtain in s!ecific time..li8e mobile !hones n all

#sin$ the %define statement& how would you declare a manifest constant that returns the number of seconds in a year? Disre$ard leap years in your answer'
#define SECONDS_PER_YEAR (60 * 60 * 2 * !6"#$%

Write the (standard( M)* macro-that is& a macro that ta+es two ar$uments and returns the smaller of the two ar$uments'
#define CIA(+9B) ((+):= (B) D (+) 4 (B))

Define small function in macro


#define EUBE(x) x%x%x #incl'de :0tdi"&2> 5ain () 3 int 6 = <# int 7 = 0# 7 = EUBE(6)# $$ 8

7 = 6%6%6

( intf (=E'-e !al'e "f 7 i0 >d\n=9 7)#

)nfinite loops often arise in embedded systems' ,ow does you code an infinite loop
in C? &hile('# ( )

#sin$ the variable a& $ive definitions for the followin$a) An inte$er b) A pointer to an inte$er c) A pointer to a pointer to an inte$er d) An array of ./ inte$ers e) An array of ./ pointers to inte$ers f) A pointer to an array of ./ inte$ers $) A pointer to a function that ta+es an inte$er as an ar$ument and returns an inte$er h) An array of ten pointers to functions that ta+e an inte$er ar$ument and return an inte$er
5he ans)ers are3 a) int a# FF 2n integer

b) int %a# FF 2 !ointer to an integer c) int %%a# FF 2 !ointer to a !ointer to an integer d) int a.10/# FF 2n array of 10 integers e) int %a.10/# FF 2n array of 10 !ointers to integers f) int (%a).10/# FF 2 !ointer to an array of 10 integers g) int (%a)(int)# FF 2 !ointer to a function a that ta8es an integer argument and returns an integer h) int (%a.10/)(int)# FF 2n array of 10 !ointers to functions that ta8e an integer argument and return an integer

What are the uses of the +eyword static?


5his sim!le *uestion is rarely ans)ered com!letely. ?tatic has three distinct uses in ;3 2 +ariable declared static )ithin the body of a function maintains its +alue bet)een function in+ocations 2 +ariable declared static )ithin a module- (but outside the body of a function) is accessible by all functions )ithin that module. 1t is not accessible by functions )ithin any other module. 5hat is- it is a locali7ed global ,unctions declared static )ithin a module may only be called by other functions )ith in that module. 5hat is- the sco!e of the function is locali7ed to the module )ithin )hich it is declared

What does the +eyword const mean?


suffice it to say that const means "read(only."

What do the followin$ declarations mean? con*t int a+ int con*t a+ con*t int *a+ int * con*t a+ int con*t * a con*t+
5he first t)o mean the same thing- namely a is a const (read(only) integer. 5he third means a is a !ointer to a const integer (that is- the integer isn:t modifiable- but the !ointer is). 5he fourth declares a to be a const !ointer to an integer (that is- the integer !ointed to by a is modifiable- but the !ointer is not). 5he final declaration declares a to be a const !ointer to a const integer (that is- neither the integer !ointed to by a- nor the !ointer itself may be modified). 1f the candidate correctly ans)ers these *uestions- 1:ll be im!ressed. 1ncidentally- you might )onder )hy 1 !ut so much em!hasis on constsince it is easy to )rite a correctly functioning !rogram )ithout e+er using it. 1 ha+e se+eral reasons3
JJ5he use of const con+eys some +ery useful information to someone reading your code. 1n effect-

declaring a !arameter const tells the user about its intended usage. 1f you s!end a lot of time cleaning u! the mess left by other !eo!le- you:ll *uic8ly learn to a!!reciate this extra !iece of information. (=f course- !rogrammers )ho use const - rarely lea+e a mess for others to clean u!.) JJconst has the !otential for generating tighter code by gi+ing the o!timi7er some additional information JJ;ode that uses const liberally is inherently !rotected by the com!iler against inad+ertent coding constructs that result in !arameters being changed that should not be. 1n short-

What does the +eyword volatile mean? 0ive three different e1amples of its use'
+olatile +ariable is one that can change unex!ectedly. ;onse*uently- the com!iler can ma8e no assum!tions about the +alue of the +ariable. 1n !articular- the o!timi7er must be careful to reload the +ariable e+ery time it is used instead of holding a co!y in a register. 9xam!les of +olatile +ariables are3 Hard)are registers in !eri!herals (for exam!le- status registers) >on(automatic +ariables referenced )ithin an interru!t ser+ice routine Hariables shared by multi!le tas8s in a multi(threaded a!!lication

;an a !arameter be both const and +olatile K 9x!lain. ;an a !ointer be +olatile K 9x!lain. Bhat:s )rong )ith the follo)ing functionK3 int s'uare#volatile int (ptr& ) return (ptr ( (ptr* + 5he ans)ers are as follo)s3 Des. 2n exam!le is a read(only status register. 1t is +olatile because it can change unex!ectedly. 1t is const because the !rogram should not attem!t to modify it Des- although this is not +ery common. 2n exam!le is )hen an interru!t ser+ice routine modifies a !ointer to a buffer 5his one is )ic8ed. 5he intent of the code is to return the s*uare of the +alue !ointed to by *!tr . Ho)e+er- since *!tr !oints to a +olatile !arameter- the com!iler )ill generate code that loo8s something li8e this3 int ( int a 1 0 1 *,uare(-olatile int *.tr# a/0+ *.tr+ *.tr+

return a * 0+ ) $ecause it:s !ossible for the +alue of *!tr to change unex!ectedly- it is !ossible for a and b to be different. ;onse*uently- this code could return a number that is not a s*uareL 5he correct )ay to code this is3 long square(volatile int *ptr) { int a; a = *ptr; return a * a; }

2mbedded systems always re3uire the user to manipulate bits in re$isters or variables' 0iven an inte$er variable a& write two code fra$ments' he first should set bit 4 of a' he second should clear bit 4 of a' )n both cases& the remainin$ bits should be unmodified'
5hese are the three basic res!onses to this *uestion3 >o idea. 5he inter+ie)ee cannot ha+e done any embedded systems )or8 Gse bit fields. $it fields are right u! there )ith trigra!hs as the most brain(dead !ortion of ;. $it fields are inherently non(!ortable across com!ilers- and as such guarantee that your code is not reusable. 1 recently had the misfortune to loo8 at a dri+er )ritten by 1nfineon for one of their more com!lex communications chi!s. 1t used bit fields and )as com!letely useless because my com!iler im!lemented the bit fields the other )ay around. 5he moral3 ne+er let a non(embedded !erson any)here near a real !iece of hard)areL Gse Adefines and bit mas8s. 5his is a highly !ortable method and is the one that should be used. 4y o!timal solution to this !roblem )ould be3 #define 234! (05'66!# *tatic int a+ -oid *et_0it!(-oid# (

a 71 234!+ ) -oid clear_0it!(-oid# ( a 81 9234!+ ) ?ome !eo!le !refer to define a mas8 together )ith manifest constants for the set and clear +alues. 5his is also acce!table. 5he element that 1:m loo8ing for is the use of manifest constants- together )ith the M= and &= N constructs

All about 5it field 5it fields


; also !ro+ides a s!ecial ty!e of structure member 8no)n as a bit field- )hich is an integer )ith an ex!licitly s!ecified number of bits. 2 bit field is declared as a structure member of ty!e int- 0igned int- 'n0igned int- or _B""l- follo)ing the member name by a colon (4) and the number of bits it should occu!y. 5he total number of bits in a single bit field must not exceed the total number of bits in its declared ty!e. 2s a s!ecial exce!tion to the usual ; syntax rules- it is im!lementation(defined )hether a bit field declared as ty!e int- )ithout s!ecifying 0igned or 'n0igned- is signed or unsigned. 5hus- it is recommended to ex!licitly s!ecify 0igned or 'n0igned on all structure members for !ortability. 9m!ty entries consisting of Cust a colon follo)ed by a number of bits are also allo)ed; these indicate !adding.

5he members of bit fields do not ha+e addresses- and as such cannot be used )ith the address(of (F) unary o!erator. 5he 0iGe"f o!erator may not be a!!lied to bit fields. 5he follo)ing declaration declares a ne) structure ty!e 8no)n as f and an instance of it 8no)n as g. ;omments !ro+ide a descri!tion of each of the members3
0t 'ct f 3 'n0igned int "ff (0) %$ 0igned int HJ&&&I %$ 4 ?# %$ 8 g# #include <stdio.h> typedef struct { signed s : 1; unsigned u : 1; } BitStruct; int main( oid!

flag 4 1# n'5 4 4#

$% a -it flag4 can eit2e $% a 0igned 4H-it field# $% ? -it0 "f (adding t"

-e "n (1) " ange HI&&&I " "'nd "'t J -it0

BitStruct "; ".s # 1; ".u # 1; printf($s: %d &t u: %d&r&n$' ".s' ".u!; printf($s>(: %d &t u>(: %d&r&n$' ".s > (' ".u > (!; return (;

} Output s: )1 s>(: ( u: 1 u>(: 1

Bit field: There are number of questions which have only two answers. To store such type of data there is necessity of only one bit either 0 or 1. Maximum child of any person must be less than 15. So for this four binary bit is sufficient to store such information. C introduces a powerful tool bit level pro rammin in which we can store data which si!e can be in bit. So there is not necessity to declare int or char data type for only one or two bit data. "n this way we can save the memory space. Syntax# struct { name1:bit_length; name2:bit_length; }; must be si ned or unsi ned int "f it si ned int then minimum bit len th must be two$ one for si n and another for data. %xample# void main() { struct emplo ee { unsigned id: !;

unsigned se":1; unsigned age:#; }; struct emplo ee emp1${2%&'1'2&}; clrscr(); printf(()d*t)d*t)d('emp1+id'emp1+se"'emp1+age); getch(); } ,utput: &0' 1 &' (e can access the data member in same way. -o. bit data is stored in the memor : Minimum si!e of structure which data member in bit is two byte i.e 1) bit.This is called word si!e of microprocessor. (ord si!e depends on microprocessor. Turbo c is based on *0*) microprocessor which word si!e is two byte.

+its are filed in from ri ht to left direction * bit for id$1 bit for sex and , bit for a e
Look for padding in Bitfield

What are the advanta$es of usin$ pointers in a pro$ram? 1.


0. O. .. @ointers allo) us to !ass +alues to functions using call by reference. 5his is useful )hen large si7ed arrays are !assed as arguments to functions. 2 function can return more than one +alue by using call by reference. 6ynamic allocation of memory is !ossible )ith the hel! of !ointers. Be can resi7e data structures. ,or instance- if an arrayIs memory is fixed- it cannot be resi7ed. $ut in case of an array )hose memory is created out of malloc can be resi7ed. @ointers !oint to !hysical memory and allo) *uic8er access to data.

Which bit wise operator is suitable for turning O!! a particular bit in a number?
$it)ise 2>6 o!erator (&)- oneIs com!lement o!erator(N) ,-ample. 5o unset the .th bit of bytePdata or to turn off a !articular bit in a number. ,-planation. E"n0ide 9 c2a -;te_data= 0-00010111# -;te_data= (-;te_data)F(K(1::4))# 1 can -e e( e0ented in -ina ; a0 0-00000001 = (1::4) :: i0 a left -it 02ift "(e at" 9 it 02ift0 t2e -it 1 -; 4 (lace0 t"1a d0 left&

(1::4) -ec"5e0

0-00010000

+nd K i0 t2e "neL0 c"5(le5ent "(e at" in E lang'age& S" K(1::4) = c"5(le5ent "f 0-00010000 = 0-11101111 Re(lacing !al'e "f -;te_data and K(1::4) in (-;te_data)F(K(1::4))# 1e get (0-00010111) F (0-11101111) Be f" 5 +AM "(e ati"n t" -el"1 -;te0& 00010111 11101111 HHHHHHHHHHH 00000111 HHHHHHHHHHH *2'0 t2e 4t2 -it i0 'n0et&

Ho) to s)a! t)o numbers using bit)ise o!eratorsK


Program.

01 #incl'de :0tdi"&2> 02 int 5ain() 3 0? int i = N<# 04 int 6 = 120# 0< ( intf(=\n !al'e "f i=>d 6=>d -ef" e 01a((ing=9 i9 6)# 0N i = i O 6# 0I 6 = i O 6# 0J i = i O 6# 0@ ( intf(=\n !al'e "f i=>d 6=>d afte 10 et' n 0# 11 8 9x!lanation3 i = N<# -ina ; eP'i!alent "f N< i0 0100 0001 6 = 120# -ina ; eP'i!alent "f 120 i0 0111 1000 i = iO6# i&&&0100 0001 6&&&0111 1000 HHHHHHHHH !al "f i = 0011 1001 HHHHHHHHH 6 = iO6 i&&&0011 1001 01a((ing=9 i9 6)#

6&&&0111 1000 HHHHHHHHH !al "f 6 = 0100 0001 -ina ; eP'i!alent "f t2i0 i0 N< HHHHHHHHH(t2at i0 t2e initial !al'e "f i) i = iO6 i&&&0011 1001 6&&&0100 0001 HHHHHHHHH !al "f i = 0111 1000 -ina ; eP'i!alent "f t2i0 i0 120

Bhat is recursionK Brite a !rogram using recursion (factorial)K


/ecursion. 2 function is called Qrecursi+eI if a statement )ithin the body of a function calls the same function. 1t is also called Qcircular definitionI. <ecursion is thus a !rocess of defining something in terms of itself.

Program. 5o calculate the factorial +alue using recursion.


01 #incl'de :0tdi"&2> 02 int fact(int n)# 0? 04 int 5ain() 3 0< int x9 i# 0N ( intf(=Ente

a !al'e f"

x4 \n=)#

0I 0canf(=>d=9 Fx)# 0J i = fact(x)# 0@ ( intf(=\nFact" ial "f >d i0 >d=9 x9 i)# 10 et' n 0# 11 8 12 1? int fact(int n) 3 14 $% n=0 indicate0 a te 5inating c"nditi"n %$ 1< if (n := 0) 3 1N et' n (1)# 1I 8 el0e 3 1J $% f'ncti"n calling it0elf %$ 1@ 20 21 8 22 8 =ut!ut3 et' n (n % fact(n H 1))# $%n%fact(nH1) i0 a ec' 0i!e ex( e00i"n %$

9nter a +alue for x3 . ,actorial of . is 0. ,-planation. fact(n) = n % fact(nH1) If n=4 fact(4) = 4 % fact(?) fact(?) = ? % fact(2) fact(2) = 2 % fact(1) fact(1) = 1 % fact(0) fact(0) = 1

t2e e i0 a call t" fact(?)

fact(1) = 1 % 1 = 1 fact(2) = 2 % 1 = 2 fact(?) = ? % 2 = N *2'0 fact(4) = 4 % N = 24 5erminating condition(n R= 0 here;) is a must for a recursi+e !rogram. =ther)ise the !rogram enters into an infinite loo!

Bhat is an 9numerationK 9numeration is a data ty!e. Be can create our o)n data ty!e and define +alues that the +ariable can ta8e. 5his can hel! in ma8ing !rogram more readable. enum definition is similar to that of a structure. ,-ample. consider lightPstatus as a data ty!e. 1t can ha+e t)o !ossible +alues S on or off.
iew source print"

en'5 lig2t_0tat'0 23 1 ? "n9 "ff 4 8# < N en'5 lig2t_0tat'0 -'l-19 -'l-2# I $% -'l-19 -'l-2 a e t2e !a ia-le0 %$

6eclaration of enum has t)o !arts3 a) ,irst !art declares the data ty!e and s!ecifies the !ossible +alues- called QenumeratorsI. b) ?econd !art declares the +ariables of this data ty!e. Be can gi+e +alues to these +ariables3 bulb1 = on; bulb0 = off;

Bhat is the use of ty!edefK typedef declaration hel!s to ma8e source code of a ; !rogram more readable. 1ts !ur!ose is to redefine the name of an existing +ariable ty!e. 1t !ro+ides a short and meaningful )ay to call a data ty!e. ty!edef is useful )hen the name of the data ty!e is long. Gse of ty!edef can reduce length and com!lexity of data ty!es. 0ote. Gsually u!!ercase letters are used to ma8e it clear that )e are dealing )ith our o)n data ty!e. ,-ample.
iew source print"

0t 'ct e5(l";ee 3 2 c2a na5e.20/# 1 ? int age# 4 8# < N 0t 'ct e5(l";ee e#

5he abo+e declaration of the structure )ould be easy to use )hen renamed using ty!edef as3
iew source print"

0t 'ct e5(l";ee 3 2 c2a na5e.20/# 1 ? int age# 4 8# < N t;(edef 0t 'ct e5(l";ee ECB# I ECB e19 e2#

./. Bhat are header filesK 2re functions declared or defined in header files K ,unctions and macros are declared in header files. Header files )ould be included in source files by the com!iler at the time of com!ilation. Header files are included in source code using Ainclude directi+e.AincludeRsome.h% includes all the declarations !resent in the header file Isome.hI.

2 header file may contain declarations of sub(routines- functions- macros and also +ariables )hich )e may )ant to use in our !rogram. Header files hel! in reduction of re!etiti+e code. 1ynta- of include directive. AincludeRstdio.h% FFincludes the header file stdio.h- standard in!ut out!ut header into the source code !unctions can be declared as well as defined in header files. 2ut it is recommended only to declare functions and not to define in the header files. When we include a header file in our program we actually are including all the functions% macros and variables declared in it. 1n case of !re(defined ; standard library header files ex(stdio.h)- the functions calls are re!laced by e*ui+alent binary code !resent in the !re(com!iled libraries. ;ode for ; standard functions are lin8ed and then the !rogram is executed. Header files )ith custom names can also be created. #ead more about
header files

1 $%%%%%%%%%%%%%%%% 2 Index4 e0ta' ant&2 ? %%%%%%%%%%%%%%%%$ 4 int -ill+ll(int f""d_c"0t9 int tax9 int ti()#

01 $%%%%%%%%%%%%%%%% 02 Index4 e0ta' ant&c 0? %%%%%%%%%%%%%%%%$ 04 #incl'de:0tdi"&2> 0< int -ill+ll(int f""d_c"0t9 int tax9 int ti() 3 0N int e0'lt# 0I e0'lt = f""d_c"0t ) tax ) ti(# 0J ( intf(=*"tal -ill i0 >d\n=9 e0'lt)# 0@ et' n e0'lt# 10 8 01 $%%%%%%%%%%%%%%%% 02 Index4 5ain&c 0? %%%%%%%%%%%%%%%%$ 04 #incl'de:0tdi"&2> 0< #incl'de= e0ta' ant&2= 0N 0I int 5ain() 3 0J int f""d_c"0t9 tax9 ti(#

0@ f""d_c"0t = <0# 10 tax = 10# 11 ti( = <# 12 -ill+ll(f""d_c"0t9tax9ti()# 1? 14 et' n 0#

Where does various variables stored in C? It de(end0 enti el; "n 12at (latf" 5 ;"' a e '0ing& In an e5-edded en!i "n5ent9 f" in0tance

gl"-al$0tatic !a ia-le0 g" int" diffe ent R+C 5e5" ; 0eg5ent0 de(ending "n 12et2e " n"t t2e; a e initiali0ed& c"n0tant0 a e "ften left in RQC a't"5atic !a ia-le0 a e n" 5all; (laced "f t2e 0tac6 "f t2e c' 'nning ta06 -'t n"t al1a;0& entl;

,"cal Ra ia-le0 a e 0t" ed in Stac6& Regi0te !a ia-le0 a e 0t" ed in Regi0te & Sl"-al F 0tatic !a ia-le0 a e 0t" ed in data 0eg5ent& *2e 5e5" ; c eated d;na5icall; a e 0t" ed in Tea( +nd t2e E ( "g a5 in0t 'cti"n0 get 0t" ed in c"de 0eg5ent and t2e exte n !a ia-le0 al0" 0t" ed in data 0eg5ent& More about Heap? 3ynamic 3ata 1tructures. 4he 5eap

5he o!erating system and se+eral a!!lications- along )ith their global +ariables and stac8 s!aces- all consume !ortions of memory. Bhen a !rogram com!letes executionit releases its memory for reuse by other !rograms. >ote that !art of the memory s!ace remains unused at any gi+en time. 2 ty!ical personal computer or )or8station today has some)here bet)een 1T and T. megabytes of <24 installed. Gsing a techni*ue called virtual memory- the system can s)a! !ieces of memory on and off the machine:s hard dis8 to create an illusion for the ;@G that it has much more memory- for exam!le 000 to /00 megabytes. Bhile this illusion is com!lete as far as the ;@G is concerned- it can sometimes slo) things do)n tremendously from the user:s !ers!ecti+e. 6es!ite this dra)bac8- +irtual memory is an extremely useful techni*ue for "increasing" the amount of <24 in a machine in an inex!ensi+e )ay. #et:s assume for the sa8e of this discussion that a ty!ical com!uter has a total memory s!ace of- for exam!le- /0 megabytes (regardless of )hether that memory is im!lemented in real <24 or in +irtual memory). 5he operating system on the machine is in charge of the /0(megabyte memory s!ace. 5he o!erating system uses the s!ace in se+eral different )ays- as sho)n here.

5his is- of course- an ideali7ation- but the basic !rinci!les are correct. 2s you can seememory holds the executable code for the different a!!lications currently running on the machine- along )ith the executable code for the o!erating system itself. 9ach a!!lication has certain global +ariables associated )ith it. 5hese +ariables also consume memory. ,inally- each a!!lication uses an area of memory called the stack)hich holds all local +ariables and !arameters used by any function. 5he stac8 also remembers the order in )hich functions are called so that function returns occur correctly. 9ach time a function is called- its local +ariables and !arameters are "!ushed onto" the stac8. Bhen the function returns- these locals and !arameters are "!o!!ed." $ecause of this- the si7e of a !rogram:s stac8 fluctuates constantly as the !rogram is running- but it has some maximum si7e. 2s a !rogram finishes execution- the o!erating system unloads it- its globals and its stac8 s!ace from memory. 2 ne) !rogram can ma8e use of that s!ace at a later time. 1n this )ay- the memory in a com!uter system is constantly "recycled" and reused by !rograms as they execute and com!lete. 1n general- !erha!s /0 !ercent of the com!uter:s total memory s!ace might be unused at any gi+en moment. 5he o!erating system o)ns and manages the unused memoryand it is collecti+ely 8no)n as the heap. 5he hea! is extremely im!ortant because it is a+ailable for use by a!!lications during execution using the ; functions malloc (memory allocate) and free. 5he hea! allo)s !rograms to allocate memory exactly )hen they need it during the execution of a !rogram- rather than !re(allocating it )ith a s!ecifically(si7ed array declaration. 6s it really important to check that the pointer is 7ero after each allocation? Des. ?ince the hea! +aries in si7e constantly de!ending on )hich !rograms are running- ho) much memory they ha+e allocated- etc.- there is ne+er any guarantee that a call to malloc )ill succeed. Dou should chec8 the !ointer after any call to malloc to ma8e sure the !ointer is +alid. What happens if 6 forget to delete a block of memory before the program terminates? Bhen a !rogram terminates- the o!erating system "cleans u! after it-" releasing its executable code s!ace- stac8- global memory s!ace and any hea! allocations for recycling. 5herefore- there are no long(term conse*uences to lea+ing allocations !ending at !rogram termination. Ho)e+er- it is considered bad form- and "memory lea8s" during the execution of a !rogram are harmful- as discussed belo).

Bhat is hea! and stac8K


$he stack is a place in the computer memory where all the ariables that are declared and initiali%ed before runtime are stored. $he heap is the section of computer memory where all the ariables created or initiali%ed at runtime are stored.

Bhat are the memory segmentsK


$he distinction between stack and heap relates to programming. &hen you loo' at your computer memory( it is organi%ed into three segments)

"

text (code) segment

" "

stac' segment heap segment $he text segment (often called code segment) is where the compiled code of the program itself resides. &hen you open some *+* file in ,otepad( you can see that it includes a lot of -.ibberish- language( something that is not readable to human. It is the machine code( the computer representation of the program instructions. $his includes all user defined as well as system functions.

,ow let/s get to some details

What is stack?
$he two sections other from the code segment in the memory are used for data. $he stack is the section of memory that is allocated for automatic variables within functions. 0ata is stored in stac' using the 1ast In 2irst 3ut (1I23) method. $his means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. 4tac' is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrie ed.

Bhat is heapK
3n the other hand( heap is an area of memory used for dynamic memory allocation. 5loc's of memory are allocated and freed in this case in an arbitrary order. $he pattern of allocation and si%e of bloc's is not 'nown until run time. Heap is usually being used by a program for many different purposes. $he stac' is much faster than the heap but also smaller and more expensi e.

5eap and stack from programming perspective


6ost ob7ect8oriented languages ha e some defined structure( and some come with so8 called 5ain() function. &hen a program begins running( the system calls the function

5ain() which mar's the entry point of the program. 2or example e ery !( !99( or !: program must ha e one function named 5ain(). ,o other function in the program can be called 5ain(). 5efore we start explaining( let/s ta'e a loo' at the following example)

int x# !"id 5ain() 3 int ;# c2a 0t # 0t = 5all"c(<0)# 0t" age %$ 0iGe = calcSiGe(10)#

$% 0tatic 0tac6 0t" age %$ $% d;na5ic 0tac6 0t" age %$ $% d;na5ic 0tac6 0t" age %$ $% all"cate0 <0 -;te0 "f d;na5ic 2ea( $% d;na5ic 2ea( 0t" age %$

&hen a program begins executing in the main() function( all variables declared within main() will be stored on the stack. If the 5ain() function calls another function in the program( for example calcSiGe()( additional storage will be allocated for the ariables in calcSiGe(). $his storage will be allocated in the heap memory segment. ,otice that the parameters passed by 5ain() to calcSiGe() are also stored on the stac'. If the calcSiGe() function calls to any additional functions( more space would be allocated at the heap again. &hen the calcSiGe() function returns the alue( the space for its local ariables at heap is then deallocated and heap clears to be a ailable for other functions. $he memory allocated in the heap area is used and reused during program execution. It should be noted that memory allocated in heap will contain garbage alues left o er from pre ious usage. 6emory space for objects is always allocated in heap. 3b7ects are placed on the heap. uilt!in datatypes li'e int( double( float and parameters to methods are allocated on the stac'.

e-plain memory layout of a " program


# Ce5" ; ,a;"'t c"n0i0t0 f"ll"1ing 0eg5ent0 12e e data$text a e 5anaged4 1) *ext Seg5ent 4 E"n0i0t0 0e ial "f in0t 'cti"n& 2) Mata Seg5ent 4 E"n0i0t09 Mata defa'lt " defined cla00 0(ecifie i) Stac6 Seg5ent 4 + ea 12e e t2e c' ent f'ncti"n data i0 5anaged& ii) Tea( Seg5ent 4 + ea 12e e d;na5icall; all"cated data 0t" ed& ?) BSS Seg5ent4 F" 'ninitialiGed data& Mata 0eg5ent i0 act'all; c"ntain0 t2e 0tatic data 12ic2 i0 decided "n t2e c"5(ile ti5e& Re0t "f + ea i0 '0ed -; Stac6 and Tea( 12ic2 i0 act'all; 5"dified 'n ti5e& Stac6 g "10

f "5 2ig2e add e00 t" l"1e "(("0ite t" t2e 0tac6& # S*+EU L L V O L TE+B BSS M+*+ SESCEA* EQME SESCEA*

+dd e00& Tea( g "1 7'0t

HH>F'nc a g'5ent0 and

et' n !al(l"cal0)

HH> M;na5ic Mata HH> UninitialiGed Mata (Sl"-al and 0tatic defa'lt t" Ge ") HH> Initiliali0ed data (Sl"-al and 0tatic 1it2 init !al0) HH> In0t 'cti"n0 .,"1e Ce5" ; +dd e00/

BSS4 Block Started by Symbol

Accessing fixed memory locations


Embedded systems are often characterized by requiring the programmer to access a specific memory location. On a certain project it is required to set an integer variable at the absolute address 0x !a" to the value 0xaa##. $he compiler is a pure %&'( compiler. )rite code to accomplish this tas*. $his problem tests whether you 'now that it is legal to typecast an integer to a pointer in order to access an absolute location. $he exact syntax aries depending upon one/s style. Howe er( I would typically be loo'ing for something li'e this) int *ptr; ptr = (int *)0x6 a!; *ptr = 0xaa""; ; more obscure approach is) *(int * const)(0x6 a!) = 0xaa"";

#ode examples
$%& )hat does the follo+ing code output and +hy? void foo(void) { unsigned int a = 6; int ' = (%0; (a)' * 6) + puts(,* 6,) -puts(,.= 6,); } $his <uestion tests whether you understand the integer promotion rules in !8an area that I find is ery poorly understood by many de elopers. ;nyway( the answer is that this outputs -= >.- $he reason for this is that expressions in ol ing signed and unsigned types ha e all operands promoted to unsigned types. $hus comes a ery large positi e integer and the expression e aluates to greater than >. $his is a ery important point in embedded systems where unsigned data

types should be used fre<uently (see #eference ?). If you get this one wrong( you are perilously close to not getting the 7ob. unsigned int /ero = 0; unsigned int comp/ero = 0x0000; 1*$2s complement of /ero *1 3n machines where an int is not @> bits( this will be incorrect. It should be coded) unsigned int comp/ero = 30; $his <uestion really gets to whether the candidate understands the importance of word length on a computer. In my experience( good embedded programmers are critically aware of the underlying hardware and its limitations( whereas computer programmers tend to dismiss the hardware as a necessary annoyance.

What is 0899 pointer and what is its use?

A: 5he language definition states that for each !ointer ty!e- there is a s!ecial +alue((
the EEnull !ointer::(()hich is distinguishable from all other !ointer +alues and )hich is EEguaranteed to com!are une*ual to a !ointer to any obCect or function.:: 5hat is- a null !ointer !oints definiti+ely no)here; it is not the address of any obCect or function. 5he address(of o!erator F )ill ne+er yield a null !ointer- nor )ill a successful call to 5all"c.[footnote] (5all"c does return a null !ointer )hen it fails- and this is a ty!ical use of null !ointers3 as a EEs!ecial:: !ointer +alue )ith some other meaning- usually EEnot allocated:: or EEnot !ointing any)here yet.::) 2 null !ointer is conce!tually different from an uninitiali7ed !ointer. 2 null !ointer is 8no)n not to !oint to any obCect or function; an uninitiali7ed !ointer might !oint any)here. ?ee also *uestions @.A0- B.@- and B.A@. 2s mentioned abo+e- there is a null !ointer for each !ointer ty!e- and the internal +alues of null !ointers for different ty!es may be different. 2lthough !rogrammers need not 8no) the internal +alues- the com!iler must al)ays be informed )hich ty!e of null !ointer is re*uired- so that it can ma8e the distinction if necessary

What is malloc function do?

6tem :alue Header file stdlib.h 6eclaration +oid *malloc(si7ePt si7e); <eturn returns a !ointer to the memory. 1f there is insufficient memory- malloc() returns a null !ointer. 1t is im!ortant to +erify that the return +alue is not null before using it.

Allocate sufficient memor! to hold structures of t!pe addr


#incl'de:0tdi"&2> 0t 'ct add 3 char na5e.40/#

char char char char 8#

0t eet.40/# cit;.40/# 0tate.?/# Gi(.10/#

5ain() 3 0t 'ct add

%(#

( = 5all"c(0iGe"f(0t 'ct add ))# if((==AU,,) 3 ( intf(=+ll"cati"n E exit(1)# 8 return (# " \n=)#

:hat i* e5it(# function do;


"include #stdlib.h$
oid oid exit (int status) _Exit (int status)

Description
$he exit and %&xit functions terminate a process. 5efore termination( exit performs the following functions in the order listed) !all the functions registered with the of their registration. 2lush all open output streams. !lose all open streams. Cnlin' all files created with the
tmpfile atexit

function( in the re erse order

function.

Return Values
$he exit and %&xit functions ne er return.

3ifference between /61" and "61" processor


CISC "ronounced sisk, and stands for Complex Instruction Set Computer. #ost "C$s use C"% based on this architecture. &or instance Intel and A#' C"%$s are based on CI(C architectures. )!picall! CI(C chips ha*e a large amount of different and complex instructions. )he philosoph! behind it is that hardware is alwa!s faster than software, therefore one should

make a powerful instructionset, which pro*ides programmers with assembl! instructions to do a lot with short programs. In common CI(C chips are relati*el! slow (compared to +I(C chips) per instruction, but use little (less than +I(C) instructions. ISC "ronounced risk, and stands for educed Instruction Set Computer. +I(C chips e*ol*ed around the mid,-./0 as a reaction at CI(C chips. )he philosoph! behind it is that almost no one uses complex assembl! language instructions as used b! CI(C, and people mostl! use compilers which ne*er use complex instructions. Apple for instance uses +I(C chips. )herefore fewer, simpler and faster instructions would be better, than the large, complex and slower CI(C instructions. 1owe*er, more instructions are needed to accomplish a task. An other ad*antage of +I(C is that , in theor! , because of the more simple instructions, +I(C chips re2uire fewer transistors, which makes them easier to design and cheaper to produce. &inall!, it$s easier to write powerful optimised compilers, since fewer instructions exist.

ISC vs CISC )here is still considerable contro*ers! among experts about which architecture is better. (ome sa! that +I(C is cheaper and faster and therefor the architecture of the future. Others note that b! making the hardware simpler, +I(C puts a greater burden on the software. (oftware needs to become more complex. (oftware de*elopers need to write more lines for the same tasks. )herefore the! argue that +I(C is not the architecture of the future, since con*entional CI(C chips are becoming faster and cheaper an!wa!.

'(S' vs )(S' In the early days of the computer industry( high le el programming languages were not a ailable and most of the wor' was done in assembly language. $herefore !DC designers tried to ma'e instructions that would do as much wor' as possible. $he general thin'ing at that time was that hardware design was easier than compiler design( so large parts of the complexity of the software went into the hardware (andEor microcode). $his design philosophy was named !omplex Instruction 4et !omputer (!I4!). &ith the ad ent of higher le el languages( a new design strategy started to gain more and more popularity) instructions which -do less- may still pro ide high performances if this simplicity can be utili%ed to ma'e instructions execute ery <uic'ly. $he focus on -reduced instructions- led to the resulting machine being called a #educed Instruction 4et !omputer (#I4!).

5he goal )as to ma8e instructions so sim!le that they could easily be !i!elined- in order to achie+e a single cloc8 through!ut at high fre*uencies. ;urrently- many !rocessors are called <1?; because there is a !erce!tion tat <1?; is faster than ;1?;. 5his can be confusing because many of the !rocessors a+ailable are identified as being "<1?;( li8e"- but are- in fact- ;1?; !rocessors. Dou must be a)are that in some a!!lications- ;1?; !rocessor can execute code faster than <1?; !rocessors- or execute a!!lications that a <1?; !rocessor cannot.
!haracteristics of a #I4! architecture) *mphasis on softwareF 4ingle8cloc'( reduced instruction onlyF Cniform instruction format( using a single word with the opcode in the same bit positions in e ery instruction( demanding less decodingF Identical general purpose registers( allowing any register to be used in any context( simplifying compiler designF 4imple addressing modes. !omplex addressing performed ia se<uences of arithmetic andEor load8store operationsF $ypically larger code si%esF 2ew data types in hardwareF

!haracteristics of a !I4! architecture) *mphasis on hardwareF Includes multi8cloc' complex instructionsF 4mall code si%esF !omplex data types in hardwareF some !I4!s ha e byte string instructions( or support complex numbersF

what is the difference between structure and union?


&hile structure enables us treat a number of different ariables stored at different in memory ( a union enables us to treat the same space in memory as a number of different ariables. $hat is a Cnion offers a way for a section of memory to be treated as a ariable of one type on oneoccasion and as a different ariable of a different type on another occasion. Cnion allocates the memory e<ual to the maximum memory re<uired by the member of the unionbut structure allocates the memory e<ual to the total memory re<uired by the members. Cnion allocates the memory e<ual to the maximum memory re<uired by the member of the unionbut structure allocates the memory e<ual to the total memory re<uired by the members. *xample) union exforsysGt H char cF int iF float fF I exforsysF

0efines three elements)

exforsys.c exforsys.i exforsys.f *ach one with a different data type. 4ince all of them are referring to the same location in memory( the modification of one of the elements will affect the alue of all of them. &e cannot store different alues in them independent from each other.

-ifference are# 1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. &. "n union$ one bloc. is used by all the member of the union but in case of structure$ each member have their own memory space. '. union is best in the environment where memory is less as it shares the memory allocated.+ut structure can not implemented in shared memory. /. 0s memory is shared$ambi uity are more in union$but less in structure. 5. self referencial union can not be implemented in any datastructure $but self referencial structure can be implemented. *ifference between *efinitions versus *eclarations

$oth data obCects (+ariables) and functions are defined or declared. 5his cha!ter discusses only +ariables; ho)e+er- the conce!ts are the same for a function also. 5he difference bet)een defining and declaring a data obCect is that- )hen a data obCect is declared- only its attributes are made 8no)n to the com!iler. Bhen an obCect is defined- not only are its attributes made 8no)n- but also the obCect is created. ,or a +ariable- memory is allocated to hold it; for a function- its code is com!iled into an obCect module.

Bhat is a local bloc8K


2 local bloc8 is any !ortion of a ; !rogram that is enclosed by the left brace (3) and the right brace (4). 2 ; function contains left and right braces- and therefore anything bet)een the t)o braces is contained in a local bloc8. Hariables can be declared )ithin local bloc8s- but they must be declared only at the beginning of a local bloc8. Hariables declared in this manner are +isible only )ithin the local bloc8. 6u!licate +ariable names declared )ithin a local bloc8 ta8e !recedence o+er +ariables )ith the same name declared outside the local bloc8.
&hy is !;, needed"

&hat are different type of !;,

How the framesE!;, message are decoded &hat is meaning of physical layer here

&hat is !;, protocol"

&hat is difference between !;, ; and !;, 5 &hat arbitration is in !;, message"

&hat are error chec's in !;," How ecu distinguish between normal can and extended can message"

What is cache memory? A C"% cache is a cache used b! the central processing unit of a computer to reduce the a*erage time to access memor!. )he cache is a smaller, faster memor! which stores copies of the data from the most fre2uentl! used main memor! locations 1igh,speed static +A# ((+A#) directl! connected to the C"%, usuall! between 56789,5#9. Cache memor! operates like a :fre2uentl! used data: file for !our C"%. )he cache d!namicall! stores and accesses the data !our C"% and applications access most often so that it can be more 2uickl! retrie*ed. ...
2 ;@G cache is a cache used by the central processing unit of a computer to reduce the a+erage time to access memory. 5he cache is a smaller- faster memory )hich stores co!ies of the data from the most fre*uently used main memory locations. 2s long as most memory accesses are cached memory locations- the a+erage latency of memory accesses )ill be closer to the cache latency than to the latency of main memory. ;ache memory is random access memory (#;6) that a com!uter microprocessor can access more *uic8ly than it can access regular <24. 2s the micro!rocessor !rocesses data- it loo8s first in the cache memory and if it finds the data there (from a !re+ious reading of data)- it does not ha+e to do the more time(consuming reading of data from larger memory. ;ache memory is sometimes described in le+els of closeness and accessibility to the micro!rocessor. 2n 1@ cache is on the same chi! as the micro!rocessor. (,or exam!le- the DowerD! T01 !rocessor has a O0 8ilobyte le+el(1 cache built into its chi!.) 1? is usually a se!arate static <24 (4#;6) chi!. 5he main <24 is usually a dynamic <24 (0#;6) chi!. 1n addition to cache memory- one can thin8 of <24 itself as a cache of memory for hard dis' storage since all of <24:s contents come from the hard dis8 initially )hen you turn your com!uter on and load the o!erating system (you are loading it into <24) and later as you start ne) a!!lications and access ne) data. <24 can also contain a s!ecial area called a dis' cache that contains the data most recently read in from the hard dis8.

What is the difference between rs646 and rs789 port?


6ifference bet)een <s0O0 and <?.U/ is =nly 6istance <?0O0 is not for long or far distance .. but <?.U/ can be use for #=>V9?5 distance .. <?0O0 is single ended communication )here as <?.U/ is 6ifferential communication. 4ax.no of recei+ers in <?0O0 is only =>9- )hereas in <?.U/ is O0. 4ax.no of transmitters in <?0O0 is only =>9 )hereas in <?.U/ is O0.

)S+,+ Specifications and standard

/1;<; ?1>V#9 ode of Operation (9>696 1 4otal 0umber of 3rivers and /eceivers 6<1H9< on One 9ine #One driver active at a time 1 for /1=>? networks& <9;H< a-imum "able 9ength /0 ,5. a-imum 3ata /ate #=@ft. - =@@@ft. for 008bFs /1=;;A/1=>?& a-imum 3river Output :oltage XF(0/H 3river Output 1ignal XF(/H to 9oaded 9evel #9oaded in.& XF(1/H 3river Output 1ignal 8nloaded XF(0/H 9evel #8nloaded a-& 3river 9oad 6mpedance #Ohms& O8 to Y8 a-. 3river "urrent in Power On >F2 5igh B 1tate a-. 3river "urrent in XF(Tm2 Power Off 5igh B 1tate Z XF(0+ 1lew /ate # a-.& O0HFu? /eceiver 6nput :oltage /ange XF(1/H /eceiver 6nput 1ensitivity XF(OH /eceiver 6nput /esistance #Ohms&% #C O8 to Y8 1tandard 9oad for /1=>?&

1P,"6!6"A46O01

/1=;< /1=;; /1=>? ?1>V#9 61,,9<9>512# 61,,9<9>512# (9>696 1 6<1H9< 10 <9;H< .000 ,5. 1008bFs XF(TH XF(O.TH XF(TH %=./0 >F2 XF(100u2 2dCustable XF(10H XF(000mH .8 min. 1 6<1H9< 10 <9;H< .000 ,5. O0 6<1H9< O0 <9;H< .000 ,5.

104bFs(100WbFs 104bFs(100WbFs (0.0/H to XTH XF(0.0H XF(TH 100 >F2 XF(100u2 >F2 (10H to X10H XF(000mH .8 min. (YH to X10H XF(1./H XF(TH /. XF(100u2 XF(100u2 >F2 (YH to X10H XF(000mH %=108

What!s the " S" in S#$#% S&'(% S&##? )he +( stands for ecommended Standard. ;othing reall! agreed upon or official. At least not in the sense of the :made,b!,committee: standards like I<<<,-5/= and I<<<,->.=. ?hat does this mean@ 9ecause +( standards are merel! recommended and technicall! general, lots of manufacturers de*elop products that are at best inferior. )he! cut corners and cheat in order to manufacture cheaper products. Simple) * +uple) One of the most fundamental concepts of communications technolog! is the difference between (implex and 'uplex. Simple) can be *iewed as a communications :one,wa! street:. 'ata onl! flows in one direction. )hat is to sa!, a de*ice can be a recei*er or a transmitter exclusi*el!. A good example of simplex communications is a radio station and !our car radio. Information flows onl! in one direction where the radio station is the transmitter and the recei*er is !our car radio. (implex is not often used in computer communications because there is no wa! to *erif! ,hen or if data is recei*ed. 1owe*er, simplex communications is a *er! efficient wa! to distributed *ast amounts of

information to a large number of recei*ers. +uple) communications o*ercome the limits of (implex communications b! allowing the de*ices to act as transcei*ers. 'uplex communication data flow in both directions thereb! allowing *erification and control of data receptionAtransmission. <xactl! when data flows bi,directionall! further defines 'uplex communications. -ull +uple) de*ices can transmit and recei*e data at the same time. +(5>5 is an example of &ull 'uplex communications. )here are separate transmit and recei*e signal lines that allow data to flow in both directions simultaneousl!. +(=55 de*ices also operate &ull 'uplex. Half.+uple) de*ices allow both transmission and recei*ing, but not at the same time/ <ssentiall! onl! one de*ice can transmit at a time while all other half,duplex de*ices recei*e. 'e*ices operate as transcei*ers, but not simultaneous transmit and recei*e. +(=/6 operates in a half duplex manner.

How the )S!+,+ serial interface works


;n #48?A? interface has the following characteristics)

Cses a J pins connector -058J- (older D!s use ?K pins -058?K-). ;llows bidirectional full8duplex communication (the D! can send and recei e data at the same time). !an communicate at a maximum speed of roughly @0L5ytesEs.

DB-

connector

Mou probably already saw this connector on the bac' of your D!.

It has J pins( but the A important ones are)

pin ?) #x0 (recei e data). pin A) $x0 (transmit data). pin K) .,0 (ground).

Csing 7ust A wires( you can send and recei e data.

!erial communication
0ata is sent one bit at a timeF one wire is used for each direction. 4ince computers usually need at least se eral bits of data( the data is -seriali%ed- before being sent. 0ata is commonly sent by chun's of N bits. $he 145 (data bit 0) is sent first( the 645 (bit B) last.

"synchronous communication
$his interface uses an -asynchronous- protocol. $hat means that no cloc' signal is transmitted along the data. $he recei er has to ha e a way to -time- itself to the incoming data bits.

In the case of #48?A?( that/s done this way) @. ?. A. O. K. 5oth side of the cable agree in ad ance on the communication parameters (speed( format...). $hat/s done manually before communication starts. $he transmitter sends a -@- when and as long as the line is idle. $he transmitter sends a -start- (a -0-) before each byte transmitted( so that the recei er can figure out that data is coming. ;fter the -start-( data comes in the agreed speed and format( so the recei er can interpret it. $he transmitter sends a -stop- (a -@-) after each data byte.

1et/s see how loo's the byte 0xKK when transmitted)

5yte 0xKK is 0@0@0@0@ in binary. 5ut since it is transmitted 145 (bit80) first( the line toggles li'e that) @808@808@808@80. Here/s another example)

Here the data is 0x!O( can you see it" $he bits transitions are harder to see. $hat illustrates how important it is for the recei er to 'now at which speed the data is sent.

#ow fast can we send data?


$he speed is specified in baud( i.e. how many bits8per8seconds can be sent. 2or example( @000 bauds would mean @000 bits8per8seconds( or that each bit lasts one millisecond. !ommon implementations of the #48?A? interface (li'e the one used in D!s) don/t allow 7ust any speed to be used. If you want to use @?AOK> bauds( you/re out of luc'. Mou ha e to settle to some -standard- speed. !ommon alues are)

@?00 bauds. J>00 bauds. ANO00 bauds. @@K?00 bauds (usually the fastest you can go).

;t @@K?00 bauds( each bit lasts (@E@@K?00) P N.BQs. If you transmit N8bits data( that lasts N x N.BQs P >JQs. 5ut each byte re<uires an extra start and stop bit( so you actually need @0 x N.BQs P NBQs. $hat translates to a maximum speed of @@.KL5ytes per second. ;t @@K?00 bauds( some D!s with buggy chips re<uire a -long- stop bit (@.K or ? bits long...) which ma'e the maximum speed drop to around @0.KL5ytes per second.

$hysical layer
$he signals on the wires use a positi eEnegati e oltage scheme.

-@- is sent using 8@0R (or between 8KR and 8@KR).

-0- is sent using 9@0R (or between KR and @KR).

4o an idle line carries something li'e 8@0R

0C1233 System 4nits -eature List


>5,bit microcontroller '(", 1igh "erformance >5,bit )riCore B-.> C"% with =,(tage "ipeline C 'ual Issue super,scalar implementation C Circular 9uffer and bit,re*erse addressing modes for '(" algorithms C &lexible multi,master interrupt s!stem C Ber! fast interrupt response time C 1ardware controlled context switch for task switch and interrupts C On,chip #emor! D 67 89!te 'ata #emor! ((+A#) D 0 or / 89!te O*erla! #emor!-) D -7 89!te Code (cratchpad #emor! (("+A#) D / 89!te Instruction Cache (ICAC1<) D -.6 #9!te &lash #emor!, of which -60= 89!te is used as program flash, and >5 89!te is used as data flash (= 89!te <<"+O# emulation) D -7 89!te 9oot +O# D On,chip (+A#s optionall! with parit! error detection C '#A Controller with / channels for data transfer operations between peripheral units and memor! locations C On,chip 9us (!stems D 7=,9it Eocal #emor! 9us (E#9) to &lash memor! D (!stem "eripheral 9us (("9) for interconnections of functional units C "eripheral Control "rocessor with single c!cle instruction ("C"5) C )wo As!nchronousA(!nchronous (erial Channels (A(C0A-) with baudrate generator, parit!, framing and o*errun error detection C )wo 1igh (peed (!nchronous (erial Channels (((C0A-) with programmable data length and shift direction C One #ultiCA; #odule with two CA; nodes and 7= message buffers for high efficienc! data handling C One #icro(econd 9us Interface for connection to external power de*ices (#(C0) C )wo high speed #icro Eink Interfaces (#EI0A-) for inter,processor communication C One Feneral "urpose )imer Arra! #odule (F")A) with a powerful set of digital signal filtering and timer functionalit! to realiGe autonomous and complex InputAOutput management

C One medium speed Analog,to,'igital Con*erter %nit (A'C0) with -0,bit resolution ()%<H5E(9) supporting >5 input channels C One fast Analog,to,'igital Con*erter %nit (&A'C) with 5 input channels 'ifferent t!pe of memor!
various types and sizes of on-chip memories (DRAM, SRAM, ROM, OTPand !ash"#

Why 0C1233 is called tricore?


5ri;ore is the first single(core O0(bit 4;G(6?@ architecture o!timi7ed for realtime embedded systems. 6e+elo!ed by a team )ith o+er O00 collecti+e years of !rocessor ex!erience- 5ri;ore is an inno+ati+e- a)ard )inning architectural solution )hich truly unifies the best of three )orlds ( real(time ca!abilities of microcontrollers- com!utational !ro)ess of 6?@s- and the highest !erformanceF!rice im!lementations of <1?; load(store architectures.

eatures$%enefits
M&' eatures

I ,ast context s)itch & lo) interru!t latency I 1T(bit and O0(bit instruction formats I @o)erful bit mani!ulation su!!ort

Tricore%enefits
I 1ntegrated 4;G(6?@ instructions in one core I ,ast and efficient !rocessing of multi!le tas8s on one engine I #o) code si7e and inherent high le+el language su!!ort I =ne de+elo!ment toolset for both 4;G and 6?@ tas8s I Higher flexibility and lo)er cost

Processor

eatures

I O0(bit load(store Har+ard architecture I ?u!erscalar execution I 1T address and 1T data registers

DSP

eatures

I ?ustained through!ut of t)o 1Tx1T 42;s !er cloc8 I ?146 !ac8ed arithmetic and 7ero o+erhead loo!s I 6?@ addressing modes and saturated math

(hat is )arvard and *eumann Architecture + )he 1ar*ard architecture is computer architecture with ph!sicall! separate storage and signal pathwa!s for instructions and data.

1n a +on >eumann architecture- !rogram and data are stored in the same memory and managed by the same information(handling subsystem. 1n the Har+ard architecture!rogram and data are stored and handled by different subsystems. 5his is the essential difference bet)een the t)o architectures. Harvard architecture has separate data and instruction busses( allowing transfers to be performed simultaneously on both busses. ; von -eumann architecture has only one bus which is used for both data transfers and instruction fetches( and therefore data transfers and instruction fetches must be scheduled 8 they can not be performed at the same time. ;t higher cloc' speeds( caches are useful as the memory speed is proportionally slower. Harvard architectures tend to be targeted at higher performance systems( and so caches are nearly always used in such systems. .on -eumann architectures usually ha e a single unified cache( which stores both instructions and data. $he proportion of each in the cache is ariable( which may be a good thing. It would in principle be possible to ha e separate instruction and data caches( storing data and instructions separately. $his probably would not be ery useful as it would only be possible to e er access one cache at a time. A*' ?election of 26; 5he !arameters used in selecting an 26; are +ery similar to those considered for a 62; selection. [ 9rrorF2ccuracy3 \uantising error re!resents the difference bet)een an actual analog +alue and its digital re!resentation. 1deally- the *uantising error should not be greater than ] ^ #?$. [ <esolution3 6H to cause 1 bit change in out!ut [ =ut!ut Holtage <ange 1n!ut Holtage <ange [ =ut!ut ?ettling 5ime ;on+ersion 5ime [ =ut!ut ;oding (usually binary)

<ef 3 ;0!G0;! and ;0!N0N

Bhat is indirectionK
1f you declare a +ariable- its name is a direct reference to its +alue. 1f you ha+e a !ointer to a +ariable or any other obCect in memory- you ha+e an indirect reference to its +alue. 1f ! is a !ointer- the +alue of ! is the address of the obCect. *! means _a!!ly the indirection o!erator to !`; its +alue is the +alue of the obCect that ! !oints to. (?ome !eo!le )ould read it as _Vo indirect on !.`) *! is an l+alue; li8e a +ariable- it can go on the left side of an assignment o!erator- to change the +alue. 1f ! is a !ointer to a constant- *! is not a modifiable l+alue; it canIt go on the left side of an assignment. (?ee ,2\ 11.. and the discussion at the beginning of this cha!ter.) ;onsider the follo)ing !rogram. 1t sho)s that )hen ! !oints to i- *! can a!!ear )here+er i can.

#isting H11.1. 2n exam!le of indirection.


Ainclude Rstdio.h% int main() & int i; int *!; i = /; ! = & i; F* no) *! == i *F F* a@ is described in ,2\ H11.0U *F !rintf(_i=ad- !=a@- *!=adbn`- i- !- *!); *! = T; F* same as i = T *F !rintf(_i=ad- !=a@- *!=adbn`- i- !- *!); return 0; F* see ,2\ cH1.. *F ' 2fter ! !oints to i (! = &i)- you can !rint i or *! and get the same thing. Dou can e+en assign to *!- and the result is the same as if you had assigned to i.

Where does malloc function allocate the memory? 1n ;- the library function 5all"c is used to allocate a bloc8 of memory on the hea!.

Bhat is a null !ointerK Answer:


5here are times (see ,2\ H11..) )hen itIs necessary to ha+e a !ointer that doesnIt !oint to anything. 5he macro >G##- defined in Rstddef.h%- has a +alue thatIs guaranteed to be different from any +alid !ointer. >G## is a literal 7ero- !ossibly cast to +oid* or char*. ?ome !eo!le- notably ;XX !rogrammers- !refer to use 0 rather than >G##. Dou canIt use an integer )hen a !ointer is re*uired. 5he exce!tion is that a literal 7ero +alue can be used as the null !ointer. (1t doesnIt ha+e to be a literal 7ero- but thatIs the only useful case. 2ny ex!ression that can be e+aluated at com!ile time- and that is 7ero- )ill do. 1tIs not good enough to ha+e an integer +ariable that might be 7ero at runtime.) ?A+;I;F

Dou should ne+er go indirect on a null !ointer. 1f you do- your !rogram might get garbage- get a +alue thatIs all 7eros- or halt gracelessly.

Bhen is a null !ointer usedK Answer:


5he null !ointer is used in three )ays3 5o sto! indirection in a recursi+e data structure 2s an error +alue 2s a sentinel (5o )atch o+er as a guard) +alue

Gsing a >ull @ointer to ?to! 1ndirection or <ecursion


<ecursion is )hen one thing is defined in terms of itself. 2 recursi+e function calls itself. 5he follo)ing factorial function calls itself and therefore is considered recursi+e3
AJ 'umb implementationK should use a loop JA unsigned factorial( unsigned i ) 3 if ( i HH 0 LL i HH - ) 3 return -K 4 else 3 return i J factorial( i , - )K 4 4

2 recursi+e data structure is defined in terms of itself. 5he sim!lest and most common case is a (singularly) lin8ed list. 9ach element of the list has some +alue- and a !ointer to the next element in the list3
struct stringMlist 3 char JstrK AJ string (in this case) JA struct stringMlist JnextK 4K

5here are also doubly lin8ed lists ()hich also ha+e a !ointer to the !receding element) and trees and hash tables and lots of other neat stuff. DouIll find them described in any good boo8 on data structures. Dou refer to a lin8ed list )ith a !ointer to its first element. 5hatIs )here the list starts; )here does it sto!K 5his is )here the null !ointer comes in. 1n the last element in the list- the next field is set to >G## )hen there is no follo)ing element. 5o +isit all the elements in a list- start at the beginning and go indirect on the next !ointer as long as itIs not null3
while ( p NH ;%EE ) 3 AJ do something with p,Ostr JA p H p,OnextK 4

>otice that this techni*ue )or8s e+en if ! starts as the null !ointer.

Gsing a >ull @ointer 2s a ?entinel Halue

5he third )ay a null !ointer can be used is as a _sentinel` +alue. 2 sentinel +alue is a s!ecial +alue that mar8s the end of something. ,or exam!le- in main()- arg+ is an array of !ointers. 5he last element in the array (arg+[argc ) is al)ays a null !ointer. 5hatIs a good )ay to run *uic8ly through all the elements3
AJ

A simple program that prints all its arguments. It doesnPt use argc (Qargument countR)K instead, it takes ad*antage of the fact that the last *alue in arg* (Qargument *ectorR) is a null pointer. JA #include Sstdio.hO #include Sassert.hO int main( int argc, char JJarg*) 3 int iK printf(Qprogram name H TRUsTRTnR, arg*V0W)K for (iH-K arg*ViW NH ;%EEK XXi) printf(Qarg*VUdW H TRUsTRTnR, i, arg*ViW)K assert(i HH argc)K AJ see &AY ZI.6 JA return 0K AJ see &AY ZBI.= JA 4

Bhat is a void !ointerK Answer:


2 +oid !ointer is a ; con+ention for _a ra) address.` 5he com!iler has no idea )hat ty!e of obCect a +oid !ointer _really !oints to.` 1f you )rite
int *i!; i! !oints to an int. 1f you )rite +oid *!; ! doesnIt !oint to a +oidL

1n ; and ;XX- any time you need a +oid !ointer- you can use another !ointer ty!e. ,or exam!le- if you ha+e a char*- you can !ass it to a function that ex!ects a +oid*. Dou donIt e+en need to cast it. 1n ; (but not in ;XX)- you can use a +oid* any time you need any 8ind of !ointer- )ithout casting. (1n ;XX- you need to cast it.)
#still more to be seen&

What is bit padding and bit stuffing?


2it 1tuffing 1nserting bits in data in order to brea8 u! a bit !attern that may cause the transmission to go out of synchroni7ation. ,or exam!le- in 51 lines- timing is maintained by detecting a change from 0 to 1. 1f too many 7ero bits are transmitted consecuti+ely- the recei+ing end may lose synchroni7ation because too much time has !assed )ithout sensing +oltage. 5herefore- in long strings of 7eros- a set of bits that begins )ith a 1 and functions as a timing signal is "stuffed" into the stream of 7eros at certain inter+als.

Bhene+er a transmitter detects fi+e consecuti+e bits (including stuff bits) of identical +alue in the bitstream to be transmitted- it automatically inserts a com!limentary bit in the bitstream actually being transmitted. 5his stuff(bit )ill be remo+ed automatically by the recei+er. 1f more than fi+e consecuti+e bits )ith the same !olarity are detected bet)een ?tart of ,rame and the ;<; 6elimiter- the bit(stuffing rule has been +iolated. 2 ?tuff(9rror occurs and an 9rror ,rame is generated. 5he message is then re!eated.

5he bits )ithin the ;2> frame are encoded using a method 8no)n as non(return to 7ero (><d) )ith bit stuffing. ><d sim!ly means that the bit le+el remains in either the dominant or recessi+e state for the duration of the bit time- as o!!osed to an encoding scheme such as !ulse )idth modulation )here the bit le+el al)ays )ill return to 7ero at some !oint during the bit time. ><d schemes ha+e a !otential synchroni7ation !roblem if the number of bits sent in the frame is more than N10 since there may not be any le+el translation edges for the recei+ing node to synchroni7e on. 5o resol+e this issue- if more than fi+e bits are !resent )ith the same logic state- the ;2> s!ecifies that the transmitting node )ill stuff a bit of the o!!osite state into the bit stream. 2s a result- a synchroni7ation edge )ill be generated. 5he recei+ing node )ill remo+e this extra bit- so the stuffed bit has no effect on the resulting messageFdata being transmitted. $it stuffing a!!lies to the maCority of the fields in the data and remote frame3 start of frame- arbitration- control- data- and ;<;. $it stuffing also ser+es as one method for error detection on the bus. 1f more than fi+e bits are recei+ed )ith the same bit +alue- the bit( stuffing rule is +iolated- and an error is immediately detected. 2 &!rotocol' )hich guarantees the recei+er of &synchronous' data can reco+er the sender:s cloc8. Bhen the data stream sent contains a large number of adCacent bits )hich cause no transition of the signal- the recei+er cannot adCust its cloc8 to maintain !ro!er synchronised rece!tion. 5o eliminate the !ossibility of such a !athological case- )hen a !reset number of transitionless bits ha+e been transmitted- a bit )hich does cause a transition is "stuffed" (transmitted) by the sender. 5he recei+er follo)s the same !rotocol and remo+es the stuffed bit after the s!ecified number of transitionless bits- but can use the stuffed bit to reco+er the sender:s cloc8. 5he ad+antage of bit stuffing is that only a bit (not a &byte') is inserted in the data stream- and that only )hen the content of the data stream fails to !ro+ide a timing signal to the recei+er. 5hus +ery nearly 100a of the bits trans!orted are useful data. 1n contrast&asynchronous' transmission of data "thro)s a)ay" a start bit and one or more sto! bits for each data byte sent. (1eeT(0.(0O)

Padding
$its or characters that fill u! unused !ortions of a data structure- such as a field- !ac8et or frame. 5y!ically- !adding is done at the end of the structure to fill it u! )ith data- )ith the !adding usually consisting of 1 bits- blan8 characters or null characters. 1n storage- irrele+ant material- usually 7ero (0) bits- added to a data bloc8 in order to fill it to a minimum si7e- to force certain fields of control data or user data into certain !ositions or si7esor to !re+ent the user data from du!licating a bit !attern that has a s!ecific control meaning. ?ee also bloc'. 1n transmission- irrele+ant material added to a data bloc8- !ac8et- or frame in order to fill it to a minimum si7e- to force certain fields of control data or user data into certain !ositions or si7es- or to !re+ent the user data from du!licating a bit !attern that has a s!ecific control meaning. ?ee also bloc'- frame- and pac'et. 1n transmission- redundant- irrele+ant bits- usually one (1) bits- a!!ended to a bit stream in order to increase the bit rate or to maintain a session or connection during !eriods of inacti+ity. ?ee also 'eep ali e bits. 1n some synchronous !rotocols- such as binary synchronous communications ($isync- or $?;)- one or more o!tional characters that alert the recei+ing de+ice of the transmission of a bloc8 of data and ensure that the recei+ing de+ice is in sync )ith the data bits. ?ee also 54!protocol- and synchronous.

What is "/" in "A0? ("yclical /edundancy "hec8ing) 2n error chec8ing techni*ue used to ensure the accuracy of transmitting digital data. 5he transmitted messages are di+ided into !redetermined lengths )hich- used as di+idends- are di+ided by a fixed di+isor. 5he remainder of the calculation is a!!ended onto and sent )ith the message. 2t the recei+ing end- the com!uter recalculates the remainder. 1f it does not match the transmitted remainder- an error is detected.

What is deadlock?
1 ha+e t)o !rocesses3 one holds a sema!hore and the other )aits for the sema!hore. Bhen the !rocess )hich holds the sema!hore is deadthe deadloc8 occurs.

What is semaphore in operating system?


?ema!hores are de+ices used to hel! )ith synchroni7ation. 1f multi!le !rocesses share a common resource- they need a )ay to be able to use that resource )ithout disru!ting each other. Dou )ant each !rocess to be able to read from and )rite to that resource uninterru!ted. 2 sema!hore )ill either allo) or disallo) access to the resource- de!ending on ho) it is set u!. =ne exam!le setu! )ould be a sema!hore )hich allo)ed any number of !rocesses to read from the resource- but only one could e+er be in the !rocess of )riting to that resource at a time.

What is recursive function with e-ample?

What is virtual memory? What is page thrashing?


?ome o!erating systems (such as G>1c or Bindo)s in enhanced mode) use +irtual memory. Hirtual memory is a techni*ue for ma8ing a machine beha+e as if it had more memory than it really has- by using dis8 s!ace to simulate <24 (random(access memory). 1n the U0OUT and higher 1ntel ;@G chi!sand in most other modern micro!rocessors (such as the 4otorola TU0O0- ?!arc- and @o)er @;)- exists a !iece of hard)are called the 4emory 4anagement Gnit- or 44G. 5he 44G treats memory as if it )ere com!osed of a series of _!ages.` 2 !age of memory is a bloc8 of contiguous bytes of a certain si7e- usually .0eT or U1e0 bytes. 5he o!erating system sets u! and maintains a table for each running !rogram called the @rocess 4emory 4a!- or @44. 5his is a table of all the !ages of memory that !rogram can access and )here each is really located. 9+ery time your !rogram accesses any !ortion of memory- the address (called a _+irtual address`) is !rocessed by the 44G. 5he 44G loo8s in the @44 to find out )here the memory is really located (called the _!hysical address`). 5he !hysical address can be any location in memory or on dis8 that the o!erating system has assigned for it. 1f the location the !rogram )ants to access is on dis8- the !age containing it must be read from dis8 into memory- and the @44 must be u!dated to reflect this action (this is called a _!age fault`). Ho!e youIre still )ith me- because hereIs the tric8y !art. $ecause accessing the dis8 is so much slo)er than accessing <24- the o!erating system tries to 8ee! as much of the +irtual memory as !ossible in <24. 1f youIre running a large enough !rogram (or se+eral small !rograms at once)- there might not be enough <24 to hold all the memory used by the !rograms- so some of it must be mo+ed out of <24 and onto dis8 (this action is called _!aging out`). 5he o!erating system tries to guess )hich areas of memory arenIt li8ely to be used for a )hile (usually based on ho) the memory has been used in the !ast). 1f it guesses )rong- or if your !rograms are accessing lots of memory in lots of !laces- many !age faults )ill occur in order to read in the !ages that )ere !aged out. $ecause all of <24 is being used- for each !age read in to be accessed- another !age must be !aged out. 5his can lead to more !age faults- because no) a different !age of memory has been mo+ed to dis8. 5he !roblem of many !age faults occurring in a short time- called _!age thrashing-` can drastically cut the !erformance of a system. @rograms that fre*uently access many )idely se!arated locations in memory are more li8ely to cause !age thrashing on a system. ?o is running many small !rograms that all continue to run e+en )hen you are not acti+ely using them. 5o reduce !age thrashing- you can run fe)er !rograms simultaneously. =r you can try changing the )ay a large !rogram )or8s to maximi7e the ca!ability of the o!erating system to guess )hich !ages )onIt be needed. Dou can achie+e this effect by caching +alues or changing loo8u! algorithms in large data

structures- or sometimes by changing to a memory allocation library )hich !ro+ides an im!lementation of malloc() that allocates memory more efficiently. ,inally- you might consider adding more <24 to the system to reduce the need to !age out.

What is the difference between ::var and var::?


5he XX o!erator is called the increment o!erator. Bhen the o!erator is !laced before the +ariable (X X*ar)- the +ariable is incremented by 1 before it is used in the ex!ression. Bhen the o!erator is !laced after the +ariable (*arXX)- the ex!ression is e+aluated- and then the +ariable is incremented by 1. 5he same holds true for the decrement o!erator (,,).Bhen the o!erator is !laced before the +ariable- you are said to ha+e a !refix o!eration. Bhen the o!erator is !laced after the +ariable- you are said to ha+e a !ostfix o!eration. ,or instance- consider the follo)ing exam!le of !ostfix incrementation3
int x, !K x H -K ! H (xXX J 6)K

1n this exam!le- !ostfix incrementation is used- and x is not incremented until after the e+aluation of the ex!ression is done. 5herefore- ! e+aluates to 1 times /- or /. 2fter the e+aluation- x is incremented to 0. >o) loo8 at an exam!le using !refix incrementation3
int x, !K x H -K ! H (XXx J 6)K

5his exam!le is the same as the first one- exce!t that this exam!le uses !refix incrementation rather than !ostfix. 5herefore- x is incremented before the ex!ression is e+aluated- ma8ing it 0. Hence- ! e+aluates to 0 times /- or 10.

What is re-entrant function?


2ctually re(entrancy is only a issue in a !rogram that has more than one thread of execution (either multi!le treads or a thread )ith interru!t routines or multi!le treads )ith interru!t routines).

1n a single thread en+ironment re(entrancy is not an issue since the is only 1 thread of execution only 1 instance of the function can be running at any time.

1n a multi!le threaded en+ironment then accessing non(constant global data ma8es a thread non(re(entrant. 5hat is both your functions ,unction1 and ,unction0 are not re(entrant because the access the non(constant global +ariable V#=$2#.

5he non(constant !art is im!ortant because if the +ariable is a constant then it can not be changed so it al)ays has the same effect on function execution so this is re(entrant.
*xpandS4electS&rapS1ine ,umbers

1& 2& ?&

c"n0t int S,QB+, = <# int F'ncti"n1(!"id) 3 l"cal = S,QB+,# $% l"cal)=1# et' n l"cal#

$% define c"n0tant Sl"-al !a ia-le %$

.. /. T. Y.

int l"cal # $% define l"cal !a ia-le %$ ead t2e gl"-al !a ia-le %$

J& @&

2nother alternati+e is to !ass the data that is ma8ing the function not re(entrant so this is re(enrant
*xpandS4electS&rapS1ine ,umbers

1& 2& ?& 4&

int S,QB+, = <#

$% define Sl"-al !a ia-le %$

int F'ncti"n1(int in('t_!al'e) 3 int l"cal # $% define l"cal !a ia-le %$ l"cal = in('t_!al'e# $% l"cal)=1# et' n l"cal# 8 $% S"5e "t2e c"de el0e12e e %$ ead t2e l"cal (a a5ete %$

/. T. Y. U.
@& 10& 11& 12& 1?& 14&

e0'lt = F'ncti"n1(S,QB+,)#

!unctionC is no re-entrant as it only works on local variables and parameters% however th place calling !unctionC is not re-entrant as it uses the global variable D9O2A9. 5he real test of re(entrancy is are you access data that a different !ortion of the !rogram (including the same function running in a different thread) might be changing at the same timeK

A1. 4ust hold no static (or global) non(constant data.

int V#=$2# F* define Vlobal +ariable *F int ,unction 1(+oid) & int local ; F* define local +ariable *F local= V#=$2#; F* read the global +ariable *F localX=1; '

int ,unction 0 (+oid) & V#=$2#X=1; F* modifying the +alue of global +ariable *F

' 5he function reentrancy is de!ends on ?ingle thread en+ironment -the function 1 irres!ecti+e of accessing global +ariable in read mode is termed as reentrant 4ulti!le thread en+ironment- there is a !ossibility that Halue of _V#=$2#` may be o+errun by !arallel execution of function1 and function0 and hence function 1 called as >on reentrant

$ A reentrant function is one that can be used b! more than one task concurrentl! without

fear of data corruption. Con*ersel!, anon-reentrant function is one that cannot be shared b! more than one task unless mutual exclusion to the function is ensured either b! using a semaphore or b! disabling interrupts during critical sections of code. A reentrant function can be interrupted at an! time and resumed at a later time without loss of data. +eentrant functions either use local *ariables or protect their data when global *ariables are used. A reentrant function 'oes not hold static data o*er successi*e calls 'oes not return a pointer to static dataK all data is pro*ided b! the caller of the function %ses local data or ensures protection of global data b! making a local cop! of it #ust not call an! non,reentrant functions # It$s easier to remember when !ou understand what the term means. )he term :re,entrant: means that it is safe to :re.enter: the function while it is alread! executed, t!picall! in a concurrent en*ironment. In other words, when two tasks can execute the function at the same time without interfering with each other, then the function is re,entrant. A function is not re,entrant when the execution b! one task has an impact on the influence of another task. )his t!picall! is the case when a global state or data is used. A function that uses onl! local *ariables and arguments is t!picall! re,entrant. ?hat is compiler@
2 program that translates source code into object code. 5he com!iler deri+es its name from the )ay it )or8s- loo8ing at the entire !iece of source code and collecting and reorgani7ing the instructions.

; is a computer programming language. 5hat means that you can use ; to create lists of instructions for a com!uter to follo). ; is )hat is called a compiled language. 5his means that once you )rite your ; !rogram- you must run it through a compiler to turn your !rogram into an e-ecutable that the com!uter can run (execute). 5he ; !rogram is the human(readable form- )hile the executable that comes out of the com!iler is the machine(readable and executable form. 1n short to ma8e it machine readable code.

Why "ompiler are needed?


5<26151=>2##D- 4=?5 U($15 94$96696 @<=V<24? H2H9 $99> B<1559> 1> 2??94$#D #2>VG2V9. H=B9H9<- 6G9 5= 2 H2<195D =, <92?=>?- 4=?5 >9B 41;<=;=>5<=##9<? 1>;#G61>V 5H9 U($15 =>9? 2<9 >=B 9c@9;596 5= H2H9 2 ; ;=4@1#9< 2H21#2$#9. ;=4@2<96 5= 2> 9\G1H2#9>5 2??94$#D @<=V<24- 2 B9##(B<1559> ; @<=V<24 1? 5D@1;2##D 4=<9 <9262$#9 2>6 421>521>2$#9. @#G?- B15H ?=49 ;2<9 2>6 ?=49 24=G>5 =, ;H2>V9?- 5H9 ; @<=V<24 42D $9 4=H96 5= =5H9< 52<V95?.

What is po,er on reset circuit? 2 power-on reset (Po/) generator is a microcontroller or microprocessor peripheral that generates a reset signal )hen !o)er is a!!lied to the de ice. 1t ensures that the de+ice starts o!erating in a 8no)n state.
Bhen !o)ering u! any microcontroller- before the !o)er su!!ly +oltage reaches its final state-it goes through +oltage ranges- )here the de+ice is not guaranteed to o!erate. ?ince some de+iceIs cells )ill start o!erating at +oltage le+els lo)er than others- the de+ice may !o)er(u! in an un8no)n state. 5o guarantee that the de+ice starts u! in a 8no)n state- it is necessary to control the reset !hase.

# H5 +W5 6 IM7L6M68050I98S
6:06 85L 79W6 .98 6S60 )he &igures - [ 5 present simple circuits which onl! pro*ide power,on reset. )he! use an external +C to generate the reset pulse. )he time constant of the +C should be long enough to guarantee that the reset pulse is still present until B'' reaches B'' min.

)hese two circuits howe*er, do not protect against brown,out situations where power does not drop to Gero, but merel! dips below B'' min. In such situations, *oltage at 6S60 pin will not go low enough to guarantee a reset pulse.

4hread
Be can thin8 of a thread as basically a li$htwei$ht !rocess. Benefits of Threads vs Processes 1f im!lemented correctly then threads ha+e some ad+antages of (multi) !rocesses5hey ta8e3 #ess time to create a ne) thread than a !rocess- because the ne)ly created thread uses the current !rocess address s!ace. #ess time to terminate a thread than a !rocess. #ess time to s)itch bet)een t)o threads )ithin the same !rocess!artly because the ne)ly created thread uses the current !rocess address s!ace. #ess communication o+erheads (( communicating bet)een the threads of one !rocess is sim!le because the threads share e+erything3 address s!acein !articular. ?o- data !roduced by one thread is immediately a+ailable to all the other threads.

/* LLIST.C Program to illustrate linked lists */ #include <stdio.h> struct list { int value struct list *ne!t " main#$ { struct list n%& n'& n( int i n%.value ) %** n'.value ) '** n(.value ) (** n%.ne!t ) +n' n'.ne!t ) +n(

i ) n%.ne!t,>value -rint.#/0d1n/& n'.ne!t,>value$ "

>ot only this- but consider the follo)ing


n%.ne!t ) n'.ne!t n'2(.ne!t ) n'.ne!t n'.ne!t ) +n'2( /* deletes n' */ /* adds struct n'2( */

1n using lin8ed list structures- it is common to assign the +alue of 0 to the last !ointer in the list- to indicate that there are no more nodes in the list- egn(.ne!t ) *

,ngine 4opics "ommon /ail 1ystem 4,"50O9ODE "ommon /ail 1ystem


1-U00 bar inCection !ressure is e*ui+alent to the )ater !ressure of a de!th of 1U-000 meters dee!er than the 4ariana 5rench- the )orldIs dee!est ocean bottom. High res!onse inCectors by electronic control feature the highly accurate multi!le inCections (fi+e times). 2s a result- !articulate matter and >=x are mar8edly reduced- )hile im!ro+ements in fuel efficiency and tor*ue are achie+ed. @lus- engine noise and +ibration are also reduced. 6iesel engines ha+e the ad+antages of good fuel efficiency and lo) ;=0 emissions.

3iesel ,ngine

anagement 1y stem

59;H>=#=VD 1 ?u!!ly @um!


5he first(generation su!!ly !um! could generate !ressures of u! to 1-./0 bar- )hich re!resented the mechanical limits of the inner cam design. Ho)e+er- to achie+e !ressures abo+e this- 69>?= de+elo!ed re+olutionary ne) coating and machining technologies- and by the secondgeneration designchanged to an outer cam design in the su!!ly !um! to achie+e !ressures u! to 1-U00.

59;H>=#=VD 0 ;ommon <ail


1n order to )ithstand 1-U00(bar !ressure the rail must be )ithout any clearance or machining error. 1n res!onse- 69>?= de+elo!ed a s!ecial material that contains fe)er com!ounds- )hile exhibiting high !urity and high hardness !ro!erties to enable ultra high(!recision machining. 5hus- it is !ossible to

create engine com!onents that can )ithstand !ressures u! to 1-U00 bar for use in the common rail system.

59;H>=#=VD O ?olenoid 1nCector


;om!ared )ith its first(generation inCector (1-./0 bar- t) o(times inCection) 69>?= de+elo!ed higherres!onse solenoid actuator and high(!ressure inCector design. 5he second(generation inCector is able to inCect u! to 1-U00 bar !ressure and multi!le inCection- )ith timing inter+als of .F10-000 seconds during )hich a maximum of fi+e inCections ta8es !lace. 2s a result- exhaust gas is cleaner )hile noise is minimi7ed. 1n inCection control systems- ad+anced !ie7oelectric com!onents are em!loyed because of their high s!eed o!eration ca!ability. $ut 69>?= )as successful in de+elo!ing con+entional solenoids that o!erate in 0.0 millisecond units.

Purpose of injections ( Pi6 f..!re(conditioning of cylinder and >oise reduction ( 6 f...tor*ue ( Po6 f..conditioning of 9xhaust and <egeneration @o103 can !roduce tor*ue @o113 does not !roduce tor*ue #imitation of 1nCections "riteria for 9imitation. [ #oad $alance of the $ooster ;ondenser [ \uantity $alance of High @ressure @um! [ max allo)ed inCections de!ending on 1nCection ?ystem

1>;23 1>tegrated ;alibration and 2c*uisition system ?B 5ool used for ;alibration (2!!lication) - 6ata 2c*uisition- 6iagnostics etc. @ro,3 @rogram ,lash ?B @lug(in module )ith 1>;2 Gsed for !rogramming the 9;G (,lashing F ,lash(@rogramming) >eeds a com!atible @ro, ;onfiguration for )or8ing. 9?Te03 HB communication interface bet)een 1>;2 @; and 5est 9;G. 5est 9;G (2!!lication 9;G F 95W 9;G)3 9;G )ith 9mulator 5est @robe (95W) 95W emulates the data memory area of the 9;G. @roduction F ?eries 9;Gs ha+e no 95W. G69 (1n(;ircuit 6ebugger)3 1n(circuit debugging ?B 5ool Gses the ;@GIs on chi! debug system +ia the g52V interface. g52V S goint 5est 2ction Vrou! G26 (Gni+ersal 2ccess 6e+ice)3 HB communication interface bet)een G69 and the 5est 9;G @rogramming the ;=69 to the control unit ,#2?H using @ro, F G69 6o)nloading the 6252 from 1>;2 to the 95W

<+CO( <mbedded +eal,)ime Control Operating (!stem


6mission standards are re2uirements that set specific limits to the amount of pollutants that can be released into the en*ironment. (tandards generall! regulate the emissions of nitrogen oxides (;Ox),sulfur oxides, particulate matter ("#) or soot, carbon monoxide (CO), or *olatile h!drocarbons (see carbon dioxide e2ui*alent In the %nited (tates, emissions standards are managed b! the <n*ironmental "rotection Agenc! (<"A). Currentl! there are no standards for CO5 emissions.( in <urope) )he <% is to introduce <uro = effecti*e \anuar! -, 500/, <uro 6 effecti*e \anuar! -, 50-0 and <uro 7 effecti*e \anuar! -, 50-=.

Bhat are t)o ban8 systems in

ETAS Engineering Tools Application and Services Design and Software Engineering Tools (ASCET) Test and Validation Tools (Relevant LABCAR) Measurement, Cali ration, and Diagnosti! Tools ("#CA) Diagnosti! Servi!e Tools
-ame of ' programming language compiler.

2ns)er3 >ame Bor8 on =.? >ame of micro!rocessor 1. )urbo c #.( 'O( /0/7 0. Ansic c EI;%ZA%;IZ /0>/7 O. 9orland c ?I;'O? /0>/7 .. #icrosoft c #.( 'O( /0/7 /. Bisual cXX ?I;'O? /0>/7 >ote3( U0UT is 1T bit micro!rocessor )hile U0OUT is O0 bit micro!rocessor
*escribe /urbo c compiler.

2ns)er3 5urbo c com!iler is one of the most !o!ular c com!ilers. 1t is based on 6=? o!erating system. 1t uses U0UT micro!rocessor )hich is 1T bit micro!rocessor. 1t has 00 address buses and 1T data bus. 1ts )ord length is t)o byte.
*ifference between /S) and /S0 program .

41O means terminate but stay outside. 1t is those !rogram- )hich release the main memory after the execution of the !rogram. 9.g. Hcd cutter- turbo c com!iler. 41/ means terminate but stay residence .1t is those !rogram- )hich after the execution of the !rogram does not release the <24 (main memory).e.g. anti+irus.
Warning code in c programming

?.>. Barning message A016 :iolations 1 2ssigning :ty!e: to :enumeration: 0 $it fields must be signed or unsigned int O $oth return and return )ith a +alue used . 6eclare ty!e :ty!e: !rior to use in !rototy!e / 6i+ision by 7ero T Hexadecimal +alue contains more than O digits Y 1nitiali7ing :enumeration: )ith :ty!e: U :identifier: is declared as both external and static e 1ll(formed !ragma 10 1nitiali7ation is only !artially brac8eted 11 <edefinition of :macro: is not identical 10 ?us!icious !ointer con+ersion 1O Gndefined structure :structure: 1. Hoid functions may not return a +alue !re'uent ,rrors 1 ;ode has no effect 0 ,unction should return a +alue O @arameter :!arameter: is ne+er used . @ossible use of :identifier: before definition / @ossibly incorrect assignment

;ode eas bbf ret d!u 7di big bei ext ill !in du! sus stu +oi eff r+l !ar def !ia

Gnreachable code 9ess !re'uent ,rrors 1 2mbiguous o!erators need !arentheses 0 2rray +ariable :identifier: is near O ;all to function )ith no !rototy!e . ;all to function :function: )ith no !rototy!e / ;ondition is al)ays false T ;ondition is al)ays true Y :identifier: declared but ne+er used U :identifier: is assigned a +alue that is ne+er used e >o declaration for function :function: 10 ?tructure !assed by +alue 11 ?u!erfluous & )ith function Portability Warnings 1 ;onstant is long 0 ;onstant out of range in com!arison O ;on+ersion may lose significant digits . >on !ortable !ointer com!arison / >on !ortable !ointer con+ersion T 4ixing !ointers to signed and unsigned char Bhat is memory cellK 2ns)er3

rch amb ias !ro !ro )ccc )ccc use aus nod st+ am! cln rng sig c!t r!t uc!

9ntire <24 has di+ided in number of e*ual !art- )hich is 8no)n as memory cell.;a!acity of each cell is to store one(byte data. i.e char a reser+e one memory cell )hile float a reser+e four memory cell. 9ach memory cell has uni*ue address.2ddress are al)ays in )hole number an increasing order. (0) Bhat is residence memoryK

2ns)er3

<24 has di+ided into t)o !arts3 (1) 9xtended memory (useless) (0) <esidence memory3 Bhen any !rogram is executed it is stored in the residence memory .,or turbo c- it has 14$ residence memory i.e )hen )e o!en turbo c it store 14$ in the <24. (O) Bhat is !hysical addressK 2ns)er3

00 bit address of the memory cell is 8no)n as !hysical address or real address.1n 00 bit )e can re!resent address from 0x00000 to 0x,,,,,.

(.) Bhat is segmentationK 2ns3

<esidential memory of <24 of si7e 14$ has di+ided into 1T e*ual !art.5hese !art is called segment.9ach segment has si7e is T.W$. 14$=1T*T.W$ 5his !rocess of di+ision is 8no)n as segmentation. (/) Bhat is necessity of segmentationK 2ns3 @hysical address are 00 bit.$ut )e ha+e no !ointer of 00 bit.?o !ointer can not access )hole residential address .?o to sol+e this !roblem )e ha+e three different !ointer and segmentation has done (T) Bhat is offset addressK

2ns3

9ach segment has di+ided into t)o !arts.

1. ?egment no (.bit) 0. =ffset address (1T bit) 9ach segment has same offset address but different segment number.

?u!!ose !hysical address is 0x/00,1 5hen itIs segment number is / and offset address is 00,1

&

(Y) Brite a !rogram to find the offset address of any +ariableK 2ns3 Hoid main () & int x; scanf(_ad`-&x); !rintf(_a!`-x); ' >ote. a! is used to find the offset address (in hexadecimal) of any +ariable. (U) Bhat is data segmentK 2ns3 ?egment number U has s!ecial name )hich is 8no)n as data segment. 1t has di+ided into four !arts.

1. ?tac8 area3( 2ll automatic +ariables are created into stac8 area.6efault storage class of any local +ariable is auto.5his +ariable may be int- char- float- array- !ointer- struct- union etc.1t also return function argument and return address.1t follo) #1,= data structure. 1t has t)o !art one for initiali7e +ariable another for non(initiali7e +ariable.2ll initiali7e +ariable are more nearer than uninitiali7ed +ariable and +ice +ersa. 0. 6ata area 3 2ll static and extern +ariable are created in the data area. O. Hea! area3 4alloc and calloc al)ays allocate memory in the hea! area.1t is used for dynamic memory allocation.1tIs si7e de!ends u!on free s!ace in the memory. .. ;ode area3 ,unction !ointer can only access code area.?i7e of this area is al)ays fixed and it is read only area.

What is near pointer ?

1n 5G<$= ; there are three ty!es of !ointers. 5G<$= ; )or8s under 6=? o!erating system )hich is based on U0U/ micro!rocessor. 1. near !ointer 0. far !ointer O. huge !ointer 0ear pointer. 5he !ointer )hich can !oints only T.W$ data segment or segment number U is 8no)n as near !ointer.

(If you donTt 'now what is data segment the clic' here)

5hat is near !ointer cannot access beyond the data segment li8e gra!hics +ideo memory- text +ideo memory etc. ?i7e of near !ointer is t)o byte. Bith hel! 8ey)ord near- )e can ma8e any !ointer as near !ointer. 9xam!le3 void main()& int x=0/; int near* !tr; !tr=&x; !rintf(_ad`-si7eof !tr); '

=ut!ut3 0 >ear !ointer only hold 1T bit offset address. =ffset address +aries from 0000 to ,,,, (in hexadecimal). 0ote. 1n !rintf statement to !rint the offset address in hexadecimal- a! is used. "onsider following c program. void main()& int near * !tr=( int *)0c,,,,; !trXX; !trXX; !rintf(_a!`-!tr); ' =ut!ut3 000O 9x!lanation3 Bhen )e increment or decrement the offset address from maximum and minimum +alue res!ecti+ely then it re!eats the same +alue in cyclic order. 5his !ro!erty is 8no)n as cyclic nature of offset address. "yclic property of offset address. 1f you increment the near !ointer +ariable then mo+e cloc8)ise direction. 1f you decrement the near !ointer then mo+e anti cloc8)ise direction.

What is default type of pointer in "? 2ns)er3 1t de!ends u!on memory model.

What is 3angling pointer. 1f any !ointer is !ointing the memory address of any +ariable but after some +ariable has deleted from that memory location )hile !ointer is still !ointing such memory location. ?uch !ointer is 8no)n as dangling !ointer and this !roblem is 8no)n as dangling !ointer !roblem. 1nitially3

#ater3

,or exam!le3 #'&What will be output of following c program? int *call(); void main()& int *!tr; !tr=call(); clrscr(); !rintf("ad"-*!tr);

' int * call()& int x=0/; XXx; return &x; ' =ut!ut3 Varbage +alue (>ote3 =ut!ut +alue is 0T- discuss) 9x!lanation3 +ariable x is local +ariable. 1ts sco!e and lifetime is )ithin the function call hence after returning address of x +ariable x became dead and !ointer is still !ointing !tr is still !ointing to that location. 1olution of this problem. 4a8e the +ariable x is as static +ariable. 1n other )ord )e can say a !ointer )hose !ointing obCect has been deleted is called dangling !ointer& >ote3 2ns)er of all *uestion is based u!on that com!ilers )hose )ord si7e is t)o byte
What is far pointer ?

5he !ointer )hich can !oint or access )hole the residence memory of <24 i.e. )hich can access all 1T segments is 8no)n as far !ointer.

!ar pointer. ?i7e of far !ointer is . byte or O0 bit. 9xam!les3 #C& What will be output of following c program? void main()& int x=10; int far *!tr; !tr=&x; !rintf("ad"-si7eof !tr); ' =ut!ut3 . #;&What will be output of following c program? void main()& int far *near*!tr; !rintf("ad ad"-si7eof(!tr) -si7eof(*!tr)); ' =ut!ut3 . 0 9x!lanation3 !tr is far !ointer )hile *!tr is near !ointer. #<&What will be output of following c program? void main()& int far *!-far **; !rintf("ad ad"-si7eof(!) -si7eof(*)); ' =ut!ut3 . . !irst CF bit stores. ?egment number 0e-t CF bit stores. =ffset address Bhat is segment number and offset addressK

9xam!le3 void main()& int x=100; int far *!tr; !tr=&x; !rintf("a,!"-!tr); ' =ut!ut3 U,6U3,,,. Here U,6U is segment address and ,,,. is offset address in hexadecimal number format. 0ote. a,! is used for !rint offset and segment address of !ointer in !rintf function in hexadecimal number format. 1n the header file dos.h there are three macro functions to get the offset address and segment address from far !ointer and +ice +ersa. 1. !PGO!!#&. 5o get offset address from far address. 0. !PG1,D#&. 5o get segment address from far address. O. HG!P#&. 5o ma8e far address from segment and offset address.

9xam!les3 #C&What will be output of following c program? $include "dos.h" void main()& int i=0/; int far*!tr=&i; !rintf("ac ac"-,@P?9V(!tr)-,@P=,,(!tr)); ' =ut!ut3 2ny segment and offset address in hexadecimal number format res!ecti+ely. #;&What will be output of following c program? $include "dos.h" void main()&

int i=0/; int far*!tr=&i; unsigned int s-o; s=,@P?9V(!tr); o=,@P=,,(!tr); !rintf("a,!"-4WP,@(s-o)); ' =ut!ut3 U,6e3,,,. (2ssume) 0ote. Be cannot guess )hat )ill be offset address- segment address and far address of any far !ointer .5hese address are decided by o!erating system. 9imitation of far pointer. Be cannot change or modify the segment address of gi+en far address by a!!lying any arithmetic o!eration on it. 5hat is by using arithmetic o!erator )e cannot Cum! from one segment to other segment. 1f you )ill increment the far address beyond the maximum +alue of its offset address instead of incrementing segment address it )ill re!eat its offset address in cyclic order. 9xam!le3 #'&What will be output of following c program? void main()& int i; char far *!tr=(char *)0x$U00,,,2; for(i=0;iR=10;iXX)& !rintf("a,! bn"-!tr); !trXX; ' ' =ut!ut3 $U003,,,2 $U003,,,$ $U003,,,; $U003,,,6

$U003,,,9 $U003,,,, $U0030000 $U0030001 $U0030000 $U003000O $U003000. 5his !ro!erty of far !ointer is called cyclic nature of far !ointer )ithin same segment. 6mportant points about far pointer. 1. ,ar !ointer com!ares both offset address and segment address )ith relational o!erators. 9xam!les3 #C&What will be output of following c program? void main()& int far *!=(int *)0cY00O0000; int far **=(int *)0c$0010000; if(!==*) !rintf("$oth !ointers are e*ual"); else !rintf("$oth !ointers are not e*ual"); ' =ut!ut3 $oth !ointers are not e*ual #;&What will be output of following c program? void main()& int far *!=(int *)0cY00O0000; int far **=(int *)0c$0010000; int near *x-near*y; x=(int near *)!;

y=(int near *)*; if(x==y) !rintf("$oth !ointer are e*ual"); else !rintf("$oth !ointer are not e*ual"); ' =ut!ut3 $oth !ointers are e*ual 0. ,ar !ointer doesnIt normali7e. What is normali7ation of pointer? 5urbo ; com!iler is based on U0UT micro!rocessor in )hich !hysical address of memory is re!resented in 00 bit. ;on+ersion of . byte or O0 bit huge address into 00 bit actual !hysical address is 8no)n as normali7ation. 5uge pointer. 5he !ointer )hich can !oint or access )hole the residence memory of <24 i.e. )hich can access all the 1T segments is 8no)n as huge !ointer.

?i7e of huge !ointer is . byte or O0 bit. #C&What will be output of following c program? void main()& char huge * far *!; !rintf("ad ad ad"-si7eof(!)-si7eof(*!)-si7eof(**!)); ' =ut!ut3 . . 1 9x!lanation3 ! is huge !ointer- *! is far !ointer and **! is char ty!e data +ariable. 0ormali7ation of huge pointer. 5urbo ; com!iler is based on U0U/ micro!rocessor in )hich !hysical address of memory is re!resented in 00 bit. ;on+ersion of . byte or O0 bit huge address into 00 bit actual !hysical address is 8no)n as normali7ation. !ormula to calculate physical address.

9xam!le3 #'& What will be physical address of huge address @I?JJJ====? 2ns)er3 Huge address3 0c/eee.... =ffset address3 0x.... ?egment address3 0x/eee @hysical address= ?egment address * 0c10 X =ffset address =0c/eee * 0c10 X0c.... =0c/eee0 X 0c.... =0c/666. 1n binary3 0101 1101 1101 1101 0100 0ote. 9ach hexadecimal digit is re!resented in . bit binary number. Bhen any relation o!eration is !erformed bet)een t)o huge !ointers first it normali7es in actual !hysical address. 9xam!le3 #'&What will be output of following c program? void main()& int huge*!=(int huge*)0c;0/TOOO1; int huge**=(int huge*)0x;0//1O.1; clrscr(); if(!==*) !rintf("9*u*l"); else !rintf(">ot e*ual"); ' =ut!ut3 9*ual 9x!lanation3 2s )e 8no) huge !ointers com!are its !hysical address. Physical address of huge pointer p Huge address3 0c;0/TOOO1 =ffset address3 0xOOO1 ?egment address3 0c;0/T

@hysical address= ?egment address * 0c10 X =ffset address =0c;0/T * 0c10 X0cOOO1 =0c;0/T0 X 0cOOO1 =0c;OUe1 Physical address of huge pointer ' Huge address3 0c;0//1O.1 =ffset address3 0x1O.1 ?egment address3 0c;0// @hysical address= ?egment address * 0c10 X =ffset address =0c;0// * 0c10 X0c1O.1 =0c;0//0 X 0c1O.1 =0c;OUe1 ?ince both huge !ointers ! and * are !ointing same !hysical address so if condition )ill true.

#'&What will be output of following c program? void main()& double near *!-far **; !rintf("ad ad ad"-si7eof(*)-si7eof(!)-si7eof(*!)); ' =ut!ut3 . 0 U 9x!lanation3 * is far !ointer- ! is near !ointer- *! is double data ty!e constant. 3onKt use huge pointer. 1f you )ill increment huge !ointer it )ill increment both offset and segment address unli8e to far !ointer )hich only increments offset address. ?o if you ha+e little 8no)ledge about huge !ointer and you are using huge !ointer then you can easily access and modify the 1H5- de+ice dri+er memory+ideo memory etc. 5his might be dangerous for your com!uter. #'&Why there are three types of pointer in 4urbo c compiler? 2ns)er3 5urbo c com!iler is based on 6os o!erating system )hich is based on U0U/ micro!rocessors. 1n U0U/ micro!rocessor actual !hysical address is re!resented in 00 bit. 2ut there are not any pointers which can point ;@ bit address. "onsidering simplicity of calculations% access to actual physical address% security etc. c has introduced three ty!e of !ointer i.e. near- far and huge !ointer. Wild pointer. 2 !ointer in c )hich has not been initiali7ed is 8no)n as )ild !ointer. 9xam!le3 #'&What will be output of following c program? void main()& int *!tr; !rintf("aubn"-!tr); !rintf("ad"-*!tr);

' =ut!ut3 2ny address Varbage +alue Here !tr is )ild !ointer because it has not been initiali7ed. 5here is difference bet)een the >G## !ointer and )ild !ointer. >ull !ointer !oints the base address of segment )hile )ild !ointer doesnIt !oint any s!ecific memory location.

0899 pointer.
#iteral meaning of >G## !ointer is a !ointer )hich is !ointing to nothing. 0899 pointer points the base address of segment. 9xam!les of >G## !ointer3 1. int *!tr=(char *)0; 0. float *!tr=(float *)0; O. char *!tr=(char *)0; .. double *!tr=(double *)0; /. char *!tr=Ib0I; T. int *!tr=>G##; #'& What is meaning of 0899? 2ns)er3 >G## is macro constant )hich has been defined in the heard file stdio.h- alloc.h- mem.h- stddef.h and stdlib.h as $define >G## 0 9xam!les3 #C&What will be output of following c program? $include "stdio.h" void main()& if(L>G##) !rintf("1 8no) !re!rocessor"); else !rintf("1 don:t 8no) !re!rocessor"); '

=ut!ut3 1 8no) !re!rocessor 9x!lanation3 L>G## = L0 = 1 1n if condition any non 7ero number mean true. #;&What will be output of following c program? $include "stdio.h" void main()& int i; static int count; for(i=>G##;iR=/;)& countXX; iX=0; ' !rintf("ad"-count); ' =ut!ut3 O #<&What will be output of following c program? $include "stdio.h" void main()& $ifndef >G## $define >G## / $endif !rintf("ad"->G##Xsi7eof(>G##)); ' =ut!ut3 0 9x!lanation3 >G##Xsi7eof(>G##)

=0Xsi7eoof(0) =0X0 FFsi7e of int data ty!e is t)o byte. We cannot copy any thing in the 0899 pointer. 9xam!le3 #'&What will be output of following c program? $include "string.h" void main()& char *str=>G##; strc!y(str-"c(!ointer.blogs!ot.com"); !rintf("as"-str); ' =ut!ut3 (null)

Deneric pointer.
+oid !ointer in c is 8no)n as generic !ointer. #iteral meaning of generic !ointer is a !ointer )hich can !oint ty!e of data. 9xam!le3 void *!tr; Here !tr is generic !ointer. 6mportant points about generic pointer in c? 1. Be cannot dereference generic !ointer. $include "malloc.h" void main()& void *!tr; !rintf("ad"-*!tr); ' =ut!ut3 ;om!iler error 0. Be can find the si7e of generic !ointer using si7eof o!erator. $include "string.h" void main()& void *!tr; !rintf("ad"-si7eof(!tr)); ' =ut!ut3 0

9x!lanation3 ?i7e of any ty!e of near !ointer in c is t)o byte. O. Veneric !ointer can hold any ty!e of !ointers li8e char !ointer- struct !ointer- array of !ointer etc )ithout any ty!ecasting. 9xam!le3 void main()& char c=:2:; int i=.; void *!; char **=&c; int *r=&i; !=*; !rintf("ac"-*(char *)!); !=r; !rintf("ad"-*(int *)!); ' =ut!ut3 2. .. 2ny ty!e of !ointer can hold generic !ointer )ithout any ty!ecasting. /. Veneric !ointers are used )hen )e )ant to return such !ointer )hich is a!!licable to all ty!es of !ointers. ,or exam!le return ty!e of malloc function is generic !ointer because it can dynamically allocate the memory s!ace to stores integer- float- structure etc. hence )e ty!e cast its return ty!e to a!!ro!riate !ointer ty!e. 9xam!les3 1. char *c; c=(char *)malloc(si7eof(char)); 0. double *d; d=(double *)malloc(si7eof(double)); O. ?truct student& char *name; int roll; '; ?truct student *stu; ?tu=(struct student *)malloc(si7eof(struct student))

What is a memory 9eak?


2 memory lea8 is the gradual loss of a+ailable com!uter memory)hen a !rogram (an application or !art of the operating system) re!eatedly fails to return memory that it has obtained for tem!orary use. 2s a result- the a+ailable memory for that a!!lication or that !art of the o!erating system becomes exhausted and the !rogram can no longer function. ,or a !rogram that is fre*uently o!ened or called or that runs continuously- e+en a +ery small memory lea8 can e+entually cause the !rogram or the system to terminate. 2 memory lea8 is the result of a !rogram bug. ?ome o!erating systems !ro+ide memory lea8 detection so that a !roblem can be detected before an a!!lication or the o!erating system crashes. ?ome !rogram de+elo!ment tools also !ro+ide automatic

"house8ee!ing" for the de+elo!er. 1t is al)ays the best !rogramming !ractice to return memory and any tem!orary file to the o!erating system after the !rogram no longer needs it.

Bhat is command line argumentK


Vetting the arguments from command !rom!t in c is 8no)n as command line arguments. 1n c main function has three arguments. 5hey are3 (a)2rgument counter (b)2rgument +ector (c)9n+ironment +ector F" exa5(le4 -oid <ain(int a gc9char %a g!./9char %en!./) 3 int i# for(i=1#i:a gc#i)))3 ( intf(=>0\n=9a g!.i/)# 8 8

:olatile
5he *ualifier :+olatile: is normally a+oided- understood only marginally- and *uite often forgotten. 1t indicates to the com!iler- that a +ariable may be modified outside the sco!e of the !rogram. ?uch situations may occur for exam!le in multitas8ingF(threading systems- )hen )riting dri+ers )ith interru!t ser+ice routines- or in embedded systems- )here the !eri!heral registers may also be modified by hard)are alone.

When do you need to use ;volatile;?


5he basic !rinci!le is sim!le3 9+ery time )hen a +ariable is used in more than one context- *ualify it )ith :+olatile:3 h Bhene+er you use a common +ariable in more than one tas8 or thread; h Bhene+er you use a +ariable both in a tas8 and one or more interru!t ser+ice routines; h Bhene+er a +ariable corres!onds to !rocessor(internal registers configured as in!ut (consider the !rocessor or external hard)are to be an extra context).

Does it hurt to use ;volatile; unnecessarily?


Bell- yes and no. 5he functionality of your code )ill still be correct. Ho)e+er- the timing and memory foot!rint of your a!!lication )ill change3 Dour !rogram )ill run slo)er- because of the extra read o!erations- and your !rogram )ill be larger- since the com!iler is not allo)ed to o!timi7e as thoroughly- although that )ould ha+e been !ossible
3efault data type in c

(1) lon= a=2<# It i0 eP'i!alent t"4 lon= int a=2<# (2) *i=ned a=2<# It i0 eP'i!alent t"4 *i=ned int a=2<# (?) re=i*ter a=2<# It i0 eP'i!alent t"4 un*i=ned re=i*ter int a=2<#

Modifiers
short long signed unsigned

Rules for usin$ modifier in c


Rule '> We cann"t '0e t1" 5"difie 0 "f 0a5e g "'(0 in an; (a tic'la data t;(e "f c& F" exa5(le9 f"ll"1ing decla ati"n "f c a e illegal4 *hort lon= int i# *tatic auto char c# *i=ned un*i=ned int a a;.</# .a*cal cdecl di*.la?()# F"ll"1ing a e !alid decla ati"n "f c4 con*t -olatile float f# *i=ned *tatic lon= -olatile int i# @ue*tion> I0 f"ll"1ing decla ati"n i0 !alid in cD 1& 2& ?& 4& intnear % far % hu=e %(# char con*t % con*t %c# *hort *hort int i# con*t con*t int i#

Rule 2> We can 1 ite 5"difie eit2e -ef" e t2e data t;(e " afte t2e data t;(e& F" exa5(le9 -"t2 "f f"ll"1ing decla ati"n i0 c" ect4 un*i=ned char c# char un*i=ned c# Rule !> Q de "f 5"difie incl'ding data t;(e d"e0nXt affect t2e 5eaning "f decla ati"n& F" exa5(le all "f t2e f"ll"1ing 2a!e 0a5e 5eaning4 int con*t *hort e5tern int e5tern con*t *hort int *hort e5tern con*t con*t int *hort e5tern e5tern *hort con*t int i# i# i# i# i#

Rule > *2e e i0 "ne exce(ti"n in 'le ?& BQIA*ER9 FUAE*IQA and IA*ERRUB* 5"difie 5'0t -e 1 itten afte t2e data t;(e& F" exa5(le9 in t2e f"ll"1ing decla ati"n4 un*i=ned con*t char far char un*i=ned con*t %c# char fa un*i=ned con*t con*t char fa un*i=ned far char con*t un*i=ned con*t un*i=ned fa char %c# %c# %c# %c# %c#

Fi 0t f"' decla ati"n0 a e !alid a0 1ell a0 eP'i!alent& B't la0t t1" decla ati"n0 a e in!alid&

<ossible modifiers with $iven data type in c

We cann"t '0e all 5"difie 0 1it2 eac2 data t;(e0 in c& F"ll"1ing ta-le e( e0ent0 (e 5itted " 5eaningf'l 5"difie 0 12ic2 can -e '0ed 1it2 a (a tic'la data t;(e&

1i7e of data type in c


?1d9 4=61,19<?4 Qnl; t1" 5"difie 0 02" t and l"ng can affect t2e 0iGe "f an; data t;(e in c& 0iGe "f ( i5iti!e data t;(e in c de(end0 '("n t2e 1" d lengt2 "f t2e 5ic "( "ce00" & In gene al 1e can 0a; 0iGe "f int i0 1" d lengt2 "f 5ic "( "ce00" & SiGe "f data t;(e0 in t2e 1N -it c"5(ile 09 li6e *URBQ c))9 B" land c)) etc&

)ange of each data types in c F"ll"1ing ta-le ill'0t ate t2e B" land c)) c"5(ile 0&

ange "f data t;(e in *URBQ E)) and

What is difference between %define and const=


c"n0t identifiers are often better than #define because3 they obey the languageLs scoping rules you can see them in the debugger you can take their address if you need to you can pass them by con*t-reference if you need to they don:t create ne) "8ey)ords" in your !rogram.

1n short- c"n0t identifiers act li8e they:re !art of the language because they are !art of the language. 5he !re!rocessor can be thought of as a language layered on to! of ;XX. Dou can imagine that the

!re!rocessor runs as a se!arate !ass through your code- )hich )ould mean your original source code )ould be seen only by the !re!rocessor- not by the ;XX com!iler itself. 1n other )ords- you can imagine the !re!rocessor sees your original source code and re!laces all #define symbols )ith their +alues- then the ;XX com!iler !ro!er sees the modified source code after the original symbols got re!laced by the !re!rocessor. 5here are cases )here #define is needed- but you should generally a+oid it )hen you ha+e the choice. Dou should e+aluate )hether to use c"n0t vs. #define based on business +alue3 timemoney- ris8. 1n other )ords- one si7e does not fit all. 4ost of the time you:ll use c"n0t rather than #define for constants- but sometimes you:ll use #define. $ut !lease remember to )ash your hands after)ards. AA 2 Adefine is either an immediate +alue or a macro. 2 constant is a +ariable that doesn:t change in +alue. Dou can delcare a !ointer to a const- but not to a Adefine- although a define could be a !ointer (for exam!le "Adefine @110O. ((int *)0x10O.)". 1n ;- Adefines are local only- so there:s no )ay to ma8e a Adefine externally a+ailable to the lin8er. 1nstead- the Adefine must be included in the source code for all modules that need to access the define. 2 const +ariable can be global and accessed +ia other lin8ed modules- but re*uires a memory access and occu!ies s!ace. 1n assembly- global e*uates are !ossible- and lin8ers ty!ically generate global e*uates to indicate the bounds of 8ey !oints )ithin the code- li8e the start and end of data and code. Vlobal e*uates don:t re*uire a memory access and don:t occu!y any s!ace. Bithin a module- a ; com!iler could o!timi7e a const as if it )ere a Adefine- if there are no !ointers declared to the constant. 1n ;@G terms- the const )ould become an "immediate" +alue. =ther alternati+es is that a const +ariable could be !laced in the code area as o!!osed to the data area since it doesn:t change. =n some machines- declaring a !onter to a constant could cause an exce!tion if you tried to modify the constant +ia the !ointer (if the constant )ere !laced in a read(only code section).

Memory representation of float data type in c


(B"t2 in *' -" c c"5(ile and ,in'x gcc c"5(ile ) Fl"at n'5-e 0 a e 0t" ed in ex("nential f" 5 i&e& (Aanti**a#*'0B (E5.onent# Te e % indicate0 5'lti(licati"n and O indicate0 ("1e & In 5e5" ; "nl; Canti00a and Ex("nent i0 0t" ed n"t %9 10 and O& *"tal 0iGe "f fl"at data t;(e4 ?2 -it *2"0e -it0 a e '0ed in f"ll"1ing 5anne 4 Ex("nent -it4 J Canti00a -it4 24 Canti00a i0 0igned n'5-e 9 0" 24 -it a e '0ed a04 Canti00a_0ign -it4 1 Canti0aa_data -it4 2? F" "nl; 5anti00a4 Canti00a_0ign -it 1ill Ge " if n'5-e i0 ("0iti!e and Canti00a_0ign -it 1ill "ne if n'5-e i0 negati!e& Ex("nent i0 al0" 0igned n'5-e 9 S" J -it a e '0ed a04 Ex("nent_0ign -it4 1 Ex("nent_data -it4 I F"ll"1ing fig' e ill'0t ate 2"1 fl"ating ("int n'5-e 5e5" ;& i0 0t" ed in

Ci-e i<.ortant rule*> Rule '> *" find t2e 5anti00a and ex("nent9 1e c"n!e t data int" 0cientific f" 5& Rule 2> Bef" e t2e 0t" ing "f ex("nent9 12I i0 added t" ex("nent& Rule !> Ex("nent i0 0t" ed in 5e5" ; in fi 0t -;te f "5 ig2t t" left 0ide& Rule > If ex("nent 1ill negati!e n'5-e it 1ill -e 0t" ed in 2X0 c"5(le5ent f" 5& Rule "> Canti00a i0 0t" ed in t2e 5e5" ; in 0ec"nd -;te "n1a d f "5 ig2t t" left 0ide& /emor representation of float a$&+&f Ae<or? re.re*entation of> float a = H10&?f# F" t2i0 ;"' 2a!e t" f"ll"1 f"ll"1ing 0te(04

*te.'> c"n!e t t2e n'5-e (10&?) int" -ina ; f" 5 Bina ; !al'e "f 10&? i04 1010&0100110011001100110011001100110011Y *te.2> c"n!e t t2e a-"!e -ina ; n'5-e in t2e 0cientific f" 5& Scientific f" 5 "f 1010&0100110011001100110011001100110011Y= 1&0'00'00''00''00''00''00 11001100110011Y%10O? A"te4 Fi 0t digit i&e& 19 deci5al ("int 0;5-"l9 -a0e "f ("1e i&e& 109 ("1e 0;5-"l O and 5'lti(licati"n 0;5-"l % a e n"t 0t" ed in t2e 5e5" ;& Ste.!> find ex("nent and 5anti00a and 0igned -it Canti00a_data -it in -ina ; = 0100100 11001100 1100110' (Qnl; fi 0t 2? -it f "5 left 0ide) Canti00a_0ign -it4 1 (Since it i0 a negati!e n'5-e ) Ex("nent in deci5al4 ? Z'e0ti"n4 W2; 1e 2a!e ta6en ig2t 5"0t -it "f 5anti00a_data -it "ne in0tead "f Ge "D Ste. "> +dd 12I in t2e ex("nent and c"n!e t in t2e -ina ; n'5-e f" 5&

(W2; 12ID 0ince 0iGe "f ex("nent_data -it i0 I and 5axi5'5 ("00i-le n'5-e in 0e!en -it 1ill 1111111 in -ina ; " 12I in deci5al) Ex("nent= 12I)?=1?0 Bina ; !al'e "f 1?0 in eig2t -it4 1000001 0 Ex("nent_data -it4 1000001 (*a6e fi 0t 0e!en -it f "5 left 0ide) Ex("nent_0ign -it4 0 (*a6e ig2t5"0t -it) Ste. 6> A"1 0t" e t2e Canti00a_data -it9 Canti00a_0ign -it9 Ex("nent_data -it and Ex("nent_0ign -it at a(( "( iate l"cati"n a0 02"1n in t2e f"ll"1ing fig' e&

Note> Canti00a_data -it0 a e 0t" ed f "5 left t" ig2t 12ile Ex("nent_data -it0 a e 0t" ed f "5 ig2t t" left& Do& to checE a0o-e <e<or? re.re*entation i* correct; +n01e 4 We 1ill ta6e "ne c2a ("inte and !i0it eac2 -;te "f a fl"at n'5-e and "-0e !e t2e "'t('t& E ( "g a54 #include:0tdi"&2> int <ain()3 int i# float f=H10&?f# char %(=(char %)Ff# for(i=0#i:4#i))) ( intf(=>d =9%()))# return 0# 8 Q't('t4 H<1 H<2 ?N HN? Ex(lanati"n4 Bina ; !al'e "f H<1 in eig2t -it4 11001101 Bina ; !al'e "f H<2 in eig2t -it4 11001100 Bina ; !al'e "f ?N in eig2t -it4 00100100 Bina ; !al'e "f HN? in eig2t -it4 11000001 *2i0 i0 exactl; 0a5e a0 12ic2 1e 2a!e e( e0ented in 5e5" ; in t2e a-"!e fig' e&

Address of a variable in c
,"cati"n in a 5e5" ; 12e e a !a ia-le 0t" e0 it0 data " !al'e i0

6n"1n a0 ( "!ided "(e at" n"t 1it2

add e00 "f !a ia-le& *" 6n"1 add e00 "f an; !a ia-le c 2a0 a 0(ecial 'na ; "(e at" F 12ic2 i0 6n"1n a0 de efe encing " add e00 "(e at" & It "(e at" i0 "nl; '0ed 1it2 !a ia-le0 t2e c"n0tant& F" exa5(le4

#incl'de:0tdi"&2> #incl'de:c"ni"&2> !"id 5ain()3 int a=<# ( intf([+dd e00 "f !a ia-le a i04 >d\9Fa)# getc2()# 8 We cann"t 1 ite4 FFa 9-eca'0e4 FFa=F (Fa) =F (add e00 "f !a ia-le a) =F (a c"n0tant n'5-e ) +nd 1e cann"t '0e add e00 "(e at" 1it2 c"n0tant&

3<.ortant .oint* a0out addre** of -aria0le* in c>


(1)+dd e00 "f an; !a ia-le in c i0 an 'n0igned intege & It cann"t -e a negati!e n'5-e & S" in ( intf 0tate5ent 1e 02"'ld '0e >' in0tead "f >d9 t" ( int t2e add e00 "f an; !a ia-le& >d4 It i0 '0ed t" ( int 0igned deci5al n'5-e & >'4 It i0 '0ed t" ( int 'n0igned deci5al n'5-e & Since9 if t2e add e00 "f an; !a ia-le 1ill -e;"nd t2e ange "f 0igned 02" t int it 1ill ( int a c;clic !al'e& (2)+dd e00 "f an; !a ia-le 5'0t -e 1it2in t2e ange 0000 t" FFFF in 2exadeci5al n'5-e f" 5at " 0 t" N<<?< i&e& ange "f 'n0igned 02" t int in c& *" ( int t2e add e00 "f an; !a ia-le in 2exadeci5al n'5-e f" 5at -; ( intf f'ncti"n 1e 02"'ld '0e >x " >]& >x4 *" ( int a n'5-e in 2exadeci5al f" 5at '0ing 0 t" @ and a9 -9 c9 d9 e9 f& >]4 *" ( int a n'5-e in 2exadeci5al f" 5at '0ing 0 t" @ and +9 B9 E9 M9 E9 F& (?)+ ( "g a55e can n"t 6n"1 at 12at add e00 a !a ia-le 1ill 0t" e t2e data& It i0 decided -; c"5(ile " "(e ating 0;0te5& (4)+n; t1" !a ia-le0 in c cann"t 2a!e 0a5e (2;0ical add e00& (<)+dd e00 "f an; !a ia-le e0e !e t1" -;te0 "f 5e5" ; 0(ace0& (N) +dd e00 "f an; !a ia-le in c i0 n"t intege t;(e 0" t" a00ign an add e00 t" a integ al !a ia-le 1e 2a!e t" t;(e ca0t t2e add e00& F" exa5(le4 #incl'de:0tdi"&2> #incl'de:c"ni"&2> !"id 5ain()3 int a=100# 'n0igned int -=('n0igned)F-# ( intf([>'\9-)# getc2()# 8 +dd e00 a it25etic in c4 (1) Be can subtract address of any t)o +ariables. ,or exam!le3 #incl'de:0tdi"&2> #incl'de:c"ni"&2> !"id 5ain()3 int a# int -# ( intf([>d\9F-HFa)# getc2()# 8 (0)Be cannot add- multi!ly- di+ide t)o addresses. (O) )e can add or subtract a integer number )ith address. (O)=ther o!erators )hich can be used )ith addresses are3

(a)>egation o!erator3L (b)?i7e o!erator3 si7eof (c)5y!e casting o!erator3 (5y!e) (e) 6ereferencing o!erator3 * (f)#ogical o!erator3 &&- MM 9xam!le3 #incl'de:0tdi"&2> #incl'de:c"ni"&2> !"id 5ain()3 int a=12# ( intf([>d\9^Fa)# ( intf([>d\90iGe"f(Fa))# ( intf([>d\9(int)Fa)# ( intf([>d\9%Fa)# ( intf([>d >d >d >d\9FaFFF-9FaFF-9FaVVF-9FaVV-)# getc2()# 8 *eclaration of a variable in c Mecla ati"n "f !a ia-le0 5ean0 t" ac6n"1ledge t2e c"5(ile "nl; a-"'t !a ia-le na5e and it0 data t;(e 1it2 it0 5"difie 0 -'t c"5(ile d"e0nXt e0e !e an; 5e5" ; f" t2e !a ia-le0& In c 1e can decla ed an; !a ia-le 1it2 2el( "f exte n 6e;1" d 12ile it 2a0 n"t initialiGed& Exa5(le "f decla ati"n4 (1) exte n int a# (2)exte n 0t 'ct 0t'dent3 c2a % na5e# int "ll# d"'-le 5a 60# 8# I5(" tant ("int0 a-"'t decla ati"n !a ia-le0 in c4 1& Since decla ati"n !a ia-le d"e0nXt get an; 5e5" ; 0(ace 0" 1e cann"t a00ign an; !al'e t" !a ia-le& F" exa5(le4 #incl'de:0tdi"&2> #incl'de:c"ni"&2> exte n int a# !"id 5ain()3 a=100# ( intf([>d\9a)# getc2()# 8 Q't('t4 E"5(ilati"n e " (2) We cann"t '0e an; "(e at" 1it2 decla ed "nl; !a ia-le0& (?)We can decla e an; !a ia-le eit2e gl"-all; " l"call;& (4)+ 0a5e !a ia-le can -e decla ed 5an; ti5e0& Mefiniti"n "f !a ia-le0 in c4 + c 0tate5ent in 12ic2 a !a ia-le get0 a 5e5" ; i0 6n"1n a0 definiti"n "f !a ia-le& +ll a't"9 egi0te 9 0tatic and initialiGed exte n !a ia-le a e exa5(le "f definiti"n "f !a ia-le0& F" exa5(le4 (a) int a# $$Mefiniti"n "f !a ia-le a 0tatic int a# $$Mefiniti"n "f !a ia-le a egi0te int a# $$Mefiniti"n "f !a ia-le a exte n int a=<# $$Mefiniti"n "f !a ia-le a A"te4 In t2e a-"!e c 0tate5ent all !a ia-le0 a e decla ing and defining at t2e 0a5e ti5e& (-) #incl'de:0tdi"&2> #incl'de:c"ni"&2> exte n int a# $$Mecla ati"n "f !a ia-le a

exte n int a# $$+gain decla ati"n "f !a ia-le a int a=<# $$Mefiniti"n "f !a ia-le a !"id 5ain()3 (tintf([>d\9a)# getc2()# 8 Q't('t4 < I5(" tant ("int0 a-"'t definiti"n "f !a ia-le4 (1)If an; !a ia-le 2a0 n"t decla ed t2en it decla ati"n "cc' 0 at t2e ti5e "f definiti"n& (2)We can '0e an; "(e at" afte t2e definiti"n "f !a ia-le& (?)Mefiniti"n "f !a ia-le0 can -e gl"-all; " l"call;& (4)+ egi0te !a ia-le get0 EBU in0tead "f 5e5" ; in definiti"n& (<)+ 0tatic " exte n !a ia-le get0 5e5" ; at t2e c"5(ile ti5e 12ile a't" and egi0te !a ia-le0 get 5e5" ; " EBU at t2e 'n ti5e

>?A>#2 A*D R?A>#2


,Ral'e4 ,!al'e 0tand0 f" left !al'e& In an; a00ign5ent 0tate5ent ,Ral'e 5'0t -e a c"ntaine i&e& 12ic2 2a!e a-ilit; t" 2"ld t2e data& In c 2a0 "nl; "ne t;(e "nl; c"ntaine and 12ic2 i0 !a ia-le& Tence ,Ral'e 5'0t -e an; !a ia-le in c it cann"t -e a c"n0tant9 f'ncti"n " an; "t2e data t;(e "f c& F" exa5(le #define :0tdi"&2> #define :c"ni"&2> #define 5ax 12< 0t 'ct a-c3 c2a %na5e# int "ll# 8# en'5 3REM9B,UE8# !"id 5ain()3 int a# c"n0t -# a=<# $$10=<# ,Ral'e cann"t -e a intege c"n0tant $$5ax=20# $$,!al'e cann"t -e a 5ic " c"n0tant $$-=11# ,!al'e cann"t -e a c"n0tant !a ia-le $$fl"at=?&?f# ,!al'e cann"t -e a data t;(e $$a-c=3[0ac2in\9<8# ,!al'e cann"t -e a data t;(e $$B,UE =2# ,!al'e cann"t -e a en'5 c"n0tant getc2()# 8 RRal'e4 In an; a00ign5ent 0tate5ent RRal'e 5'0t -e an; t2ing 12ic2 can et' n and c"n0tant !al'e " it i0 it0elf a c"n0tant& RRal'e can -e an; c c"n0tant09 !a ia-le09 f'ncti"n 12ic2 i0 et' ning a !al'e etc& F" exa5(le4 #incl'de:0tdi"&2> #incl'de:c"ni"&2> #define 5ax < !"id di0(la;()# fl"at 0'5()# en'5 3B,+EU9 WTI*E8# !"id 5ain()3 int x=2# $$RRal'e can -e a c"n0tant& fl"at ;=@&0# c"n0t int G=x# $$RRal'e can -e a c"n0tant !a ia-le x=5ax# $$RRal'e can -e a !a ia-le& x=B,+EU# $$RRal'e can -e a en'5 c"n0tant& ;=0'5()#$$RRal'e can -e a f'ncti"n&

;=di0(la;()# $$RRal'e can -e a f'ncti"n 12ic2 i0 $$ et' ning n"t2ing& 8

What is )dentifier?
1n c any name is called identifier. 5his name can be +ariable name- function name- enum constant name- micro constant name- goto label name- any other data ty!e name li8e structure- union- enum names or ty!edef name.

What is Storage class in c


In 1& 2& ?& 4& c t2e e a e f"' a't" egi0te 0tatic exte n t;(e0 "f 0t" age cla00& *2e; a e4

St" age cla00 i0 5"difie decide04

"

P'alifie

"f data t;(e0 12ic2

1& In 12ic2 a ea "f 5e5" ; a (a tic'la 2& W2at i0 0c"(e "f !a ia-leD ?& W2at i0 !i0i-ilit; "f !a ia-leD

!a ia-le 1ill -e 0t" edD

What is Scope of a variable in c

Ceaning "f 0c"(e i0 t" c2ec6 eit2e !a ia-le i0 ali!e " dead& +li!e 5ean0 data "f a !a ia-le 2a0 n"t de0t ";ed f "5 5e5" ;& U( t" 12ic2 (a t " a ea "f t2e ( "g a5 a !a ia-le i0 ali!e9 t2at a ea " (a t i0 6n"1n a0 0c"(e "f a !a ia-le& In t2e a-"!e fig' e 0c"(e "f !a ia-le a e( e0ented "'te ed -"x i&e& 12"le ( "g a5& A"te4 If an; !a ia-le i0 n"t !i0i-le it 5a; 2a!e 0c"(e i&e& it i0 ali!e " 5a; n"t 2a!e 0c"(e& B't if an; !a ia-le 2a0 n"t 0c"(e i&e& it i0 dead t2en !a ia-le 5'0t n"t t" -e !i0i-le& *2e e a e f"' t;(e "f 0c"(e in c4 1& Bl"c6 0c"(e& 2& F'ncti"n 0c"(e& ?& File 0c"(e& ?& B "g a5 0c"(e&
What is block scope in c ?

In c -l"c6 i0 e( e0ented a ea -et1een "(ening c' l; - ac6et i&e& 3and cl"0ing c' l; - ac6et i&e&8& Exa5(le "f -l"c60 in c&

In t2e c c"de t2e e a e t2 ee -l"c60 02"1n in ;ell"1 c"l" & In t2e a-"!e c"de "ne 5" e -l"c6 12ic2 i0 5ain f'ncti"n -l"c6& If a !a ia-le " f'ncti"n 2a0 0c"(e "nl; 1it2in t2e 0c"(e 12e e it 2a0 decla ed t2en 0c"(e "f !a ia-le " f'ncti"n i0 called a0 -l"c6 0c"(e and if a !a ia-le " f'ncti"n i0 !i0i-le i0 "nl; 1it2in a -l"c6 12e e it 2a0 decla ed t2en t2at !a ia-le " f'ncti"n i0 called a0 -l"c6 !i0i-le&

What is 1unction scope in c ?


+ -l"c6 "f f'ncti"n -"d; 2a0 0(ecial na5e9 f'ncti"n -l"c6& F "5 0t" age cla00 ("int "f !ie1 t2e e i0 n"t an; diffe ence -et1een f'ncti"n -l"c6 and an; "t2e -l"c60 in c& Beca'0e t2e e i0 n"t an; 5"difie 0 "f 0t" age cla00 g "'( 12ic2 2a0 f'ncti"n -l"c6& I5(" tance "f f'ncti"n -l"c6 can 'nde 0tand 12en 1e deal 1it2 g"t" 0tate5ent& ,a-el "f g"t" 0tate5ent 2a0 f'ncti"n -l"c6 0c"(e& ,a-el "f (a tic'la g"t" 0tate5ent i0 n"t !i0i-le in an"t2e f'ncti"n& F" exa5(le4 #include:0tdi"&2> -oid di*.la?()# int <ain()3 ( intf(=In C+IA=)# =oto x;G# return 0# 8 -oid di*.la?()3 x;x4# ( intf(=In MISB,a;=)# 8 Q't('t4 E"5(ilati"n e "

1ile scope of variable in c?


If an; !a ia-le " f'ncti"n 2a0 0c"(e "nl; 1it2in a file 12e e it 2a0 decla ed t2en 0c"(e "f !a ia-le " f'ncti"n i0 6n"1n file 0c"(e& If an; !a ia-le " f'ncti"n i0 !i0i-le "nl; 1it2in a file 12e e it 2a0 decla ed t2en !i0i-ilit; "f t2at !a ia-le " f'ncti"n i0 called file !i0i-le&

What is program scope in c?


If an; !a ia-le " f'ncti"n 2a0 0c"(e 12"le "f t2e ( "g a59 ( "g a5 5a; c"ntain "ne " 5" e file0 t2en 0c"(e "f !a ia-le " f'ncti"n i0 6n"1n ( "g a5 0c"(e& If an; !a ia-le " f'ncti"n 12ic2 i0 !i0i-le in t2e 12"le ( "g a59 ( "g a5 5a; c"ntain "ne " 5" e file t2en !a ia-le " f'ncti"n9 t2at !a ia-le0 " f'ncti"n0 a e called a0 ( "g a5 !i0i-le&

What is auto storage class in c


+'t"5atic !a ia-le0 " a't" !a ia-le0 a e defa'lt 0t" age cla00 "f l"cal !a ia-le& +n a't" !a ia-le cann"t -e decla ed gl"-all;& (W2;D) Pro.ertie* of auto *tora=e cla**F (1) Mefa'lt initial !al'e "f a't" !a ia-le i0 ga -age& F" #include:0tdi"&2> int <ain()3 int i# auto char c# float f# ( intf(=>d >c >f=9i9c9f)# return 0# 8 Q't('t4 Sa -age Sa -age Sa -age (2)Ri0i-ilit; "f a't" !a ia-le i0 1it2in t2e -l"c6 12e e it 2a0 decla ed& F" exa5(le04 exa5(le4

(a) #include:0tdi"&2> int <ain()3 int a=10# 3 int a=20# ( intf(=>d=9a)# 8 ( intf(= >d=9a)# return 0# 8 Q't('t4 20 10 Ex(lanati"n4 Ri0i-ilit; "f !a ia-le a 12ic2 2a0 decla ed in0ide inne 2a0 -l"c6 "nl; 1it2in t2at -l"c6& (-) #include:0tdi"&2> int <ain()3 3 int a=20# ( intf(=>d=9a)# 8 ( intf(= >d=9a)# $$a i0 n"t !i0i-le 2e e return 0# 8 Q't('t4 E"5(ilati"n e " Ex(lanati"n4 Ri0i-ilit; "f !a ia-le a 12ic2 2a0 decla ed in0ide inne -l"c6 2a0 "nl; 1it2in t2at -l"c6& @ue*tion> W2at 1ill -e "'t('t "f f"ll"1ing c c"deD #include:0tdi"&2> int <ain()3 int a=0# 3 int a=10# ( intf(=>d=9a)# a))# 3 a=20# 8 3 ( intf(= >d=9a)# int a=?0# 3a))#8 ( intf(= >d=9a)))# 8 ( intf(= >d=9a)))# 8 ( intf(= >d=9a)# return 0# 8 (?) Sc"(e "f a't" !a ia-le i0 1it2in t2e -l"c6 12e e it 2a0 decla ed& F" exa5(le4 (a) #include:0tdi"&2> int <ain()3 int i# for(i=0#i:4#i)))3

8 return 0#

int a=20# ( intf(=>d=9a)# a))#

Q't('t4 20 20 20 20 Ex(lanati"n4 Ra ia-le decla ed in0ide t2e f" l""( -l"c6 2a0 0c"(e "nl; 1it2in t2at -l"c6& +fte t2e fi 0t ite ati"n !a ia-le a -ec"5e0 dead and it l""0e0 it0 inc e5ented !al'e& In 0ec"nd ite ati"n !a ia-le a i0 again decla ed and initialiGed and 0" "n& (-) #include:0tdi"&2> int <ain()3 int i=0# 3 auto int a=20# ]_Z4# ( intf(=>d=9a)# a))# i))# 8 if (i:?) =oto x;G# return 0# 8 Q't('t4 E"5(ilati"n e " & Ex(lanati"n4 Ra ia-le a 12ic2 decla ed in0ide inne -l"c6 2a0 0c"(e "nl; 1it2in t2at -l"c6& Qne0 ( "g a5 c"nt "l c"5e0 "'t "f t2at -l"c6 !a ia-le 1ill -e dead& If 1it2 t2e 2el( "f g"t" 0tate5ent 1e 1ill g" t" in0ide t2at inne -l"c6 in t2e ( intf 0tate5ent c"5(lie 1ill n"t 6n"1n a-"'t !a ia-le a -eca'0e it 2a0 -een de0t ";ed al ead;& Tence c"5(lie 1ill 02"1 an e " 5e00age4 'ndefined 0;5-"l a& B't if ;"' 1ill 1 ite g"t" 0tate5ent la-el -ef" e t2e decla ati"n "f !a ia-le t2en t2e e i0 n"t an; ( "-le5 -eca'0e !a ia-le a 1ill again decla ed and initialiGe& #include:0tdi"&2> int <ain()3 int i=0# 3 ]_Z4# auto int a=20# ( intf(=>d=9a)# a))# i))# 8 if (i:?) =oto x;G# return 0# 8 Q't('t4 20 20 20 (4) F "5 a-"!e exa5(le it i0 clea ti5e& (<)+n a't" !a ia-le get0 5e5" ; at a't" !a ia-le initialiGe eac2 'n ti5e&

What is register storage class in c


+ egi0te 0t" age cla00 i0 !e ; 0i5ila t" a't" 0t" age cla00 exce(t "ne 5"0t i5(" tant ( "(e t;& +ll egi0te !a ia-le in c 0t" e0 in EBU n"t in t2e 5e5" ;& 3<.ortant .oint* a0out re=i*ter *tora=e cla**F (1)In f"ll"1ing decla ati"n4 re=i*ter int a# We a e "nl; eP'e0ting n"t f" cing t" c"5(ile t" 0t" e !a ia-le a in EBU& E"5(ile 1ill decide 12e e t" 0t" e in t2e !a ia-le a& (2)+ egi0te !a ia-le exec'te fa0te t2an "t2e !a ia-le0 -eca'0e it i0 0t" ed in EBU 0" d' ing t2e exec'ti"n c"5(ile 2a0 n" ext a -' den t" - ing t2e !a ia-le f "5 5e5" ; t" EBU& (?)Since a EBU 2a!e li5ited n'5-e "f egi0te 0" it i0 ( "g a55e e0("n0i-ilit; 12ic2 !a ia-le 02"'ld decla ed a0 egi0te !a ia-le i&e& !a ia-le 12ic2 a e '0ing 5an; ti5e0 02"'ld decla ed a0 a egi0te !a ia-le& (4) We cann"t de efe ence egi0te 5e5" ; add e00& F" exa5(le4 (a) #include:0tdi"&2> int <ain()3 re=i*ter int a=10# int %(# (=Fa# ( intf(=>'=9()# 8 Q't('t4 E"5(ilati"n e " !a ia-le 0ince it 2a0 n"t an;

(-) #include:0tdi"&2> int <ain()3 re=i*ter int a9-# 0canf(=>d>d=9Fa9F-)# ( intf(=>d >d=9a9-)# 8 Q't('t4 E"5(ilati"n e " (<) Mefa'lt initial !al'e "f egi0te !a ia-le i0 ga -age& (N) Sc"(e and !i0i-ilit; "f egi0te !a ia-le i0 -l"c6&

What is static keyword in c


Ue;1" d 0tatic i0 '0ed f" decla ing 0tatic !a ia-le0 in c& *2i0 5"difie i0 '0ed 1it2 all data t;(e0 li6e int9 fl"at9 d"'-le9 a a;9 ("inte 9 0t 'ct' e9 f'ncti"n etc& 3<.ortant .oint* a0out *tatic Ee?&ord> 1& It i0 not defa'lt 0t" age cla00 "f gl"-al !a ia-le0& F" anal;Ge t2e f"ll"1ing t2 ee ( "g a50 and it0 "'t('t& (a) exa5(le9

#include:0tdi"&2> int a# int <ain()3 ( intf(=>d=9a)# return 0# 8 Q't('t4 0 (-) #include:0tdi"&2> *tatic int a# int <ain()3 ( intf(=>d=9a)# return 0# 8 Q't('t4 0 (c) #include:0tdi"&2> e5tern int a# int <ain()3 ( intf(=>d=9a)# return 0# 8 Q't('t4 E"5(ilati"n e

"

+t fi 0t glance if ;"' 1ill "-0e !e t2e "'t('t "f a-"!e t2 ee c"de0 ;"' can 0a; defa'lt 0t" age cla00 "f gl"-al !a ia-le i0 0tatic& B't it i0 n"t t 'e& W2;D Read exte n 0t" age cla00& 2& Mefa'lt initial !al'e "f 0tatic integ al t;(e !a ia-le0 a e Ge " "t2e 1i0e n'll& F" exa5(le4 #include :0tdi"&2> *tatic char c# *tatic int i# *tatic float f# *tatic char %0t # int <ain()3 ( intf(=>d >d >f >0=9c9i9f90t )# return 0# 8 Q't('t4 0 0 0&000000 (n'll) ?& + 0a5e 0tatic !a ia-le can -e decla ed 5an; ti5e0 -'t 1e can initialiGe at "nl; "ne ti5e& F" exa5(le4 (a) #include :0tdi"&2> *tatic int i# *tatic int i=2<# *tatic int i# int <ain()3 *tatic int i# $$Mecla ing t2e !a ia-le i& $$InitialiGing t2e !a ia-le& $$+gain decla ing t2e !a ia-le i& $$+gain decla ing t2e !a ia-le i&

( intf(=>d=9i)# return 0# 8 Q't('t4 2< (-) #include :0tdi"&2> *tatic int i# $$Mecla ing t2e !a ia-le *tatic int i=2<# $$InitialiGing t2e !a ia-le int <ain()3 ( intf(=>d=9i)# return 0# 8 *tatic int i=20# $$+gain initialiGing t2e !a ia-le Q't('t4 E"5(ilati"n e " 4 C'lti(le initialiGati"n !a ia-le i& 4& We cann"t 1 ite an; a00ign5ent 0tate5ent gl"-all;& F" exa5(le4 #include :0tdi"&2> *tatic int i=10# $$InitialiGati"n 0tate5ent i=2<# $$+00ign5ent 0tate5ent int <ain()3 ( intf(=>d=9i)# return 0# 8 Q't('t4 E"5(ilati"n e "

Note> +00igning an; !al'e t" t2e !a ia-le at t2e ti5e "f decla ati"n i0 6n"1n a0 initialiGati"n 12ile a00igning an; !al'e t" !a ia-le n"t at t2e ti5e "f decla ati"n i0 6n"1n a00ign5ent& (-) #include :0tdi"&2> *tatic int i=10# int <ain()3 i=2<# $$+00ign5ent 0tate5ent ( intf(=>d=9i)# return 0# 8 Q't('t4 2< (<) + 0tatic !a ia-le initialiGe0 "nl; "ne ti5e in 12"le ( "g a5& F" exa5(le4 #include :0tdi"&2> *tatic int i=10# int <ain()3 i=<# for(i=0#i:<#i)))3 *tatic int a=10# $$*2i0 $$"nl; ( intf(=>d=9a)))#$$*2i0 $$fi!e 8 return 0# 8 Q't('t4 10 11 12 1? 14

0tate5ent 1ill exec'te ti5e& 0tate5ent 1ill exec'te ti5e0&

(N)If 1e decla ed 0tatic !a ia-le l"call; t2en it0 !i0i-ilit; 1ill 1it2in a -l"c6 12e e it 2a0 decla ed& F" exa5(le4 #include:0tdi"&2> int <ain()3 3 *tatic int a=<# ( intf(=>d=9a)# 8 $$( intf(=>d=9a)# !a ia-le a i0 n"t !i0i-le 2e e& return 0# 8 Q't('t4 < I& If decla ed a 0tatic !a ia-le " f'ncti"n gl"-all; t2en it0 !i0i-ilit; 1ill "nl; t2e file in 12ic2 it 2a0 decla ed n"t in t2e "t2e file0& F" exa5(le4 (a) #include:0tdi"&2> *tatic float a=144&0f# $$gl"-al t" all f'ncti"n int <ain()3 3 ( intf(=>d=9a)# $$!a ia-le a i0 !i0i-le 2e e& $$( intf(=>d=9-)# !a ia-le - i0 n"t !i0i-le 2e e& 8 ( intf(=>d=9a)# $$!a ia-le a i0 !i0i-le 2e e& $$( intf(=>d=9-)# !a ia-le - i0 n"t !i0i-le 2e e& return 0# 8 *tatic int -=<# $$Sl"-al t" "nl; calc'lati"n f'ncti"n -oid calculation()3 ( intf(=>d=9a)# $$!a ia-le a i0 !i0i-le 2e e& ( intf(=>d=9-)# $$!a ia-le - i0 !i0i-le 2e e& 8

(-) E"n0ide a c ( "g a5 12ic2 2a0 1 itten in t1" file0 na5ed a0 "ne&c and t1"&c4 $$"ne&c #include:c"ni"&2> *tatic int i=2<# *tatic int 7=<# -oid <ain()3 cl 0c ()# 0'5()# getc2()# 8 $$t1"&c

#include:0tdi"&2> e5tern int i# $$Mecla ati"n e5tern int 7# $$Mecla ati"n $%% +-"!e t1" line0 1ill 0ea c2 i and 7 eit2e in t1"&c (if " "ne&c (if initialiGed !a %$ e5tern -oid *u<()3 int 0# 0=i)7# ( intf(=>d=90)# 8

"f !a ia-le i& "f !a ia-le 7& t2e initialiGati"n 0tate5ent "f !a ia-le initialiGed !a ia-le i0 0tatic " exte n) ia-le i0 exte n)

Q't('t4 E"5(ilati"n e " 4 Un6n"1n 0;5-"l i and 7& Tence 1e can 0a; !a ia-le i and 7 12ic2 2a0 initialiGed int" t1"&c i0 n"t !i0i-le in file "ne&c& *2i0 exa5(le ( "!e0 !i0i-ilit; "f gl"-all; decla ed 0tatic !a ia-le i0 file& Note> In t2e a-"!e exa5(le f'ncti"n 0'5 12ic2 1a0 decla ed and defined in t1"&c 2a0 al0" 0t" age cla00 exte n& S" 1e can call f "5 "t2e file ("ne&c)&If it 1ill 0tatic t2en 1e cann"t call f'ncti"n 0'5 0ince 0tatic 0t" age cla00 i0 "nl; !i0i-le t" t2e file 12e e it 2a0 decla ed& (J)If 1e 0tatic !a ia-le 2a0 decla ed l"call; " 1ill al1a;0 12"le t2e ( "g a5& F" exa5(le4 (a) $$l"call; decla ati"n "f 0tatic !a ia-le #include:0tdi"&2> -oid -i*it()# int <ain()3 int i=0# 3 $$Q(ening inne -l"c6 *tatic int a=<# $$l"call; decla ati"n ]_Z4# $$,a-el "f g"t" 0tate5ent ( intf(=>d =9a)# a))# i))# 8 $$cl"0ing inne -l"c6& !i0it()# $% ( intf(=>d=9a)# Ra ia-le a i0 n"t !i0i-le 2e e -'t it i0 ali!e& %$ if(i:<) =oto ]_Z# return 0# 8 -oid -i*it()3 8 Q't('t4 < N I J @ Ex(lanati"n4 W2en ( "g a5 c"nt "l 1ill c"5e "'t "f inne -l"c6 12e e !a ia-le a 2a0 decla ed t2en "'t0ide "f inne -l"c6 !a ia-le a i0 n"t !i0i-le -'t it0 0c"(e i0 "'t0ide t2e ( "g a5 i&e& !a ia-le a 2a0nXt dead &If 1it2 2el( "f g"t" 0tate5ent c"nt "l again c"5e0 in0ide t2e inne -l"c6 it ( int0 ( e!i"'0 inc e5ented !al'e0 12ic2 1a0 n"t ("00i-le in ca0e "f a't" " egi0te !a ia-le0& (-) gl"-all; it0 0c"(e

$$,"call; decla ati"n0 "f !a ia-le *2e e a e t1" c 0"' ce c"de file04 $$"ne&c #include:0tdi"&2> #include:c"ni"&2> -oid <ain()3 int i# for(i=0#i:?#i)))3 3 *tatic int a=<# ( intf(=>d\n=9a)# a))# 8 !i0it()# 8 getc2()# 8 $$t1"&c #include:0tdi"&2> -oid -i*it()3 ( intf(=M"nXt di0t' -9 I a5 lea ning 0t" age cla00=)# $% ( intf(=>d=9a)# Ra ia-le a i0 n"t !i0i-le 2e e -'t It i0 ali!e& %$ 8 A"1 c"5(ile and exec'te -"t2 file0 t"get2e 4 Q't('t4 < di0t' -9 I a5 lea ning 0t" age cla00 N di0t' -9 I a5 lea ning 0t" age cla00 I di0t' -9 I a5 lea ning 0t" age cla00 Ex(lanati"n4 W2en c"nt "l g"e0 t" an"t2e file and c"5e0 e!en t2at !a ia-le didnXt dead and it ( int0 ( e!i"'0 inc e5ented !al'e& Note> In -"t2 exa5(le0 if ;"' 1ill decla e 0tatic !a ia-le gl"-all; ;"' 1ill get 0a5e "'t('t& @& + 0tatic !a ia-le0 " f'ncti"n0 2a!e inte nal lin6age& +n inte nal lin6age !a ia-le0 " f'ncti"n0 a e !i0i-le t" t2e file 12e e it 2a0 decla ed&

What is extern keyword in c


Ue;1" d exte n i0 '0ed f" decla ing exte n !a ia-le0 in c& *2i0 5"difie i0 '0ed 1it2 all data t;(e0 li6e int9 fl"at9 d"'-le9 a a;9 ("inte 9 0t 'ct' e9 f'ncti"n etc& 3<.ortant .oint* a0out e5tern Ee?&ord> 1& It i0 defa'lt 0t" age cla00 "f all gl"-al !a ia-le0 a0 1ell all f'ncti"n0& F" exa5(le9 +nal;Ge f"ll"1ing t1" c c"de and it0 "'t('t4 (a) #include :0tdi"&2> int i# $$B; defa'lt it i0 exte n !a ia-le int <ain()3

( intf(=>d=9i)# return 0# 8 Q't('t4 0 (-) #include :0tdi"&2> e5tern int i# $$exte n !a ia-le int <ain()3 ( intf(=>d=9i)# return 0# 8 Q't('t4 E"5(ilati"n e " 9 'ndefined 0;5-"l i&

@ue*tion> In B"t2 ( "g a5 !a ia-le i i0 exte n !a ia-le& B't 12; "'t('t i0 diffe entD Read 0ec"nd and t2i d ("int0& (c) #include :0tdi"&2> -oid *u<(int9int) $$B; defa'lt it i0 exte n& int <ain()3 int a=<9-=10# 0'5(a9-)# return 0# 8 -oid *u<(int a9int -)3 ( intf(=>d\=9a)-)# 8 Q't('t4 1< 2& W2en 1e '0e exte n 5"difie 1it2 an; !a ia-le0 it i0 "nl; decla ati"n i&e& 5e5" ; i0 n"t all"cated f" t2e0e !a ia-le& Tence in 0ec"nd ca0e c"5(ile i0 02"1ing e " 'n6n"1n 0;5-"l i& *" define a !a ia-le i&e& all"cate t2e 5e5" ; f" exte n !a ia-le0 it i0 nece00a ; t" initialiGe t2e !a ia-le0& F" exa5(le4 #include :0tdi"&2> e5tern int i=10# $$exte n !a ia-le int <ain()3 ( intf(=>d=9i)# return 0# 8 Q't('t4 10 ?& If ;"' 1ill n"t '0e exte n 6e;1" d 1it2 gl"-al !a ia-le0 t2en c"5(ile 1ill a't"5aticall; initialiGe 1it2 defa'lt !al'e t" exte n !a ia-le& 4& Mefa'lt initial !al'e "f exte n integ al t;(e !a ia-le i0 Ge " "t2e 1i0e n'll& F" exa5(le4 #include :0tdi"&2> char c#

int i# float f# char %0t # int <ain()3 ( intf(=>d >d >f >0=9c9i9f90t )# return 0# 8 Q't('t4 0 0 0&000000 (n'll) <& We cann"t initialiGe exte n !a ia-le l"call; i&e& 1it2in an; -l"c6 eit2e at t2e ti5e "f decla ati"n " 0e(a atel;& We can "nl; initialiGe exte n !a ia-le gl"-all;& F" exa5(le4 (a) #include :0tdi"&2> int <ain()3 e5tern int i=10# $$* ; t" initialiGe exte n !a ia-le $$l"call;& ( intf(=>d=9i)# return 0# 8 Q't('t4 E"5(ilati"n e " 4 Eann"t initialiGe exte n !a ia-le& (-) #include :0tdi"&2> int 5ain()3 e5tern int i# $$Mecla ati"n "f exte n !a ia-le i& int i=10# $$* ; t" l"call; initialiGati"n "f $$exte n !a ia-le i& ( intf(=>d=9i)# return 0# 8 Q't('t4 E"5(ilati"n e " 4 C'lti(le decla ati"n "f !a ia-le i& N& If 1e decla e an; !a ia-le a0 exte n !a ia-le t2en it 0ea c2e0 t2at !a ia-le eit2e it 2a0 -een initialiGed " n"t& If it 2a0 -een initialiGed 12ic2 5a; -e eit2e exte n " 0tatic% t2en it i0 "6 "t2e 1i0e c"5(ile 1ill 02"1 an e " & F" exa5(le4 (a) #include :0tdi"&2> int <ain()3 e5tern int i# $$It 1ill 0ea c2 t2e initialiGati"n "f $$!a ia-le i& ( intf(=>d=9i)# return 0# 8 int i=20# $$InitialiGati"n "f !a ia-le i& Q't('t4 20 (-) #include :0tdi"&2> int 5ain()3 e5tern int i# $$It 1ill 0ea c2 t2e an; initialiGed $$!a ia-le i 12ic2 5a; -e 0tatic " $$exte n& ( intf(=>d=9i)# return 0#

8 e5tern int i=20# $$InitialiGati"n "f exte n !a ia-le i& Out.ut> 20 (c) #include :0tdi"&2> int 5ain()3 e5tern int i# $$It 1ill 0ea c2 t2e an; initialiGed $$!a ia-le i 12ic2 5a; -e 0tatic " $$exte n& ( intf(=>d=9i)# return 0# 8 *tatic int i=20# $$InitialiGati"n "f 0tatic !a ia-le i& Q't('t4 20 (d) #include :0tdi"&2> int <ain()3 e5tern int i# ( intf(=>d=9i)# return 0#

$$!a ia-le i 2a0 decla ed -'t n"t $$initialiGed

Q't('t4 E"5(ilati"n e

" 4 Un6n"1n 0;5-"l i&

I& + (a tic'la exte n !a ia-le can -e decla ed 5an; ti5e0 -'t 1e can initialiGe at "nl; "ne ti5e& F" exa5(le4 (a) e5tern int i# $$Mecla ing t2e !a ia-le i& int i=2<# $$InitialiGing t2e !a ia-le& e5tern int i# $$+gain decla ing t2e !a ia-le i& #include :0tdi"&2> int <ain()3 e5tern int i# $$+gain decla ing t2e !a ia-le i& ( intf(=>d=9i)# return 0# 8 Q't('t4 2< (-) e5tern int i# $$Mecla ing t2e !a ia-le int i=2<# $$InitialiGing t2e !a ia-le #include :0tdi"&2> int <ain()3 ( intf(=>d=9i)# return 0# 8 int i=20# $$InitialiGing t2e !a ia-le Q't('t4 E"5(ilati"n e " 4 C'lti(le initialiGati"n !a ia-le i& exa5(le4

J& We cann"t 1 ite an; a00ign5ent 0tate5ent gl"-all;& F"

#include :0tdi"&2> e5tern int i# int i=10# $$InitialiGati"n 0tate5ent i=2<# $$+00ign5ent 0tate5ent int <ain()3 ( intf(=>d=9i)# return 0# 8 Q't('t4 E"5(ilati"n e " Note> +00igning an; !al'e t" t2e !a ia-le at t2e ti5e "f decla ati"n i0 6n"1n a0 initialiGati"n 12ile a00igning an; !al'e t" !a ia-le n"t at t2e ti5e "f decla ati"n i0 6n"1n a00ign5ent& (-) #include :0tdi"&2> e5tern int i# int <ain()3 i=2<# $$+00ign5ent 0tate5ent ( intf(=>d=9i)# return 0# 8 int i=10# $$InitialiGati"n 0tate5ent Q't('t4 2<
What is the si2e of void pointer in c?

+n01e 4 SiGe "f an; t;(e "f ("inte in c i0 inde(endent "f data t;(e 12ic2 i0 ("inte i0 ("inting i&e& 0iGe "f all t;(e "f ("inte (nea ) in c i0 t1" -;te eit2e it i0 c2a ("inte 9 d"'-le ("inte 9 f'ncti"n ("inte " n'll ("inte & R"id ("inte i0 n"t exce(ti"n "f t2i0 'le and 0iGe "f !"id ("inte i0 al0" t1" -;te&

Scope and visibility of storage class in c

1s static file can be defined in header file and can be used by many c files Bhat )ill be address !rinted if the address is !rocured from the function. Bhat is !ur!ose to declare register +ariable. Ho) to define Vlobal +ariable- used in more than 0 c(files.

,or eg 1.c file 1nt a; main() & a=10((((( @rint a; 1nt ,unc(); ' 0.c file main() & a=10; x() & 1nt a2=00 @rint a return' !rint a '

Pointer as a Parameter eg.


#incl'de :0tdi"&2> int 01a(_int0(int %fi 0t_n'5-e 9 int %0ec"nd_n'5-e )# int 5ain() 3 int a = 49 - = I#

( intf(=( eH01a( !al'e0 a e4 a == >d9 - == >d\n=9 a9 -) 01a(_int0(Fa9 F-)# ( intf(=("0tH01a( !al'e0 a e4 a == >d9 - == >d\n=9 a9 -) 8 et' n 0#

int 01a(_int0(int %fi 0t_n'5-e 9 int %0ec"nd_n'5-e ) 3 int te5(# $% te5( = =12at i0 ("inted t" -;= fi 0t_n'5-e # etc&&& %$ te5( = %fi 0t_n'5-e # %fi 0t_n'5-e = %0ec"nd_n'5-e # %0ec"nd_n'5-e = te5(# et' n 0# 8
Bhy c is !referred o+er other language ga+a for embedded systemK

9ecause of a combination of desirable 2ualities. It is small, simple, fairl! eas! to write a compiler for, has *er! little runtime o*erhead, maps *er! efficientl! to most hardware architectures, and is extremel! portable if carefull! used. C language has compliers for almost e*er! platfroms, so it is *er! portable. It also makes possible to low,le*el programming throught assembl! insertions.

5ll about C C is what is called a compiled language. )his means that once !ou write !our C program, !ou must run it through a C compiler to turn !our program into an e)ecutable that the computer can run (execute). )he C program is the human,readable form, while the executable that comes out of the compiler is the machine,readable and executable form. ?hat this means is that to write and run a C program, !ou must ha*e access to a C compiler. If !ou are using a %;IZ machine (for example, if !ou are writing CFI scripts in C on !our host$s %;IZ computer, or if !ou are a student working on a lab$s %;IZ machine), the C compiler is a*ailable for free. It is called either :cc: or :gcc: and is a*ailable on the command line. If !ou are a student, then the school will likel! pro*ide !ou with a compiler ,, find out what the school is using and learn about it. If !ou are working at home on a ?indows machine, !ou are going to need to download a free C compiler or purchase a commercial compiler. A widel! used commercial compiler is #icrosoft$s Bisual CXX en*ironment (it compiles both C and CXX programs). %nfortunatel!, this program costs se*eral hundred dollars. If !ou do not ha*e hundreds of dollars to spend on a commercial compiler, then !ou can use one of the free compilers a*ailable on the ?eb. (ee http AAdelorie.comAd]gppA as a starting point in !our search. A library is simpl! a package of code that someone else has written to make !our life easier (we$ll discuss libraries a bit later).

?hat is main() function 1ow so man! files are interrelated@ The main function serves as the starting point for program execution It is user defined
'hecking the complete toggling time of our code What is a clock fre3uency we are using in our project of Airbag? What is token? In a passage of text( indi idual words and punctuation mar's are called to'ens. 4imilarly in ! program the smallest indi idual units are 'nown as ! to'ens. ! has six types of to'ens Leywords( !onstants( 4trings( 3perators( Identifiers( 4pecial symbols *ata types 2our classes of data type @) Drimary (or fundamental) data types Integer( char( floats etc. ?) Cser8defined data types $ypdef( enum A) 0eri ed data types

Arra!, "ointer, &unction,structure


O) *mpty data set Hoid ( is em!ty data ty!e in ;

4assing Addresses to function Roid swap (intU( intU)F int main() H 88888888 8888888 4wap (&a(&b)F I Roid swap(intUx(intUy) H I 1unctions returning pointers IntUfun()F Int main () H Int i( Up pP fun()F I 2un() H Int 7F return (&i)F I 5itwise operator

,ote) ;n arithmetic operation cannot be performed on oid pointers. Crinary operator 99p 888888888888888 pPp9@F 88p 88888888888888888p Pp8@F mPp99( if pP>( then after the abo e operation( mP>( pPBF & 88888888 bit anding S 888888888 bit oring V888888888 bitwise exclusi e 3# W 88888888 3neTs complement !onditional operator *xp@ " exp? ) expAF If *xp@ is true( the exp? will be e aluated otherwise expA will be e aluated. What is called late binding 1n a nutshell- late binding is run(time resolution F dynamic loading. ;om!ile time resolution (sometimes called :early binding:)- uses the com!iler and lin8er to +erify that the argument ty!es used to call a function matches the function:s signature. 1n my mind- early binding allo)s me to chec8 my grammar (ho) 1 am calling functions) before chec8ing in my code. $y contrast- late binding re*uires the de+elo!er to s!ecify the function signatures and to ensure that the correct ty!es are used. Bhy is it called :late: bindingK :#ate: refers to the fact that the binding decisions ()hich binary to load- )hich function to call) is deferred as long as !ossible- often until Cust before the function is called- rather than ha+ing the binding decisions made at com!ile time (early).

&hen a program is compiled( the compiler con erts each statement in your !99 program into one or more lines of machine language. *ach line of machine language is gi en itTs own uni<ue se<uential address. $his is no different for functions X when a function is encountered( it is con erted into machine language and gi en the next a ailable address. $hus( each function ends up with a uni<ue machine language address. inding refers to the process that is used to con ert identifiers (such as ariable and function names) into machine language addresses. ;lthough binding is used for both ariables and functions( in this lesson weTre going to focus on function binding. &arly binding 6ost of the function calls the compiler encounters will be direct function calls. ; direct function call is a statement that directly calls a function. 2or example) #incl'de :i"0t ea5> !"id B intRal'e(int nRal'e) 3 0td44c"'t :: nRal'e# 8 int 5ain()

3 8

B intRal'e(<)# $$ *2i0 i0 a di ect f'ncti"n call et' n 0#

6irect function calls can be resol+ed using a !rocess 8no)n as early binding. &arly binding (also called static binding) means the com!iler is able to directly associate the identifier name (such as a function or +ariable name) )ith a machine address. <emember that all functions ha+e a uni*ue machine address. ?o )hen the com!iler encounters a function call- it re!laces the function call )ith a machine language instruction that tells the ;@G to Cum! to the address of the function. #etIs ta8e a loo8 at a sim!le calculator !rogram that uses early binding3 #incl'de :i"0t ea5> '0ing na5e0(ace 0td# int +dd(int n]9 int n_) 3 et' n n] ) n_# 8 int S'-t act(int n]9 int n_) 3 et' n n] H n_# 8 int C'lti(l;(int n]9 int n_) 3 et' n n] % n_# 8 int 5ain() 3 int n]# c"'t :: =Ente cin >> n]# int n_# c"'t :: =Ente cin >> n_#

a n'5-e 4 =#

an"t2e

n'5-e 4 =#

=#

int nQ(e ati"n# d" 3 c"'t :: =Ente

an "(e ati"n (0=add9 1=0'-t act9 2=5'lti(l;)4

cin >> nQ(e ati"n# 8 12ile (nQ(e ati"n : 0 VV nQ(e ati"n > 2)# int nRe0'lt = 0# 01itc2 (nQ(e ati"n) 3 ca0e 04 nRe0'lt = +dd(n]9 n_)# - ea6# ca0e 14 nRe0'lt = S'-t act(n]9 n_)# - ea6# ca0e 24 nRe0'lt = C'lti(l;(n]9 n_)# - ea6# 8 c"'t :: =*2e an01e et' n 0# i04 = :: nRe0'lt :: endl#

8 5ecause ;dd()( 4ubtract()( and 6ultiply() are all direct function calls( the compiler will use early binding to resol e the ;dd()( 4ubtract()( and 6ultiply() function calls. $he compiler will replace the ;dd() function call with an instruction that tells the !DC to 7ump to the address of the ;dd() function. $he same holds true for for 4ubtract() and 6ultiply(). 5ate inding

In some programs( it is not possible to 'now which function will be called until runtime (when the program is run). $his is 'nown as late binding (or dynamic binding). In !99( one way to get late binding is to use function pointers. $o re iew function pointers briefly( a function pointer is a type of pointer that points to a function instead of a ariable. $he function that a function pointer points to can be called by using the function call operator (()) on the pointer. 2or example( the following code calls the ;dd() function) int +dd(int n]9 int n_) 3 et' n n] ) n_# 8 int 5ain() 3 $$ E eate a f'ncti"n ("inte and 5a6e it ("int t" t2e +dd f'ncti"n int (%(Fcn)(int9 int) = +dd# c"'t :: (Fcn(<9 ?) :: endl# $$ add < ) ? et' n 0# 8 !alling a function ia a function pointer is also 'nown as an indirect function call. $he following calculator program is functionally identical to the calculator example abo e( except it uses a function pointer instead of a direct function call) #incl'de :i"0t ea5> '0ing na5e0(ace 0td# int +dd(int n]9 int n_) 3 et' n n] ) n_# 8 int S'-t act(int n]9 int n_) 3 et' n n] H n_# 8 int C'lti(l;(int n]9 int n_) 3 et' n n] % n_# 8 int 5ain() 3

int n]# c"'t :: =Ente cin >> n]# int n_# c"'t :: =Ente cin >> n_#

a n'5-e 4 =#

an"t2e

n'5-e 4 =#

int nQ(e ati"n# d" 3 c"'t :: =Ente =#

an "(e ati"n (0=add9 1=0'-t act9 2=5'lti(l;)4

cin >> nQ(e ati"n# 8 12ile (nQ(e ati"n : 0 VV nQ(e ati"n > 2)# $$ E eate a f'ncti"n ("inte int (%(Fcn)(int9 int)# na5ed (Fcn (;e09 t2e 0;ntax i0 'gl;) c2"0e

$$ Set (Fcn t" ("int t" t2e f'ncti"n t2e '0e 01itc2 (nQ(e ati"n) 3 ca0e 04 (Fcn = +dd# - ea6# ca0e 14 (Fcn = S'-t act# - ea6# ca0e 24 (Fcn = C'lti(l;# - ea6# 8

$$ Eall t2e f'ncti"n t2at (Fcn i0 ("inting t" 1it2 n] and n_ a0 (a a5ete 0 c"'t :: =*2e an01e i04 = :: (Fcn(n]9 n_) :: endl# et' n 0# 8
/ype of 5oop @) *ntry controlled loop ?) *xit controlled loop 6umping out of the loop reak

5he brea8 statement terminates the execution of the enclosing loo! or conditional statement. Bhen loo!s are nested - the brea8 )ould only exit form the loo! containing it. "ontinue 5he continue - as the name im!lies- causes the loo! to be continued )ith next iteration after s8i!!ing any statement in bet)een.
*isadvantage of Array $here is no con enient way to initiali%e only selected elements $here is no shortcut method for initiali%ing a large number of array elements li'e the one a ailable 2ortran. *isadvantage String ! does not pro ide operators that wor' on strings directly &e cannot assign one string to another. )ecursive function 7se

Bhen should you use iteration- and )hen use recursionK 5here are (at least) these three factors to consider3 (1) 1terati+e functions are ty!ically faster than their recursi+e counter!arts. ?o- if s!eed is an issue- you )ould normally use iteration. (0) 1f the stac8 limit is too constraining then you )ill !refer iteration o+er recursion. ,or exam!le- an embedded a!!lication may not be able to tolerate the !ossibly
unbounded stac8 usage of a recursi+e function.

(O) ?ome !rocedures are +ery naturally !rogrammed recursi+ely- and all but unmanageable iterati+ely. Here- then- the choice is clear.
on the other hand recursion often !roduces more elegant- and therefore more easy to maintain code. What is embedded system?

2ny sim!le system im!lemented in intend to !erform any s!ecificFdeddicated tas8 com!romised of microcontroller- !eri!herals- such as memory- sensors- actuatorssignal conditioning circuits and so on. $y contrast- a general(!ur!ose com!uter- such as a personal computer (@;)- is designed to be flexible and to meet a )ide range of end( user needs. ?ince the embedded system is dedicated to s!ecific tas8s- design engineers can o!timi7e it reducing the si7e and cost of the !roduct and increasing the reliability and !erformance.
What is real time system?

3efinition /eal-4ime 1ystems.

A +eal,)ime (!stem responds in a (timel!) predictable wa! to unpredictable external stimuli arri*als. In short, a +eal,)ime (!stem has to fulfil under extreme load conditions -. timeliness meet deadlines, it is re2uired that the application has to finish certain tasks within the time boundaries it has to respect. 5. simultaneit! or simultaneous processing more than one e*ent ma! happen simultaneousl!, all deadlines should be met. >. predictabilit! the real,time s!stem has to react to all possible e*ents in a predictable wa!. =. dependabilit! or trustworthiness it is necessar! that the real,time s!stem en*ironment can rel! on it

In computing, real,time refers to a time frame that is *er! brief, appearing to be immediate. ?hen a computer processes data in real time, it reads and handles data as it is recei*ed, producing results without dela!. &or example, a website that is updated in real,time will allow its *iewers to see changes as soon as the! occur, rather than waiting for updates to be *isible at some later date. A non,real,time computer process does not ha*e a deadline. (uch a process can be considered non,real,time, e*en if fast results are preferred. A real,time s!stem, on the other hand, is expected to respond not ]ust 2uickl!, but also within a predictable period of time. A good example of a real, time computer s!stem is a carPs anti,lock break s!stem. An anti,lock brake s!stem is expected to release a *ehiclePs brakes, pre*enting dangerous wheel locking, in a predictabl! short time frame. %nfortunatel!, there are times when real,time s!stems fail to respond as desired. A +eal,time process fails when its task is not completed before its deadline. In computing, there is no grace

period gi*en because of other demands on a s!stem. +eal,time deadlines must be kept without

regard to other factorsK the! are considered mission critical. ?hen a process is considered hard real,time, it must complete its operation b! a specific time. If it fails to meet its deadline, its operation is without *alue and the s!stem for which it is a component could face failure. ?hen a s!stem is considered soft, real,time, howe*er, there is some room for lateness. &or example, in a soft, real,time s!stem, a dela!ed process ma! not cause the entire s!stem to fail. Instead, it ma! lead to a decrease in the usual 2ualit! of the process or s!stem. 1ard, real,time s!stems are often used in embedded s!stems. Consider, for example, a car engine control s!stem. (uch a s!stem is considered hard, realDtime because a late process could cause the engine to fail. 1ard real,time s!stems are emplo!ed when it is crucial that a task or e*ent is handled b! a strict deadline. )his is t!picall! necessar! when damage or the loss of life ma! occur as a result of a s!stem failure. (oft real,time s!stems are usuall! emplo!ed when there are multiple, connected s!stems that must be maintained despite shifting e*ents and circumstances. )hese s!stems are also used when concurrent access re2uirements are present. &or example, the software used to maintain tra*el schedules for ma]or transportation companies is often soft real,time. It is necessar! for such software to update schedules with little dela!. 1owe*er, a dela! of a few seconds is not likel! to cause the kind of ma!hem possible when a hard, real,time s!stem fails.

Memory:
Basic type of memory ROM, RAM , EPROM,EEPROM,FLASH ROM, EEPROM, Flash, allow random access of memory, that is we need not required to access them serially or sequentially. RAM

1) DRAM !ynamic random access memory,


"ery dense #$ transistor %er &it' and (ery e)%ensi(e. Require refresh and often not the fastest access time. 0) SRAM Static random access memory Fast and no refresh required. *ot so dense and not so chea%. Often used for caches

Potrebbero piacerti anche