Uploaded by Mustafa Shiple

Embeded C DataTypes

advertisement
Agenda
1.
2.
3.
4.
Data Types
Data Types Qualifications
Data Types Modifications
Data Types Storage class
Embedded C
Numerical Systems
example
system
starts with
16.1 , 61e1
Decimal
non-zero
075
0xfab
Octal
Hexadecimal
zero
0x
‘a’
“abc”
Character
String add null char (4 chars)
“\n” , “\r”
control character
Data type Variables
Type
Bytes
char
1
int
2
float
4
double
4
Description
Range
A single byte of memory that
unsigned 0…255
defines characters
Used to define integer numbers unsigned 0…65535
Single precision floating point
1.175e-38…3.40e+38
number in IEEE format
Single precision floating point
1.175e-38…3.40e+38
number in IEEE format
1 if enum
< 256 Used to define a list of aliases
enum
that represent integers.
2 if enum
> 256
0…65535
Data Type Modifiers
Type
Bytes
char
1
int
2
short
2
Description
A single byte of memory that
defines characters
Range
unsigned 0…255
signed -128…127
unsigned 0…65535
Used to define integer numbers
signed 32768…32767
unsigned 0…65535
Standard type specifying 2signed byte integers
32768…32767
Data Type Modifiers
Type
Bytes
long
4
float
4
double
4
Description
Range
unsigned
0…4294967295
Standard type specifying the
signed largest integer entity
2147483648…21474
83647
Single precision floating point
1.175e-38…3.40e+38
number in IEEE format
Single precision floating point
1.175e-38…3.40e+38
number in IEEE format
1 if enum
< 256 Used to define a list of aliases
enum
that represent integers.
2 if enum
> 256
0…65535
Type casting
– To convert from long to int the programmer has to
manually type cast the value
– To do type casting, the value to cast has to be
preceded by the target type enclosed in parantheses
int Val16bit;
// short integer (16 bits)
long Val32bit; // long integer (32 bits)
Val16bit = (int) Val32bit; // type casting
// the higher 16 bits are lost
– Information may be lost by type casting
– The range of a value should be checked before doing
manual type casting
Data Type Naming Conventions
Avoid basic types (‘char’, ‘int’, ‘short’, ‘long’) in application code
Memory Alignment
*Source: Freescale
Data Type Storage Class
• Size modifier
Variables life time
• Global
• Global storage and global scope
• Static
• Global storage and local scope
• Local
• Local storage and local scope
Example (static)
Static Functions
Static Functions
Data Type Qualifiers
• Constant Qualifier
• Volatile Qualifier
Volatile Variables
Volatile Variables are Never
Optimized
Const
The addition of a 'const' qualifier indicates that the (relevant part of the)
program may not modify the variable
const int x = 5;
An alternate form
int const x = 5;
• It is safe to assume that a parameter along with the keyword
“const” means a “read-only” parameter.
• Some compilers create a genuine variable in RAM to hold the
const variable. Upon system software initialization, the “read-only”
value is copied into RAM. On RAM-limited systems, this can be a
significant penalty.
int * const cp = &i;
const int * ptci;
int const * ptci;
/* const pointer to int */
/* pointer to const int */
/* pointer to const int */
Typedef Const.
typedef int * ip_t;
const ip_t cp1 = &i;
ip_t const cp2 = &i;
/* const pointer to int */
/* const pointer to int!! */
const int x = 5;
int * y = &x;
:::::::::
::::::::
*y = 16; /* Completely valid! */
const int * const y = &x;
Any variation Will be caught by compiler
C operators
C operators
• Assignment
=
Assignment
MyVar = 5; // assign five to variable MyVar
• Relational operators
<, <=, >=, >,==
Less than, less than or equal to, greater than or equal to, greater than,
equality.
• Take care
MyVar2 = MyVar >= 5;
// if MyVar is greater than or equal to five
// the result is one (TRUE), otherwise zero (FALSE)
Arithmetic operators
• +, -, *, /
Add, subtract, multiply, divide
– Var2 = Var + 5;
• % Modulo
– Var2 = Var % 5; // if Var=13 the result= 3
• ++, -Increment, decrement
– Var++; // Var is incremented by one
• |, &, ^, ~ Bit wise OR, AND, Xor and NOT
– Var2 = Var & 0x0F;
• >>, <<
Bit wise shift right and left
– Var2 = Var << 3;
// Var =00010010.
// Var2 =10010000.
Increment and decrement
Assume X is 5
Boolean operators
–~
Boolean NOT
Var2 = ~ Var;
// Var=00010010
// Var2=11101101
– ==, != Equal to, not equal to
Var2 = Var == 5;
// Var=5,
// Var2 =TRUE
– &&, ||
Boolean AND and OR
Expressions compressed forms
Compiler Basics (Operators)
Compiler Basics (Precedence)
Operators of the same precedence are applied right to left
Program Syntax
• Comments
• For multiple line comments
– /* comment
comment
comment */
• Single line comment
– /* comment */
– // comment
Functions
type
name
int FunctionName( )
{
return
{}
return 5;
}
Calling function
Examples
FunctionName( );
Result = FunctionName( );
The void Keyword
• The void keyword is used to
explicitly state no value
int FunctionName(void)
void FunctionName(int Param)
void* pNothing
Conditional Expressions
– Conditional execution using the if and else
statements
– Conditional expressions with the ternary
operator ? :
– Conditional execution using switch and case
if (a > b)
z = a;
else
z = b;
// checks the expression a > b
// statement executed if true
// statement executed if false
z = (a > b) ? a : b; // z = max(a, b)
Nested conditions
if(d == 1)
printf("Mon");
else if(d == 2)
printf("Tue");
else if(d == 3)
printf(“Wed");
else if(d == 4)
else
printf("<invalid>");
Note where ; is
?!!
Nested conditions (Cont.)
• switch(d)
{ case 1:
printf("Mon");
// d = 1
case 2:
printf("Tue");
// d=2
break;
default:
// d= others
printf("<invalid>"); }
Flow control
– Loop statements
• for
• do
• while
– Jump statements
• break
• continue
• goto
• return
Loop Statements
int c = 0;
While (c < 8)
Printf ("%d ", ++c);
Check then do!!
int c = 0;
do printf("%d\n", ++c);
while(c < 8);
Do then check!!
For loop
For (c = 1; c <= 8; c++)
Printf ("%d ", c);
for( ; ; )
Jump statements
Loop begin (for, do , while);
break
Loop end;
Next st;
For (c = 1; c <= 8; c++)
{Continue;
Statment1;
Statement2;}
Loop begin ( do , while);
Continue; Test condition
Statment1;
Statement2;
Loop end;
Jump statments
goto err;
…
err:
// jump directly to label err
// define label err
Download
Random flashcards
State Flags

50 Cards Education

Countries of Europe

44 Cards Education

Art History

20 Cards StudyJedi

Sign language alphabet

26 Cards StudyJedi

Create flashcards