Download presentation
Presentation is loading. Please wait.
1
1 Lecture 2 Input-Process-Output The Hello-world program A Feet-to-inches program Variables, expressions, assignments & initialization printf() and scanf() #include Readings: Chapter 1 Section 2 to 6
2
2 The traditional first C program /* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972 */ #include int main() { printf("Hello, world!\n"); return 0; }
3
3 Program Structure Comments begin with /* and end with */ anything between the delimiters is ignored “ #include …” - called include directives, tells the compiler to include the header file into the program Every program must have a main function where program execution begins; inside the main function is a sequence of statements specifying the actions to be done.
4
4 Some Syntax Rules C is case-sensitive Thus, “ main ” is not the same as “ Main ” All keywords used in C are in lower-case Syntax of preprocessing directives: # must start at leftmost margin no space after no semi-colon at the end, each include directive must be on its own line
5
5 Some Syntax Rules (cont’d) Syntax of Statements: most statements are ended with semicolons spacing is not important programmer can put in suitable spacing and indentation to increase readability
6
6 The printf() function printf() is a library function It resides in the library with header file stdio.h It outputs data to the screen “Hello, world!\n” is the string to be printed; ‘\n’ represents a newline character non-printable characters are preceded by \ in C
7
7 The return statement return signifies the end of a function The number after the keyword return is sent back as the return value of the function main() A return value of 0 usually means that the execution of the function is successful
8
8 A Feet-to-inches program /* To convert length in feet to inches */ #include int main(void) { int inches, feet; scanf(“%d”, &feet); inches = 12 * feet; printf(“%d feet is equivalent to %d inches”, feet, inches); return 0; }
9
9 Program dissection 1 int inches, feet; Define inches and feet as integer variables Variables must be defined before they are referred scanf(“%d”, &feet); The statement causes the computer to input an integer from keyboard and store it in variable feet The first string is called a control string / formatter string The formatter %d indicates that an integer is expected
10
10 Program dissection 2 inches = 12 * feet; It is NOT an equality statement It is an assignment statement, which assigns the value of expression 12 * feet to the variable inches The asterisk “ * ” stands for the multiplication operation printf(“%d feet is equivalent to %d inches”, feet, inches); The first string is a control or formatter string The formatter %d in the control string causes the two parameters after the string to be printed as an integer
11
11 Layout of a simple C program /* comments */ #include int main() { statement-1 statement-2 statement-3... return 0; }
12
12 Variables To store data (e.g. user input, intermediate result) Implemented as memory bytes Each variable must have a name and a type
13
13 Variable Names A variable name consists of a sequence of letters, digits and underscores, but must not begin with a digit As a convention, user-defined variables always start with a lower-case letter Certain reserved words (keywords) are used by C and cannot be used as variable names, e.g. int, return, etc.
14
14 Data Types There are 3 major data types in C: int e.g. 12 float e.g. 2.54 char e.g. ‘A’, ‘e’, ‘#’, ‘ ’ Data of different types are stored using different encoding schemes and require different number of memory bytes Exact schemes differ from system to system
15
15 Typical Encoding Schemes int –2’s complement –4 bytes (32 bits) –[-2 31, 2 31 -1] = [-2147483648, 2147483647] float –Floating point representation –4 bytes – 3.4 x 10 38 (7 decimal places of accuracy) char –ASCII –1 byte
16
16 Variable Definitions All variables should be defined (and initialized) before they are referenced The purpose is to tell the computer the names and types of the variables so that sufficient memory bytes are allocated for the variables; and content in these memory bytes are interpreted appropriately.
17
17 Expression An expression is a meaningful combination of constants, variables and operators (e.g., +, -, *, / ) Constants are the simplest expressions, e.g., 7, ‘A’ and “Hello World\n” Some operators only work on certain types of variables, e.g., ‘A’ * ‘B’ doesn ’ t make much sense
18
18 Assignment A variable is assigned (given) a value using the assignment operator “ = ” An assignment expression consists of an =, a variable on its left and an expression on its right. E.g. inch = 12*feet An assignment expression followed by a semi-colon is called an assignment statement. E.g. inch = 12*feet; Some illegal assignment statements: a + b = c; /* illegal */ 12 = a; /* illegal */
19
19 Variable Initialization When variables are defined, they may also be initialized, e.g., char grade = ‘A’; int k = 10; Only fathoms is initialized to 7 below int inches, feet, fathoms = 7; Both inches and fathoms are initialized below int inches = 8, feet, fathoms = 7;
20
20 The use of printf() The first argument of printf() is a control string which may contain k formatters for the second to (k+1)-st arguments of printf(), e.g., printf(“%s attained %d courses\nAverage” “ mark is %f\nOverall grade is %c\n”, “Jimmy Liu”, 4, 66.5, ‘B’); The output is Jimmy Liu attained 4 courses Average mark is 66.500000 Overall grade is B
21
21 The use of printf() ( cont’d ) printf() conversion characters (see p.16 of [Kelly & Pohl 2001]) cas a character das a decimal integer eas a floating point number in scientific notation ( float or double ) f as a floating point number ( float or double ) gin the e-format or f-format, whichever is shorter ( float or double ) sas a string
22
22 The use of printf() ( cont’d ) The field width and the precision (for floating point number only) of an output data can be controlled, e.g., %5.2f means the field width is 5 (including the decimal point) with 2 decimal places What will happen when the field width is too long or too short for the output data?
23
23 The use of scanf() scanf() is to read input from keyboard Like printf(), the first argument is a control string, which is followed by an arbitrary number of arguments (and those arguments are addresses of data items that store the input) Address operator is represented by an ampersand &, e.g., int feet; scanf(“%d”, &feet);
24
24 The use of scanf() ( cont’d ) scanf() conversion characters (see p.18 of [Kelly & Pohl 2001]) cas a character das a decimal integer f as a floating point number (float) lf as a floating point number (double) Lf as a floating point number (long double) sas a string
25
25 The use of scanf() ( cont’d ) When reading in numbers, scanf() will skip white space (blanks, newlines and tabs) scanf() ends when the input does not match the corresponding formatter in the control string an end-of-file signal (EOF) is detected Keyboard inputs are stored in keyboard buffer and they are “ consumed ” by scanf() ; excessive data inputs will be left in the buffer for subsequent scanf() statements
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.