Вы находитесь на странице: 1из 127

A Course on REAL-TIME EMBEDDED SYSTEMS

. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Presents

A Module
on

C - PROGRAMMING

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

C
Program is set of instructions, these instructions are executing by the system, that system is providing result. Ken Thomson developed BCPL (Basic Combined Programming Language) programming language. This programming is difficult to learn and execute. His co-programmer Dennis Ritche was introduced a programming called C. Is from Bell Laboratories in 1972. Here is nothing, in BCPL from second letter C Combined programming language. It is high level structured programming language. Every Program must have one main () function. This section contains two parts: 1) Declaration Part 2) Executable Part.

The Declaration part declares all the variables used in the executable part. There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The Program execution begins at the opening brace and ends at the closing brace. All statements in the declaration and executable parts ends with a semicolon. The Sub Program section contains all the user defined functions that are called in the main function. Executing a C Program: The Steps involved in executing a C Program are: 1. Creating the Program. 2. Compiling the Program. 3. Linking then Program with function that is needed from C Library. 4. Execute C Program. Basic Structure of C Program: Documentation Section - suggestion Link Section - optional Definition Section - optional Global Declaration Section - optional

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Main () function section { Declaration Part Executable Part }

- necessary

Sub Program Section Function 1 Function 2 Function 3

The Documentation section consists of a set of comment Liner giving the name of the Program the author and other details. Which the Programmer would like to use Later. The Link section Provides instructions to the compiler to link functions from the system Library. The Definition section defines all symbolic constants. There are some variables that are used in more than one function such variables are called global variables and declared in the global declaration section i.e., outside of all functions. Getting Started with C: There is a Close analogy between learning English Language and learning C Language. The Classical method of learning English is to first learn the alphabets or characters used in the language, then learn to combine these alphabets to form words. Which in turn are combined to learn sentences and sentences are combined to form paragraphs. Learning C is much similar and much easier. Like English to learn C. We must first know what alphabets, numbers and special symbols are used in C, then how using these constants, variables and keywords are constructed, and finally how are combined these to form an instruction. A group-s of instructions would be combined to form a program. Steps in Learning English Language: Alphabets----->Words------>Sentences----->Paragraph Steps in Learning C: Alphabets, Constants

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Digits, Special Symbols

-------> Variables Keywords

------> Instructions ------>Program

Character set

Tokens

The C Character Set: The C character denotes any alphabet, digit or special symbol used to represent information. Alphabets A,B,C,<<<<..Y,Z.

a,b,c<<<<<.y,z. Digits Special Symbols C Tokens: The smallest individual units in a C Program are Known as C Tokens. C Tokens Keywords C Keywords: Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. The keywords are also called Reserved words. Keywords serve as basic building blocks for program statements.All keywords must be in lowercase letters. Some compilerws may use additional keyword that must be identified from the manual. There are only 32 keywords available in C. Following is the List of keywords in c. Auto Break Case Char double else enum extern int long volatile register static struct switch type def Constants Variables 0,1,2,3,4,5,6,7,8,9 {, }, [,], (, ) , !,

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Const Continue Default Do Constants:

float far go to if

return short signed size of

union unsigned void while

A constant is a fixed value that do not change during the execution of a Program for example in the equation 3x+y=20 Since 3 and 20 cannot change, they are called constants, where as the quantities x&y can vary or change hence are called variables.

Constants Numeric constants Character constants Integer constants Real constants Single character constants String constants Integer Constants: An integer constants refers to a sequence of digits. There are 3 types of integers namely decimal, octal and hexadecimal. Decimal integer constants of set of digits, 0 through 9, preceded by an optional or + sign.

123 -321 +78 0 An octal integer constants consists of any combination of digits from the set 0 through 7 with leading 0. Eg: 037 0435
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

Eg:

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

0551 0 A sequence of digits preceded by 0x or 0x is considered as hexadecimal integer. They may also include alphabets A through F or a through f. The Letters A through F represents the numbers 10 through 15. Eg: 0x2 0x9f 0xbcd 0x Rules for Constructing Integer Constants: 1) An Integer constant must have at least one digit. 2) It must not haver a decimal point. 3) It could be either positive or negative. 4) If no sign preceds on integer constant it is assumed to be +ve. 5) No commas or blanks are allowed within an integer constant. 6) The allowable range for integer constants is -32768 to 32767. Real Constants: Integer numbers are inadequate to represent quantities that vary continuously. Such as distances, heights, temperatures, prices and so these quantities are represented by numbers containing actional parts like 17.548. Such numbers are called real or floating point constants. Eg: 0.0083 -0.75 435.36 247.0

A real number may also be expressed in exponential (scientific) notation. The general form is Mantissa e exponent

The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign. The letter e separting the mantissa and the exponent can be written in either lowercase or uppercase. Eg: 0.65e4 12e-2 3018e3 -102e-1 For example the value 215.65 may be written as 2.1565e2 in exponential notation. E2 means multiply by 102.
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Rules for constructing Real Constants: Following rules must be observed while constructing real constants expressed in fractional form. 1) A real constant must have at least one digit. 2) It must have a decimal point. 3) It could be either positive or negative 4) Default sign is positive. 5) No commas or blanks are allowed within a real constant. Following rules must be observed while constructing real constant expressed in exponential form: 1) The mantissa part and the exponential part should be separated by a letter e. 2) The mantissa part may have a positive or negative sign. 3) Default sign of mantissa part is positive. 4) The exponent must have at least one digit which must be a positive or negative. Default sign is positive. 5) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

Character Constants: There are 2 types of character constants. 1) Single character constants Single Character constants: A character constant is either a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. The maximum length of a character constant can be 1 character. Eg: A , 1, 5, = 2) String constants

String Constants: String constant is a sequence of characters enclosed in double quotes. The characters may be letters numbers, Special characters and blank space. Eg: Hello!, 2005, Well Done, ?, T
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Variables: Variable names are names given to locations in the memory of computer where different constants are stored. These locations can contain integer, real or character constants, unlike constants the variables remains unchanged during the execution of a program a variable may take different values at different times during execution. Rules for constructing variable names: 1) A variable name is any combination of 1 to 8 alphabets, digits or underscores. Some compilens allow variable names whose length could be up to 40 characters. 2) The first character in the variable name must be an alphabet. 3) No commas or blanks are allowed within a variable name. 4) No special symbol other than an underscore can be used in a variable name. A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program. Eg: average, Height, Total, Counter-1, Class-strength The data value stored by a variable can be of integer, float, real number or character constant. So the variable is declared with a data type depending on the type of the data value each data item in a c Program related with a type known as data type.

Declaration of variables: The declaration of variables must be done before they are used in the program variable declaration does there things. 1) It tells the compiler what the variable name is. 2) It specifies what type of data the variable will hold 3) What set of operations that are performed against this location. The syntax for declaring a variable is as follows: Data type v1, v2, <<. vn;

V1, v2,<<.vn are the names of variables. Varaibles are separated by commas. A declaration statement must end with a semicolon. Eg: int count; int number, total;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Double ratio;

Assigning values to variables: 1) Values can be assigned to variables using the assignment operator = as follows. Variable name = constant; Eg: a = 10; B = 20; Balance = 75.84; ANSI C supports four classes of data types.

1) Primary dat types 2) User defined data types 3) Derived data types 4) Empty data type. 2) C point multiple assignments in one line. Eg: a = 10; b = 20; 3) It is also possible to assign a value to a variable at the time the variable is declared. The general form is Data type variable-name = constant; Eg: int a =10; Char ch =x; Double balance = 75.84; 4) The process of giving initial values to variables is called initialization C permits the initialization of more than one variables in one statement using multiple assignment operations. Eg: p = q =s =0; X =y =z =max The first statement initializes the variables p,q and s to aero, while the second initialize x,y and z with max. Note that max is a symbolic constant defined at the beginning. Operators :-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Operator is a symbol it can operate the two or more operands. 1) Arithmetic operator :It is used to construct the mathematical expressions as in algebra. +, - *, / , % a+b , a-b, a/b, a*b, a%b = reminder value 2) Relational operator :- to compare the two quantities. <, <=, >, >=, = =, != a>b a>=b a<b a<=b a==b a!=b 3) Logical Operators:- are && and || are used when we want to form compound conditions by combining two or more relations. && -> logical AND, ||-> logical OR, ! -> logical NOT 4) Assignment operators are used to assign the value of an expression to a variable. =. V = exp or value; a = 10; or a= b + c; 5) Increment ++ operator is to adds 1 to the operand while Decrement -- operator is to subtracts 1. Pre post ++ a a++ = a=a+1 --a a- = a= a-1 6) Conditional operator ? : is a ternary operator exp1 ? exp2 : exp3 ; X= (a>b)? a is big : b is big ; 7) Bit wise operators are for manipulation of data at values of bit level. These are used for testing the bits or shifting them to the right or left. &,!, ~, ^, <<, >> 8) Special operators of interest for sizeof(), typedef(), *(pointer), this operator, arrow () operator

Input/ Output functions :To read the data from the key board, and to print the data on screen using the I/O functions. 1. Character I/O functions 2. String I/O functions 3. Formatted I/O functions

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

1. Character I/O functions :getchar( ):- to read the single character from the key board. x=getchar( ); putchar( ):- to write the single character on the screen. Putchar(x); 2. String I/O functions:gets( ):- to read the string(group of characters). x=gets(); put( ):- to write the string. puts(x); getch( ):- is a reading function, but it cant print the value. getche( ):- is a reading function, it can print the value. Ex:- k it can print the k. 3. Formatted I/O functions:scanf ( ):- to read any type of data, means int, float, char, string and double scanf(controle string,&v1,&v2<<); control strings are:%d - integer %c - single char %s - string %f - float %g - double %u - unsigned values %ld - long int <<<<<<.. Ex:- scanf(%d%c%f,&a,&b,%c); printf( ):- to print the any type of data. printf(control string, v1,v2<<..); Ex:- printf(control strings,v1,v2<.);

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Decision Making Statements (We are calling Control statements.) If statements Switch statements Conditional operator statements.

IF Statement : If statement is a powerful DMS and is used to control the

flow of executing statements. It is a two-way decision statements and is used in conjunction with an expression. if ( test expression)

Example :if ( age > 58) { Printf(Employee is retired ); }

Simple if statement :- here the condition is checks first, if it is true execute the body statements, O.w another stmts are execute. Syntax :if (test exp) { statement block; } Statement x;

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Program:#include<stdio.h> #include<conio.h> Void main() { int age; printf(enter the age); scanf(%d,&age); if(age> 58) { Printf(the employee is retired): } }

If else: -

Here the condition is checks first, if it is true execute the body statements, if it is false execute the else part.

if (test exp) { statement x; } else { statement y; } Statement z;

#include<stdio.h> #include<conio.h> Void main() { int age; printf(enter the age); scanf(%d,&age); if(age > 18)
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

{ Printf(the preson is major); } else { Printf(the preson is minor); } getch(); }

Nested if :- here the condition is checks first, if it is true, again it checks the within the condition, it is true then execute the body stmt, or else stmt, or outer if else part will be execute

if (test exp) { if(test exp) { statement x; } else { statement y; } } else { Statement z } Statement x; #include<stdio.h> #include<conio.h> Void main() { int a,b,c;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

printf(enter the a b c values); scanf(%d%d%d,&a,&b,&c); if(a > b) { if(a>c) { printf( a is big); } else { Printf( c is big); } } else if(b>c) { Printf( b is big); } else { Printf( c is big); } getch(); }

Else if or Ladder if :- here the condition is checks first, if it is true execute the body statements, o.w it can false it will checks the another else if stmt and so on<<.. up to the conditions are true.

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

if (cond 1) statement 1; else if (cond2) statement 2; else if (cond3) statement 3; <<<. <<<. else default stmt; <<<<< Statement x;

