Embedded C Part 1
Embedded C Part 1
Team Emertxe
Introduction
C
Have you ever pondered how
- powerful it is?
- efficient it is?
- flexible it is?
- deep you can explore your system?
C
Where is it used?
{
/* To display Hello world */ Comment
}
Data Representations
Embedded C
Number Systems
Type Dec Oct Hex Bin
A number is generally Base 10 8 16 2
represented as 0 0 0 0 0 0 0
Decimal 1
2
1
2
1
2
0 0 0 1
0 0 1
0
0 0
Octal 3 3 3 0 0 1
0 1
0
4 4 4 0 0
1 0 0
Hexadecimal 5 5 5 0 1
0 0 1
0
6 6 6 0 1
0 1
0 0
Binary 7 7 7 0 1
0 1
0 1
0
8 10 8 0 0 0 0
1
9 11 9 0 0 0 1
1 0
10 12 A 0 0 1
1 0 0
Type Range (8 Bits)
11 13 B 0 0 1
1 0 1
0
Decimal 0 - 255
12 14 C 0 1
1 0 0 0
Octal 000 - 0377
13 15 D 0 1
1 0 0 1
0
Hexadecimal 0x00 - 0xFF
14 16 E 0 1
1 0 1
0 0
Binary 0b00000000 - 0b11111111
15 17 F 0 1
1 0 1
0 1
0
Embedded C
Data Representation - Bit
Value No of Bits
0 0
1 1
Embedded C
Data Representation - Byte
Value No of Bits
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1
Embedded C
Data Representation - Word
Value No of Bits
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Embedded C
Integer Number - Positive
Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
Embedded C
Integer Number - Negative
Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
1's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
Add 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
2's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
n
Mathematically : -k ≡ 2 - k
Basic Data Types
Embedded C
Basic Data Types
char
Integral
int
Data
Types
float
Floating Point
double
Embedded C
Data Type Modifiers and Qualifiers
short T
Size long T
signed T
Signedness
unsigned T
const V
Qualifiers
volatile V
V Variables
T Data Types
- ANSI says, ensure that: char ≤ short ≤ int ≤ long
F Functions
Conditional Constructs
Embedded C
Conditional Constructs
do while
Embedded C
Conditional Constructs - if
Syntax Example
if (condition) #include <stdio.h>
{
statement(s); int main()
} {
int num1 = 2;
code code
Embedded C
Conditional Constructs – if else
Example
#include <stdio.h>
int main()
{
int num1 = 10;
if (num1 < 5)
{
printf(“num1 < 5\n”);
}
else
{
printf(“num1 > 5\n”);
}
return 0;
}
Embedded C
Conditional Constructs – if else if
Syntax Flow
if (condition1)
{
statement(s);
} true
else if (condition2) cond1? code
{
statement(s); false
} true
else
cond2? code
{
statement(s); false
}
code
Embedded C
Conditional Constructs – if else if
Example
#include <stdio.h>
int main()
{
int num1 = 10;
if (num1 < 5)
{
printf(“num1 < 5\n”);
}
else if (num1 > 5)
{
printf(“num1 > 5\n”);
}
else
{
printf(“num1 = 5\n”);
}
return 0;
}
Embedded C
Conditional Constructs – switch
Syntax Flow
switch (expression)
{
case constant:
statement(s); expr
break;
case constant:
statement(s); true
break; case1? code break
case constant:
statement(s); false
break; true
default: case2? code break
statement(s);
} false
int main()
{
int option;
printf(“Enter the value\n”);
scanf(“%d”, &option);
switch (option)
{
case 10:
printf(“You entered 10\n”);
break;
case 20:
printf(“You entered 20\n”);
break;
default:
printf(“Try again\n”);
}
return 0;
}
Embedded C
Conditional Constructs – while
Syntax Flow
while (condition)
{
Controls the loop.
Evaluated before each
statement(s);
} execution of loop body
Example
false
#include <stdio.h>
cond?
int main()
{ true
int i; code
i = 0;
while (i < 10)
{
printf(“Looped %d times\n”, i);
i++;
}
return 0;
}
Embedded C
Conditional Constructs – do while
Syntax Flow
do
{
Controls the loop.
Example
code
#include <stdio.h>
i = 0;
do false
{
printf(“Looped %d times\n”, i);
i++;
} while (i < 10);
return 0;
}
Embedded C
Conditional Constructs – for
Syntax Flow
for (init; condition; post evaluation expr)
{
statement(s); Controls the loop.
} Evaluated before each
init
execution of loop body
Example
#include <stdio.h> false
cond?
int main()
{ true
int i; code
return 0;
}
Embedded C
Conditional Constructs – continue
Flow
A continue statement causes a
jump to the loop-continuation
portion, that is, to the end of
code block
the loop body
The execution of code appearing cond?
true
continue?
after the continue will be
skipped false
code block
Can be used in any type of multi
iteration loop
loop
Syntax cond?
true
do
{ false
conditional statement
continue;
} while (condition);
Embedded C
Conditional Constructs – continue
Example
#include <stdio.h>
int main()
{
int i;
return 0;
}
Embedded C
Conditional Constructs – break
Flow
A break statement shall appear
only in “switch body” or “loop
body”
“break” is used to exit the loop, code block
true false
Syntax
do loop
{ cond?
conditional statement
break; false
} while (condition);
Embedded C
Conditional Constructs – break
Example
#include <stdio.h>
int main()
{
int i;
return 0;
}
Embedded C
Conditional Constructs – break
Example
#include <stdio.h>
int main()
{
int i;
return 0;
}
Operators
Embedded C
Operators
Example
#include <stdio.h>
return 0;
}
Embedded C
Operators - Logical
Operator Description Associativity
! Logical NOT R to L
Example && Logical AND L to R
|| Logical OR L to R
#include <stdio.h>
int main()
{ What will be
int num1 = 1, num2 = 0;
the output?
if (++num1 || num2++)
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
num1 = 1, num2 = 0;
if (num1++ && ++num2)
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
else
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
return 0;
}
Embedded C
Operators - Relational
Operator Description Associativity
> Greater than L to R
< Lesser than
>= Greater than or equal
<= Lesser than or equal
== Equal to
Example
!= Not Equal to
#include <stdio.h>
int main()
{
float num1 = 0.7;
return 0;
}
Embedded C
Operators - Assignment
Example Example
#include <stdio.h> #include <stdio.h>
return 0;
}
Embedded C
Operators - Bitwise
Operand
Value Value
Bitwise ANDing of 0x60
A 0x61 0 1 1 0 0 0 0 1
& Bitwise AND all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
&B 0x01 0 0 0 0 0 0 0 1
Operand
Value Value
Bitwise ORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
| Bitwise OR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
|B 0x73 0 1 1 1 0 0 1 1
Operand
Value Value
Bitwise XORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
^ Bitwise XOR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
^B 0x72 0 1 1 1 0 0 1 0
Embedded C
Operators - Bitwise
Complimenting Operand
Value Value
~ Compliment all the bits of the 0x60
A 0x61 0 1 1 0 0 0 0 1
operand
0x13
~A 0x9E 1 0 0 1 1 1 1 0
Say A = 91 A << 2
Say A = 91 A >> 2
Sign
Zero
bitfilling
fillingright
rightshift
shift
Embedded C
Operators – Language - sizeof()
Example
#include <stdio.h>
int main()
{
int num = 5;
return 0;
}
Example
#include <stdio.h>
int main()
{
int num1 = 5;
int num2 = sizeof(++num1);
return 0;
}
Embedded C
Operators – Ternary
Syntax
Condition ? Expression 1 : Expression 2;
Example
#include <stdio.h>
int main()
{ #include <stdio.h>
int num1 = 10;
int num2 = 20; int main()
int num3; {
int num1 = 10;
if (num1 > num2) int num2 = 20;
{ int num3;
num3 = num1;
} num3 = num1 > num2 ? num1 : num2;
else printf(“Greater num is %d\n”, num3);
{
num3 = num2; return 0;
} }
printf(“%d\n”, num3);
return 0;
}
Embedded C
Over and Underflow
Overflow
(127 + 1)
Underflow
(-128 - 1)
Embedded C
Overflow – Signed Numbers
Say A = +127
Add 1 0 0 0 0 0 0 0 1
Say A = -128
Add -1 1 1 1 1 1 1 1 1
Example
int age[5] = {10, 20, 30, 40, 50};
10 20 30 40 50
base addr + 12
base addr + 16
base addr + 4
base addr + 8
base addr
Embedded C
Arrays – Point to be noted
int main()
#include <stdio.h>
{
int num1 = 10; int main()
int num2 = 20; {
int num3 = 30; int num_array[5] = {10, 20, 30, 40, 50};
int num4 = 40; int index;
int num5 = 50;
for (index = 0; index < 5; index++)
printf(“%d\n”, num1); {
printf(“%d\n”, num2); printf(“%d\n”, num_array[index]);
printf(“%d\n”, num3); }
printf(“%d\n”, num4);
printf(“%d\n”, num5); return 0;
}
return 0;
}
Embedded C
Arrays - Reading
Example
#include <stdio.h>
int main()
{
int array[5] = {1, 2, 3, 4, 5};
int index;
index = 0;
do
{
printf(“Index %d has Element %d\n”, index, array[index]);
index++;
} while (index < 5);
return 0;
}
Embedded C
Arrays - Storing
Example
#include <stdio.h>
int main()
{
int num_array[5];
int index;
return 0;
}
Embedded C
Arrays - Initializing
Example
#include <stdio.h>
int main()
{
int array1[5] = {1, 2, 3, 4, 5};
int array2[5] = {1, 2};
int array3[] = {1, 2};
int array4[]; /* Invalid */
printf(“%u\n”, sizeof(array1));
printf(“%u\n”, sizeof(array2));
printf(“%u\n”, sizeof(array3));
return 0;
}
Embedded C
Arrays – Copying
int main()
{
int array_org[5] = {1, 2, 3, 4, 5}; Waow!! so simple?
int array_bak[5];
But can I do this?
array_bak = array_org;
if (array_bak == array_org)
{
printf(“Copied\n”);
}
return 0;
}
Pointers
Embedded C
Pointers – Why?
return 0;
}
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 5 Say 4 bytes
ptr = 5;
return 0;
}
So pointer is an integer
But remember the “They may not be of same size”
32 bit system = 4 Bytes
64 bit system = 8 Bytes
Embedded C
Pointers
&
Variable Address
*
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 ? Say 4 bytes
return 0;
}
return 0;
}
return 0;
}
return 0;
}
int main()
{
int number = 10;
int *ptr;
ptr = &number;
return 0;
}
Embedded C
Pointers
Example
#include <stdio.h>
int main()
{
int number = 10;
int *ptr;
ptr = &number;
return 0;
}
Embedded C
Pointers
Example
#include <stdio.h>
int main()
{
int number = 10;
int *ptr;
ptr = &number;
*ptr = 100;
return 0;
}
Example 1000
#include <stdio.h> a 10
1004
int main()
{ 1008
int a = 10;
int *ptr; 1012
ptr = &a;
1016
ptr 1000
return 0;
} 1020
1024
Embedded C
Pointers
return 0;
}
int main()
{
if (sizeof(char *) == sizeof(long long *))
{
printf(“Yes its Equal\n”);
}
return 0;
}
Embedded C
Pointers