DOC PREVIEW
UIUC CS 101 - lect11

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11-2 • Identify the parts of the “main” function, which include • Preprocessor Directives • main function header • main function body which includes • Declaration statements • Executable statements which include • Assignment statements • Function statements, e.g. printf(...),scanf(...) (a.k.a. expression statements) Related Chapters: ABC Chapters 2 & 8.1, 8.2 & 11.1, 11.211-3 1. Requirements Specification (Problem Definition) Write a simple C program to request the users’ SSN and print the message “Hello (SSN)!” (where SSN is the value the user supplied) to the screen of the computer monitor. 2. Analysis---Refine, Generalize, Decompose the problem definition (i.e., identify subproblems, I/O, etc.) Input = SSN Output= A prompt for the SSN then “Hello (SSN)!”11-4 /* C Program to greet the user */ #include <stdio.h> /* makes the function printf “known” */ /* to the C compiler */ void main(void) { int ssn; /* variable ssn declared */ printf(“Please enter your SSN (no dashes):”); scanf(“%i”,&ssn); /* read the SSN */ printf(”Hello (%i)!\n”,ssn); /* print the message */ } 3. Design---Develop Algorithm Pseudo-code Algorithm print “Enter SSN (no dashes)” read SSN print “Hello (SSN) !” 4. Implementation --- Write the "Program" (Code)11-5 #include ... /* preprocessor directive */ . . . void main(void) /*function header*/ { . . /* function body */ . } In the following set of slides we will define the component parts of the main function.11-6 #include ... /* preprocessor directive */ . . . void main(void) { . . . } Statements beginning with the symbol # are called preprocessor directives. The #include directives allow the C program to access libraries of functions that are not available directly in C. These libraries are referred to as header files: stdio.h standard input-output functions math.h math functions stdlib.h a “standard” library of functions11-7 #include ... . . . void main(void) /*function header*/ { . . . } The “initial” input is any value(s) passed at initial execution, e.g. >./a.out 1.0 3.5 Typing the above at the Unix prompt attempts to pass the values 1.0 and 3.5 to main. At this point we will not do this. See p. 48 in ABC if you really want to know how. The header for the programs in some C programs is of the form: int main(void) and there is a corresponding statement in the function body: return (0); however, again, we won’t need this feature of main in CS101. main has no “initial” input values. (see below) main function returns no value11-8 #include ... . . . void main(void) { . . /* function body */ . } • The body of a function is all the declarations and executable statements between the braces (in the block). The declarations must precede the executable statements in this block. • When you run a program, the first statement executed is the first (from the top of the file) executable statement in main. The execution of statements proceeds until the terminating “}” right brace is encountered or a return statement is executed. a “block” in C is defined by a set of curly braces.11-9 One form of a declaration in C : data-type variable_name ; #include <stdio.h> void main(void) { int ssn; /* variable ssn declared */ printf(”Please enter your SSN (no dashes):”); scanf(”%i”,&ssn); printf(”Hello (%i)!\n”,ssn); }11-10 Fixed Point(Integers) Floating Point Character short float char int double long long double See ABC Chapter 3.2 p. 110 for a complete list of the numeric data-types. Character strings in C are implemented as arrays. See ABC Chapter 6.10 p. 270.11-11 Variable Names - reference to memory locations storing data values. A variable name is one example of an identifier in C. An identifier can use a combination of letters, numerical digits, and the underscore ( _ ) that starts with a letter. Only the first 31 characters of an identifier are recognized by most compilers. Identifiers cannot be the same as a reserved word or keyword, such as void, float, or return. (ABC Chapter 2.4 p. 77 list the reserved words) Use descriptive variable names. Examples: x sum force2 rate_Of_Change Examples of invalid variable names (why?): 2force rate-of-change x.5 return C case-sensitive, so that the following three names all represent different variables (i.e., different storage locations): time Time TIME11-12 #include <stdio.h> void main(void) { int ssn; printf(”Please enter your SSN (no dashes):”); scanf(”%i”,&ssn); printf(”Hello (%i)!\n”,ssn); } The printf and scanf functions appear in ‘expression’ statements. 1st executable statement 3rd executable statement 2nd executable statement11-13 Statements are grouped by their type: • assignment statement • expression statement • do-while and while statements • for statement • if and if-else statements • switch statement • return statement Every executable statement in C must be followed by a “ ; “ semicolon. selection statements loop statements11-14 • Interpretation: compute the value on the right-hand-side of the = and put this value in memory at location named (or tagged) by the name variable . • an expression can be a constant, a variable or operators with operands. variab le = expression;11-15 Literal Constants(examples) Numeric: 3 6.248 -7.25 1.5e4 (=15000) Single Character: 'A' 'a' '9' '+' ' ' Character String: “Enter a positive number: ” Symbolic Constants(example) #define PI 3.14159265 /* preprocessor directive */ By default, constants declared in #define are of type double. Standard practice is to use all upper-case letters for the constant name, to avoid confusion with a variable name. A “constant” means you cannot change the value, e.g. PI = 3.0; will generate a compiler error from gcc.11-16 - for integer arithmetic, the / operator yields an integer result, and % gives the remainder after dividing two integers. E.g., 5 / 3 1 5 / 6 0 -5%3 -2 5 % 3 2 5 % 6 5 5%-3 2 (Note: division by 0 creates an "overflow" run-time error.) - use arithmetic operators, with parentheses for grouping; e.g., (a - b) / (2 * c)


View Full Document

UIUC CS 101 - lect11

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect25

lect25

31 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect11
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lect11 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lect11 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?