Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

1 embedded_system

Embedded System
PART 1(C Programming)
C-Basics
ENG.KEROLES SHENOUDA

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

2
Why C, and not another language?
embedded_system

 The primary design of C is to produce portable code while maintaining


performance and minimizing footprint (CPU time, memory, disk I/O, etc.). This
is useful for operating systems, embedded systems or other programs where
performance matters a lot ("high-level" interface would affect performance)
 One powerful reason is memory allocation. Unlike most programming
languages, C allows the programmer to write directly to memory.
 C gives control over the memory layout of data structures
 Moreover dynamic memory allocation is under the control of the programmer
(which also means that memory deallocation has to be done by the
programmer).

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

3
History
embedded_system

 Kernighan and Ritchie, in their first edition (1978) of "The C Programming


Language", provided the initial description of C. We call this description
"K&R" C. The language has since undergone three major revisions: C89, C95
and C99.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

4
Compilation Process
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

5
C - Environment Setup
embedded_system

IDE + Compiler
1. Install MinGW GCC
Install Eclipse C/C++ Development Tool (CDT) Minimalist GNU for Windows
https://sourceforge.net/projects/mingw/file
Java Downloads for All Operating Systems s/

2. Cygwin GCC
https://www.cygwin.com/setup-x86.exe
Cygwin is:
a large collection of GNU and Open Source tools
which provide functionality similar to a Linux
distribution on Windows.
a DLL (cygwin1.dll) which provides substantial
POSIX API functionality.
https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Install MinGW GCC


6 embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Install MinGW GCC


7 embedded_system

En you can see all the basic packets


became green

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Install MinGW GCC


8 embedded_system

Then you can see the


gcc and gdb and other
binary utilities installed
(Native Toolchain )

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Install MinGW GCC


9 embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Install MinGW GCC


10 embedded_system

En you can see all the basic packets


became green

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Install MinGW GCC


11 embedded_system

Then you can see the


gcc and gdb and other
binary utilities installed
(Native Toolchain )

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

12
Variable Name
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

13
Comments
embedded_system

 Sometimes programmers need


to add some notes beside their
code. Those notes are very
important to describe the code
and to clarify complex operation.
Notes or comments can be
added in two ways as shown in
the following example.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press

Data Types
here
#LEARN_IN DEPTH

#Be_professional_in

14 embedded_system

Derived Primitive/Basic Types


User Defined
pointer Real Values Integer Values
Arrays

structure union
enum typedef unsigned signed

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here

Integer Values 15
#LEARN_IN DEPTH

#Be_professional_in
embedded_system

Unsigned Integer
size_in_bits
0 >>> (2 -1)
Signed Integer
long long 8 –9,223,372,036,854,775,808 to (size_in_bits-1)
9,223,372,036,854,775,807 (size_in_bits-1)

unsigned long long 8 0 to 18,446,744,073,709,551,615


-(2) >>> +(2 -1)

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

16
For example “char”
embedded_system

For example if the Unsigned charvalue uses one byte (8 bits) to


hold the numeric value:

Another example if the Signed char value uses one byte (8 bits)
to hold the numeric
value. If Tow‟s Complement method is used to represent the
negative values:

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

17
For example “char”
embedded_system

For example if the Unsigned short value uses one byte (16 bits)
to hold the numeric value:

Another example if the Signed Integer value uses four byte (32 bits) to hold
the numeric
value. If Tow‟s Complement method is used to represent the negative values:

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

18
Note
embedded_system

int, this data type called the machine dependent data type, which means its
size and range
vary from a machine type to another machine type (EX: in 8 bit computers int
is 1 byte, in
16 bit computers int is 2 bytes, in 32 bit computers int is 4 bytes, in 64 bit
computers int is 8
bytes).
Know that (32 Bits computers) means the principle data unit size in those
computers are 32
bit, which mean the computer is designed and optimized to process 32 bit
values

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

19
Floating-Point Types
embedded_system

Type Storage size Value range Precision


