Presentation is loading. Please wait.

Presentation is loading. Please wait.

+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.

Similar presentations


Presentation on theme: "+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on."— Presentation transcript:

1 + Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on your machine, you may need 1 byte for characters, 2 bytes for integers and 4 bytes for float. You also know that integers are in fact a subset of float & as such you may decide to use float type for all numerical values. Although this is mathematically correct however you would not have an efficient memory management here. You could have used 2-byte for integers but you used 4-byte instead! 1

2 + 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. 2

3 + 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. 3

4 + 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 4

5 + 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 5

6 + 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. 6

7 + 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. 7

8 + 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.

9 + PLEASE CHECK CLASS EXAMPLES FOR PLACEHOLDERS 9

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

11 + 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; 11

12 + 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); 12

13 + /* The = operator puts the value on the right */ /* into the variable on the left */  #include  int main (void){ /* declarations */  int a, b, c, d, e;  a = 10; /* fill variable a */  a = 22; /* modify variable a few times*/  a = 10 + a;  a = a + a + 2;  a = 4 + a;  b = a; /* a few more assignments */  c = b = 7;  c = 10 + a;  d = a + a + 2;  e = 22 + 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); } 13

14 + Example Programs Visit this website for more c.ihypress.ca 14

15 + /* Numeric Placeholders */ #include 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); }

16 + Answer

17 Format specifiers for PRINTF( ) functions SpecifierPurpose %d, %iSigned decimal integer %fSigned floating point %lfSigned double %eSigned floating point using e notation %cA single character %sStrings 17

18 +


Download ppt "+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on."

Similar presentations


Ads by Google