Program to print the different colors: #include<stdio.h> #inlude<conio.h> Void main() { int choice; Printf(enter the choice); Scanf(%d,&choice); If( choice==1) { Printf(the color is Red);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

} Else If( choice==2) { Printf(the color is Orange); } Else If( choice==3) { Printf(the color is Yellow); } Else If( choice==4) { Printf(the color is Green); } Else If( choice==5) { Printf(the color is Blue); } Else If( choice==6) { Printf(the color is Indigo); } Else If( choice==7) { Printf(the color is Violet); } Else Printf(you choice is invalid number ); getch(); } Switch statement: The switch stmt tests the value of a given variable against a list if case value and when a match is found, a block of stmt associated with that case is executed.

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Switch(v or exp) { case 1: case 2: stmt 1; break; stmt 2; break; <<<<<<<< <<<<<<<< default: stmt x; } Program to print the different colors. #include<stdio.h> #inlude<conio.h> Void main() { int choice; Printf(enter the choice); Scanf(%d,&choice);

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Switch(choice) { case 1: Printf(the color is Red); break; case 2: Printf(the color is Orange); break; case 3: Printf(the color is Yellow); break; case 4 : Printf(the color is Green); break; case 5: Printf(the color is Blue); break; case 6: Printf(the color is Indigo); break; case 7: Printf(the color is Violet); break; default : Printf(you choice is invalid number ); getch(); }

Decision Making Looping Statements :A statement is executing more than one time with in the condition. 1. Entry level looping stmt 2. Exit level looping stmt We have 3 looping statements are there, 1. While 2. Do while 3. For loop
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

1. While : it is an entry level looping statement and first test the condition, it can be true then execute the body of the loop statements up to the given condition is false, if not execute the out of the body statements. Initialization; While(condition) { Body of the statements; Increment/ decrement stmt; }

Program to print the math table. #include<stdio.h> #include<conio.h> Void main() { Int a,i=1; Printf(enter the a value); Scanf(%d,&a); While(i<=10) { C= a * i; Printf(\n%d * %d = %d,a, i, c); i=i+1; // i++; // i+=1; } getch(); }

2. Do- while: it is an exit level looping statement and first execute the body of the statement and check the given condition, if it can be true again execute the body of the loop statement up to the given condition is false, if false it exit that from the loop. Initialization; do { Body of the statements; Increment/ decrement stmt; } While(condition);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Program to print the math table. #include<stdio.h> #include<conio.h> Void main() { Int a,i=1; Printf(enter the a value); Scanf(%d,&a); do { C= a * i; Printf(\n%d * %d = %d,a, i, c); i=i+1; // i++; // i+=1; } While(i<=10); getch(); } 3. For loop :- it is also entry level looping statement, it more comfortable to execute the statements, because in that stmt initialize and test condition and inc/decrement stmts are writing in single line. for( init ; test-condition; incr/decr) { Body of the loop; } Program to print the math table. #include<stdio.h> #include<conio.h> Void main() { int a,i; Printf(enter the a value); Scanf(%d,&a); for(i=1;i<=10;i++) C= a * i; Printf(\n%d * %d = %d,a, i, c); } getch(); }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Nested for loop:A loop statement is executed within the another looping is called nested loop. for(init; test-condition 1; incr/decr) { for( init ; test-condition 2 ; incr/decr) { Body of the loop; } }

Program to print the multiple math table.

#include<stdio.h> #include<conio.h> Void main() { int a,i; Printf(enter the a value); Scanf(%d,&a); for(a=1;a<=5;a++) { for(i=1;i<=10;i++) { c= a * i; Printf(\n%d * %d = %d,a, i, c); } Printf(\n); } getch(); }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Arrays and Strings :Array is similar type of data collection name. It is a collection of same data type elements. An array is a group of related data items, which share common name. it is a set of homogeneous data. Ex:- in a class room if 30 students are there, they have snos, names we declare 30 variable, some it is not possible so we are rectifying this problem using an array. Types of Array: 1. Linear Array 2. Non Linear Array Linear Array:This type of array is also called One dimensional array. This is also called list

array. in this array only one subscript is used. It is written either in row or in column form. Syntax:- datatype arrayname[size];

Where data types are int, float or char. Array name is any name or word or character. Ex:= int a[20]; Float sal[10]; Char name[20]; Size should be one cell more than the given data length, because last cell should be empty or null cell that is the end of the string has null character. Char name[10]; Keer thi 0123456 int a[5]; a[0]=20, a[1]=30, a[2]=40, a[3]=50, a[4]=60. Assigning Values to an array:Data type arrayname[size]={list of values}; Int a[5]={4,5,6,7,8,9};

To read and print the single dimensional array elements


-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h> #include<conio.h> Void main() { int a[5],i; Printf(enter the a values); for(i=0;i<5;i++) { Scanf(%d,&a*i+); } Printf(the entered values); for(i=0;i<5;i++) { printff(\n%d,a*i+); } getch(); } Non Linear Array:- Array of having different dimensions or n subscripts is in the form of non linear array. 1. 2. 3. 4. Two dimensional array Three dimensional array. <<<<<<<<< N dimensional array.

1. Two dimensional array:- these arrays are also called double dimensional array. Is tabular or rectangular array. these array are in row and column form. datatype arrayname[rowsize][columnsize]; int a[2][2]; int

a[2][2]

{2,3,4,5};

a[0][0]=2, a[0][1]=3, a[1][0]=4, a[11]=5 2 3 4 5 int a[ ][ ] = {{2,3},{4,5}};

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

2. Three dimensional array:- called space array. in this space, row and column are taken. These are also called xyz array. int a[2][2][2]; a[0][0][0], a[0][0][1], a[0][1][0], a[0][1][1], a[1][1][1];

To read and print the two dimensional array elements #include<stdio.h> #include<conio.h> Void main() { int a[5][5],i,j; Printf(enter the a values); for(i=0;i<5;i++) { for(j=0;j<5;j++) { Scanf(%d,&a*i+*j+); } } Printf(the entered values); for(i=0;i<5;i++) { for(j=0;j<5;j++) { printff(%d,a*i+*j+); } } getch(); }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Strings :More than on character is string. Group of characters is a string. I LOVE INDIA Printf( I Love India); But if the string is stored in variable, then such type of variable should be of character type. Also character variable should be declared in the array form (one dimensional array) data type arrayname[10];

Char name[10]; Printf(%S, name); Char name*10+=,k,e,e,r,t,h,i}; Multiple string Char name[4][10]; for(i=0;i<5;i++) { Printf(%s, name*i+); } data type arrayname[2][10]; Char name*2+*10+=,,k,e,e,r,t,h,i},,v,a,r,d,h,a,n}}; String Handling Functions:Operate some operations with string. We use some powerful string handling functions. All these function are linked with the string.h header file stored in the include. 1. 2. 3. 4. 5. Strcat() Strcmp() Strcpy() Strlen() Strrev()

Strcat :- this is to concatenate or combined two different strings together. Strcat(string1,string2); String1[10], string2[10]; String1= keerthi, string2= vardhan Puts(string1);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Strcmp :- this is to compare two strings. It will check which string is alphabetically above the other.(ASCII) values are used. Strcmp(string1,string2) Strcpy :- this is to copy one string into another string. Note that, destination field should be larger than the source filed. In other words size of the string1 should be larger to receive the contents of the string2. Strcpy(string1,string2) Printf(%s, string1); Strlen :- is to count the number of character in a string, to find the length of the string. n = strlen(string1); printf(%d,n); strrev : - is to reverse a string. This function takes string variable as its single argument. here the first char becomes last and last becomes first in a string. This is to find string palindrome or not madam . strrev(string1) Program On String Functions #include<stdio.h> #include<conio.h> Void main() { char a[10],b[10]; int n; printf(enter the string a,b); gets(a); // scanf(%s,&a); // scanf(%c,&a*i+); here i is define gets(b); n = strlen(a); printf(%d,n); strcpy(a,b); printf(%s,a); strrev(a); printf(%s,a); getch(); }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Function:Definition Function is block or part of program. When any program is very long or same code is repeating many times then we try to cut the program in different parts (or blocks) so that whole program became more understandable, easier to debug (error checking) and size of code will be lesser. Syntax of function Data type function(parameters); Or Data type function() { Statements x; Statements-y; -----------------------} Functions are dividing in to types in c programming

1. Function no arguments and no return type 2. Function with arguments and no return type 3. Function with arguments and with return type.

Simple example of function structure int sum (int,int); //function declaration void main() { int p; p=sum(3,4); //function call printf(%d,sum); } int sum( int a,int b) //function definition { int s; //function body s=a+b; return s; //function returning a value }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Detail explanation of syntax of function: (1) Function_name : Function naming rule in c programming: Rule 1. Name of function includes alphabets, digit and underscore. Valid name: Invalid name: world, addition23, sum_of_number etc. factorial#, avg value, display*number etc.

Rule2. First character of name of any function must be either alphabets or underscore. Valid name: _calulate, _5,a_, __ etc. Invalid name: 5_, 10_function, 123 etc. Rule 3. Name of function cannot be any keyword of c program. Invalid name: interrupt, float, asm, enum etc. Rule 4. Name of function cannot be global identifier. Valid name: __TOTAL__, __NAME__ ,__TINY__etc. Invalid name: __TIME__,__DATE__, __FILE__,__LINE__,__STDC__ Note: It is good practice to not write the variable name in the above format.

Rule 5: Name of function cannot be register Pseudo variables Rule 6. Name of function cannot be exactly same as of name of other function or identifier within the scope of the function. Rule 7. Name of function is case sensitive. Rule 8. Only first 32 characters are significant of the functions name. Example: abcdefghijklmnopqrstuvwxyz123456aaa, abcdefghijklmnopqrstuvwxyz123456bbb, abcdefghijklmnopqrstuvwxyz123456cad

All three function name are same because only first 32 characters has meaning. Rest has not any importance.

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Return type of function in c programming return is keyword of c. When the control reaches to the return keyword it immediately terminates the execution of that function and transfer the control to the calling function. Syntax of return statement: Here expression is optional which has indicated by [ ]. Example: void dev(); void main() { clrscr(); printf("one\n"); dev(); printf("two\n"); getch(); } void dev() { printf("three\n"); return; printf("four\n"); }

Output: one three two Return type of function can be:

1. Primitive data type. Primitive data types are: char, int, float, double, void Examples: a. function which is returning char data type b. function which is returning int data type c. function which is returning float data type d. function which is returning double data type e. function which is returning void data type 2. Derived data type.
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Derived data types are: array, function, pointer Examples: a. Function which is returning array b. function which is returning function c. function which is returning pointer 3. User defined data type. User defined data types are: structure, union, enum Examples: a. Function which is returning structure b. Function which is returning union c. Function which is returning enum

Feature of functions parameter in c 1.Default parameter of function is void. 2.Only register storage class is allowed with function parameter. int devD(register int,register int); void main(){ int temp; temp=devD(5,25); printf("%d",temp); getch(); } int devD(int register x,int register y) { static int num; num=x+y+1; return num; } Output: 20 3. Default storage class of function parameter is auto. 4. If function declaration has not written then during the function call it doesnt check prototype of function if function return type is int data type and parameter is not float data type.

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Nesting of Functions:When a function call another function or a function processing occurs in within another function. It is used in the handling of multi function. Main() { <<<..; fun1(); <<<... } fun1() { <<<<. fun2(); <<<< return; } fun2() { <<<.. return; }

Example on nested functions: #include<stdio.h> main() { int a,b,c; float ratio(); Clrscr(); Printf(enter the three number:); Scanf(%d%d%d,&a,&b,&c); Printf(ratio is =%f, ratio(a,b,c)); getch(); } float ratio(int x,int y, int z) { if diff(y,z) { return(x/(y-z)); } else { return (0.0); }} diff(p,q) Int p,q; { if(p!=q) { return(1); } else { return(0); } Return; }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

RECURSION:
When called function in turn calls another function, then a process of chaining occurs Main() { printf( krest); main(); }