float 4 byte 1.2E-38 to 6 decimal
3.4E+38 places
double 8 byte 2.3E-308 to 15 decimal
1.7E+308 places
long double 10 byte 3.4E-4932 to 19 decimal
1.1E+4932 places

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

20
2′s Complement
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

21
bool is a data type ?
embedded_system

#include <stdio.h>
#include <stdbool.h>
int main()
{
bool a = true;
return 0;
}

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

22
bool
embedded_system

 Traditionally, there was no boolean type in C. However, C99 defines a standard


boolean type under <sdbool.h> header file. A boolean type can take one of
two values, either true or false. For example:
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool a = true;
return 0;
}
https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

23
quiz:  what is the output
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

24
quiz:  what is the output
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

25
Integer and Float Conversions
embedded_system

 In order to effectively develop C programs, it will be necessary to understand


the rules that are used for the implicit conversion of floating point and integer
values in C. These are mentioned below. Note them carefully.
 An arithmetic operation between an integer and integer always yields an integer
result.
 An operation between a real and real always yields a real result.

 An operation between an integer and real always yields a real result. In this
operation the integer is first promoted to a real and then the operation is
performed. Hence the result is real.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

26
Type Conversion in Assignments
embedded_system

 Here in the first assignment statement though the expression’s value is a float
(3.5) it cannot be stored in i since it is an int.
 In such a case the float is demoted to an int and then its value is stored. Hence
what gets stored in i is 3.
 Exactly opposite happens in the next statement. Here, 30 is promoted to
30.000000 and then stored in b, since b being a float variable cannot hold
anything except a float value.

int i ;
float b ;
i = 3.5 ;
b = 30 ;

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

27
Type Conversion in Assignments
embedded_system

 in the assignment statement some operands are ints whereas others are
floats. As we know, during evaluation of the expression
 the ints would be promoted to floats and the result of the expression would
be a float. But when this float value is assigned to s it is again demoted to an
int and then stored in s.

float a, b, c ;
int s ;
s = a * b * c / 100 + 32 / 4 - 3 * 1.1 ;

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here

Quiz  What is the output


#LEARN_IN DEPTH

#Be_professional_in

28 embedded_system

k is an integer variable and a is a real variable.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here

Quiz  What is the output


#LEARN_IN DEPTH

#Be_professional_in

29 embedded_system

k is an integer variable and a is a real variable.

In the first statement, since both 2 and 9 are integers, the result is
an integer, i.e. 0. This 0 is then assigned to k. In the second
statement 9 is promoted to 9.0 and then the division is performed.
Division yields 0.222222. However, this cannot be stored in k, k
being an int. Hence it gets demoted to 0 and then stored in k.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

30
Hierarchy of Operations
embedded_system

While executing an arithmetic statement, which has two or more Example 1.1: Determine the hierarchy of operations and
evaluate
operators, we may have some problems as to how exactly does it the following expression:
get executed. For example, does the expression 2 * x - 3 * y i=2*3/4+4/4+8-2+5/8
correspond to (2x)-(3y) or to 2(x-3y)? Similarly, does A / B * C
correspond to A / (B * C) or to (A / B) * C? To answer these i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: *
questions satisfactorily one has to understand the ‘hierarchy’ of i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /
operations. i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i = 1 + 1 + 8 - 2 + 0 operation: /
i = 2 + 8 - 2 + 0 operation: +
i = 10 - 2 + 0 operation: +
i = 8 + 0 operation : -
i = 8 operation: +

https://www.facebook.com/groups/embedded.system.KS/
Follow us

C Programming Input Output (I/O):


Press
here
#LEARN_IN DEPTH

#Be_professional_in

31 embedded_system

printf() and scanf()


 C programming has several in-built library functions to perform input and
output tasks.

 Two commonly used functions for I/O (Input/Output) are printf() and scanf().

 The scanf() function reads formatted input from standard input (keyboard)
whereas the printf() function sends formatted output to the standard output
(screen).

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

32
Input Output
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press

Eclipse's terminal emulator issue


here
#LEARN_IN DEPTH

#Be_professional_in

33 embedded_system

with scanf()
 Eclipse's terminal emulator
