25
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

L ecture 2

Embed Size (px)

DESCRIPTION

L ecture 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. The traditional first C program. /* The traditional first program in honor of - PowerPoint PPT Presentation

Citation preview

Page 1: L ecture  2

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

Page 2: L ecture  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 <stdio.h>

int main()

{

printf("Hello, world!\n");

return 0;

}

Page 3: L ecture  2

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 <stdio.h> 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.

Page 4: L ecture  2

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 < and before > no semi-colon at the end, each include

directive must be on its own line

Page 5: L ecture  2

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

Page 6: L ecture  2

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

Page 7: L ecture  2

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

Page 8: L ecture  2

8

A Feet-to-inches program/* To convert length in feet to inches */

#include <stdio.h>

int main(void) { int inches, feet;

scanf(“%d”, &feet); inches = 12 * feet; printf(“%d feet is equivalent to %d inches”, feet, inches); return 0;}

Page 9: L ecture  2

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

Page 10: L ecture  2

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

Page 11: L ecture  2

11

Layout of a simple C program

/* comments */#include <stdio.h>

int main()

{

statement-1 statement-2 statement-3 . . . return 0;

}

Page 12: L ecture  2

12

Variables

To store data (e.g. user input, intermediate result)

Implemented as memory bytes Each variable must have a name and a

type

Page 13: L ecture  2

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.

Page 14: L ecture  2

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

Page 15: L ecture  2

15

Typical Encoding Schemes int

– 2’s complement– 4 bytes (32 bits)– [-231, 231-1] = [-2147483648, 2147483647]

float– Floating point representation– 4 bytes 3.4 x 1038 (7 decimal places of accuracy)

char– ASCII– 1 byte

Page 16: L ecture  2

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.

Page 17: L ecture  2

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

Page 18: L ecture  2

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 */

Page 19: L ecture  2

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 belowint inches, feet, fathoms = 7;

Both inches and fathoms are initialized belowint inches = 8, feet, fathoms = 7;

Page 20: L ecture  2

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 coursesAverage mark is 66.500000Overall grade is B

Page 21: L ecture  2

21

The use of printf() (cont’d)

printf() conversion characters (see p.16 of [Kelly & Pohl 2001])

c as a characterd as a decimal integere as a floating point number in scientific

notation (float or double)f as a floating point number (float or double)

g in the e-format or f-format, whichever is shorter (float or double)

s as a string

Page 22: L ecture  2

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?

Page 23: L ecture  2

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);

Page 24: L ecture  2

24

The use of scanf() (cont’d)

scanf() conversion characters (see p.18 of [Kelly & Pohl 2001])

c as a characterd as a decimal integerf as a floating point number (float)lf as a floating point number (double)Lf as a floating point number (long

double)s as a string

Page 25: L ecture  2

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