Title: Ch 1: Intro to Computer and Programming
14
CHAPTER
Processing of Input and Output Functions and
Operators
2Objectives
- Output function
- Input function
- Character set
- Tokens
- Reserved words (keywords)
- Identifiers
- Constants
- String literals
- Punctuators
- Operators
3Input/output operations
- Input operation
- an instruction that copies data from an input
device into memory - Output operation
- an instruction that displays information stored
in memory to the output devices (such as the
monitor screen) - The input/output operations are performed by C
input/output functions. - The standard C functions predefined in stdio.h
header file include - printf()
- scanf()
- getchar()
- putchar()
4The Output Function printf()
- Used to send data to the standard output (usually
the monitor) to be printed according to specific
format. - Syntax
- printf(FormatControlString)
- printf(FormatControlString, PrintList)
- FormatControlString is a combination of
characters, format specifier and escape sequence. - PrintList is any constants, variables,
expressions, and functions calls separated by
commas.
5- Example
- printf(Thank you)
- printf(Total sum is d\n, SUM)
- d is a placeholder (format specifier)
- marks the display position for a type integer
variable - \n is an escape sequence
- moves the cursor to the new line
6Formatting Output
- Format specifier (placeholder)
- Tells the printf() function the format of the
output to be printed put.
7 8- Escape Sequence
- is used in the printf() function to do something
to the output. - The common escape
- sequence
- and more
- (see Uckan pp. 729)
9Exercise
10The Input Function scanf()
- Read data from the standard input device (usually
keyboard) and store it in a variable. - The general format is pretty much the same as
printf() function. - Syntax
- scanf(FormatControlString, InputList)
- InputList one or more variables addresses, each
corresponding to a format specifier in the
FormatControlString. - One format specifier for each variable in
InputList. - The two or more variables in InputList must be
separated by commas. - Each element of InputList must be an address of a
memory location (using prefix address operator)
11- Common format specifier used in printf() and
scanf() functions.
12Example
- include ltstdio.hgt
- main()
-
- int Age
- printf(Enter your age )
- scanf(d, Age)
-
- printf(Your age is d,Age)
-
- Output?
Address operator
13- If you want the user to enter more than one
value, you serialise the inputs. - Example
-
- float height, weight
- printf(Please enter your height and weight)
- scanf(ff, height, weight)
14Exercise
15The getchar() and putchar() Functions
- getchar() - read a character from standard input
- putchar() - write a character to standard output
- Example
- include ltstdio.hgt
- main()
-
- char my_char
- printf(Please type a character )
- my_char getchar()
- printf(\nYou have typed this character )
- putchar(my_char)
- printf(\n)
-
- To be discussed in Chapter 9 Characters and
Strings
16- Alternatively, you can write the previous code
using normal scanf(), printf() and c format
specifier. - Example
include ltstdio.hgt void main(void) char
my_char printf(Please type a character )
scanf(c,my_char) printf(\nYou have typed
this character c , my_char)
17Character Set
- Characters are basic building blocks in C
programs - The C character set contains 92 characters
- Numeric digits (0-9)
- Letters of English alphabet (both a-z and A-Z)
- Space (blank)
- All other printable special characters on
standard English keyboard , . ? / ( )
lt gt etc EXCEPT , , and _at_
18Tokens
- Tokens are a series of continuous characters that
compilers treat as separate entities. - Tokens can be classified into
- Reserved words (also known as keywords)
- Identifiers
- Constants
- String Constants
- Punctuators
- Operators
191. Reserved Words/Keywords
- Keywords that identify language entities such as
statements, data types, language attributes, etc. - Have special meaning to the compiler, cannot be
used as identifiers in our program. - Should be typed in lowercase.
- Must not be used as variables!!
- Keywords may be classified into
- Data type related
- Flow of control related
- Miscellaneous keywords
20Data Type Related Keywords
- A number of keywords are used to identify or
qualify specific data types
- auto
- extern
- register
- static
- const
- char
- float
- int
- long
- short
- signed
- unsigned
- union
- void
- volatile
- struct
- double
21Flow of Control Related Keywords
- Keywords used for handling the flow of control in
C programs
- break
- case
- goto
- continue
- default
- do
- while
- if
- else
- for
- switch
- return
22Miscellaneous Keywords
- The keywords that dont fall under either of the
previous categories - enum
- sizeof
- typedef
232. Identifiers
- Words used to represent and reference certain
program entities (variables, function names,
etc). - Also known as programmer-defined words.
- Example
- int my_name
- my_name is an identifier used as a program
variable. - void CalculateTotal(int value)
- CalculateTotal is an identifier used as a
function name.
24Identifiers Rules
25Identifiers Naming Style
- Avoid excessively short and cryptic names such as
x, or wt. - Use reasonably descriptive name such as
student_name and StudentID. - Use userscores or capital letters to separate
words in identifiers that consist of two or more
words. - Example StudentName, student_name, or
studentname.
26Exercise
- Which are legal or illegal identifiers? Why?
- quiz grade
- Average
- Counter
- _sum
- dailyrate
- Text10
- 10thColumn
- continue
27Variables
- Variable ? a name associated with a memory cell
which value can change - Variable Declaration specifies the data type of
a variable - Example int num
- Variable Definition assigning a value to the
declared variable - Example num 5
28Basic Data Types
- There are 4 basic data types
- int
- float
- double
- char
- int
- To declare numeric program variables of integer
type - whole numbers, positive and negative - Example
- int number
- number 12
29- float
- fractional parts, positive and negative
- Example
- float height
- height 1.72
-
- double
- To declare floating point variable of higher
precision or higher range of numbers -
exponential numbers, positive and negative - Example
- double valuebig
- valuebig 12E-3
30- char
- Equivalent to letters in English language
- Example of characters
- Numeric digits 0 - 9
- Lowercase/uppercase letters a - z and A - Z
- Space (blank)
- Special characters , . ? / ( )
lt gt etc - Single character
- Example
- char my_letter
- my_letter 'U'
- In addition, there are void, short, long, etc.
The declared character must be enclosed within a
single quote!
31Strings as a Derived Data Type
- A string is a sequence of characters that is
treated as a single data item. - In C a string variable is represented in a
one-dimensional array of type char. - The max length of the string constant that can be
stored in a character array of size n is n-1 - string declared as one-dimensional array is
always terminated by NULL or \0. - Example
- char report_header41 Annual Report
323. Constants
- Entities that appear in the program code as fixed
values. - Example
- const double INTEREST_RATE 0.015
- 4 types of constants
- Integer constants
- Floating-point constants
- Character constants
- Enumeration
33- Integer constants
- Positive or negative whole numbers with no
fractional part - Example
- const int MAX_NUM 10
- const int MIN_NUM -90
- Floating-point constants
- Positive or negative decimal numbers with an
integer part, a decimal point and a fractional
part - Example
- const double VAL 0.5877e2
- (stands for 0.5877 x 102)
34- Character constants
- A character enclosed in a single quotation mark
- Example
- const char LETTER n
- const char NUMBER 1
- But, to print single quotation mark require
backslash (known as escape sequence) since
is illegal. - printf(c, \)
- - prints a single quotation mark on screen
- printf(c, n)
- - prints the letter n on the screen
- printf(c, \n)
- move cursor to the beginning of a new line
-
35Example - 1
include ltstdio.hgt void main(void) const
double KMS_PER_MILES 1.609 double miles,
kms printf(Enter the distance in
miles) scanf(lf,miles) kms
KMS_PER_MILES miles printf(\nThat equals
f kilometer.\n , kms)
36Example - 2
include ltstdio.hgt define KMS_PER_MILES
1.609 void main(void) double miles,
kms printf(Enter the distance in
miles) scanf(lf,miles) kms
KMS_PER_MILES miles printf(\nThat equals f
kilometer.\n , kms)
374. String Literals/ String Constants
- A sequence of any number of characters surrounded
by double quotation marks . - Example
- This is a string constant.
- Hello \John\.
- Example of usage in C program
- printf(My name is David Beckham.)
- simply print the string constant
- My name is David Beckham.
- Cannot print double quotation marks unless
enclosed with backslashes.
385. Punctuators (Separators)
- Symbols used to separate different parts of the C
program. - These punctuators include
- ( ) ,
- Usage example
- main()
-
- printf(Testing.)
-
- To be discussed when we come to the proper
language feature in the coming chapters.
396. Operators
- Tokens that result in some kind of computation or
action when applied to variables or other
elements in an expression. - Example of operators
- - / lt gt
- Usage example
- result total1 total2
Chapter 5
40Summary
- Output function printf(), putchar()
- Input function scanf(), getchar()
- Multiple, wrapped lines
- Character set
- Tokens
- Reserved words (keywords)
- Identifiers including variables
- Constants
- String literals
- Punctuators
- Operators