Recursion is a special case of this process or chain. So when a function calls itself, then it is called recursion. This chain continues till a specific condition met. If it has not any stop condition, then if will create an infinite loop. Recursion is used to solve for the problems, which cannot be solved by the iterative procedure for, while and do loops. It is useful to solve repetitive problems, where input of one sub program can be used as staring value having the previous output. Condition: 1) A recursive function should have specific condition. 2) A recursive function should have smaller argument each time than the previous argument value in the every recursive loop. Factorial with recursion main() { int n, fact(); printf(enter a value); scanf(%d,&n); Printf( %d factorial is %d,n,fact(n)); getch(); } Fact(n) Int n; { int m; If(n==1) { return(n); } else { m=n*fact(n-1); return(m); } }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Use of Array in function(Passing Array to function) Array plays an important role in the function handling. Arraying are declared with the function definition and so can be used in the function declaration stmt. 1) Passing one Dimensional array to fun:syn:- fun-name(array-name, array-size); Ex:- sort(a,n); => an array defined as int a[20]; and n is size of array, n<=20. 2) Passing 2-D array to fun:syn:- fun-name(array-name, row-size, col-size); Ex:- sort(a,n,m); => an array defined as int a[10][10]; and n is row size of array, n<=10,m is col size of array m<=10. 1) 1D ex biggest No main() { int a[10],I,b,big(),n; printf(enter the size of array); scanf(%d,&n); printf(enter the elements of array); for(i=0;i<n;i++) , scanf(%d,&a*i+); } b=big(a,n); printf(biggest number is %d,b); getch(); } int big(x,m) int x[],m; { int bi,I; bi=x[0]; for(i=1;i<m;i++) { ifx[i]>bi) { bi=x[i]; } } retrun(bi); }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Storage Classes Of Variables:


Variables execute in the function program locally, but others execute globally. Also variable are stored in the memory in different ways by using the function. Every time the execution period and boundary of the variable vary in the function subprograms. Every variable in c language has a powerful characteristic called Storage-class. So there be need to specify the storage class of a variable along with the data-type. a) Local variable A local variable will be visible to the function in which it is declared, but not to other. It is declare inside the function. A local variable is also called automatic variable, because it is automatically created and destroyed. b) Global variable A global variable is to visible to all the functions and it is declared outside the function. The proper place of declaration of global variable is at the beginning of the main program or before the function. 1) 2) 3) 4) Auto Register static extern

Auto storage class:

It is the default storage class to be used within a program or file. An auto variable is defined and accessed within a function. An auto variable has no defined value and storage until the function in with it is defined is called. Auto variable stored on the stack, which provides temporary storage. Int x; => auto int x Register storage class:

The lifetime and visibility of register storage class is similar to that of auto default storage class. The default initial value is unpredictable, it may be some garbage value. These variables are accessed much quickly than other variables as they reside in the registers of the microprocessor(8, 16 bits). Register variables are only of char or int data type. As these are declared in registers, so these can not be referred by the pointer variable. We are taking limited variables in registers.
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Register int countr1, countr2; Static storage class: It is a storage which is also quite similar to auto except that the variable once defined any where do not die till program ends and the value of variable will also be retained, but will be assessed only by the function declaring it. Static storage variables have initial value is zero and not garbage. Static auto variable has a global lifetime and local visibility. It cannot be accessed from another function. Main() { fun(); fun(); getch(); } fun() { static int a; a++; printf(\n%d,a); }

External storage class

When there is a need for a variable to be assessed in all the functions in a c. the external variables serve the purpose. Scope of the external variable is global. These variable are assessed globally and has the initial value as zero and not the garbage value, so these variables do not die at the start or at the end of any function program. But die at the end of the whole program. These are used to transfer the value from one function to another, their scope is from the position of their declaration to the end of the program. Here no fresh memory is allotted.

int a; main() { a=a+1; printf(%d,a); fun1(); fun2(); fun3();


-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

fun4(); printf(%d, a); getch(); } fun1() { a=a+10; Printf(%d,a); } fun2() { a=a+5; Printf(%d,a); } fun3() { a=a-2; Printf(%d,a); } fun4() { a=a-8; Printf(%d,a); } Out put :1 11 16 14 22 22

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

POINTERS :Pointers are memory address variable i.e the variables, which have address of the variable having some value, stored in the memory, pointers are directly linked to the memory address. Actually pointer is a variable having address of another variable and not the value. Variable name q memory value 280 memory address 6000 memory variable p

Here q is int variable having 280, is stored at the memory address 6000 this variable is p(pointer). P which is a memory variable and is stored further in the memory and again it has some memory address. (Pointer to pointer). Variable name q p memory value 280 6000 memory address memory variable 6000 p pointer 5980 r pointer to pointer

Advantages: 1. Pointers increases the execution speed of the c-program and more efficient. 2. Pointer reduces the length and complexity of the program. 3. Pointer access the memory elements very easily. 4. Use of the pointer to the character arrays are easier to handle and represent with the pointers. 5. Pointers have direct link with structure and union. 6. Pointer saves the memory space. This can be done by using the dynamic memory allocation technique. Declaring a pointer variable A pointer variable should be declared before they are used, similar to the common variable. The pointer variable be declared in data type. data type *pointer-variable;

Where data-type may be integer(int), float, char or double. Also here * (asteric sign) means it is pointer operator and pointer variable is any variable linked with * sign. The symbol * is called indirection operator. int *x; float *y; char *z;

Note:- pointer are classified on the basis of its declaration. i.e whether a pointer variable holds the address of an integer or a float or a char variable. int *p, q;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

p = &q; printf(%d is stored at address %u , q,p);

Here 280 is stored at address 6000 Note :- code in the control string of printf () statement be %u . Becoz address are always unsigned integer. &*p address 6000 *&p value of p 280 &q 6000 *(&q) 280

Will display the contents(value stored) of pointer variable(the * represents the value at the address) and & display the address of pointer variable( the & represents the address of the value). We can write the above stmt. main() { int q,*p,n; q=280; p=&q; printf( \n %d is stored at address %u, x, &x); printf( \n %d is stored at address %u, x, q); printf( \n %d is stored at address %u,*q, q); printf( \n %d is stored at address %u, *&x, &x); getch(); } Note :- some invalid statements to accessing the address of pointer are &12; &(x+y); int &x(10);

Pointer Expressions :pointer deals with the arithmetic, relational, logical and assignment expressions. Pointers are not directly used in the arithmetic, relational, logical and assignment expressions only pointer value are used. Some of the arithmetic expression used with pointer p1 and p2.. int x, y, z, a, b, c, *p1,*p2; x=10; y=20;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

p1 = &x; p2 = &y; z=(*p1) / (*p2); a=(*p1) + (*p2); b=(*p1) * (*p2); c=(*p1) - (*p2);

int a,b, *p1,*p2; b=280; p1=&b; a=200; p2=&a; if(*p1>*P2) { stmts; }

#include<stdio.h> #include<conio.h> int *p1, *p2, *p3; int i=1; int j=2; int a[5]={1,2,3,4,5,}; main() { int m,n,x; p1=&j; p2=&j; p3=&a[0]; m=p1-p2; n=*p1+*p2; x=*p3+2; if(p1>p2) printf(%d%d%d,m,n,x); if(p1<=p2) printf(%u%u%u,p1,p2,p3); }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Increment / Decrement operator the address pointer as : p1 = p1 +1; p1 = p1 1; p1 ++; p1 - - ; the statement p1++ will increment the address value in the pointer variable depending on the type of the variable. If the pointer is of int type, then the pointer address is value will be incremented by 2 and if it is of char type, then the value will be incre by 1; this increase or decrease is according to cells in the memory and cells works according to the scale factor. Scale factor is a scale or measurement of the length of the cells of different data type in terms in terms of bits or bytes.

data type char int float double

memory space 1 2 4 8

note :- pointer can be incr /decred. These can not be arithmetic and logical and relational and assignment operators

Pointer with array :pointer have close relationship with array. an array name is in fact a pointer to the array. pointer are linked with array by taking its first element number and its base address. ststic int a[5]={23,24,25,26,27}; a[0] 23 1000 a[1] a[2] 24 25 1002 1004 a[3] 26 1006 27 1008 a[4]

address of x[element no]= base address + element no. * scale factor 3rd element of the array and you have base address 100, x[3] = 1000 + 3 * 2 = 1006 x[4] = 1000 + 4 * 2 = 1008 x[10] = 1000 + 0 * 2 = 1000

int a[5], *p; p=a; P=&a;


-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

p=&a[0];

Above all are same Address value address value address value &a[0] a[0] a+0 *a p *p &a[1] a[1] a+1 *(a+1) p+1 *(p+1) &a[2] a[2] a+2 *(a+2) p+2 *(p+2) So on<<<<<<<<<<<<<<<<<<<<<<<<<<.

#include<stdio.h> main() { ststic int a[5]={23,24,25,26,27}; int i, n, *p; p=a; n=5; for(i=0;i<n ; i++) { printf(%4d, *(p+i)); } printf(reversed data is ); for(i=n-1;i>0; --i) { printf(%4d,*(a+i)); } getch(); } Type of array pointer:1) one dimensional array pointer:Is works according to the given base address, the number of elements in an array and the data type of array. #include<stdio.h> main() { int a[20], *p1, i, n, sum=0; clrscr(); printf(\n enter scanf(%d,&n); printf(enter the array element);

the

value

of

n:):

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

for(i=0;i<n;i++) { scanf(%d, &a*i+); } printf(\n \t element \t value \t address); p1=&a[0]; for(i=0; i<n; i++) { sum =sum + a [i]; printf(\n \t a[%d] \t %d \t %u, i+1, a*i+, p1); } printf(sum is =%d, sum); printf( the sum is stored at address %u, &sum); getch(); }

b) Two dimensional array :- varies according to their addresses. It can be viewed as a collection of 1D array. the various portions of pointer with 2 D array vary according to their row and column numbers. pointer to ith row p[i]=&p[i] value of p+ ith row and first element is *(p+i) = p[i][0] pointer to the jth element in the ith row is *(p+i)+j = p[i][j] data-type int a[2][6]; (*array name)[column size]; int (*a)[6];

Pointers And Functions


Pointer has deep relationship with function. We can pass pointer to the function and also pointer addresses can be passed to the function as pointer arguments. This can be represented by two ways as defined below: a) Pointer as function argument b) Pointer to function A) Pointer as function argument: Pointer as function argument occurs with its address cell i.e, in this function is called by using address of the variable. It is also called call by reference. In call by reference the addresses of the actual argument are passed to the function. The call of function by sending its value as argument in the function calling statement is called call by value. In call by value
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

concept, the names of the actual argument are used in the function call. So call by value of the actual argument passed to the function. Best explanation of call by value and call by reference can be done by taking the example of swapping the numeric data.. Swapping: Swapping is part of pointer as function argument. Swapping is the exchange of value from one variable to another. We can say, if we take two variables x, y and we can exchange value of x into y and y into x, then it is the process of swapping (or exchanging). We explain the process of swapping by the following program: Call By Value: /*Program to illustrate the concept of call by value (argument passing without pointer)*/ main () { int a,b; a = 5; b = 10; printf(\n Value of a = %d and Value of b = %d before swap, a, c); swap (a, b); printf(\n Value of a = %d and Value of b = %d after swap, a, b); getch(); } swap (int x, int y) { int temp; temp = x; x = y; y = temp; return; } The output is: Value of a = 5 and Value of b = 10 before swap Value of a = 5 and Value of b = 10 after swap

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Call By Reference: main () { int a, b *aa, *bb; a =5; b = 10; aa = &a; bb = &b; printf(\n Value of a = %d and Value of b =%d before swap, a, b); swap(aa,bb); printf(\n Value of a = %d and Value of b =%d after swap, a, b); getch(); } Swap( int *x, int *y) { int *temp; temp = x; x = y; y = temp; return; } The output is: Value of a = 5 and Value of b = 10 before swap Value of a = 10 and Value of b = 5 after swap

B) Pointer to function: A function works like a variable. It has also similar dat type declaration like the other variable. Pointer to a function variable occurs when you define a while function as pointer by using * . As function has address location in memory the syntax used to declare pointer to function is as: data-type (*function name)() ; type (*fptr)( );

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

where type is the data type which may ne integer, float or character, fptr () is function name and * (fptr) () is pointer to function. For example, the procedure to illustrate this concept is as follows: int mul ( ). (*p1) ( ): p1 = & mul ; By using the above example, we can say that mul ( ) is a simple function. But p1 is pointer variable which work as pointer to function and in second statement the address of the mul ( ) function is assigned to p1 variable which is the memory variable and you know memory variables or pointer. So p1 ( ) pointer function is linked to the function mul. main( ) { int mul ( ). (*p1)( ), x,y ,z; scanf (% d % d, & x, & y); p1 = &mul ; z =*p1 (x, y); printf ( % d,z); getche( ); } mul (int p, int q) { int m ; m = p * q; return (m); }

