Tutorial 3
This presentation is the property of its rightful owner.
Sponsored Links
1 / 36

Tutorial #3 PowerPoint PPT Presentation


  • 37 Views
  • Uploaded on
  • Presentation posted in: General

Tutorial #3. Summer 2005. Keywords. Names – legal or not?. the_one temp do_it! intro2cs drink-me 1st_street counter. ! is not legal. - is not legal. 1 can not be first. Names – legal or not?. int the_num, the_Num; double Double; double main; int printf;.

Download Presentation

Tutorial #3

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Tutorial 3

Tutorial #3

Summer 2005

Liza Fireman


Keywords

Keywords

Liza Fireman


Names legal or not

Names – legal or not?

the_one

temp

do_it!

intro2cs

drink-me

1st_street

counter

  • ! is not legal

  • - is not legal

  • 1 can not be first

Liza Fireman


Names legal or not1

Names – legal or not?

int the_num, the_Num;

double Double;

double main;

int printf;

Liza Fireman


Significat names

total_cost = work_hours * price_per_hour + parts_cost;

payment = total_cost * (1 + vat);

Significat names

cst = wh * pphr + prt;

pt = cst * (1 + v);

Liza Fireman


Types

27

26

25

24

23

22

21

20

Types

2 bytes = 16 bits  32,000

4 bytes = 32 bits  2,000,000,000

short int long

= 20 + 22 + 23 = 1 + 4 + 8 = 13

Liza Fireman


Hello world program

Hello World Program

2 bytes = 16 bits  65,000

4 bytes = 32 bits  4,000,000,000

unsigned short unsigned int unsigned long

Liza Fireman


Types1

Types

float double long double

חזקה 10 מנטיסה ±=

Liza Fireman


Types2

Types

5000 , -30

256L , 30l

256U , 30u

12300ul 1234567UL

1.5 , -3.0, 4.2e5 , 10e-60

36.7F , 4.2e+5f

36.7L , .5l

Liza Fireman


Characters ascii

Characters - ASCII

Liza Fireman


Chars

chars

#include <stdio.h>

int main()

{

char ch = ‘a’;

char ch2 = 67;

}

Liza Fireman


Chars1

chars

#include <stdio.h>

int main()

{

char ch = 65;

printf(“%d\n”, ch);

printf(“%c”, ch);

}

  • 65

  • A

Liza Fireman


Chars2

chars

#include <stdio.h>

int main()

{

printf(“%c %d\n”, 97, 97);

}

  • a 97

Liza Fireman


Chars3

chars

#include <stdio.h>

int main()

{

printf(“%c %d\n”, ‘b’, ‘b’);

}

  • b 98

Liza Fireman


Chars4

chars

#include <stdio.h>

int main()

{

printf(“%c %c %c\n”, ‘b’, ‘b’+1, ‘b’ + 2);

}

  • b c d

Liza Fireman


Chars5

chars

#include <stdio.h>

int main()

{

char letter;

printf("Enter a lowercase letter: ");

scanf("%c", &letter);

printf("In uppercase: %c", (letter - 'a') + 'A');

}

  • Enter lowercase letter:

f

In uppercase: F

Liza Fireman


Getchar

getchar

#include <stdio.h>

int main()

{

char letter;

printf("Enter a lowercase letter: ");

letter = getchar();

printf("In uppercase: %c", (letter - 'a') + 'A');

}

Liza Fireman


Types3

Types

char → short → int → long → float → double → long double

7 / 3

7.0 / 3

7 / 3.0

  • = 0

Liza Fireman


Types4

Types

int apples = 30, children = 12;

double juice_from_apple = 0.1;

double tot_orange_juice = 5.4;

double liters_per_cup = 0.3;

double orange_juice_per_child = tot_orange_juice / children;

double apple_juice_per_child = (apples / children) * juice_from_apple;

double total_juice_per_child = orange_juice_per_child + apple_juice_per_child;

  • int / int

  • double / int

  • int * double

  • double + double

Liza Fireman


Types5

Types

int apples = 30, children = 12;

double juice_from_apple = 0.1;

double tot_orange_juice = 5.4;

double liters_per_cup = 0.3;

int minimum_cups_per_child = total_juice_per_child / liters_per_cup;

  • int = double

  • double / double

Liza Fireman


Casting

casting

#include <stdio.h>

int main()

{

double d;

d = (double)3 / 2;

}

Liza Fireman


Casting1

casting

#include <stdio.h>

int main()

{

int x = 2;

printf(“%lf”, (double)x);

}

Liza Fireman


Casting2

casting

#include <stdio.h>

int main()

{

int cake_num = 5, children = 3;

double cake_per_child =

}

cake_num / children

Liza Fireman


Casting3

casting

#include <stdio.h>

int main()

{

int cake_num = 5, children = 3;

double cake_per_child =

}

(double)cake_num / children

Liza Fireman


Casting4

casting

#include <stdio.h>

int main()

{

int cake_num = 5, children = 3;

double cake_per_child =

}

cake_num / (double)children

Liza Fireman


Casting5

casting

#include <stdio.h>

int main()

{

int cake_num = 5, children = 3;

double cake_per_child =

}

(double)(cake_num / children)

Liza Fireman


Operators

operators

Liza Fireman


Operators1

Operators

Liza Fireman


Operators2

Operators

  • c=b

  • t = 34 s = 50

int main()

{

char c;

int t = 5, s = 7 ;

double x = 8.5, y = 7.2 ;

c = 'a' ;

t = c + 1;

c = t;

printf("c = %c\n", c);

t = (t - 'a' + 3) * x;

s = s * y;

printf("t = %d , s = %d\n", t , s);

}

  • ‘a’ + 1 = ‘b’ = 98

  • ‘b’ - ‘a’ + 3 = 1 + 3 = 4

  • 7 * 7.2 = 50.4

  • 4 * 8.5 = 34

Liza Fireman


Operators3

Operators

int main()

{

char c;

int t = 5, s = 7 ;

double x = 8.5, y = 7.2 ;

x = (int)(y * s);

y = (int)y * s;

printf(“x = %lf , y = %lf\n", x , y);

return 0 ;

}

  • 7.2 * 50 = 360

  • 7 * 50 = 350

  • x=360.000000, y=350.000000

Liza Fireman


Operators4

Operators

int x , y;

x = 8 ;

y = ++x;

printf(“x = %d y = %d”,x ,y);

  • x= x + 1;

  • y = x;

  • x=9, y=9

Liza Fireman


Operators5

Operators

int x , y;

x = 8 ;

y = x++;

printf(“x = %d y = %d”, x ,y);

  • y = x ;

  • x= x + 1 ;

  • x=9, y=8

Liza Fireman


Operators6

Operators

int n = 5, x , y;

x = n++;

y = ++n;

printf(x = %d , y = %d , x , y ) ;

  • x=5, y=7

Liza Fireman


Operators7

Operators

int x = 5, y ;

y = -x + x ;

y = ++x ;

y = x++ ;

x = ++x + x++ ;

  • -5 + 5 = 0

  • x = 6, y= 6

  • x = 7, y = 6

Liza Fireman


Int and double

Int and double

10 + 20 – 5 – 2

10 * 20 / 8 / 5

10 + 20 * 5

x = 4; y = 5;

z = y += x *= 5;

= 23

= 5

= 110

Liza Fireman


Getchar1

getchar

#include <stdio.h>

int main()

{

char ch;

ch = getchar();

if ((ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’)

printf(“a letter”);

else

printf(“not a letter”);

}

Liza Fireman


  • Login