IPC144 Session 11 The C Programming Language 1 Objectives To format a #define statement correctly To use a #define statement in a C program To construct a printf() statement To use a printf() statement in a C program To construct a scanf() statement To use a scanf() statement in a C program 2 The C Language Introduction What can you tell me about the following C statement? #define 3 The C Language Introduction What can you tell me about the following C statement? #define It is a preprocessor directive 4 The C Language #define What value is the #define preprocessor directive? Remember the Library Fines Calculator program- the fine was set to $0.05 There was one place in the program where we used the fine. What if there were many places in the program? Imagine if the fine was $0.07, and the Library had to charge PST of $0.07? Perhaps the fine need to be increased to $0.10. You would be looking at many lines of code trying to determine if the 0.07 related to the fine or the tax. 5 The C Language #define, continued A sensible way to code your program would be to 'define' names that will represent the value throughout the program. THESE ARE NOT VARIABLES- when the preprocessor processes the source code- the names are replaced the value before being passed on to the translator. For example we could create a name called PST_TAX and another name FINE to represent the values that need to be used in the Library program. We can use these names in place of the 0.07 value, and be able to differentiate between which 0.07 relates to tax and which 0.07 relates to the fine. In C this looks like: #define PST_TAX 0.07 #define FINE 0.07 ... totalFine = (overDue * FINE) + ((overDue * FINE) * PST_TAX); 6 The C Language #define, continued Anatomy of the #define statement: #define name value NOTE: there is no semicolon at the end This is the value the name represents This is the name that will be used throughout the program This is the 'define' preprocessor directive As discussed already, this indicates as preprocessor directive 7 The C Language #define, continued How the #define works: The preprocessor creates a list of the names and their values The preprocessor then searches through the program looking for the name When the preprocessor finds the name, it is replaced with the corresponding value The value is considered to start on the first non-blank character after the name, and ends at the last non-blank character before the end of the line or comment. #define MYDEFINE 32134325235 35 start end 8 The C Language #define, continued Coding Standard: When creating a #define, the name must be in uppercase letters. The underscore character is used to separate the words of a compound name The #define statements follow the #include statements in your source file All quantities that remain unchanged throughout a program must be named using the #define 9 The C Language #define, continued The only time that the changes made to a #define is reflected in your program is after you recompile the program. 10 The C Language printf() function This is the equivalent of the DISPLAY statement in pseudocode. Whatever formatted data is output by the printf() statement is sent to the standard output device of the computer - in our case the monitor. 11 The C Language printf() function, continued Anatomy of a simple printf() statement: printf("A program is born.\n"); An "escape sequence" that is used to tell the computer you want to go to a NEWLINE The beginning of a string of data to be printed EXACTLY as written The library function being called NOTE where the opening and closing brackets are, and what is placed inside of the double quotes. 12 The C Language printf() function, continued Example: #include <stdio.h> int main(void) { printf("A program is born.\n"); printf("\nOh, sure you think it's cute and cuddly now\n"); printf("but just wait!\nAs it grows up it will become a "); printf("monster before you know it!\n"); } What is the output of this program? 13 The C Language printf() function, continued #include <stdio.h> int main(void) { printf("A program is born.\n"); printf("\nOh, sure you think it's cute and cuddly now\n"); printf("but just wait!\nAs it grows up it will become a "); printf("monster before you know it!\n"); } A program is born. Oh, sure you think it's cute and cuddly now but just wait! As it grows up it will become a monster before you know it! 14 The C Language printf() function, continued We have defined variables in our C program, how do we display their contents? There are two parts to the printf() function: The format string, which contains: literal text to be output exactly as written special control characters (such as the \n) field specifications A list of variables that correspond the field specifications in the format string The format string is like a template, showing you where the text is (and what the text is) as well as where within the text the values of the variables are to be inserted 15 The C Language printf() function, continued example: printf("Total = %d Average = %f\n", ttl, avg); The field specifications are "place holders" for the variable data that the program is calculating Where-ever you place a field specification, the compiler will look for a corresponding variable in the list of variables The first field specification refers to the first variable in the list, the second field specification refers the second variable in the list and so on... 16 The C Language printf() function, continued The field specifications in the format string that correspond to the data types discussed: %c - char %d - int %f - float %hd - short int %ld - long int %Lf - long double 17 The C Language printf() function, continued The field specification format is: % flags minimum width .precision size conversion code Flags provide extra control over the formatting of numbers, and to indicate leftjustify. By default the flags are not set. - left justify the field + print positive numbers with a leading '+' sign (there is a space at the beginning of this line). Print positive numbers with leading spaces 18 The C Language printf() function, continued The field specification format is: % flags minimum width .precision size conversion code Minimum width of the print zone by default is the total width of the value including any characters added by the use of the flags and precision. If the total width of the value is less than the specified width, then the value is rightjustified within the print zone. If the '-' flag is used, the value is left-justified. If the total width of the value is greater than the minimum width, the print zone will be extended to accommodate the value. The value is not truncated. If set to '*', the width field can be based on an integer value found in the variable list int i = 5, j = 4; printf(“%*d\n”, j, i); printf(“%4d\n”, i); is the same as: 19 The C Language printf() function, continued The field specification format is: % flags minimum width .precision size conversion code Precision has different effects depending on the data type of the variable being printed Integers - minimum numbers of digits to print. Pad with leading zeroes if necessary Float maximum number of digits to print on the right side of the decimal point (default is six digits). - If set to '*', the precision field can be based on an integer value found in the variable list int i = 5, j = 4, k = 3; printf(“%*.*d\n”, j, k, i); printf(“%4.3d\n”, i); is the same as: 20 The C Language printf() function, continued The field specification format is: % flags minimum width .precision size conversion code Size modifies the data type represented by the conversion code: h - Use with 'd' to specify short int l - used with 'd' to specify a long int L - used with 'f' to specify a long double 21 The C Language printf() function, continued The field specification format is: % flags minimum width .precision size conversion code Conversion code specifies the data type, and in most cases the format, and in some cases the format in which it is to be printed: d - int, signed decimal number, no fractions f - float, signed decimal number with 6 decimal numbers (rounding takes place) %- so that you can print a '%' character 22 The C Language printf() function, continued What is the printf() format string to produce the required output given the data type and the value of the variable myVar: Data Type int float int float Value 1 1.345 56 2.1 Required output 001 +1.35 56 2.10% The 'required output' field is 15 characters in size. 23 The C Language printf() function, continued What is the printf() format string to produce the required output given the data type and the value of the variable myVar: Data Type int float int float Value 1 1.345 56 2.1 Required output 001 +1.34 56 2.10% "%15.3d" "%+15.2f“ "%-15d“ "%-15.2f%%" The 'required output' field is 15 characters in size. 24 The C Language scanf() function A computer program is fairly limited if it cannot get input data from a user as it is executing There exists a function that is very similar to the printf() function that is used for formatted input Anatomy of the scanf() statement: scanf("format string", v1, v2, ...); A list of variables that are referenced in the format string. Each variable of type char, int or float is preceded by a '&' character A list of field specifications for the data being read The function being called 25 The C Language scanf() function, continued There is one major difference with the scanf() function when dealing with the variables: scanf("%d", &i); Notice the "&" symbol before the variable name This is called the "address-of" operator For now, it means that when you place the value read from the user into the variable, you can only store the data if you know the address of the variable within the computer's memory 26 The C Language scanf() function, continued The field specification format is: % flags maximum width size conversion code Flag in this case is called assignment suppression character. It has only one value: * This indicates the input is to be discarded Its purpose is to be able to skip over a sequence of characters in the input file. Input file in this case, by definition, includes the keyboard. 27 The C Language scanf() function, continued The field specification format is: % flags maximum width size conversion code Maximum width indicates the maximum characters to be read into the variable. Scanning for integers and floats, stops when a white-space character is found, an invalid character is found (strictly based on the data type), or the scan has hit the end of the file. 28 The C Language scanf() function, continued The field specification format is: % flags maximum width size conversion code Size behaves the same way as in the printf() function. 29 The C Language scanf() function, continued The field specification format is: % flags maximum width size conversion code The conversion code also behaves the same way as in the printf() function. 30 The C Language scanf() function, continued Exercise: What would be the values of the variables if the scanf() function were to be executed within the data types specified and the input data specified: Data Type int i; Input Data scanf() function 12334 scanf("%d", &i); Variables after Read i = int i; float x; 155.347 7 scanf("%d %f", &i, &x); i = x = int i1; int i2; 1234 scanf("%3d%d", &i1, &i2); i1 = i2 = 31 The C Language scanf() function, continued Exercise: What would be the values of the variables if the scanf() function were to be executed within the data types specified and the input data specified: Data Type int i; Input Data scanf() function 12334 scanf("%d", &i); Variables after Read i = 12334 int i; float x; 155.347 7 scanf("%d %f", &i, &x); i = 155 x = 0.347 int i1; int i2; 1234 scanf("%3d%d", &i1, &i2); i1 = 123 i2 = 4 32