POINTERS AND STRINGS:


Pointers plays an important role in inputting, outputting and processing the strings and line of text. Pointers linked with array with two ways: i) Array of pointers to strings ii) Passing array of strings to function iii) An array of pointer to string can be declared as in the case of basic type variables. This can be declared in terms of variable and constants. Two-dimensional arrays in terms of pointer *t[20] and a[20][30] are defined. This can be represented as: Static char *t[20], a[20][30]; Unsigned int i; For(i=0;i<30;i++) { t[i]=a[i];
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

} This procedure completely states the definition of array of pointers to the strings. As the variable a is itself an array of type char, so an array of pointers to strings t is an array of pointer to char. For example, below is a c program illustrate the concept of array of pointer to string as:

1. Program to print the string cities and their addresses using array of pointer to the string concept main() { char *city*+ = ,krest,ameerpet,dilsukhnager}; int i; for(i =0; i<3; i++) { printf(\n%scity has address %u,*(city+1),(city+1); } getche(); } The output is: Krest city has address 1200 ameerpet city has address 1202 dilsukhnagar city has addresss 1204 ii) Array of strings can be passed to a function as arguments. Note that formal parameter can be declared as a pointer to a pointer to type character. This can be done by representing as: char** v where v is of character type variable. For example, write a c program to illustrate the concept of passing array of strings to a function as: Void main() { Char *city*3+ = ,Krest,Ameerpet,Dilsukhnagar}; Void pass (char **,int); Pass (city, 3); getche(); }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Void pass (char **m, int n) { int i; for (I =0; i<n; i++) { printf(\n %s,m*1+); } return; } The output is: Krest Ammerpet dilsukhnagar

Dynamic memory allocation:

As pointer variable are used to hold the address of a variable, Which is declared with the pointer variable during the variable declaration statement. So assigning of the address of variables to a pointer variable is one of the legal way of the initialization of pointer. This process is always dynamic. In the dynamic memory allocation of the memory, the declaration of variable is no longer required. The job of memory allocation can be done by using the three functions. These are malloc(), calloc() and realloc(). malloc(): The general syntax used for the declaration of this function is as follows: Void *malloc(unsigned size); It allocates a block of size bytes of consuctive memor7y from the heap of memory. Ot returns starting address of the allocated bytes. Note that if sufficient memory is not available, then it

returns the Null value. A cast operator is used to cast the memory allocation pointer to the desired type as the returned pointer to the allocated memory is of generic type.
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

calloc(): The general syntax used is as: Void *calloc(xyz, unsigned size); The calloc() function provides dynamic allocation of block of consecutive memory locations of size to xyz from trhe heap of the memory. It return a pointer to the newly allocated block. If enough memory is not available, then NULL value be returned. Like in the case of of malloc function the memory allocation pointer can be cast to the proper type.

realloc(): The general syntax used for this function is as: Void *realloc(void *block, unsigned size); It adjust the preciously allocated block to the size bytes. I will allocate the increasing or decreasing the block to the new size. Here block refers to the original block already allocated by malloc() or calloc() function. It returns the address of the new reallocated block. Note that if it will unable to reallocate the block , then it will display and return the Null value. For example, the concept of allocation of dynamic memory can be explained by using the c program as below:

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

STRUCTURE :We know different data type of variables can be used for different purpose. C support such type of constructed data type, which is called Structure. A structure having different type of data types in a single structure. A structure is Heterogeneous collection of related fields. Here fields are structured member elements. A field has different data type. For example, book having title, author, price, pages<< Struct library { Char Int }; Sysntax:- 1. Struct structre-tag { Datatype1 Datatype2 Datatype3 -------------Datatype n }; Main() { Structre-tag v1,v2<.vn; Local variable declaration; Executable statements; } 2. struct structre-tag { datatype1 element1; / / list of variables of stucture tag. element1; element2; element3; -----------element n; title[20]; pages; Char author[20]; Float price;

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

datatype2 datatype3 -------------datatype n } v1,v2<.vn; main() {

element2; element3; -----------element n;

executable statements; } 3. main() { Struct structure-tag { datatype1 datatype2 datatype3 -------------datatype n } Struct Structre-tag v1,v2<.vn; } Ex:- 1. Struct library { Char Int }; main() { Struct library book1,book2,book3; }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

element1; element2; element3; -----------element n;

title[20]; pages;

Char author[20]; Float price;

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

2. Struct library { Char Int title[20]; pages; Char author[20]; Float price; } book1,book2,book3; main() { Local variables; Executable statements; } 3. Main() { Struct library { Char Int }; Struct library book1,book2,book3; } Assigning values to structure variables:We are assigning values to the variables using member operator . which is called dot or period operator. This is creates a relationship between the variablename and structure elelment of the structure tag. Ex:Struct library { Char Int title[20]; pages; Char author[20]; title[20]; pages; Char author[20]; Float price;

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Float price; } book1; main() { printlf(enter the book details); scanf(%s%s%d%f,&book1.title,&book1.author,&book1.pages,&book1.price); printf(\n the book title is = %s,book1.title); printf(\n the book title is = %s,book1.author); printf(\n the book pages are = %d,book1.pages); printf(\n the book price is = %f,book1.price); getch(); } Strucuter and array:Array play very important role with sturcture.Sturcture has two types of view with the array. 1.Array of Sturcture 2.Array within Sturcture 1. Array of Structure : An array structure 1D &2D are Declared by only changing the structure variables from fundamental type to an array type with no change in the structure declaration. For 1D: Sturct sturct-tag variable name[row size]; For 2D: Sturct sturct-tag variable name[row size][column size]; For example Main() { Sturct { int student1; int student2; int student3; }; Int I,n,total; Float av; Struct student st[20]; Printf( \ n Enter the number of Students:);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

student

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Scanf(%d,&n); For(i=0, i<n; i++) { Printf( \ n Enter marks of three subjects of %d the student:i+1); total=0; scnaf(%d %d %d, &s[i].subject1, &st[i]. subject2, &st[i]. subject3); total = st[i].subject1+st[i].subject2+st[i].subject3; av = total / 3 ; printf(\n AVERAGE Marks of %d the student is =% f i+1,av); } getche(); } The Output be: Enter the number of students 10 Enter marks if three subjects 1 the student: 20 35 45 AVERAGE Marks of 1 the student is =33.33 Enter marks of three subjects 2 the student: 10 30 20 AVERAGE Marks of 2 the student is =20.00 Enter marks of three subjects 10 th student: 22 38 30 AVERAGE Marks of 10 th student is = 30.00 2.Array within Sturcture : Array are declared within the structure. 1D :Struct struct-tag { data type1 element1; data type2 element[size];
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

}variable list; 2D ;Struct structre-tag { data type1 element1; data type2 element2[row][column]; }

Structure with in structure: A structure can be embedded within another structure. In other words when a structure is declared and processed with in another structure, then it is called Nesting of structure or structure within structure. Sturct tag 1 { element-1; -------------------etruct tag 2 { element-1; <<<<<. element n; }v1; element-m; }v2; EX:Struct student { int rollno; char name[20]; char address[30]; int phno; float fees; struct exam { float tel; float eng; float compu;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

float tot; float avg; }ex[10]; Char dateofbirth[8]; }st[10]; The inner structure element can be linked with the outer structure variable by using the structure dot operator. This can be represented as: st[i].ex[j].tel; Structure and Function:Structure can be implemented with the function easily. For this process, passing of arguments takes place similar to the array and variable. This relationship of structure with the function can be viewed from three angle as; i) ii) iii) Passing structure to a function Function returning structure Passing array of structure to function.

1.Passing structure to a function:Similar to passing of array or variable, structure can be passed to the function as argument. Syn:type specifier function-name (structure-variable); // as actual arguments type specifier function-name (structure variable) // as formal arguments struct tagname structurevariable; { ----------- } main() { Struct tagname { datatype1 ele1; --------------------------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

datatype-n ele n; }v1; ------------------------------------------function name(v1); ------------------getch(); } Type function name(v2) Struct tag-name v2; { -----------} 2.Function Returning Structure: All the values can be computed in the function programs and return a combined value of whole structure back. This can be explained with a simple problem to compute the net pay of an employee having structure as:

Main() { Sturct employee { char name [20]; float bp; float da; float hra; float netpay; }emp; Sturct emp npay(emp y); Printf(\n ENTER THE NAME, BASIC PAY,DA,HRA:); Scanf(\n%s%f%f%f, emp.name, &emp.bp, &emp.ba, &emp.hra); Y = npay(y); Printf(\n Net pay is :%f,y);

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Getche(); } emp npay(emp Z) { Z =Z.bp +Z.hra; Return(Z); }

3.Passing array of Structure to function : A complete complicated passing of an array of structure to the function is a difficult job, but the passing of array becomes very easy by using the passing of array as an individual element. For example, to compute the average marks of every student (there are five students in the class ), each having three subjects can be done by using the C program as below: Main() { Res m[100]; int n,I; printf(\n How many students in the class:); scanf(\n %d,&n); printf(\n Enter the data:); for( i=0; i < n; i++) { M[i] =input (m,n); } Display(m,n); getch(); } res input (res m[],int i) { int j; float tm =0; printf (\n Enter Rollno andf marks :); scanf(\n %d, &m*i+.rn); for (j =0;j<3;j++) {
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Scanf(\n%d%*c,&m*i+.sub*j+); Tm =tm +m[i].sub[j]; } M[i].av =tm/3; J =0; Return m[i]; } Void display (res m[],int n) { int j,k; printf(\n RESULT IS:); printf(\n ROLLNO 1 2 3 Average of Marks); for(j =0; j<n; j++) { Printf(%5d,m*j+.sub*k+); Printf(%5d,m*j+.av); } } return; }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Structure And Pointer :Structure can be directly handled by its address by using the pointer with structure. we use pointer with structure through structure variable. Array of structure variable directly stands for the address of zero th element. Struct tag { Type1 element1; <<<<<<<.. Type n element n; }v, *p; Where v is structure variable and p is the pointer variable. Note that structure variable is a pointer and there are no pointer variable work as structure element. Also the structure variable is an array of pointer variable pointing to a structure. Sturct employee { char name [20]; float bp; float da; float hra; }emp,*p; Also the declaration of pointer variable p can be initialized as P=&emp; You can access the individual element by using, (*p).bp; (*p).name; (*p).da; (*p).hra; Dont use like, *p.bp because user cant access the element directly with the pointer initialization, so always avoid to use pointer with structure without the use of parenthesis. To solve such type of problem, user can use the arrow operator(). pbp; pname; pda; <..
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

<..

Ex:-

main() { Struct student { Int rn; Float fees; }st[10],*p; int n,j; printf(enter the no of students); scanf(%d,&n); printf(\n enter the data); for(p=st;p<st+n;p++) { Scanf(%d\t%f, &prn,&pfees); } p=st; printf(entered dat is); while(p<st+n) { printf(%d\t%d,prn,pfees); p++; } getch(); }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

UNION:A union is a variable that declares a set of multiple variables sharing the same memory area. The compiler allocates sufficient memory to hold the largest variable of the union. All the members of union occupy the same memory locations called addresses. The old value of the already declared member will be destroyed and new value to the new member will be assigned in the memory by using the union concept. Note :- union declaration has mote then one variable declaration, but only one can be used at a time. Also with the declaration of union, the turbo-c compiler will automatically allocates the memory locations to the variable which has the long data type member element I the union. Sys:Union union-tag { Data-type 1 member-element 1; Data-type 2 member-element 2; <<<<<<<<<<<<<<<<<<.. Data-type n member-element n; }v1,v2<.vn; Note:- here union tag is optional Sys2:Union { Data-type 1 member-element 1; Data-type 2 member-element 2; <<<<<<<<<<<<<<<<<<.. Data-type n member-element n; }v1,v2<.vn;

Sys3:Union union-tag { Data-type 1 member-element 1; Data-type 2 member-element 2; <<<<<<<<<<<<<<<<<<.. Data-type n member-element n; };


-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Data-type

union

