Presentation is loading. Please wait.

Presentation is loading. Please wait.

41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.

Similar presentations


Presentation on theme: "41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck."— Presentation transcript:

1 41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck of the Hesperus:\n”); printf(“Its depth at sea in different units:\n”); printf(“%d fathoms\n”, fathoms); printf(“%d feet\n”, feet); printf(“%d inches\n”, inches); } Wreck of the Hesperus: Its depth at sea in different units: 7 fathoms 42 feet 504 inches

2 42 Declarations A variable is declared to be of a type. This enables the compiler to: –Set aside the appropriate amount of memory to hold the variable. –Determines what operations are permissible with that variable. –What sort of data is allowed to be stored in that variable. All variables must be declared before they are assigned values and used in statements. Declaration end with a semicolon.

3 43 Assignment Allows the work and processing of data to achieved. variable; Variable = constant; function call; expression [contains any/all of above joined by operators]; Multiple assignments. Right to Left Association: 1. C = 0 A = B = C = 0;2. B = (C = 0) 3. A = (B = (C = 0))

4 44 Compound Assignments var operator = expression; var = var (operator)expression; General Form: Variable += Exp; Variable = Variable + Exp; Variable -= Exp Variable = Variable – Exp; Variable *= Exp Variable = Variable * Exp; Variable /= Exp Variable = Variable * Exp; Variable %= Exp Variable = Variable % Exp; x +=10; x = x +10; x -=100; x = x - 100;

5 45 Data Types: Preliminary info: see more in Chap. 6. Variables -Locations in memory we create which can hold data. Variables have a specific type. Type defines what sort of data is allowed to be stored in the variable. Integer Types:int, short int, long int Real Types:float, double, long double Text Types:Char1 keystroke (1 symbol) Stringmultiple characters

6 46 Initialization When variables are declared, they may also be initialized. Typically, constants or constant expressions are used to initialize a variable. General Syntax: type variableName = constant; A) Integer Constant: e.g., int x = 100; B) Real:1.63double 1.63Ffloat 1.63Llong double e.g.double x = 1.63;

7 47 C) Char : Single symbol in single quotes. e.g.Char Letter = ‘A’: D) String: Multiple symbols in double quotes. e.g. Char name[15] = “Mary”;

8 48 General Syntax: –scanf (“format string”, Arguments); Interactive Input: get data from user entering via keyboard. –fscanf (f, “format string”, Arguments); File Input: get data that is stored in file F. –sscanf (s, “format string”, Arguments); String Input: get data from string S. Return value: If error occurs, return value is EOF (usually -1), Otherwise returns the number of successful assignments of data to the arguments. Input Predefined Functions

9 49 Unsuccessful Input: –EOF (End of File) is reached before ALL arguments have been assigned data, if so the value EOF (-1) is returned. –Data Mismatch: The input data does not match the control string specifications. (e.g., indicated data would be integer and the data actually entered was REAL).

10 50 SCANf - Interactive Input –User enters data with keyboard when program interrupt occurs. –Input data treated as an input stream of characters. –Arguments are addresses. scanf(“%d”, &x);

11 51 #include int main(void) { char str[80]; printf("Enter a string: "); scanf(''%s", str); printf("Here's your string: %s", str); return 0; Why no “&” ? }

12 52 Output Predefined Functions General Syntax: –printf (“control string”, Arguments); Writes to terminal. –fprintf (f, “control string”, Arguments); Writes to another file. –sprintf (s, “control string”, Arguments); Writes to string s. Return Value: If EOF occurs, return value is - 1, Otherwise returns the number of successful assignments of data to the arguments.

13 53 Format Strings Contain Conversion Specification Characters. e.g., d - decimal data c - character f - floating point s - string printf("I like %c %s", 'C', "very much!") ; Contain Text: int Sum = 500; printf (“The Sum is %d”, Sum); New Line - printf (“hello, \n world”); Tab - printf (“hello, \t world \n”) The Sum is 500

14 54 ASIDE: scanffloat use %f doubleuse %lf printffloat or use %f double Format Specifiers (cont.)

15 55 Field Width Specification printf (“more numbers: %7.1f %7.2f %7.3f”, 4.0, 5.0, 6.0); Field width. Precision # of columns output. # of decimal digits to including dec. pt. right of decimal pt. more numbers:_ _ _ _ 4.0 _ _ _ 5.00 _ _ 6.000

16 56 Character Data: printf (“%c %5c/n %4c”, ‘x’, ‘y’, ‘z’); x_ _ _ _ y _ _ _z Field Width Specification (cont.)

17 57 Looping - Iteration Categories: Conditional Loops: while do-while Counting Loop : for While Statement: while (Expression) { Body of Loop } while (scanf(“%d”, &x)==1) { printf(“%d”, x); }

18 58 How Terminate Loop? Dummy Data Logic: –User enter a value that can’t be successfully converted. End-of-file Logic: –Indicate all data has been entered by typing EOF signal. UNIXctrl + d MS DOSctrl + z break - Exit from Loop immediately. continue - Jump to Bottom of Loop(stops just that single iteration).


Download ppt "41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck."

Similar presentations


Ads by Google