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 2 11 2 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 3 3 Design Develop Algorithm Pseudo code Algorithm print Enter SSN no dashes read SSN print Hello SSN 4 Implementation Write the Program Code 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 11 4 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 5 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 functions 11 6 main has no initial input values see below main function returns no value 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 11 7 a block in C is defined by a set of curly braces 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 11 8 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 One form of a declaration in C data type variable name 11 9 Fixed Point Integers short int long Floating Point float double long double Character char 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 10 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 TIME 11 11 include stdio h 1st executable statement 2nd executable statement 3rd executable statement 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 11 12 Statements are grouped by their type assignment statement expression statement do while and while statements loop statements for statement if and if else statements selection statements switch statement return statement Every executable statement in C must be followed by a semicolon 11 13 variable expression 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 11 14 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 15 modulus no operator as in Matlab 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 e g without parentheses the example above would evaluate b 2 first then do the multiplication then the subtraction 11 16 Precedence Order for Arithmetic Operators Higher left to right for tie left to right for tie Lower see ABC p 705 Appendix E for a complete list of operators and their precedence Examples x 3 2 3 y 3 2 3 x now has the value 9 y now has the value 0 11 17 Example int time Main Memory time unknown 1200 time time 1 time time 1 is executed time 0 time time 1 Memory map before the assignment statement Main Memory time 1 1200 Memory map after the assignment statement time time 1 is executed 11 18 Example Main Memory force double force 5 0 force 5 0 1400 Memory map before the assignment statement force force 3 0 but after force 5 0 Main Memory is executed force force 3 0 force 15 0 1400 Memory map after the assignment statement force force 3 0 is executed 11 19 Individual characters may be assign to variables of data type char Example Write a program to prompt the user for a Y N response Print the users response to the screen include stdio h void
View Full Document