c programming part2

39
C_Programming Part 2 ENG. KEROLES SHENOUDA 1

Upload: keroles-karam

Post on 21-Jan-2018

86 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: C programming part2

C_ProgrammingPart 2

ENG. KEROLES SHENOUDA

1

Page 2: C programming part2

2

Page 3: C programming part2

Features of C

- Simple, versatile, general purpose language- Programs are fast and efficient- Has got rich set of operators- more general and has no restrictions- can easily manipulates with bits, bytes and addresses- Varieties of data types are available- separate compilation of functions is possible and such functions can be called by any C program- block-structured language- Can be applied in System programming areas like operating systems, compilers & Interpreters, Assemblers etc.,

3

Page 4: C programming part2

Variable Name 4

Page 5: C programming part2

Comments

Sometimes programmers need to add some notes beside their code. Those notes are veryimportant to describe the code and to clarify complex operation. Notes or comments can beadded in two ways as shown in the following example.

5

Page 6: C programming part2

Data Types 6

Primitive/Basic TypesDerived

User Defined

enum typedef

Arrays

structure union

pointer Integer ValuesReal Values

signedunsigned

Page 7: C programming part2

Integer Values 7

long long 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

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

Unsigned Integer

0 >>> (2 -1)size_in_bits

Signed Integer

-(2)(size_in_bits-1)

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

Page 8: C programming part2

For example “char” 8

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:

Page 9: C programming part2

For example “char” 9

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 numericvalue. If Tow‟s Complement method is used to represent the negative values:

Page 10: C programming part2

Note

int, this data type called the machine dependent data type, which means its

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

10

Page 11: C programming part2

Floating-Point Types

Type Storage size Value range Precision

float 4 byte 1.2E-38 to 3.4E+38

6 decimal places

double 8 byte 2.3E-308 to 1.7E+308

15 decimal places

long double 10 byte 3.4E-4932 to 1.1E+4932

19 decimal places

11

Page 12: C programming part2

IEEE-754 32-bit Single-Precision Floating-Point Numbers

In 32-bit single-precision floating-point representation:

The most significant bit is the sign bit (S), with 0 for positive numbers and 1 for negative numbers.

The following 8 bits represent exponent (E).

The remaining 23 bits represents fraction (F).

12

Page 13: C programming part2

Example 1: Suppose that IEEE-754 32-bit floating-point representation pattern is 0 10000000 110 0000 0000 0000 0000 0000.

Sign bit S = 0 ⇒ positive number

E = 1000 0000B = 128D (in normalized form)

Fraction is 1.11B (with an implicit leading 1) = 1 + 1×2^-1 + 1×2^-2 = 1.75D

The number is +1.75 × 2^(128-127) = +3.5D

13

Page 14: C programming part2

Example 2: Suppose that IEEE-754 32-bit floating-point representation pattern is 1 01111110 100 0000 0000 0000 0000 0000.

Sign bit S = 1 ⇒ negative number

E = 0111 1110B = 126D (in normalized form)

Fraction is 1.1B (with an implicit leading 1) = 1 + 2^-1 = 1.5D

The number is -1.5 × 2^(126-127) = -0.75D

14

Page 15: C programming part2

2′s Complement 15

Page 16: C programming part2

C Programming Input Output (I/O): 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).

16

Page 17: C programming part2

Input Output 17

Page 18: C programming part2

Eclipse's terminal emulator issue 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().

18

Page 19: C programming part2

C Floats Input/Output 19

Page 20: C programming part2

int printf( const char* format, ... );%csdioxXufFeEgG*

%10s%-10s.4s

20%

writes a single character.writes a single character.converts a signed integer into decimal

converts a unsigned integer into octal representationconverts an unsigned integer into hexadecimal representation

converts an unsigned integer into decimal representationconverts floating-point number to the decimal notation

converts floating-point number to the decimal “Scientific”converts floating-point number to"Special valuesTake from , …..Shift 10-string’s length places then print the stringPrint the string then shift to 10-string’s length places Print only 4 char from string

Page 21: C programming part2

int printf( const char* format, ... ); 21

Page 22: C programming part2

Quiz 22

Page 23: C programming part2

Quiz 23

Page 24: C programming part2

24

Page 25: C programming part2

25

Page 26: C programming part2

PrintfTricks

26

Page 27: C programming part2

Solution 27

Page 28: C programming part2

28

Page 29: C programming part2

Data Conversion and Type Casting 29

Page 30: C programming part2

Data Conversion and Type Casting 30

Page 31: C programming part2

Data Conversion and Type Casting 31

The conversion from larger to smaller data types may lead to some data losses. For thatreason the compiler warns you against this type of operation. However sometimes theconversion does not affect the data like the second expression (b = y). Compiler is not able todifferentiate between safe or unsafe situation, for that reason you must use the type castingto force the conversion if you decide that it is safe.

Page 32: C programming part2

Mathematical and Logical Expressions 32

Page 33: C programming part2

Mathematical and Logical Expressions 33

Page 34: C programming part2

Mathematical and Logical Expressions 34

Page 35: C programming part2

Coding Convention

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

35

Page 36: C programming part2

Coding Convention 36

Page 37: C programming part2

Generally following roles must be obeyed towrite an arranged code.

37

Page 38: C programming part2

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

38

Page 39: C programming part2

References 39

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