c programming session3

41
C_Programming Part 3 ENG. KEROLES SHENOUDA 1

Upload: keroles-karam

Post on 21-Jan-2018

662 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: C programming  session3

C_ProgrammingPart 3

ENG. KEROLES SHENOUDA

1

Page 2: C programming  session3

C Fundamentals2

IdentifiersKeywords

Data Types

OperatorsComments

Statements

Constants1.Integer constants 2.Floating-point constants 3.Character constants 4.String constants

Control Statements if switch goto for loop while loop do-while loop break continue Nested Loop

null statement expression statement

Arithmetic operators

Relational operators

Logical operators

Assignment operators

Conditional operators

Comma operatorsBitwise Operators

min = (x < y) ? x : y;Identifier = (test expression)? Expression1: Expression2 ;

int i , j;

i=(j=10,j+20);A set of expression separated by comma is a valid

constant in the C language

User Defined

enum typedef

Derived

Arrays

structure union

pointer

Primitive/Basic Types

Integer ValuesReal Values

signe

dunsigned

Page 3: C programming  session3

Identifiers

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.

3

Page 4: C programming  session3

Identifiers

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;

4

Page 5: C programming  session3

Keywords

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:

5

Page 6: C programming  session3

Controlling Program Flow 6

Page 7: C programming  session3

Conditions 7

Page 8: C programming  session3

Example : Using Conditions

#include "stdio.h"

#include "math.h"

void main()

{

int a = 9;

int b = 8;

int c = 12;

printf("%d\r\n", a>b);

printf("%d\r\n", b>c);

printf("%d\r\n", a<=9);

printf("%d\r\n", a!=9);

printf("%d\r\n", (a-b)>(c-b));

printf("%d\r\n", a>b && c>b);

printf("%d\r\n", a>b && c<b);

printf("%d\r\n", a>b || c<b);

printf("%d\r\n", !(a<b));

printf("%d\r\n", 3 && 0);

printf("%d\r\n", -15 || 0);

printf("%d\r\n", !(-15));

}

8

Page 9: C programming  session3

Example :Using Conditions

#include "stdio.h"

#include "math.h"

void main()

{

int a = 9;

int b = 8;

int c = 12;

printf("%d\r\n", a>b); //prints 1

printf("%d\r\n", b>c); //prints 0

printf("%d\r\n", a<=9); //prints 1

printf("%d\r\n", a!=9); //prints 0

printf("%d\r\n", (a-b)>(c-b)); //prints 0

printf("%d\r\n", a>b && c>b); //prints 1

printf("%d\r\n", a>b && c<b); //prints 0

printf("%d\r\n", a>b || c<b); //prints 1

printf("%d\r\n", !(a<b)); //prints 1

printf("%d\r\n", 3 && 0); //prints 0

printf("%d\r\n", -15 || 0); //prints 1

printf("%d\r\n", !(-15)); //prints 0

}

9

Page 10: C programming  session3

if Statement

if(/*if condition*/)

{

//if body

}

else if(/*else if condition*/)

{

//else if body

}

else if(/*else if condition*/)

{

//else if body

}

else

{

//else body

}

10

Page 11: C programming  session3

Calculate Circle Area or Circumference11

In this program the user has to choose between calculating circle area or circle

circumference. The choice comes by taking a character from the keyboard using the (getche)

function. If the user presses „a‟ character it proceeds with area calculation and printing. If the

user presses „c‟ character it proceeds with circumference calculation and printing. If the user

presses other letters the program prints an error message.

Page 12: C programming  session3

12

Page 13: C programming  session3

Comparing Three Numbers13

This program finds the largest value of the three given values.

Page 14: C programming  session3

14

Page 15: C programming  session3

Inline condition / Conditional operators

Sometimes it is required to take a fast decision inside your statements; this is called the inlinecondition. Following examples illustrate the idea.

15

min = (x < y) ? x : y;Identifier = (test expression)? Expression1: Expression2 ;

Page 16: C programming  session3

Calculate the Minimumof Two Numbers

16

Page 17: C programming  session3

17

Page 18: C programming  session3

switch Statement

switch(/*switch expression*/)

{

case /*case value*/:{

//case body

}

break;

...

...

...

case /* case value*/:

{

//case body

}

break;

default:

{

}

break;

}

18

Page 19: C programming  session3

Calculate Circle Area or Circumference

19

Page 20: C programming  session3

for Statement 20

Page 21: C programming  session3

Printing Hello World in a Loop 21

Page 22: C programming  session3

Printing Hello World in a Loop 22

Page 23: C programming  session3

Calculate the Summation of values between 1 and 99

23

Page 24: C programming  session3

Calculate the Summation of values between 1 and 99

24

Page 25: C programming  session3

Calculate the Average Students Degrees

25

calculates the average students degree for any given students

number.

Page 26: C programming  session3

26

Page 27: C programming  session3

while Statement 27

Page 28: C programming  session3

Calculate the Summation of odd values between 1 and 99

28

Page 29: C programming  session3

Calculate the Average Students Degrees 29

Important:

break

statement is

used to exit

from any loop

type.

Page 30: C programming  session3

do…while Statement 30

Page 31: C programming  session3

Calculate Polynomial Value 31

Page 32: C programming  session3

goto Statement 32

Page 33: C programming  session3

goto Statement 33

Page 34: C programming  session3

break statement

The break statement is a jump instruction and can be used inside a

switch construct, for loop,

while loop and do-while loop.

The execution of break statement causes immediate exit from the concern construct and the control is transferred to the statement following the loop.

34

Page 35: C programming  session3

break statement

The break statement is a jump instruction

and can be used inside a switch construct,

for loop, while loop and do-whileloop.

The execution of break statement causes immediate exit from the concern construct and the control is transferred to the statement following the loop.

35

Page 36: C programming  session3

continue statement

Continue statement is used to continue the next iteration of for loop, while loop and do-while loops. So, the remaining statements are skipped within the loop for that particular iteration.

Syntax : continue;

36

Page 37: C programming  session3

continue statement

Continue statement is used to continue the next iteration of for loop, while loop and do-while loops. So, the remaining statements are skipped within the loop for that particular iteration.

Syntax : continue;

37

Page 38: C programming  session3

Nested loop In many cases we may use loop statement inside another looping statement.

This type of looping is called nested loop

38

Page 39: C programming  session3

Write a program that produces the following output:

39

Page 40: C programming  session3

Follow Chapter 3: Controlling Program FlowC PROGRAMMING FOR ENGINEERS, DR. MOHAMED SOBH

40

Page 41: C programming  session3

References 41

The Case for Learning C as Your First Programming Language

A Tutorial on Data Representation

std::printf, std::fprintf, std::sprintf, std::snprintf…..

C Programming for Engineers, Dr. Mohamed Sobh

C programming expert.

fresh2refresh.com/c-programming