lecture 02 data types, conversions if statements metu dept. of computer eng. summer 2002 ceng230 -...

39
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Wed Jul 3, 2002

Post on 21-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Lecture 02Data Types, Conversions

if Statements

METU Dept. of Computer Eng. Summer 2002Ceng230 - Section 01 Introduction To C Programmingby Ahmet Sacan

Wed Jul 3, 2002

Today...

• Comments

• Reserved Words: what cannot be used as a variable name.

• More on Data Types: sizes, ranges

• Type conversion

• Characters

more Today...

• if statements

• if - else

• Exercises

• Programming a simple calculator

Comments

• anything between ‘/*’ and ‘*/’ are ignored. So, you can type your comments, poems, explanations, reminders enclosed within /* and */ . Thus:

/*this is a comment*/

int /*hahaha!*/ x =/**/ 5;

is same as:

int x=5;

Reserved Words

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

Data-type Size Range

char 1 bytes -128 to 127

unsigned char 1 bytes 0 to 255

short 2 bytes -32768 to 32767

unsigned short 2 bytes 0 to 65535

int 4 bytes -2147483648 to 2147483647

unsigned int 4 bytes 0 to 4294967295

long 4 bytes -2147483648 to 2147483647

unsigned long 4 bytes 0 to 4294967295

float (6 digits) 4 bytes1.175494e-38 to 3.402823e+38

double (15 digits) 8 bytes

2.225074e-308 to 1.797693e+308

Type Conversion

• conversions among different data types may occur while evaluating an expression.

int x=3.4, y=4;

float z = x;

char c = z;• Two type of conversions:

– Implicit (automatic) – explicit (type-cast)

Implicit (automatic) Type Conversion

• If operands are of the same type, the result will be of this type too

float x,y,z;z = x+y; /*x+y is also float*/• If operands are of different types, they will

be converted to the “broader” type (i.e., int to float) before the calculation

int x; float y, z;z = x+y; /*x+y is float*/

Beware!

short s; int i; long l; float f; double d;• Safe conversion (to broader type)l=i; l=s; i=s;d=i; f=i; /*100 converted to 100.0d=f; /*?*/• Unsafe conversion (loosing information)i=f; /* truncation: 3.14 to 3 */s=l; /* dropping bits */f=d; /* truncation/rounding */

Arithmetic Exercise

int x=9; float f, k=2;

x = (x/2 + 0.5); /* x=? */

x += (x%5 + 1); /* x=? */

x = (x/2.0 + 0.5); /* x=? */

f = 5/2 + 5/k + k/0.5; /*f=?*/

Explicit Conversion (type-cast)

• Most compiler give warnings for possibly unsafe conversions. One type of data can be explicitly forced into another data type.

float f=4.3; int x, y=2;

x=3.7; /*compiler warning*/

x=(int) f; /*ok*/

f = x/(float)y;

Characters

• Character constants are given in single quotes. ‘a’, ‘A’, ‘&’ are characters.

• There are special characters like: ‘\n’, ‘\0’, ‘\t’

• Character constants are represented numerically from 0 to 255.

char c = ‘e’;

int x = c – ‘b’; /* x = ? */

Operators in C

• Unary

• Binary

• Assignment

• comma

Unary Operators

sizeof(i) : the number of bytes of storage allocated to i

+123 : positive 123 -123 : negative 123~i : one's complement (bitwise complement) !i : logical negation (i.e., 1 if i is zero, 0

otherwise) *i : returns the value stored at the address

pointed to by i &i : returns the address in memory of i

unary Examples

a = 00000110 b = 00000011

a | b = 00000111a &b = 00000010

j=3; /* j=00000011 */k=j<<2; /* k=00001100 */ /*k=12*/m=j>>2; /*m=00000000 */ /*m=0*/

more Unary Operators

++i : adds one to i, and returns the new value of i --i : subtracts one from i, and returns the new value of i i++ : adds one to i, and returns the old value of i i-- : subtracts one from i, and returns the old value of i i[j] : array indexing i (j) : calling the function i with argument j i.j : returns member j of structure i i->j : returns member j of structure pointed to by i

Binary Operators

+ : addition - : subtraction * : multiplication / : division % : remainder (e.g., 2%3 is 2), also called 'modulo' << : left-shift (e.g., i<<j is i shifted to the left by j bits) >> : right-shift & : bitwise AND | : bitwise OR ^ : bitwise exclusive-OR

more Binary Operators

&& : logical AND (returns 1 if both operands are non-zero; else 0)

|| : logical OR (returns 1 if either operand is non-zero; else 0)

< : less than (e.g., i<j returns 1 iff i is less than j) > : greater than <= : less than or equal >= : greater than or equal == : equals != : does not equal ? : conditional operator

Assignment Operatorsx<op>= is same as x = x <op>

