cpu

22
VISHWAKARMA GOVERNMENT ENGINEERING COLLAGE sky

Upload: vishwakarma-goverment-enginnering-collage

Post on 21-Feb-2017

121 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: CPU

VISHWAKARMA GOVERNMENT ENGINEERING COLLAGE

sky

Page 2: CPU

COMPUTER PROGRAMING & UTILIZATION

Prepared by: Meet Patel 140170119056Sindhi Parth 140170119057Soni Kunjan 140170119058Thakor Chetan 140170119059Thakor Milan 140170119060

Page 3: CPU

OUTLINE

Data Types & Variables

Arithmetic & Logical Operations

Conditionals Loops

Page 4: CPU

Name Description Size* Range*

char Character or small integer

1 byte signed: -128 to 127unsigned: 0 to 255

short int(short)

Short integer 2 bytes signed: -32768 to 32767unsigned: 0 to 65535

int Integer 4 bytes signed: -2147483648 to 2147483647unsigned: 0 to 4294967295

long int(long)

Long integer 4 bytes signed: -2147483648 to 2147483647unsigned: 0 to 4294967295

float Floating point number

4 bytes 3.4e +/- 38 (7 digits)

double Double precision floating point number

8 bytes 1.7e +/- 308 (15 digits)

long double

Long double precision floating point number

8 bytes 1.7e +/- 308 (15 digits)

Data types

Page 5: CPU

Variable Declarationint length = 100; char num = ‘9’; //The actual value is 57float deposit = 240.5;unsigned short ID = 0x5544;

Try the following statements, and see what happensunsigned char value = -1;printf(“The value is %d \n”, value);

unsigned char value = 300;printf(“The value is %d \n”, value);

Page 6: CPU

Result

Definition Memory layout Display comment

unsigned char value = -1

11111111 255

unsigned char value = 300

00101100 44 overflow

Page 7: CPU

Local variableLocal variables are declared within the body of a function, and can only be used within that function.

Static variableAnother class of local variable is the static type. It is specified by the keyword static in the variable declaration. The most striking difference from a non-static local variable is, a static variable is not destroyed on exit from the function.

Global variable A global variable declaration looks normal, but is located outside any of the program's functions. So it is accessible to all functions.

Variable types

Page 8: CPU

Variable Definition vs Declaration

Definition Tell the compiler about the variable: its type and name, as well as allocated a memory cell for the variable

Declaration Describe information ``about'' the variable, doesn’t allocate memory cell for the variable

Page 9: CPU
Page 10: CPU

Arithmetic Operations

Page 11: CPU

Arithmetic Assignment Operators

Page 12: CPU

Increment and Decrement Operators

awkward easy easiest

x = x+1; x += 1 x++

x = x-1; x -= 1 x--

Page 13: CPU

Example Arithmetic operatorsint i = 10;int j = 15;int add = i + j; //25int diff = j – i; //5int product = i * j; // 150int quotient = j / i; // 1int residual = j % i; // 5i++; //Increase by 1i--; //Decrease by 1

Page 14: CPU

Comparing themint I = 10;int j = 15;float k = 15.0;

j / i = ?j % i = ?k / i = ?k % i = ?

The Answerj / I = 1; j % I = 5;k / I =

1.5;k % I It

is illegal. Note: For %, the operands can only be integers.

Page 15: CPU

Logical Operations What is “true” and “false” in C

In C, there is no specific data type to represent “true” and “false”. C uses value “0” to represent “false”, and uses non-zero value to stand for “true”.

Logical OperatorsA && B=> A and BA || B => A or BA == B => Is A equal to B?A != B => Is A not equal to B?

Page 16: CPU

A > B => Is A greater than B?A >= B => Is A greater than or equal to B?

A < B => Is A less than B?A <= B => Is A less than or equal to B?

Don’t be confused&& and || have different meanings from & and |.& and | are bitwise operators.

Page 17: CPU

Conditionals if statement

Three basic formats,if (expression){ statement …}if (expression) {statement …

}else{ statement …

}

if (expression) {

statement…} else if (expression) {

statement… } else{

statement… }

Page 18: CPU

The switch statementswitch (expression) { case item1: statement; break; case item2: statement; break; default: statement; break; }

Page 19: CPU

Loops for statement

for (expression1; expression2; expression3){statement…

} expression1 initializes; expression2 is the terminate test; expression3 is the modifier;

Page 20: CPU

The while statementwhile (expression) {statement …}while loop exits only when the expression is false.

An exampleint x = 3; while (x>0) { printf("x=%d n",x); x--; }

Page 21: CPU

for <==> while

for (expression1; expression2; expression3){

statement…}

expression1;while (expression2){statement…;expression3;}

equals

Page 22: CPU

THANK YOU

sky