Professional Documents
Culture Documents
Embedded
Wireless
Telecom
Table of Contents
Telecom
Introduction to C
A simple C program
Basic data types
Variables
Operators
Control flow
Arrays and pointers
Functions
Structures and Union
Files
Wireless
Embedded
www.globaledgesoft.com
Introdunction to C
C is a middle level language that was
originally developed by Dennis Ritchie .
Embedded
Wireless
www.globaledgesoft.com
Telecom
A simple C program
Embedded
#include<stdio.h>
Wireless
int main()
{
printf(\nHello, World);
Telecom
www.globaledgesoft.com
short int
Int
long int
unsigned long
int
-32,768(-215)
32,767(215-1)
65,535(216-1)
-2,147,483,648(-231)
2,147,483,648(231-1)
4,294,967,295
-2,147,483,648(-231)
2,147,483,648(231-1)
4,294,967,295
www.globaledgesoft.com
Telecom
unsigned int
Largest Value
Wireless
unsigned
short int
Smallest Value
Embedded
char
Smallest Value
Largest Value
-128
127
-3.4E +38
3.4E+38
double
-1.7E+308
1.7E+308
Wireless
float
Embedded
Telecom
www.globaledgesoft.com
Variables
www.globaledgesoft.com
Telecom
Wireless
Embedded
Operators
Telecom
www.globaledgesoft.com
Wireless
Arithmetic operators
Relational and Logical operators
Bitwise operators
Increment and Decrement operators
Conditional operators
Embedded
Control Flow
Iteration statements
while (expr) stmt;
for (expr1;expr2;expr3) stmt;
do stmt while expr;
www.globaledgesoft.com
Telecom
Wireless
Selection statements
if (expr) stmt;
if (expr) stmt1 else stmt2;
switch (expr) {case ... default }
Embedded
Telecom
www.globaledgesoft.com
Wireless
Pointers:
A pointer is a variable that holds the
address of another variable.
Eg: int *ptr; //declaring a pointer variable
Int *ptr = &a; // initialization
Embedded
Array:
An array is a collection of variables of
the same type that are referred through a
common name.
syntax:datatype var_name[size];
Functions
Building blocks of c .
www.globaledgesoft.com
Telecom
Wireless
Embedded
Wireless
Union:
A union is a memory location that is
shared by different types of variables.
Embedded
Structures:
Structure is a collection of same or
different data type, grouped together in a
single name for
convenient handling.
Telecom
www.globaledgesoft.com
Files
Wireless
Embedded
www.globaledgesoft.com
Telecom
Embedded
Wireless
Telecom
Table of Contents
Wireless
Telecom
www.globaledgesoft.com
Embedded
Introduction
Data Types in C
Built-in Data Types
Derived Data Types
Expression
Assignment Operator and Expression
Conditional Expression
Storage class
Introduction
we used in our program and tells the
compiler how many bytes of memory to
be allocated for the variable
Telecom
Example : int a;
Wireless
Embedded
www.globaledgesoft.com
Data Types in C
1)Built-in data types
2)Derived data types
Telecom
Wireless
Embedded
Wireless
nt :
size 2 or 4 bytes
Char : size 1 Byte
Unsigned int :size 2 or 4 Bytes
Telecom
www.globaledgesoft.com
Wireless
Telecom
2 bytes
range -32,768 to 32,767 or
0 to 65,535(unsigned)
Float : size 4 bytes
range 1.2E-38 to 3.4E+38
precision 6 decimal places
Double : size 8 bytes
range 2.3E-308 to 1.7E+308
precision 15 decimal places
Long Double : size 10 bytes
range 3.4E-4932 to 1.1E+4932
precision 19 decimal places
www.globaledgesoft.com
Embedded
Short : size
Telecom
struct structname
{
Variables
}structure object;
Wireless
Syntax
Embedded
www.globaledgesoft.com
Example
int *p;
www.globaledgesoft.com
Telecom
Wireless
Embedded
Expression
Wireless
Telecom
Embedded
An expression is a combination of
Conditional Expression
evaluated
Example
z = (a > b) ? a : b;
evaluated
www.globaledgesoft.com
Telecom
Wireless
Syntax
Embedded
Storage class
Embedded
Wireless
Topics :
Automatic variables
External variables
Static variables
Register variables
Telecom
www.globaledgesoft.com
Auto
Auto declare inside a function in which
they are to be utilized.
eg. int number; auto int number;
Embedded
Wireless
www.globaledgesoft.com
Telecom
static
www.globaledgesoft.com
Telecom
Wireless
Embedded
Register
www.globaledgesoft.com
Telecom
Wireless
Embedded
Extern
Telecom
www.globaledgesoft.com
Wireless
Embedded
Operators in C
Mukesh kumar D
Embedded
Wireless
Telecom
Table of Contents
www.globaledgesoft.com
Telecom
Operators in C
Arithmetic operators
Increment/decrement operators
Relational operators
Logical operators
Conditional operator
Bitwise operators
Special operators
Wireless
Embedded
Introduction
Introduction
Wireless
Telecom
www.globaledgesoft.com
Embedded
Operators in C
Wireless
Telecom
www.globaledgesoft.com
Embedded
Arithmetic Operators
are used to perform
mathematical calculations like addition,
subtraction, multiplication, division and
modulus.
Example
Addition
A+B
Subtraction
A-B
Multiplication
A*B
Division
A/B
%
++
Modulus
Arithmetic
operators in C A%B
Increment
i++
www.globaledgesoft.com
Telecom
operations
Wireless
Operators
Embedded
Arithmetic operators
Relational Operators
Relational operators are used to compare
the value of two
variables.
Example
>
Greater than
x>y
<
Lesser than
x<y
>=
Greater than
or equal to
x>=y
<=
==
Lesser than
x<=
or equal to
Relational operators in C
Equal to
x==y
www.globaledgesoft.com
Telecom
operation
Wireless
Operator
Embedded
Logical Operators
There are 3 logical operators in C
language. They are, logical AND (&&),
logical OR (||) and logical NOT (!).
Example
&&
Logical AND
x&& y
||
LOGICAL OR
x||y
Logical NOT
!(x&&y)
www.globaledgesoft.com
Telecom
Operation
Wireless
Operator
Embedded
Conditional Operators
Telecom
Wireless
Embedded
www.globaledgesoft.com
Bitwise operators
Bitwise operator works on bits and
perform bit-by-bit operation
Operator
Meaning
Bitwise
OR
Bitwise
NOT
Bitwise
XOR
<<
Left shift
>>
Right
shift
Telecom
Bitwise
AND
Wireless
&
Embedded
www.globaledgesoft.com
Left Shift:
Shift n bits to left
Eg: a<<=3;
Wireless
Telecom
Right Shift
Shift n bits to right
Eg:a>>=5;
www.globaledgesoft.com
Special operators
There are few other operators supported
by C Language.
Example
sizeof()
sizeof(a);
Returns 4 if a is
an integer
&
Returns the
address of a
variable
Pointer to a
variable
*a will be a
pointer to a
variable
Used to link
related
expression
together
int a,b,c;
www.globaledgesoft.com
Telecom
Description
Wireless
Operator
Embedded
Precedence of operators
Precedence and Associativity of C Operators
Type of Operation
Associativity
Left to right
Unary
Right to left
typecasts
Unary
Right to left
* / %
Multiplicative
Left to right
Additive
Left to right
<< >>
Bitwise shift
Left to right
Relational
Left to right
== !=
Equality
Left to right
&
Bitwise-AND
Left to right
Bitwise-exclusive-OR
Left to right
Bitwise-inclusive-OR
Left to right
&&
Logical-AND
Left to right
||
Logical-OR
Left to right
? :
= *= /= %=
www.globaledgesoft.com
Telecom
Expression
Wireless
Embedded
Symbol
Control Flow
Embedded
Wireless
Telecom
Table of Contents
Wireless
Telecom
www.globaledgesoft.com
Embedded
If else
Else if
Switch
Loops While,For & Do While
Break
Continue
Goto
Labels
If else
Telecom
www.globaledgesoft.com
Wireless
if ( Expression )
statement1;
else
statement2;
Expression May Be checked with
Conditions.
Embedded
Else - if
decisions .
Telecom
www.globaledgesoft.com
Wireless
if(Expression1) s1;
Else if (Expression2)s2;
Else s3;
Control flows in order from top.
If expression satisfies condition,
statements within that condition gets
executed.
Embedded
Multiway
Syntax:
Switch
Wireless
Embedded
Telecom
www.globaledgesoft.com
For
Wireless
Telecom
for(initialization;test condition;inc/dec){
Statements}
Entry check loop.
Initialization for variables done and
executed.
Loop runs till condition satisfies.
Other than Initialization condition,inc/dec
done in each iteration.
for(;;) -is an infinite loop
Any of the expressions can be omitted but
'; ;' is required
www.globaledgesoft.com
Embedded
Syntax :
Do - While
Telecom
www.globaledgesoft.com
Wireless
do{
statements;}while(Condition/Expression);
Exit check loop.
No initialization for variables.
Nested loops Are Possible.
Break Stops The Execution of Loop and
Control Comes Out.
Embedded
Syntax :
Telecom
www.globaledgesoft.com
Wireless
Loop .
Continue Statement Transfers Control To
The Beginning Of The Loop.
In Do While loop Continue statement
Transfers Control To - While(con/exp).
Break And Continue Statements Used
Inside Of Loops Only.
Embedded
Telecom
www.globaledgesoft.com
Wireless
Goto label;
Label can be anywhere inside the same
function as goto
Label is followed with colon.
Goto statement is used to break the loop
of deeply nested loop structures ( Where
break cannot be used since it breaks only
innermost loop).
Embedded
Goto statement is
Syntax :
FUNCTION POINTERS
Embedded
Wireless
Telecom
Table of Contents
Embedded
Wireless
Introduction
What are function pointers .?
Definition
Need of function pointers
Assign and call using function pointers
Array of function pointers.
Telecom
www.globaledgesoft.com
Introduction
Telecom
www.globaledgesoft.com
Wireless
Embedded
Telecom
www.globaledgesoft.com
Wireless
Embedded
Telecom
Wireless
Embedded
Telecom
www.globaledgesoft.com
Wireless
Embedded
Telecom
www.globaledgesoft.com
Wireless
Embedded
Embedded
Wireless
www.globaledgesoft.com
Telecom
www.globaledgesoft.com
Telecom
Wireless
Embedded
Cont...
Wireless
Embedded
Telecom
file_cmd[n]();
www.globaledgesoft.com
Embedded
Wireless
Telecom
Table of Contents
www.globaledgesoft.com
Telecom
Wireless
Define pointer
Why pointer ?
Pointer Arithmetic
Pointer used as array
Array of pointer
Pointer to pointer
Constant pointer and pointer to a constant
Types of pointer
Embedded
Define pointer
A pointer is a variable whose value is the
address of another variable, i.e., direct
address of the memory location.
Embedded
Wireless
Int a = 3;
Int *ptr = &a;
Telecom
www.globaledgesoft.com
Why pointer ?
address.
Telecom
www.globaledgesoft.com
Wireless
#include<stdio.h>
int main()
{
int *ptr_A; /* A typed pointer */
void *ptr_B; /* A untyped pointer */
return 0;
}
Embedded
Wireless
Telecom
www.globaledgesoft.com
Embedded
Array of pointer
Telecom
www.globaledgesoft.com
Wireless
Embedded
Pointer to Pointer
pointer
Embedded
Wireless
Telecom
www.globaledgesoft.com
Telecom
www.globaledgesoft.com
Wireless
Embedded
Char ch, c;
States of pointer
Wireless
Telecom
www.globaledgesoft.com
Embedded
Null pointer
It points to nothing, when tried to
dereference it causes error
*p = NULL;
Void pointer
(void*) this indicates that it can point to
any datatype
void *p;
Wild pointer
pointer that doesn't point anything
valid
int *p;
Complicated
Declarations
Ravichandran. P
CR-08
Embedded
Wireless
Telecom
Complicated Declarations
Telecom
www.globaledgesoft.com
Wireless
Embedded
Examples
P is a pointer to
Telecom
www.globaledgesoft.com
Wireless
an integer.
P is a pointer to
Int **p;
pointer to an integer.
P is a pointer to
Int (*p) (char)
function takes char as arg and returns an
integer.
Int * ( * ( *p) (int)) [10]
P is a pointer to function that takes
int as an argument and returns a pointer to
an array of 10 pointers to integers.
Embedded
Int *p;
References:
W.KERNIGHAN,DENNIS M.RITCHIE.
ow-to-interpret-complex-C-Cdeclarations#contents
Wireless
http://www.codeproject.com/Articles/7042/H
Embedded
Telecom
www.globaledgesoft.com
STRUCTURES
Rajalakshmi
Embedded
Wireless
Telecom
Table of Contents
Embedded
Basics of structure
Structure and functions
Arrays of structure
Pointer to structure
Padding and Packing
Wireless
Telecom
www.globaledgesoft.com
Basics of Structure
Telecom
www.globaledgesoft.com
Wireless
Embedded
Telecom
www.globaledgesoft.com
Wireless
different ways
Can pass members separately
Pass entire structure
Pass Pointers
If large structure is to be passed ,passing a
pointer is more efficient.
Pointers to structure can access the
members in structure using ->operator.
Embedded
Arrays of structures
Telecom
www.globaledgesoft.com
Wireless
of structure.
This is also called structure array.
Eg: struct student{
char *name;
int rollno;
};
struct student student_details[10];
Array of structures are used when multiple
record of same structure has to be stored.
Embedded
Pointers to structure
Wireless
Embedded
Telecom
www.globaledgesoft.com
Padding
Telecom
www.globaledgesoft.com
Wireless
requirement.
Processor will have processing word length
as that of data bus.
Structures are used as data pack.
Because of the alignment requirement of
data type ,each element has to be
naturally aligned.
Eg struct padding{
int a;
char b;
}x;
Embedded
packing
Telecom
www.globaledgesoft.com
Wireless
padded bytes
In such case packing is used
Performance in less compared to padding
This can be achived with help of pragma
pack.
Eg: #pragma pack(push,1)
Struct stack{
char c;
Int a;
}x;
Embedded
http://www.geeksforgeeks.org/structure-member-alignm
ent-padding-and-data-packing/
http://ee.hawaii.edu/~tep/EE160/Book/chap12/subsectio
n2.1.1.4.html
Wireless
Embedded
REFERENCES:
Telecom
www.globaledgesoft.com
Embedded
Wireless
Telecom
Table of content
Embedded
Self-Referential structure
Typredef
Union
Bit-fields
Wireless
Telecom
www.globaledgesoft.com
Self-Referential structure
Telecom
www.globaledgesoft.com
Wireless
int data;
Struct list *next;
}
It is used in data structure such as binary
tree, linked list, stack, queue etc.
Embedded
Typedef
Telecom
www.globaledgesoft.com
Wireless
Embedded
Typedef
Telecom
www.globaledgesoft.com
Wireless
Embedded
Example:
Union
Telecom
www.globaledgesoft.com
Wireless
Embedded
Union
Telecom
www.globaledgesoft.com
Wireless
fnum , ch are
stored in the same place regardless of its
type.
Members of the union can be accessed as
union_name.member;
Using pointers:
union_name->member;
Union may occur within structures.
Embedded
Bit-fields
Telecom
www.globaledgesoft.com
Wireless
at a premium
Example:
Struct bit{
Unsigned int flag:1;
Unsigned int sign:1;
}flag;
In the above exmaple, Instead of using
4bytes for integer
One bit is used.
Embedded
References:
g99/supplements/javaLists/
e/view.php?id=2905
Telecom
W.Kernighan and
Dennis M.Ritchie
Wireless
http://www.opengurukul.com/vlc/mod/pag
Embedded
http://classes.soe.ucsc.edu/cmps203/Sprin
www.globaledgesoft.com
FILES
Embedded
Wireless
Telecom
Table of Contents
Wireless
File Operations
Reading and Writing with files
Embedded
What is a file?
Accessing a file
Telecom
www.globaledgesoft.com
What is a file?
Embedded
Wireless
Telecom
www.globaledgesoft.com
Accessing a file
Embedded
Wireless
Telecom
www.globaledgesoft.com
File Operations
Wireless
Telecom
www.globaledgesoft.com
Embedded
File Open
r+
w+
a+
Telecom
MEANING
Wireless
MODE
Embedded
Syntax:
filepointer=fopen(filename, mode);
Writing to Files
Wireless
Telecom
www.globaledgesoft.com
Embedded
Wireless
Telecom
www.globaledgesoft.com
Embedded
Wireless
Telecom
www.globaledgesoft.com
Embedded
Closing Files
Embedded
Wireless
Telecom
www.globaledgesoft.com
Compilation Process
Preprocessor, Compiler
Sampath N
Embedded
Wireless
Telecom
Table of Contents
Embedded
Overview
Compilation Stages
Wireless
Preprocessor
Compiler
Telecom
www.globaledgesoft.com
What is Compilation?
program in one language (source
language) into an equivalent program in
another language
compiler
Target
program
Wireless
Source
program
Embedded
Telecom
Error message
www.globaledgesoft.com
Preprocessor
Telecom
www.globaledgesoft.com
Wireless
-E command
Preprocessor performs following
operations prior to compilation.
Expanding macros
Inclusion of header files
Comment Removal
Conditional compilation
(gcc -E filename.c > filename.i)
Embedded
Compiler
Intermediate code
Scanner
(lexical analysis)
Parser
(syntax analysis)
Parse tree
www.globaledgesoft.com
Telecom
Code Optimization
Wireless
Tokens
Embedded
Wireless
tokens
Grouping characters into non-separatable
units (tokens)
Changing a stream to characters to a
stream of tokens
Embedded
Telecom
www.globaledgesoft.com
Symbol Table
Telecom
www.globaledgesoft.com
Wireless
Embedded
Telecom
id1
+
*
id2
id3
Wireless
the grammatical
specification of the language and generates
the syntax tree
Example for Syntax tree (Parse tree)
Pos = init + rate *:=60 id1 = id2 + id3 *
const
Embedded
60
www.globaledgesoft.com
Embedded
Wireless
www.globaledgesoft.com
Telecom
Code Optimization
Telecom
www.globaledgesoft.com
Wireless
Embedded
Wireless
assembly code.
This assembly code is given as input to
assembler.
The extension of preprocessed file is .s
It can be obtained by compiling code with
-S command (gcc -S filename.c)
Embedded
Telecom
www.globaledgesoft.com
Compilation Process
Assembler and Linker
Embedded
Wireless
Telecom
Table of Contents
Wireless
Assembler
Overview
Object file format
Process for producing an
executable file
Embedded
Compilation process
Telecom
Linker
Loader
www.globaledgesoft.com
Assembler
Telecom
www.globaledgesoft.com
Wireless
Embedded
Assembler (contd...)
Wireless
Embedded
Telecom
www.globaledgesoft.com
Object
header
Text
Segment
Data
Segment
Relocation
Information
Symbol
Table
Debugging
information
Telecom
files.
Text segment contains machine instructions.
Data segment contains the binary
representation of data in assembly file.
Relocation info identifies instructions and data
that depend on adsolute address.
Symbol table associates addresses with the
external labels and lists unresolved
www.globaledgesoft.com
references. Debug info.
Wireless
Embedded
Source
file
Assembler
Object
file
Source
file
Assembler
Object
file
Linker
.exe
file
www.globaledgesoft.com
Telecom
Program
library
Wireless
Assembler
Embedded
Source
file
Linker
Embedded
Wireless
Telecom
Loader
Embedded
Wireless
Telecom
Steps involved :
Read .exe file header to determine the
size of the text and data segments.
Create new address space for program.
Copies instructions and data into
address space.
Copies arguments passed to the
program into stack.
Jumps to the startup routine that copies
the program's arguments from the stack
to registers and calls the program's main
routine.
www.globaledgesoft.com
Stack Frame
Embedded
Wireless
Telecom
Table of Contents
Embedded
Terminologies
Subroutine
Stack Frame Structure
Example Program
Stack Related Issues
Wireless
Telecom
www.globaledgesoft.com
Terminologies
Telecom
www.globaledgesoft.com
Wireless
stack
associated with one subroutine
call.
Stack Pointer: register that contains the
top of the stack.
Frame Pointer: register that set to the
address of the stack frame when a
subprogram begins executing.
Embedded
Subroutine
Embedded
Wireless
Telecom
www.globaledgesoft.com
arg2
arg1
frame pointer
old sp
local2
Stack pointer
local3
www.globaledgesoft.com
Telecom
local1
Wireless
return address
Embedded
arg3
Example Program
Embedded
Wireless
int p1 = 1, p2 =2 , p3 = 3;
foo( p1, p2, p3 )
int p1, p2, p3;
{
int v1, v2 ,v3;
return 0;
}
main( )
{
foo( p1, p2, p3);
}
Telecom
www.globaledgesoft.com
Stack Structure
v3
v2
v1
Old FP
p1
p2
Old FP
FP
return addr
Registers
SP
102
108
argv
argc
www.globaledgesoft.com
Telecom
p3
Wireless
return addr
Static Memory
-P1 500
1
-P2 502
2
-P3 504
3
Embedded
100:
102:SP->
103:
106:
108:FP->
110:
112:
114:
116
118:
120:
122:
124
www.globaledgesoft.com
Telecom
Wireless
The
Embedded
There
Cont......
Wireless
Embedded
Telecom
www.globaledgesoft.com
THE C PREPROCESSOR
Girija Pretha.A
Embedded
Wireless
Telecom
Table of Contents
Embedded
C Preprocessor
Preprocessing Phases
Conditional compilation
Wireless
Telecom
www.globaledgesoft.com
C Preprocessor
Telecom
www.globaledgesoft.com
Wireless
compilation process.
Preprocessor instructs the compiler to do
some tasks before actual compilation.
It is a Macro processor that is used
automatically by c compiler to transform
program before compilation.
It allows to define macros which are
abbreviations of larger constructs
Embedded
Preprocessing....
Embedded
cpp
(C
preprocessor)
(C compiler)
process directives:
file inclusion (#include)
macro expansion (#define)
conditional compilation (#if, #ifdef, )
www.globaledgesoft.com
Telecom
expanded
code
compiled
code
cc1
Wireless
source
program
Preprocessing Phases
Embedded
Wireless
Telecom
www.globaledgesoft.com
Wireless
Telecom
www.globaledgesoft.com
Embedded
#include <file>
The file is searched for in the standard
compiler include paths.
#include "file"
The search path is expanded to include
the current source directory and
searches in standard path if file not
found in current directory.
Macro Expansion
Telecom
www.globaledgesoft.com
Wireless
Embedded
Conditional Compilation
Telecom
www.globaledgesoft.com
Wireless
Embedded
#include<stdio.h>
#ifndef PI
#define PI 3.14
#endif
int main()
{
/*This is the macro example*/
printf("\nThis is macro example\n");
printf("Value of macro is%d",PI);
return 0;
}
www.globaledgesoft.com
Comments are
replaced with blank
lines
www.globaledgesoft.com
Telecom
Macro is
Substituted with its
value.
Wireless
Embedded
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "test.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 28 "/usr/include/stdio.h" 3 4
int main()
{
Memory Layout
Embedded
Wireless
Telecom
Table of Contents
Embedded
Wireless
Text Segment
Initialized Data Segment
Uninitialized Data Segment (bss)
Heap
Stack
Telecom
www.globaledgesoft.com
Stack
Text Segment
www.globaledgesoft.com
Telecom
Wireless
Heap
Embedded
Text Segment
Telecom
www.globaledgesoft.com
Wireless
Embedded
Wireless
Embedded
Int a = 10;
Static int a = 10;
Telecom
www.globaledgesoft.com
Wireless
variables.
Data in the BSS segment are initialized to
zero.
Lifetime of uninitialized data is the
complete execution time of the program.
Embedded
Telecom
Static int a;
Int a;
www.globaledgesoft.com
Heap
Wireless
Telecom
www.globaledgesoft.com
Embedded
Stack
Embedded
Wireless
Telecom
References
Wireless
http://see-programming.blogspot.in/2013/10/memory-layoutof-c-program.html
Embedded
http://www.geeksforgeeks.org/memory-layout-of-c-program/
Telecom
www.globaledgesoft.com
Embedded
Wireless
Telecom
www.globaledgesoft.com