= assignment += addition assignment -= subtraction assignment *= multiplication assignment /= division assignment %= remainder/modulus assignment &= bitwise AND assignment |= bitwise OR assignment ^= bitwise exclusive OR assignment <<= left shift assignment >>= right shift assignment

Comma operator

• multiple expressions can be separated by comma. The expressions are evaluated in right to left order, and the value of the overall expression is equal to that of the rightmost expression.

x = ((y=3, 2)); /* y=3; x=2; */x = ((y=3, y+1)); /* y=3; x=4; */

evaluation order of Operators

• precedence: the order in which operators are applied. the higher precedence operators are applied first.

• associativity: the order in which operators of the same precedence are applied.

Operator Associativity

() [] ->> . left-to-right

-+ (unary)++ -- ! ~ * & sizeof (type) right-to-left

* / % left-to-right

+ - (arithmetic) left-to-right

<< >> left-to-right

< <= > >= left-to-right

== != left-to-right

& left-to-right

^ left-to-right

| left-to-right

&& left-to-right

|| left-to-right

?: right-to-left

= += -= *= /= %= &= ^= |= <<= >>= right-to-left

, left-to-right

highest precedence

lowest precedence

int x, y=0;

x = ++y; /* x = ?, y = ? */

x = y++; /* x = ? y = ? */

x = (++y); /* x = ? y = ? */

x = (y++); /* x = ? y = ? */

Question...

Expressions, Statements

• Expression: consists of operands (variables or constants) and operators:– Relational: x==0, x<y, x!=10– Arithmetic: x+2, x++, 2*5– Assignment: x=5, y=x

• Statements:– declarative: int x;– simple: x=3;– compound: { x*=4; printf(“%d”, x); }– conditional: if, if-else, switch– controlling (loops): while, do-while, for, (goto)

Programming Goal

• Simple Calculator: Write a program that reads two integers separated with one of the four arithmetic operators, and prints out the result of this arithmetic expression.

If Statement

• if(<test>) <statement>– test can be a relational expression, or any

integer-valued expression. – statement can be

• simple: one-statement ending with one-semicolon.• compound: block of statements surrounded with

{...}• or, the statement can be another if-statement.

A note on relational statements

• 0 (zero) means ‘false’, and any number other than zero means ‘true’.

• also, a conditional truth evaluates to a numerical 1, whereas falsity evaluates to 0.x = (3==2); /* x = ? */

x = (4>-5); /* x = ? */

if examples

if(sayi==10)

printf(“sayi 10’a esittir.”);

if(!(sayi<10))

printf(“sayi >=10 dur.”);

if(sayi)

printf(“sayi 0 degildir.”);

int x=3;if(x != 3) printf(“t”); printf(“a”);if(x % 2){ printf(“hm”); if(x>0){ printf(“e”); }}if(x-3) if(x==3) printf(“s”); printf(“t”);

Find the output of the following:

if-else statement

if (<test>)

<statement>

else

<statement>

• if(x==y) printf(“x is equal to y”);

else

print(“x is NOT equal to y”);

you already know: (if... else if...)

• if(x==y)

printf(“x is equal to y”);

else if(x>y)

printf(“x is greater than y”);

else

printf(“x is smaller than y”);

.

Tip for if statements:

if (<test>) <statement>

• To solidify the scope of the if statements, follow the procedure.– Look at right after the ‘)’ that closes the

<test>. if there’s a ‘{‘, then the scope of the ‘if’ ends at the corresponding ‘}’. if not,

– search for the first semicolon ‘;’ after the ‘)’. The semicolon will determine the end of the if-statement.

exercises (a)

#include <stdio.h>int main() {

int gender = 2;gender--; if (Gender == 1)

printf("The student is male\n"); else

printf("The student is female\n"); return 0;

}

exercises (b)

#include <stdio.h>int main() {

int gender = 2;gender--; if (gender == 1)

printf("The student is male\n"); else;

printf("The student is female\n"); return 0;

}

exercises (c)

#include <stdio.h>int main() {

int gender = 2;if (--gender == 1)

printf("The student is male\n"); else

printf("The student is female\n"); return 0;

}

exercises (d)

#include <stdio.h>int main() {

int gender = 2;if (gender-- == 1)

printf("The student is male\n"); else

printf("The student is female\n"); return 0;

}

exercises (e)

#include <stdio.h>int main() {

int gender = 2;gender--; if (gender == 1);

printf("The student is male\n"); else

printf("The student is female\n"); return 0;

}

exercises (f)

#include <stdio.h>int main() {

int gender = 2;gender = ((gender / 2) % 2) + 0.9; if (gender == 1)

printf("The student is male\n"); else

printf("The student is female\n"); return 0;

}

Programming Homework

• Simple Calculator: Write a program that reads two integers separated with one of the four arithmetic operators, and prints out the result of this arithmetic expression.