180 likes | 218 Views
Dive into floating-point conversions, IEEE-754 format, binary-decimal transformations, placeholder usage in I/O statements, and more in C programming with detailed examples and explanations.
E N D
Conversion • Check your class notes and given examples at class.
Floating point • Check your class notes and given examples at class.
Floating point • IEEE 754.
Choose the biased exponent (32-bits)(floating point numbers IEEE-754) -18 25 Binary to decimal(32-bit-floating point IEEE-754) 1100 0100 0011 0000 0000 0000 0000 0000
Conversion • Decimal number to signed integer(32-bit)(2-compliment for negative numbers) • -48 • 118 • 64 • Binary to decimal • 0000 0000 0000 0000 0000 0000 0111 1010
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.
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.
Integer placeholders • The number is always displayed in its entirety, even when the format is too narrow. With -1234 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 -1234 and a %-8d placeholder, you will get -1234___ . 3 trailing blanks
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 _4.560 1 leading blank
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.
Double placeholders • If the value is -32.4573 and the placeholder is %7.3lf, then you will get 3 decimal digits as requested plus the significant numbers: -32.457. • If the value is -32.4578 and the placeholder is %8.3lf, then you will get 3 decimal digits as requested plus the significant numbers and padding: _-32.458. See the rounding-up effect. • A %8.7lf for a value of 187.123 will produce a display of 187.1230000. • Note: The internal value is unaffected by the placeholder, only its screen appearance.
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 -54.6793 and the placeholder is %7.3lf ,then you will have 3 decimal digits and the display will be: -54.679 • If the value is -54.6778 and the placeholder is %8.3lf ,then you will have: b-54.678 .ROUNDING-UP effect. • Also you could have TRUNCATE. • A %8.7lf will produce: -54.6793000 • Note!!!!!! The internal value is UNAFFECTED by the placeholder, only its screen appearance.
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 27.62 */
Initializing Variable Giving a value to the variable at the time that the variable is declared. char Var1='X'; int Var2=1095; float Var3=2277.25;
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);
/* 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); }
/* Numeric Placeholders */ #include <stdio.h> int main (void) { /* declarations */ int a; double x; /* executable statements */ a = 1000; x = 100.383665; 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); }