union-tag v1,v2<.vn;

Note:- here data-type is optional

Ex:Union student { char a; int b; float c; double d; } 1000 K a b C d Here double can takes 8 bytes so union taking maximum size is 8 bytes only. 30 75.50 1008 234.23332222

Note:- but in a structure it can take how many data types are declared that much memory is taking. 1000 K a 2001 30 b 2003 75.50 c 2007 234.23332222 d 2015

here char 1byte, int 2 bytes, float 4 bytes, double 8 bytes total=15 bytes main() { union student {
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

char grade; int rollno; float marks; double fees; }u; printf(\enter the the grade rollno and marks and fee); scanf(%c%d%f%g,&u.grade,&u.rollno,&u.marks,&u.fees); printf(the grade is:%c ,u.grade); printf(the rollno is :%d,u.rollno); printf(marks are %f,u.marks); printf(fees %g,u.fees); getch(); } Structure and union:A union can be used within the structure. Struct student { int rn; Char name[20]; Union subject { Char grade; int marks; float percenatage; }u; }s;

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

FILES:A file is a collection of related data structure pertaining to a single entity. A file having payroll data processes the information of pay elements and not about the personal and educational information and a single entity. In c the Stream word is used in place of a file. A stream may be device like printer or monitor or a file containing a stream of bytes. So stream is finite continuous flow of bytes that go either as an out put to a file or come as an input from the file. Files

Program files

data files

System IO files

standard IO files

Unformatted files

text files

Char IO

string IO

record IO

Standard IO is the easier and popular way for handling and processing the files. Standard IO is also called stream IO files. 1) Text mode :- all the text streams consists of lines of characters and each line is terminated by a combination of carriage return(CR) and line feed(LF), when enter key is presses. It is the pair, which is translated into a single newline character (\n) as input and the new line is translated back to a combination of two characters carriage return and linefeed. 2) Binary mode:- no such types of translation take place. All characters are processed without change. Binary files store floating point data more accurately and stored in compact way as compared to the text files. In binary mode characters are stored and processed as in the main memory.

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

FILE OPERATIONS:
The processing of files has number of operation according to the users requirement and the problem definition. But some of the commonly used file operations are as follows: (i) (ii) (iii) (iv) (v) (vi) Naming a file Opening a file Reading data from a file Writing data to a file or creation of data file Closing a file Updating a file

Some of the file operations be discussed as below: (i) Naming a file: File name (data file name ) should not be more than eight characters and three characters for extension. File name can be defined and enclosed by using the fopen() high-level I/O function. The general syntax is as follows: fopen (filename, mode); For example, student.dat in the fopen() function as: fopen(student.dat, mode); To define a file three things must be required. These are: (i) (ii) (iii) Filename Data structure or creating the buffer area for files Purpose As we know the file name should not be more than eight characters as discussed above in naming the files. Data structure used is file in the file definition and should be in the upper case letters. This contains nine structure members. These nine are given information on status of file a stream covering the fill or empty level of buffer, file handler, buffer size, data transfer buffer, temporary file indicator, creation of buffer, buffer indicator, storage in the buffer and the status flag. This is also the way to creating buffer area. It is the area were information or data is temporary stored while being transferred between the computers memory and the data file. The buffer area can be established as: FILE *fn;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

is a data file name, Which is defined and enclosed

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Where fn is the pointer variable that indicates the beginning of the buffer area. File is a data structure, which creates the buffer area.

There are three purpose to define and open file according to their modes. We will discuss these modes in the next operation (opening a file). These are writing data into existing file or new file (It is also called creation of data files), reading data from the data file and appending (inserting data into file) data file. (ii) Opening a file: A file is opened by using fopen () in-built high-level input/output function. The general format for declaring and opening a file is as: FILE *fp; fp = fopen (filename, mode);

Where FILE is the data structure and is the low level reserve word of C-Language for the creating a buffer area as discussed already. Here fp is the file pointer tells us beginning of the buffer area for storing the data, filename is the name of the data file having less than eight characters, mode is the way file is opening and there are three modes in opening the files. These are as follows: 1. 2. 3. w r a Open the file for writing only Open the file for reading only Open the file for appending only.

Note that when you use w mode, new file will be created. If the file is already created, then it will overwrite the data. For example, some valid file opening statements are as: FILE *p1,*p2,*p3; /* file opened for reading the data from Raja file */

P1 = fopen(Raja, r);

p2 = fopen (Sunny, w); /* file opened for writing the data to Sunny file */ p3 = fopen (MONA, a); data file*/ /*file opened to insert more data to MONA already created

Note that some compiler use r+, w+ and a+ mode, which have meaning as: r+ both for reading and writing w+ both for reading and writing
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

a+ both for reading and appending Also there are three more modes related with the binary mode files: rb reading the binary files wb writing the binary files ab appending the binary files Some more modes linked with binary files are r+b, w+b, a+b. Closing a file: A file, which is opened in any mode, should be closed. For this purpose fclose () high-level input/output function is used. The general syntax is as: Fclose (file-pointer); For example, FILE *f1; f1 = fopen(Raj, w); fclose(f1) Input/Output statements used in file handling: Files have different Input/Output statements used for different purposes. These are used to put data from variable to data file. The various Input/Output functions used with standard I/O are discussed as below: a) Character Input/Output (standard I/O) b) String Input/Output (standard I/O) c) Formatted Input/Output (standard I/O) d) Record (block) input/Output (standard I/O) a). Character Input/Output functions: Now there need to be handle character data by using the following high-level input/output functions. There are mainly four character I/O used which are discussed as: 1. putc() 2. getc() 3. putw() 4. getw() 1. putc(): This function is used to put or write a single character in the data file pressed from the keyboard. The general syntax used is as: putc (v, file-pointer); (iii)

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Here putc() is the high level character I/O function used in the C-Language, v is variable name of character type. Note that when you use putc (), file should be open in w mode. It means a new file will be created or it overwrites the data to an existing created file. For example, below written C program explain this concept: /* program to illustrate the concept of writing a single character to the data file*/ main() { FILE *f1; F1 = fopen (Raj, w); char x; printf (\n Enter any key from keyboard:); x = getchar (); putc (x,f1); fclose (f1); getche (); } The Output be: Enter any key from keyboard: M 2. getc (): The purpose of getc() function is to read a single character from the existing data file. The general syntax used for this purpose is as: V = getc(fp); Where fp is the file pointer, v is the character type variable and getc() is the highlevel input/output function used for reading the character data. Note that the file should be opened in the read mode only also file should be already created. Also note that data be stored in the file before reading, otherwise it shows the empty space. For example, suppose if you want to read a single character from the data file, the C-Program procedure is as follows: /* Program to illustrate the concept of reading a single character from the file*/ main() { FILE *f1; f1 = fopen (Raj, r); char ab; ab = getc (f1); printf (\n The data stored in the file is :); printf (%c, ab);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

getche (); } The Output be: The data stored in the file is : M 3. putw (): This function is used to put or write a single integer digit in the data file pressed from the keyboard. Note that when you use putw() function, file should be opened in the w mode. The general syntax used for this statement is as: putw (v, filepointer); Here v is the integer variable name. For example, write a C program to explain this concept: /* Program to illustrate the concept of writing a single digit to the data file*/ main() { FILE *f1; f1 = fopen (ABC,w); int x; printf (\n press any numeric key from the keypad :); scanf (%d, &x); putw (x,f1); fclose (f1); getche(); } The Output be: Press any numeric key from the keypad: 5 4. getw (): The purpose of getw () is to read a single integer value from the data file. The general syntax is as: v = getw (f1); Here v is the integer variable and f1 is the file pointer, getw() is the high level input/output function. For example, suppose if you want to read a single integer digit from the data file, then the C procedure is as: /*Program to illustrate the concept of reading a single digit from data file */ main() { FILE *f1; f1 = fopen(Raj, r); int ab; ab = getw (f1); printf (\n Data stored in file is: %d ab); fclose (f1);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

getche (); } The Output be: Data stored in the file is: 5 b) String Input/Output: Strings are the major element of the character data, which can be handled by the files. It is very easy to handle group of characters instead if individual character using the file operations. There are mainly two types of string I/O functions used. These are as follows: 1. fputs() 2. fgets() 1. fputs(): The purpose of this file is to write a string to the opened file in write mode. The general syntax used is as follows: fputs (ab,fp); Where fp is the file pointer and ab is a pointer, which points to an array of characyers. For example, below written are the valid statements for the proper use of fputs (). FILE *fp; char ab[20]; fp = fopen(Mona, w); fputs(ab,fp); program to illustrate the concept of writing the string by using the fputs() main() { FILE *f; char xy[50]; printf(\n Enter any string from the keyboard :); gets(xy); f = fopen(Mona, w); fputs(xy,f); fclose(f); getche(); } The 0utput be: Enter any string from the keyboard: Raj Kumar 2. fgets(): The purpose of this file is to read a string from the opened file in read mode. The general syntax used is as follows: fgets (ab, n, fp);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

where fp is the file pointer, ab is a pointer, which points to an array of characters and n is the length of the array (size of the array). For example main() { FILE *f; char xy[50],ab[50]; int i =0 printf(\n Enter any string from the keyboard :); gets(xy); f = fopen(Mona, w); fputs(xy, f); fclose(f); f = fopen(Mona,r); printf(\n Entered tring is :\n); if(fgets(ab,50,fp) !=\0) { While(ab*i+ !=\0) { Putchar(ab[i]); i++; } } fcloser(f); getche(); }

The Output be: Enter any string from the keyboard: Raj Kumar Entered string is : Raj Kumar c) Formatted Input/Output: We have discussed here character I/O and string I/O function to enter single character and group of character data. Now question arises how can you handle mixed data. i.e. data of integer, float, double and character type. The answer is the formatted Input/Output functions. There are mainly two formatted I/O functions are used, which are discussed as below: 1. fscanf () 2. fprintf () 1.fprintf():

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

The function fprintf() is to write mixed data type in the data file. The nixed data type are of integer, float, double, string and character type. The general syntax used for this purpose is as follows: Where fp is the file pointer, control string has all format codes or character conversion codes (like % d for integer, %f for float value, % s for the string data, % c for character only etc.). Which are used for the output statement. Here v1,v2<..vn is the list of variable. Note that the file should be opened in the write mode. For example Program to illustrate the concept of to write mixed data by using fprintf()*/ main() { FILE *f1; f1 = fopen(Raj,w); int rn; float fees; char name[20]; printf(\n enter the roll no fees and name of the studet); scanf(%d%f%s,&rn,&fees,&name); fprintf(f1,%d%f%s,rn,fees,name); fclose(); getch(); } 2.fscanf() : The purpose of this function is to read mixed data from the data file. Note that file should be opened in the read mode only. fscanf(fp,control string, &v1, &v2,<..&vn); Where fp is the file pointer and control strings are %d for the int, %f for the float, %s for the string, %c for the char data. V1,v2 <vn are all the variables. Also & is used to read data at the address of the memory cell. main() { FILE *fp; f1=fopen(Raj,w); int a; float b;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

char name[20]; fscanf(f1, %d%f%s, &a,&b,&name); printf(\n the entered data is ); fprintf(%d%f%s,a,b,name); fclose(f1); getch(); }

