c programming day#1

31
With/Mohamed Fawzy Quick Review for C basics 1 Day #1

Upload: mohamed-fawzy

Post on 16-Jul-2015

329 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: C programming day#1

With/Mohamed Fawzy

Quick Review for C basics

1 Day #1

Page 2: C programming day#1

2

Lecture Notes:

Set your phone to vibration mode.

Ask any time.

During labs feel free to check any materials or internet.

Page 3: C programming day#1

3

Contents

History of C programming language.

Introduction to C programming language.

Variables and data types.

Expressions and operators.

Decision making.

Hands ON.

Page 4: C programming day#1

4

Contents

4

History of C programming language.

Introduction to C programming language.

Variables and data types.

Expressions and operators.

Decision making.

Hands ON.

Page 5: C programming day#1

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

Page 6: C programming day#1

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++

Page 7: C programming day#1

7

Page 8: C programming day#1

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.

Page 9: C programming day#1

9

Comments.

/*

……………

……………

Multi-line comment

……………..

……………..

*/

// single row comment

• Ignored by compiler. • Can place any where.

Next#include macro.

.

Page 10: C programming day#1

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.

.

Page 11: C programming day#1

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.

.

Page 12: C programming day#1

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.

.

Page 13: C programming day#1

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.

Page 14: C programming day#1

14

C data type Modifiers:

Note:

Signed modifier is the default modifier for any data type.

EX:

Signed char x=char x;

Page 15: C programming day#1

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 !!!!!!

Page 16: C programming day#1

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:

Page 17: C programming day#1

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.

Page 18: C programming day#1

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.

Page 19: C programming day#1

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.

Page 20: C programming day#1

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++);

Page 21: C programming day#1

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 (==).

Page 22: C programming day#1

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" );

}

}

Page 23: C programming day#1

23

Logical Operators.

Note:

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

Page 24: C programming day#1

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);

}

Page 25: C programming day#1

25

Bitwise Operators.

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

Page 26: C programming day#1

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)

Page 27: C programming day#1

27

Decision Making.

If statement.

if (condition)

{

}

else if (condition)

{

}

else

{

}

Page 28: C programming day#1

28

Decision Making.

Switch (var)

{

Case 1:

statement;

break;

Case 1:

statement;

break;

Case 1:

statement;

break;

default:

statement;

break;

}

Page 29: C programming day#1

29

Hands ON

Page 30: C programming day#1

Email: [email protected]

Phone: 01006032792

Facebook: [email protected]

Contact me:

30

Page 31: C programming day#1

31