C programming day#1

Post on 16-Jul-2015

330 views 5 download

Transcript of C programming day#1

With/Mohamed Fawzy

Quick Review for C basics

1 Day #1

2

Lecture Notes:

Set your phone to vibration mode.

Ask any time.

During labs feel free to check any materials or internet.

3

Contents

History of C programming language.

Introduction to C programming language.

Variables and data types.

Expressions and operators.

Decision making.

Hands ON.

4

Contents

4

History of C programming language.

Introduction to C programming language.

Variables and data types.

Expressions and operators.

Decision making.

Hands ON.

C Programming History:

In 1969, Dennis Ritchie and Ken Thompson at “AT&T”

labs (Bell Labs).

C came into being in the years 1969-1973, in parallel

with the early development of the Unix operating

system.

To make UNIX operating system portable.

5

Why C in Embedded Systems?

6

Next program structure.

C is very close to assembly programming and it allows very easy access

to underlying hardware. A huge number of high quality compilers and

debugging tools are available for the C language.

Though C++ is theoretically more efficient than C, but some of its

compilers have bugs due to the huge size of the language. These

compilers may cause a buggy execution.

C language can definitely claim to have more mature compilers C++. Now

in order to avail the extra benefits of C++ and plus to avoid buggy

execution, experts are doing efforts to identify a subset of C++ that can be

used in embedded systems and this subset is called Embedded C++

7

8

C program Structure.

/**information about the file*/

/**includes files and libraries*/

#include <stdio.h>

#include <stdlib.h>

/**functions prototypes*/

void main();

void function1();

int main(){

//main code of the program

return 0;

}

void function1(){

//function1 code

}

NextComments.

9

Comments.

/*

……………

……………

Multi-line comment

……………..

……………..

*/

// single row comment

• Ignored by compiler. • Can place any where.

Next#include macro.

.

10

#include macro.

•It is used to include header files in our project. •It may include constants, functions , declarations.

•#include reads the content of header file.

•#include <header.h> searches for a header file in the include path.

•#include “header.h” searches for a header file in the current directory for main project.

Next Variables in C.

.

Variables in C.

Variable names are names given to locations in memory.

Rules for Constructing Variable Names:

oA variable name is any combination of 1 to 31 alphabets ”compiler based” oThe first character in the variable name must be an alphabet or

underscore.

oNo commas or blanks are allowed within a variable name.

oNo special symbol other than an underscore.

Examples: int student_num;

char _student_num;

int 22student-num;

char student,num;

int student@num;

11

Practical Advice:

It is a good practice to exploit

this enormous choice in naming

variables by using meaningful

variable names.

Next declare a variable in C.

.

How to declare a variable in C?

<Data Type> <variable name>;

<Data Type> <variable name> = <initial value>;

Examples:

//variable declaration with no initialization.

int student_num;

//variable declaration with intiial value.

char student_num=15;

//multiple variable declaration.

int num,student,id;

12

Note:

If you don't initiate the variable it will take garbage value.

Next Variables in C.

.

13

C data types:

Note:

You can use sizeof() function to

know the default size of variable.

EX: Printf(“%d”,sizeof(int));

Practical Advice:

In E.S you should take care for

choosing the best suitable to

avoid wasting memory.

14

C data type Modifiers:

Note:

Signed modifier is the default modifier for any data type.

EX:

Signed char x=char x;

15

Type Casting.

Type casting is to put variable in different type during run time.

Example:

int main ()

{

char x;

int y;

X=(char)y;

}

Note:

Some C compilers my give warning if assigned large data types values to small

data types values without using casting.

int main ()

{

char x;

int y=40000;

x=(char)y;

printf(“%d”,y);

printf(“%d”,x);

}

Try this !!!!!!

16

printf.

To print constants :

printf(“Hello World !”);

To print variables: Int x=50;

Float y=5.6;

Printf(“x is equal %d”,x);

Printf(“x is equal %d and y is equal %f”,x,y);

Format Specifiers:

17

scanf.

To receive an input from user: int x;

printf(“plz enter x value”);

scanf(“%d”,&x);

Hands ON

Write a C program to print “Hello World!”

Declare to variables and ask user to assign their values.

18

Operators & Expressions.

Expression: it is a statement which an operator links identifiers.

Operators: they are some symbols that indicate to the compiler which type of

Operator is to be performed using its surrounding identifiers.

Arithmetic Operators.

Relational Operators.

Logical Operators.

Bitwise Operators.

Assignment Operators.

19

Arithmetic Operators.

Note:

• (%) is used only with integers.

• (++) and (- -) increment or decrement by one not add or subtract one .

Assume variable A holds 10 and variable B holds 20.

20

Example:

#include <stdio.h>

main()

{

int a = 21;

int b = 10;

int c ;

c = a + b;

printf("Line 1 - Value of c is %d\n", c );

c = a - b;

printf("Line 2 - Value of c is %d\n", c );

c = a * b;

printf("Line 2 - Value of c is %d\n", c );

c = a % b;

printf("Line 2 - Value of c is %d\n", c );

c = a++;

printf("Line 2 - Value of c is %d\n", c );

printf("Line 2 - Value of c is %d\n", a++);

21

Relational Operators.

Relational operators are used in decision making and loops in C programming.

Note:

• If the relation is true, it returns value 1 and if the relation is false, it returns value 0.

• Don't be confused between (=) and (==).

22

Examples:

#include <stdio.h>

main()

{

int a = 21;

int b = 10;

int c ;

if( a == b )

{

printf("Line 1 - a is equal to b\n" );

}

else

{

printf("Line 2 - a is not equal to b\n" );

}

}

23

Logical Operators.

Note:

Don't be confused between (&&) and (&) or (||) and (|).

24

Examples:

int a=5;

int b=6;

if (a && b) printf(“true”);

if (a || b) printf(“true”);

int x=0;

int y=5;

if(x && y++)

{

printf (“false\n”);

printf(“%d”,y);

}

Take Care!!

int x=0;

int y=5;

if(y || x++)

{

printf (“true\n”);

printf(“%d”,x);

}

25

Bitwise Operators.

Bitwise operator works on bits and perform bit-by-bit operation.

26

Control single bit in Register or Byte:

Reset one bit.

REG &=~(1<<bit_no)

PORTA &=~(1<<5)

Toggle one bit.

REG ^=(1<<bit_no)

PORTA ^=(1<<5)

Set one bit.

REG |=(1<<bit_no)

PORTA |=(1<<5)

Examples:

Check one bit.

REG &=(1<<bit_no)

PORTA &=(1<<5)

27

Decision Making.

If statement.

if (condition)

{

}

else if (condition)

{

}

else

{

}

28

Decision Making.

Switch (var)

{

Case 1:

statement;

break;

Case 1:

statement;

break;

Case 1:

statement;

break;

default:

statement;

break;

}

29

Hands ON

Email: mo7amed.fawzy33@gmail.com

Phone: 01006032792

Facebook: mo7amed_fawzy33@yahoo.com

Contact me:

30

31