d)Record Input/Output :The purpose of this function is to read and write the data at once i.e used to read or write the entire block using the permanent disk storage device. Every block has some fixed and work similar to an array or a structure. This is mainly used t write the numeric data. To the disk file in the binary format. For this purpose mainly two record input/ouput functions are used. 1) fwrite() 1) fwrite():Is to write group of record to a data file on the secondary storage device disk. fwrite(p,n,s,fp); where p is an address of an array or structure, n is the size of array s is the number of array declare for writing the data fp is a file pointer. File should be opened in binary write mode. Ex:main() { Struct student { int rn; char name[20]; float fees; }s[20]; FILE * fp; Int i,j,n; fp= fopen(KREST, w); printf(\n how many record you want to store in the disk); scanf(\n %d,&n); for(i=0;i<n;i++) {
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

2) fread()

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Printf(\n enter the student details); Scanf(%d%f%s,&student[i].rn, &student[i].fee, &student[i].name); for(j=0; j<n; j++) { fwrite(&student, sizeof(student), 1, fp); } } fclose(fp); getch(); } 2.fread():Is to read group of record from the existing created data file stored on the secondary storage device disk. fread(p,n,s,fp); where p is an address of an array or structure, n is the size of array s is the number of array declare for writing the data fp is a file pointer. main() { Struct student { int rn; char name[20]; float fees; }s[20]; FILE * fp; Int i,n=0; fp= fopen(KREST, w); while(! feof(fp)) { fread(&student,sizeof(student),1,fp); n++; } for(i=0;i<n;i++) { Printf(\n enter the student details); Scanf(%d%f%s,&student*i+.rn, &student*i+.fee, &student[i].name); }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

fclose(fp); getch(); } Randomly Accessing Data From File:It is very ease to access the data sequentially from the beginning of file to the end of the file. But some time problem arises to access the data from the middle or from the particular location directly, then to solve this problem direct or random accessing is used. To randomly or directly accessing the data from the opened file, three major functions are used. 1. ftell() 2. fseek() 3. rewind() 1) ftell():- it takes a file pointer and returns a number of long type that corresponds to the current posion. This function is useful in saving the current position of a file. Which can be used later is the program. n=ftell(fp); where n is the int variable, fp is file pointer this means n bytes have already been read or written. 2) fseek():- this function is used to move the file position to a declared location within the file. fseek(filepointers, offset, position) where file pointer is the pointer to the file declaration, offset is a number or variable of long and position is an integer number. fseek(fp, 0l, 0) It means it goto the beginning of the file. It is similar to the rewind a file. fseek(fp, m, 0) it means pointer moves the m+1 th byte in the file. fseek(fp, m, 1) it means pointer goes forward by m bytes. 3) rewind():- this function takes a file pointer and resets the position to the start of the file. rewind(fp); fp is the pointer of current opend file. Error Handling in file I/O operations:there are no.of problems or errors occurs in the processing of the file operations. Error will occurs during the processing in the different situations. 1. If you try to read a file having no data in it, then an error will occur. 2. If in any case device errors flow will file. 3. If you try to open an unopened file. 4. If you try to open the file in another mode which is not described.
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

5. If you try to open the file having an invalid name. and password or physically floppy disk be write protected. Some functions are used to handle any error occurs during the execution of the files. feof(), ferror(), fp()< the function feof() is used to test test the condition, whether a file has at end or not. It stores a file pointer as argument and returns a non zero int value, if all the data from the specified file had been read and return the zero value in the other case. Ex: Program On Random Operations main() { FILE *fp; long b; int s; char name[20]; if((fp=fopen(MONA, r))==\0) { ferror(\n file does not exists); exit(0); } s=fseek(fp,0L, 2); if(s!=0) { fprintf(stderr, %s not be positioned at the EOF,fp); fclose(fp); exit(-1); } b=ftell(fp); while(b>0) { S=fseek(fp, b-16L, 0) If(s!=0) { fprintf(stderr,%s file not positioned for reading , fp); fclose(fp); exit(-1); } fscanf(fp,%s, name); b= ftell(fp); printf(%s, name); b=b-strlen(name)-1; } rewind(fp); getch(); }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

DATA STRUCTURES

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

DATA STRUCTURES
Stack:A stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack to be removed). The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state can also occur in stack top operation if stack is empty. A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are those that have been on the stack the longest.

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Stack using with an array #define max 20 #define true 1 #define false 0 Struct stack { int s[max]; Int top; }; typedef struct stack stack; int isempty(stack *a) { If(a->top==-1) return 1; else return 0; } int isfull(stack *a) { If(a->top==max -1) return 1; else return 0; }

Void push(stack *a, int ch) { If( isfull(a)) { Puts(stack is overflow); Return; } else { a->top = a-> top+1; a->s[a->top] = ch; } }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

int pop(stack *a) { if (isempty(a)) { Puts(stack is underflow); return -1; } else { return a->s[a->top--]; } int stacktop(stack *a) { If(isempty(a)) { Puts(stack is underflow): } else { return a->s[a->top]; } } Void main( ) { Stack s; Int n,ch; s.top = -1; do { puts(1. Push); puts(\n2. Pop); puts(\n3. exit); puts( enter your choice); scanf(%d, &ch); switch(ch) { Case 1: puts(enter the element you want to push); Scanf(%d, &n); Puts(&s, n); break; case 2: n = pop(&s); if( n != -1) printf( the element poped is %d, n); break;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

} } while(ch!=3); } Stack using linked list #include <stdio.h> #include<malloc.h> Struct node { Int data; Struct node *next; }; typedef struct node node; void push(node **s, int n) { node *temp; temp = (node *) malloc(sizeof(node)); temp -> data = n; if(*s == NULL) { *s = temp; temp-> next = NULL; } else { temp -> next = *s; *s= temp; } } Int pop(node **s) { int n; node *temp; if(*s==NULL) { Puts(\n stack underflow); return -1; } else { temp= *s; n=temp->data; *s = (*s)->next;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

free(temp); return n; } } Void main( ) { node *top =NULL; int ch,n; do { printf( \n Menu); printf(\n 1. Push); printf(\n 2. Pop); printf(\n 3. Exit); switch(ch) { Case 1: printf(\enter the elements to push); scanf(%d, &n); push(&top, n); break; case 2: n=pop(&top); if( n!= -1) printf(\n the element poped is %d, n); break; } } while( ch!=3); }

Stack Program 2: #include <stdio.h> #include <stdlib.h> #define SIZE 50 void push(int i); int pop(void); int *top, *p1, stack[SIZE]; int main(void) { int value; top = stack; /* tos points to the top of stack */ p1 = stack; /* initialize p1 */

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

do { printf("Enter value: "); scanf("%d", &value); if(value != 0) push(value); else printf("value on top is %d\n", pop()); } while(value != -1); return 0; } void push(int i) { p1++; if(p1 == (top+SIZE)) { printf("Stack Overflow.\n"); exit(1); } *p1 = i; } int pop(void) { if(p1 == top) { printf("Stack Underflow.\n"); exit(1); } p1--; return *(p1+1); } Program for Stack implementation through Array #include <stdio.h> #include<ctype.h> # define MAXSIZE 200
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

int stack[MAXSIZE]; int top; { void push(int); int pop(); int will=1,i,num; clrscr(); while(will ==1) { printf("MAIN MENU: 1.Add element to stack scanf("%d",&will); switch(will) { case 1: printf("Enter the data... "); scanf("%d",&num); push(num); break; case 2: i=pop(); printf("Value returned from pop function is %d ",i); break; default: printf("Invalid Choice . "); } printf(" Do you want to do more operations on Stack ( 1 for yes, any other key to exit) "); scanf("%d" , &will); } } void push(int y) { if(top>MAXSIZE) { printf("STACK FULL"); return; } else { top++;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

//index pointing to the top of stack

void main()

2.Delete element from the stack");

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

stack[top]=y; } } int pop() { int a; if(top<=0) { printf(" STACK EMPTY"); return 0; } else { a=stack[top]; top--; } return(a); }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

QUEUE:A Queue is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure. Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer. Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes. Common implementations are circular buffers and linked lists. #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <time.h> struct queue_node { struct queue_node *next; int data; }; struct queue { struct queue_node *first; struct queue_node *last; }; int enqueue(struct queue *q, const int value) { struct queue_node *node = (struct queue_node *)malloc(sizeof(struct queue_node)); if (node == NULL) { errno = ENOMEM; return 1; } node->data = value; if (q->first == NULL)
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

q->first = q->last = node; else { q->last->next = node; q->last = node; } node->next = NULL; return 0; } int dequeue(struct queue *q, int *value) { if (!q->first) { *value = 0; return 1; } *value = q->first->data; struct queue_node *tmp = q->first; if (q->first == q->last) q->first = q->last = NULL; else q->first = q->first->next; free(tmp); return 0; } void init_queue(struct queue *q) { q->first = q->last = NULL; } int queue_empty_p(const struct queue *q) { return q->first == NULL; } int main(void) { struct queue Q; init_queue(&Q); srand(time(NULL)); unsigned int k = 0;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

while (k < 100) { enqueue(&Q, (rand() % 100) + 1); /* Fill with random numbers from 1 to 100 */ ++k; } k = 1; while (!queue_empty_p(&Q)) { int data; dequeue(&Q, &data); printf("(%d) %d\n", k, data); ++k; } putchar('\n'); system("pause"); return 0; }

DOUBLE-ENDED QUEUE:
A Double-Ended Queue (dequeue, often abbreviated to deque, pronounced deck) is an abstract data structure that implements a queue for which elements can only be added to or removed from the front (head) or back (tail) A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams. An example that could possibly use an overwriting circular buffer is with multimedia. If the buffer is used as the bounded buffer in the producer-consumer problem then it is probably desired for the producer (e.g., an audio generator) to overwrite old data if the consumer (e.g., the sound card) is unable to momentarily keep up. Another example is the digital waveguide synthesis method which uses circular buffers to efficiently simulate the sound of vibrating strings or wind instruments. The "prized" attribute of a circular buffer is that it does not need to have its elements shuffled around when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well suited as a FIFO buffer while a standard, non-circular buffer is well suited as a LIFO buffer. Circular buffering makes a good implementation strategy for a Queue that has fixed maximum size. Should a maximum size be adopted for a queue, then a circular buffer is a completely ideal implementation; all queue operations are constant time. However, expanding a circular buffer
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

requires shifting memory, which is comparatively costly. For arbitrarily expanding queues, a Linked list approach may be preferred instead. How it works A circular buffer first starts empty and of some predefined length. For example, this is a 7element buffer:

Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):

Then assume that two more elements are added 2 & 3 which get appended after the 1:

If two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 & 2 leaving the buffer with just a 3:

If the buffer has 7 elements then it is completely full:

A consequence of the circular buffer is that when it is full and a subsequent write is performed, then it starts overwriting the oldest data. In this case, two more elements A & B are added and they overwrite the 3 & 4:

Alternatively, the routines that manage the buffer could prevent overwriting the data and return an error or raise an exception. Whether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular buffer. Finally, if two elements are now removed then what would be returned is not 3 & 4 but 5 & 6 because A & B overwrote the 3 & the 4 yielding the buffer with:
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Program For Implementation Of Queues Using Arrays #include<stdio.h> #define max 10 struct queue { int q[max]; int front,rear; }; typedef struct queue queue; void insert (queue *a,int n) { if (a->rear == max -1) { puts(queue overflow); } else { a->rear = a->rear+1; a->q[a->rear]=n; if(a->front==-1) a->front=0; } } int delete (queue *a) { if (a->front==-1) { puts (queue underflow); return -1; } else { int n; n=a->q[a->front]; if (a->front=a->rear) { a->front=a->rear=-1; } else {
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

a->front = a->rear+1; } return n; } } void main() { queue q; int n,ch; q.front=q.rear=-1; do { puts(1-insert); puts(2-delet); puts(3-exit); puts(enter your choice); scanf(%d,&ch); switch(ch) { case 1: puts (enter the element you want to insert); scanf (%d,&n); insert(&q,n); break; case 2: n=delete(&q); printf(the element deleted is %d,n); } } while(ch!=3); } Queues Using Linked Lists: #include<stdio.h> #include<alloc.h> struct node { int data; struct node *next; }; typedef struct node node; void insert (node **front,int n) {
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

node *temp, *p; temp=(node *) malloc (sizeof(node)); temp->data=n; temp->next=null; if (*front==null) { *front=temp; } else { p=*front; while(p->next!=null) p=p->next; p->next=temp; } } int delete(node **front) { node *t; int n; if (*front==null) { printf(\nqueue underflow); return -1; } else { t=*front; n=t->data; *front = (*front)->next; free(t); return n; } } void main() { node *front=null; int ch,n; do { printf(\nmenu); printf(\n1-insert); printf(\n2-delete);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

printf(\n3-exit); printf(\n enter your choice); scanf(%d,&ch); switch(ch) { case 1: printf(\nenter the element to insert); scanf(%d,&n); insert(&front,n); break; case 2: n=delete(&front); if(n!=-1) printf(\nthe elememt deleted is %d,n); break; } } while(ch!=3); } circular queues typedef struct queue { void **uffer; int head; int tail; int size; int inqueue; int nslot; int checksum; } queue; cq.h #ifndef_damnit_ #define_damnit_ #include <stdio.h> #include <stdiib.h> making, destroying and re-fixing queues extern void * qmake(int size); extern intqkill(void *);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