might be different and do
more buffering. Try calling
fflush(stdout); between the
printout and the call to
scanf().

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

34
C Floats Input/Output
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

35
int printf( const char* format, ... );
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

36
Quiz
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

37
Quiz
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

38 embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

39 embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Printf
Press
here
#LEARN_IN DEPTH

#Be_professional_in

40 embedded_system

Tricks

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

41
Solution
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

42 embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

43
Mathematical and Logical
embedded_system

Expressions

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

44
Mathematical and Logical
embedded_system

Expressions

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

45
Mathematical and Logical
embedded_system

Expressions

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

46
Coding Convention
embedded_system

 Coding convention is a set of rules that enhance the readability and the
understandability of
the code. At the end of each chapter a list of standard and related coding
convention is
mentioned.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

47
Coding Convention
embedded_system

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

48
Generally following roles must be obeyed to
embedded_system

write an arranged code.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

C Fundamentals 49 embedded_system

Statements
 Control Statements
 if
 switch
Comments
 goto Operators
 for loop Keywords Identifiers
Relational operators
 while loop
Logical operators
 do-while loop
 break
Assignment operators Constants Data Types
 continue Arithmetic operators 1.Integer constants
 Nested Loop 2.Floating-point User Defined
 null statement constants
 expression statement 3.Character constants Primitive/Basic Types
4.String constants
Conditional operators
enum typedef Real Values Integer Values
min = (x < y) ? x : y;
Identifier = (test expression)? Expression1: Expression2 ;
Derived unsigned signe
Bitwise Operators d
Comma operators

int i , j; Arrays pointer


i=(j=10,j+20);
A set of expression separated by comma is a valid structure union
constant in the C language
https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

Identifiers
#Be_professional_in

50 embedded_system

 Identifiers are the names that are given to various program elements
such as variables, symbolic constants and functions.
 Identifier can be freely named, the following restrictions.

 Alphanumeric characters ( a ~ z , A~Z , 0~9 ) and half underscore ( _ )


can only be used.
 The first character of the first contain letters ( a ~ z , A~Z ) or half
underscore ( _ ) can only be used.

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

51
Identifiers
embedded_system

 Here are the rules you need to know:


 1. Identifier name must be a sequence of letter and digits, and must begin with a letter.
 2. The underscore character (‘_’) is considered as letter.
 3. Names shouldn't be a keyword (such as int , float, if ,break, for etc)
 4. Both upper-case letter and lower-case letter characters are allowed. However, they're not interchangeable.
 5. No identifier may be keyword.
 6. No special characters, such as semicolon,period,blank space, slash or comma are permitted
 Examples of legal and illegal identifiers follow, first some legal identifiers:
 float _number;
 float a;

 The following are illegal (it's your job to recognize why):


 float :e; float for; float 9PI; float .3.14; float 7g;

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

52
Keywords
embedded_system

 Keywords are standard identifiers that have standard predefined meaning in C. Keywords
are all lowercase, since uppercase and lowercase characters are not equivalent it's possible
to utilize an uppercase keyword as an identifier but it's not a good programming practice.
 1. Keywords can be used only for their intended purpose.
 2. Keywords can't be used as programmer defined identifier.
 3. The keywords can't be used as names for variables.
 The standard keywords are given below:

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

53
Integer Literals
embedded_system

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix


specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for
decimal.
An integer literal can also have a suffix that is a combination of U and L, for
unsigned and long, respectively. The suffix can be uppercase or lowercase and
can be in any order.
Here are some examples of integer literals:

https://www.facebook.com/groups/embedded.system.KS/
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

54
Floating-point Literals
embedded_system

A floating-point literal has an integer part, a decimal point, a fractional part, and
an exponent part. You can represent floating point literals either in decimal form
or exponential form.
While representing decimal form, you must include the decimal point, the
exponent, or both; and while representing exponential form, you must include
the integer part, the fractional part, or both. The signed exponent is introduced
by e or E.
Here are some examples of floating-point literals:

https://www.facebook.com/groups/embedded.system.KS/

You might also like