Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conversion Check your class notes and given examples at class.

Similar presentations


Presentation on theme: "Conversion Check your class notes and given examples at class."— Presentation transcript:

1 Conversion Check your class notes and given examples at class.

2 Check your class notes and given examples at class.
Floating point Check your class notes and given examples at class.

3 Floating point IEEE 754.

4 Choose the biased exponent
(32-bits)(floating point numbers IEEE-754) -18 25 Binary to decimal(32-bit-floating point IEEE-754)

5 Conversion Decimal number to signed integer(32-bit)(2-compliment for negative numbers) -48 118 64 Binary to decimal

6 Placeholders in I/O Statements
Placeholders (or conversion specifiers) will substitute the value of the variable inside the output string (printf). You must absolutely match the placeholder with the variable type. %lf: long floating point (double). %d: decimal (int). %c: character (char). \n: a special character meaning that a new line will be inserted.

7 Integer placeholders %d is the default integer placeholder. When used it will simply display the value as is without any padding. To add padding, to have columns for example, we need formatted placeholders. %nd will reserve n places to display the number. Justification will be to the right. The negative sign takes one place. If the value is 17 and %4d is used, then it will display 2 spaces followed by 17 on the screen. __17 These are spaces, not underscores.

8 Integer placeholders The number is always displayed in its entirety, even when the format is too narrow. With and a %3d placeholder, you would see -1234, therefore using 5 spaces instead of the 3 requested. A negative number change the justification to the left of the field. With a value of and a %-8d placeholder, you will get -1234___ . 3 trailing blanks

9 Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The formatted double placeholder has this format: %w.dlf, where w is the total width of the field (including sign and decimal point) and d the number of decimal digits. If the value is 4.56 and the placeholder is %6.3lf then the display will be _ leading blank

10 Double placeholders With a double formatted, you always get the requested number of decimal digits (even if the field is not wide enough). You also always (like the integer placeholder) get all the significant numbers. However, if there are more decimal precision in the value than in the placeholder, the value is truncated and rounded-up if need be.

11 Double placeholders If the value is and the placeholder is %7.3lf, then you will get 3 decimal digits as requested plus the significant numbers: If the value is and the placeholder is %8.3lf, then you will get 3 decimal digits as requested plus the significant numbers and padding: _ See the rounding-up effect. A %8.7lf for a value of will produce a display of Note: The internal value is unaffected by the placeholder, only its screen appearance.

12 Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The value is 5.87 and the placeholder is %6.3lf then the display will be b5.870 If the value is and the placeholder is %7.3lf ,then you will have 3 decimal digits and the display will be: If the value is and the placeholder is %8.3lf ,then you will have: b ROUNDING-UP effect. Also you could have TRUNCATE. A %8.7lf will produce: Note!!!!!! The internal value is UNAFFECTED by the placeholder, only its screen appearance.

13 Simple Assignment Operator (=)
Assign a value to a variable Does not mean equality, it means assignment Var1='a'; /* Var1  a */ Var2=15; /* Var2 15 */ Var3=27.62; /* Var3  */

14 Initializing Variable
Giving a value to the variable at the time that the variable is declared. char Var1='X'; int Var2=1095; float Var3= ;

15 Scanf() Another way to fill a variable is to ask the user for its value. We do that with the scanf statement. printf(“Enter your score:”); Scanf(“%d”,&score);

16 /. The = operator puts the value on the right. / /
/* The = operator puts the value on the right */ /* into the variable on the left */ #include <stdio.h> int main (void) {/* declarations */ int a, b, c, d, e; a = 10; /* fill variable a */ /* modify variable a few times*/ a = 20; a = 10 + a; a = a + a + 2; a = 2 + a; /* a few more assignments */ b = a; c = b = 5; c = 10 + a; d = a + a + 2; e = 20 + a; a = a - b + c; /* the final values are... */ printf ("a:%4d\n b:%4d\n c:%4d\n d:%4d\n e:%4d\n", a, b, c, d, e); return (0); }

17 /* Numeric Placeholders */
#include <stdio.h> int main (void) { /* declarations */ int a; double x; /* executable statements */ a = 1000; x = ; printf ("%d\n", a); printf ("%3d\n", a); printf ("%4d\n", a); printf ("%5d\n", a); printf ("\n"); printf ("%f\n", x); printf ("%15f\n", x); printf ("%15.4f\n", x); printf ("%18.2f\n", x); printf ("%12.0f\n", x); return (0); }

18 Answer


Download ppt "Conversion Check your class notes and given examples at class."

Similar presentations


Ads by Google