data operations extern int qadd(void *,void *); extern void *qget(void *); extern int qnum (void *); extern int qfree(void *); extern int isfull(void *); int validqueue(/* queue * */); #endif cq.c #include cq.h typedef struct queue { void **buffer; int head; int tail; int size; int inqueue; int nslots; int checksum; } queue; void *qmake(int size) { queue *cq; int i; if (i<0) { return null; } cq=(queue*)malloc(sizeof(queue)); if (cq==null) { return null; } cq->buffer=(void**)malloc((size+1)*sizeof(void*)); cq->head=0; cq->tail=0; cq->size=size; cq->nslots=size; cq->inqueue=0; cq->checksum=666;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

return cq; } int qkill(void *q) { int i; queue *queue=(queue*)q; if(vaildqueue(q)) { return -1; } free (queue->buffer); free(queue); return 0; } int qadd (void *q,void *item) { queue *queue=(queue*)q; int current; if(vaildqueue(q)) { return -1; } if (queue->nslots==0) { return 0; } queue->buffer[queue>head]=item; queue->head=(queue->head+1)%queue->size; queue->nslots--; queue->inqueue++; return 0; } void *qget|(void *q) { queue *queue=(queue*)q; void *current_item; if(validqueue(q)) { return null; } if (queue->inqueue==0)
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

{ return null; } current_item=queue->buffer[queue->tail]; queue->tail=(queue->tail+1)%queue->size; queue->nslots++; queue->inqueue--; return current_item; } int qnum(void *q) { queue *queue=(queue *)q; if (vaildqueue(q)) { return-1; } return queue->inqueue; } int isfull(void *q) { queue *queue=(queue*)q; if (vaildqueue(q)) { return -1; } if (queue->nslots==0) { return 1; } else { return 0; } } int qfree(void *q) { queue *queue=(queue*)q; if (vaildqueue(q)) { return -1; } return queue->nslots; } int validqueue(queue *queue)
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

{ if (queue==null) { return 0; } else if (queue->checksum!=666) { return 0; } else { return 1; } }

LINKED LIST:A linked list is a data structure used for collecting a sequence of objects, which allows efficient addition, removal and retrieval of elements from any position in the sequence. It is implemented as nodes, each of which contains a reference (i.e., a link) to the next and/or previous node in the sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list. Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data structures, including stacks, queues, associative arrays, and symbolic expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation. The principal benefit of a linked list over a conventional array is that the list elements can easily be added or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal. Linear and circular lists In the last node of a list, the link field often contains a null reference, a special value used to indicate the lack of further nodes. A less common convention is to make it point to the first node
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open or linear.

A circular linked list Singly, doubly, and multiply linked lists Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list.

A singly linked list whose nodes contain two fields: an integer value and a link to the next node In a doubly linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) and backwards, or next and prev(ious).

A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node The technique known as XOR-linking allows a doubly linked list to be implemented using a single link field in each node. However, this technique requires the ability to do bit operations on addresses, and therefore may not be available in some high-level languages. In a multiply linked list, each node contains two or more link fields, each field being used to connect the same set of data records in a different order (e.g., by name, by department, by date of birth, etc.). (While doubly linked lists can be seen as special cases of multiply linked list, the fact that the two orders are opposite to each other leads to simpler and more efficient algorithms, so they are usually treated as a separate case.) In the case of a doubly circular linked list, the only change that occurs is the end, or "tail" of the said list is linked back to the front, "head", of the list and vice versa.

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

LINKED LIST #include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }; typedef struct node node; node *create() { node *head=null, *ptr, *temp; int n; while (1) { printf(enter the data type 0 to stop); scanf(%d, &n); if(n==0) break; temp=(node *) malloc(sizepf(node)); temp-> data = n; if(head == null) head = temp; else ptr -> next = temp; ptr = temp; } temp -> next = temp; return head; } void { print(node *s) printf(the list is<.n); while( s!= null) { printf(%d, s->data); s=s -> next; } }

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

node *addatbeg(node *s) { node *temp; temp = (node *)malloc(sizeof(node)); printf(enter the data:); scanf(%d, & temp-> data);

if( s==null) { temp -> next = null; } else { temp -> next = s; } return temp; } node *deleteatbeg(node *s) { node *temp; if( s!= null) { temp = s; s = s->next; free(temp); } } void freelist(node *s) { node *temp; while(s!= null) { temp = s; s = s-> next; free(temp); } } int count(node *s) { int c= o;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

while ( s) { c++; s = s->next; } return c; } node * addatend(node *s) { node *temp, *p; temp = (node *)malloc(sizeof(node)); tem -> next = null; puts(\n enter the scanff(%d, &temp ->data); if(s==null) { s = temp; } else { p=s; while(p->next ! = null) p=p->next; p-> next = temp; } return s; } node *deleteatend(node *s) { node *p; if( s==null) return null; if( s->next==null) { free(s); return null; } p= s; while(p ->next-> !=null) p=p->next; free(p-> next); p->next=null;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

data

to

be

inserted):

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

return s; } node *deleteatpos(node *s, int pos) { node *temp, *p; int i; if(pos <1 || pos>xount(s)) { puts( invalid position); return s; } if(pos ==1) { s = deleteatbeg(s); return s; } for( i=1, p=s; i<pos-1; i++, p= p->next); temp = p_next; p->next=temp->next; free(temp); return s; } void main () { int ch, n; node *start; start= create(); do { printf(start); puts(\n 1. insert at beginning); puts(\n 2. insert at end); puts(\n 3. insert at position); puts(\n 4.delete at beginning); puts(\n 5. delete at end); puts(\n 6. delete at position ); puts(\n 7. count list); puts(\n8.exit); puts( enter the choice); scanf(%d, &ch);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

switch(ch) { case 1: start= addatbeg(start); break; case 2: start = addatend(start); break; case 3: printf(enter the position to be insert); scanf(%d, &n); start = insert(start, n); break; case 4: start = deleteatbeg(start); break; case 5: start = deleteatend(start); break; case 6: printf(enter the position to be insert); scanf(%d, &n); start = deleteatpos(start,n); break; case 7: printf(\n no of elements is %d, count(start)); break; case 8: freelist(start); break; default : printf(your choice is invalid); } } while(ch!=8) getch(); } circularly liked list #include<stdio.h> #include<conio.h> struct node { char data[30]; struct node *next; }; typedef struct node node; node *create() { node *start = null, *temp, *ptr; char d[30];
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

while(1) { printf(\n enter the data type end to stop); fflush(stdtin); gets(d); if(strcmp(d, end)==0) break; temp = (node *)malloc (sizeof(node)); strcpy(temp->data, d); if(start == null) start=temp; else ptr->next=temp; ptr = temp; } temp -> next = start; return start; } void print(node *s) { node *p=s; printf(\n the list is<..); do{ printf(%s, p ->data); } while(p!=s) } main() { node *head; head = create(); print(head); }

Double Linked List


#include<stdio.h> #include<conio.h>
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

struct node { struct node *left; int data struct node *right; }; typedef struct node node; node *create() { int n; node *start=null, *temp, *ptr; while(1) { printf(\n enter the data type 0 to stop); scanf(%d, &n); if(n==0) break; temp = (node *) malloc(sizeof(node)); temp -> data = n; temp ->left = temp -> right = null; if(start == null) start = temp; else { ptr -> right =temp; temp -> left = ptr; } ptr = temp; } return start; } void print(node *s) { printf(n the list is<); while(s!=null) { printf(%d, s->data); s = s->right; } }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

node *addbeg(node *s) { node *temp; temp=(node *)malloc(sizeof(node)); printf(|n enter the data to b e insertd); scanf(%d, &temp ->data); temp->right=null; if( s==null) { temp->right=null; s=temp; } else { temp->right =s; s->left= temp; s = temp; } return s; } node *addend(node *s) { node *temp, *p; temp =(node *)malloc(sizeof(node)); printf(enter the data to be inserted); scanf(%d, &temp-> data); temp->right=null; if(s ==null) { temp-> left=null; s=temp; } else { p=s; while(p->right!=null) p=p->right; p->right=temp; temp->left = p; } return s; }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

node *deletebeg(node *s) { node *t; if(s==null) { printf(enter list); return null; } if(s->right == null) { printf(the element deleted is %d, s->data); free(s); return null; } t=s; s=s->right; s->left =null; printf(the element deleted is %d, t->data); free(t); return s; }

node *deleteend(node *s) { node *t; if( s== null) { printf(empty list); return null; } if(s->right==null) { printf(the element deleted is%d, s->data); free(s); return null; } t=s; while(t->right->right!= null) t=t->right; printf(the element deleted is %d, t->right ->data); free(t->right); t->right= null;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

return s; } int count(node *a) { int c=0; while(s!=0) { s= s->right; c=c+1; } return c; } node *addpos(node *s, int pos) { int i; node *temp, *p; if(pos<1 || pos> count(s)+1) { printf(insertion not possible); } else if(pos==1) { s=addbeg(s); } else if(pos==count(s)+1) { s= addend(s); } else { i=1; p=s; while(i<pos-1) { p=p->right; i=i+1; } temp=(node *)malloc(sizeof(node)); printf(enter the data to be inserted); scanf(%d, &temp->data);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

temp->right= p->right; temp->left = p; p-> right =temp; temp-> right -> left= temp; } return s; } node *deletepos(node *s, int pos) { int i; node *temp, *p; if( pos <1 || pos>count(s) || s==null) { printf(deletion not possible); } else if(pos==1) { s= deletebeg(s); } else if(pos==count(s)) { s=deleteend(s); } else { i=1; p=s; while(i<pos-1) { p=p->right; i=i+1; } temp=p->right; printf(the element deleted is%d,temp->data); p->right=temp->right; p->right->left=p; free(temp); } return s; }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

void main () { int ch, n; node *start; start= create(); do { printf(start); puts(\n 1. insert at beginning); puts(\n 2. insert at end); puts(\n 3. insert at position); puts(\n 4.delete at beginning); puts(\n 5. delete at end); puts(\n 6. delete at position ); puts(\n 7. count list); puts(\n8.exit); puts( enter the choice); scanf(%d, &ch); switch(ch) { case 1: start= addbeg(start); break; case 2: start = addend(start); break; case 3: printf(enter the position to be insert); scanf(%d, &n); start = addpos(start, n); break; case 4: start = deletebeg(start); break; case 5: start = deleteend(start); break; case 6: printf(enter the position to be insert); scanf(%d, &n); start = deletepos(start,n); break; case 7: printf(\n no of elements is %d, count(start)); break; case 8: printf(end of the program); break; default : printf(your choice is invalid); } }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

while(ch!=8) getch(); }

**********END************

1. What will print out? main() { char *p1=name; char *p2; p2=(char*)malloc(20); memset (p2, 0, 20); while(*p2++ = *p1++); printf(%sn,p2); } Answer:empty string. 2. What will be printed as the result of the operation below:
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(%d%dn,x,y); } Answer : 5794 3. What will be printed as the result of the operation below: main() { int x=5; printf(%d,%d,%dn,x,x< <2,x>>2); } Answer: 5,20,1 4. What will be printed as the result of the operation below: #define swap(a,b) a=a+b;b=a-b;a=a-b; void main() { int x=5, y=10; swap (x,y); printf(%d %dn,x,y); swap2(x,y); printf(%d %dn,x,y); } int swap2(int a, int b) { int temp; temp=a; b=a; a=temp; return 0; }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Answer: 10, 5 10, 5 5. What will be printed as the result of the operation below: main() { char *ptr = Cisco Systems; *ptr++; printf(%sn,ptr); ptr++; printf(%sn,ptr); } Answer:Cisco Systems isco systems 6. What will be printed as the result of the operation below: main() { char s1[]=Cisco; char s2[]= systems; printf(%s,s1); } Answer: Cisco 7. What will be printed as the result of the operation below: main() { char *p1; char *p2; p1=(char *)malloc(25); p2=(char *)malloc(25); strcpy(p1,Cisco); strcpy(p2,systems); strcat(p1,p2); printf(%s,p1); }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Answer: Ciscosystems 8. WHat will be the result of the following code? #define TRUE 0 // some code while(TRUE) { // some code } Answer: This will not go into the loop as TRUE is defined as 0.

9. What will be printed as the result of the operation below: int x; int modifyvalue() { return(x+=10); } int changevalue(int x) { return(x+=1); } void main() { int x=10; x++; changevalue(x); x++; modifyvalue(); printf("First output:%dn",x); x++; changevalue(x);
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

printf("Second output:%dn",x); modifyvalue(); printf("Third output:%dn",x); } Answer: 12 , 13 , 13 10. What will be printed as the result of the operation below: main() { int x=10, y=15; x = x++; y = ++y; printf(%d %dn,x,y); } Answer: 11, 16 11. What will be printed as the result of the operation below: main() { int a=0; if(a==0) printf(Cisco Systemsn); printf(Cisco Systemsn); } Answer: Two lines with Cisco Systems will be printed.

1. What will be output if you will compile and execute the following c code? void main() { int i=320; char *ptr=(char *)&i; printf("%d",*ptr); }
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

(A) 320 (B) 1

(C) 64 (E) None of above (D) Compiler error

2. What will be output if you will compile and execute the following c code? #define x 5+2 void main() { int i; i=x*x*x printf("%d",i); } (A) 343 (B) 27 (C) 133 (E) None of above (D) Compiler error 3. What will be output if you will compile and execute the following c code? void main() { char c=125; c=c+10 printf("%d",c); } (A) 135 (B) +INF (C) -121 (D) -8 (E) Compiler error 4. What will be output if you will compile and execute the following c code? void main() { float a=5.2; if(a==5.2) printf("Equal"); else if(a<5.2) printf("Less than"); else printf("Greater than"); } (A) Equal (B) Less than (C) Greater than (D) Compiler error (E) None of above

5.What will be output if you will compile and execute the following c code? void main() {
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

int i=4, x; x=++i + ++i + ++i printf("%d",x); } (A) 21 (B) 18 (C) 12 (E) None of above (D) Compiler error Solution:In ++a, ++ is pre increment operator. In any mathematical expression pre increment operator first increment the variable up to break point then starts assigning the final value to all variable. Step 1: Increment the variable I up to break point. X= ++I + ++I + ++I; 5 6 7 Step 2: Start assigning final value 7 to all variable i in the expression. X= ++I + ++I + ++I; 7 7 7 So, i=7+7+7=21 6. What will be output if you will compile and execute the following c code? void main() { int a=2; if(a==2){ a=~a+2<<1 printf("%d",a); } else{ break; } } (A) It (B) -3 (C) -2 (D) 1 (E) Compiler error. will print nothing. 7. What will be output if you will compile and execute the following c code? void main() { int a=10; printf("%d %d %d",a,a++,++a); } (A) 12 11 11 (B) 12 10 10 (C) 11 11 12 (D) 10 10 12 (E) Compiler error
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction. ("%d %d %d", a, a++, ++a) 3 2 1 So first ++a will pass and value of variable will be a=10 then a++ will pass now value variable will be a=10 and at the end a will pass and value of a will be a=12.

8.What will be output if you will compile and execute the following c code? void main() { char *str="Hello world"; printf("%d",printf("%s",str)); } (A) 11Hello world (B) 10Hello world (C) Hello world10 (D) Hello world11 (E) Compiler error Return type of printf function is integer and value of this integer is exactly equal to number of character including white space printf function prints. So, printf(Hello world) will return 13.

9.What will be output if you will compile and execute the following c code? #include "stdio.h" #include "string.h" void main() { char *str=NULL; strcpy(str,"cquestionbank"); printf("%s",str); } (A) cquestionbank (B) cquestionbank\0 (C) (null) (D) Itwill print nothing (E) Compiler error We cannot copy any thing using strcpy function to the character pointer pointing to NULL. 10. What will be output if you will compile and execute the following c code?
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

#include "stdio.h" #include "string.h" void main() { int i=0; for(;i<=2;) printf(" %d",++i); } (A) 0 1 2 (B) 01 2 3 (C) 1 2 3 (D) Compiler error (E) Infinite loop

11. What will be output if you will compile and execute the following c code? void main() { int x; for(x=1;x<=5;x++); printf("%d",x); } (A) 4 (B) 5 (C) 6 (E) None of above (D) Compiler error

12. What will be output if you will compile and execute the following c code? void main() { printf("%d",sizeof(5.2)); } (A) 2 (B) 4 (C) 8 (D) 10 (E) Compiler error Default type of floating point constant is double. So 5.2 is double constant and its size is 8 byte. 13.What will be output if you will compile and execute the following c code? #include "stdio.h" #include "string.h" void main() {
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

char c='\08'; printf("%d",c); } (A) 8 (B) 8 (C) 9 (D) null (E) Compiler error In c any character is starting with character \ represents octal number in character. As we know octal digits are: 0, 1, 2, 3, 4, 5, 6, and 7. So 8 is not an octal digit. Hence \08 is invalid octal character constant. 14.What will be output if you will compile and execute the following c code? #define call(x,y) x##y void main() { int x=5,y=10,xy=20; printf("%d",xy+call(x,y)); } (A) 35 (B) 510 (C) 15 (D) 40 (E) None of above

15.What will be output if you will compile and execute the following c code? int * call(); void main() { int *ptr; ptr=call(); clrscr(); printf("%d",*ptr); } int * call(){ int a=25; a++; return &a; } (A) 25 (B) 26 (D) Garbage value (E) Compiler error (C) Any address In this question variable a is a local variable and its scope and visibility is within the function call. After returning the address of a by function call variable a became dead while pointer ptr is still pointing to address of variable a. This problem is known as dangling pointer problem.
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

16. What is error in following declaration? struct outer { int a; struct inner { char c; }; }; (A) Nesting (B) It is necessary to initialize the member variable. (C) Inner structure must have name. (D) Outer structure must have name. (E) There is not any error. It is necessary to assign name of inner structure at the time of declaration other wise we cannot access the member of inner structure. So correct declaration is: above same 17.What will be output if you will compile and execute the following c code? void main() { int array[]={10,20,30,40}; printf("%d",-2[array]); } (A) -60 (B) -30 (C) 60 (E) Compiler error (D) Garbage value 18. What will be output if you will compile and execute the following c code? void main() { int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater than"); else printf("Less than"); } (A) Equal (C) Less than (D) Compiler error (E) None of above (B) Greater than

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable. In this example i is run time variable while x is load time variable. 19. What will be output if you will compile and execute the following c code? #define max 5; void main() { int i=0; i=max++; printf("%d",i++); } (A) 5 (B) 6 (C) 7 (D) 0 (E) Compiler error #define is token pasting preprocessor. If you will see intermediate file: test.i test.c 1: test.c 2: void main(){ test.c 3: int i=0; test.c 4: i=5++; test.c 5: printf("%d",i++); test.c 6: } test.c 7: It is clear macro constant max has replaced by 5. It is illegal to increment the constant number. Hence compiler will show Lvalue required.

20. What will be output if you will compile and execute the following c code? void main() { double far* p,q; printf("%d",sizeof(p)+sizeof(q)); } (A) 12 (B) 8 (C) 4 (D) 1 (E) Compiler error It is clear p is far pointer and size of far pointer is 4 byte while q is double variable and size of double variable is 8 byte. Pointers:
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

1. What is meaning of following declaration? into(*ptr[5]()); (a)ptr is pointer to function. (b)ptr is array of pointer to function (c)ptr is pointer to such function which return type is array. (d)ptr is pointer to array of function. (e)None of these Ans: (b) Explanation: Here ptr is array not pointer. (2) What is meaning of following pointer declaration? int(*(*ptr1)())[2]; (a)ptr is pointer to function. (b)ptr is array of pointer to function (c)ptr is pointer to such function which return type is pointer to an array. (d)ptr is pointer array of function. (e)None of these Answer: (c) (3)What is size of generic pointer in c? (a)0 (b)1 (c)2 (d)Null (e)Undefined Ans: (c) Size of any type of pointer is 2 byte (In case of near pointer) Note. By default all pointers are near pointer if default memory model is small. 4. What will be output of following c code? #include<stdio.h> int main() { int *p1,**p2; double *q1,**q2; clrscr(); printf("%d %d ",sizeof(p1),sizeof(p2)); printf("%d %d",sizeof(q1),sizeof(q2)); getch(); return 0; } (a)1 2 4 8 (b)2 4 4 8 (c)2 4 2 4 (d)2 2 2 2 (e)2 2 4 4 Answer: (d) Size of any type of pointer is 2 byte (In case of near pointer) 5. What will be output if you will compile and execute the following c code?
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h> int main() { char huge *p=(char *)0XC0563331; char huge *q=(char *)0XC2551341; if(p==q) printf("Equal"); else if(p>q) printf("Greater than"); else printf("Less than"); return 0; } (a)Equal (b)Greater than (c)Less than (d)Compiler error (e)None of above Output: (a) 6. What will be output if you will compile and execute the following c code? #include<stdio.h> int main() { int a=5,b=10,c=15; int *arr[]={&a,&b,&c}; printf("%d",*arr[1]); return 0; } (a)5 (b)10 (c)15 (d)Compiler error (e)None of above Output: (d) Array element cannot be address of auto variable. It can be address of static or extern variables. 7. What will be output if you will compile and execute the following c code? #include<stdio.h> int main() { int a[2][4]={3,6,9,12,15,18,21,24}; printf("%d %d %d",*(a[1]+2),*(*(a+1)+2),2[1[a]]); return 0; } (a)15 18 21 (b)21 21 21 (c)24 24 24 (d)Compiler error (e)None of above
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

Output: (b) In c, a [1][2] =*(a [1] +2) =*(*(a+1) +2) =2[a [1]] =2[1[a]] Now, a [1] [2] means 1*(4) +2=6th element of an array staring from zero i.e. 21. 8. What will be output if you will compile and execute the following c code? #include<stdio.h> int main() { const int x=25; int * const p=&x; *p=2*x; printf("%d",x); return 0; } (a)25 (b)50 (c)0 (d)Compiler error (e)None of above Output: (b) const keyword in c doesnt make any variable as constant but it only makes the variable as read only. With the help of pointer we can modify the const variable. In this example pointer p is pointing to address of variable x. In the following line: int * const p=&x; p is constant pointer while content of p i.e. *p is not constant. *p=2*x put the value 50 at the memory location of variable x. 9.What will be output if you will compile and execute the following c code? #include<stdio.h> int main() { static char *s[3]={"math","phy","che"}; typedef char *( *ppp)[3]; static ppp p1=&s,p2=&s,p3=&s; char * (*(*array[3]))[3]={&p1,&p2,&p3}; char * (*(*(*ptr)[3]))[3]=&array; p2+=1; p3+=2; printf("%s",(***ptr[0])[2]); return 0;
-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com

A Course on REAL-TIME EMBEDDED SYSTEMS


. . . by Krest Technologies ----------------------------------------------------------------------------------------------------------------------------------------------------------

(a) math (b) phy (c) che (d) Compiler error (e) None of these Answer: (c) Explanation: Here ptr is pointer to array of pointer to string. P1, p2, p3 are pointers to array of string. array[3] is array which contain pointer to array of string. Pictorial representation: 10. What will be output if you will compile and execute the following c code? #include<conio.h> #include<stdio.h> int display(); int(*array[3])(); int(*(*ptr)[3])(); int main() { array[0]=display; array[1]=getch; ptr=&array; printf("%d",(**ptr)()); (*(*ptr+1))(); return 0; } int display() { int x=5; return x++; } (a)5 (b)6 (c)0 (d)Compiler error (e)None of these Answer: (a)

*** All The Best ***

-------------------------------------------------------------------------------------------------------------------------------------------------Head Office: II Floor, Solitaire Plaza, Near Image Hospitals, Ameerpet, Hyd. Branch Office: #310, Sreemaan Rama Towers, Opp. Kalaniketan, Chaitanyapuri, Dilshuknagar, Hyd. E-mail: embedded.course@kresttechnology.com, Web: www.kresttechnology.com