1
C Building Blocks
Ch: 2
2
The printf() Function
¾
Powerful and versatile output function.
¾
Uses a unique format for printing constants and
variables.
printf(“This is the number two: 2”);
¾
The function printf() can be given more than 2
arguments.
3
Format Specifier
A format specifier is used to control where to put a value in a string and what format will be used by printf( ) to print out a particular value.
Here is a listing of the format specifiers.
%c for single character %d for signed decimal integer %f for floating point value
%s for string
%e floating point (exponential notation)
%Lf for long double
%u unsigned decimal integer
%x unsigned hexadecimal integer
%o unsigned octal integer %ld for long integer
%lf for double float
4
Example
1)void main(void) {
printf(“%s is %d million miles from the sun.”, “Venus”,67); }
Output:
Venus is 67 million miles from the sun
2)void main(void) {
printf(“My percentage is %f”, 67.5); }
5
Field Width Specifier
To control how many digits will be printed after the
decimal point, we use the field width specifiers.
Example
void main(void) {
printf(“My percentage is %.2f”, 67.5); }
Output:
My percentage is 67.50
6
Escape Sequences
¾ Special sequence of two characters (\ and code characters) that
provide formatting instructions to a line printed.
¾ So called because the backslash symbol is considered an escape
character. It causes an escape form the normal interpretation of a string so that the next character is recognized as having a special meaning.
\n New Line \t Horizontal Tab \v Vertical tab \b Backspace \r Carriage return \f Form Feed \’ Single quote \” Double Quotes \\ Backslash
\xdd ASCII code in hexadecimal notation, each d represents a hexa-digit, range 0-ff
7
1) void main (void)
{ clrscr();
printf(“Each \t word \t is \n”); printf(“Tabbed \t over\t once\n”); }
Output
Each Word is
Tabbed over once
2) void main(void)
{ clrscr();
printf(“Department of Automotive \n”); printf(“\n B.E programme \n”);
}
Output:
Department of Automotive B.E Programme
8
Variables & Constants
Variable is a space in computer memory set aside for
a certain kind of data and given a name for easy
reference. Naturally the content of the variable can
change.
A Constant is a quantity that doesn’t change. This
9
Example:
1)void main(void) {
printf(“My lucky number is %d”,2); }
Output: My lucky number is 2.
¾ Prints the constant 2 by plugging it in the format specifier. ¾ Can be achieved by writing above program as:
2)void main(void) {
printf(“My lucky number is two : 2”); }
10
3)
void main(void)
{ int num;
num =2;
printf(“My lucky number is %d”,num);
}
Output:
My lucky number is 2.
¾
Give the same output as before but achieved in a
different way.
11
Variable Definition
All variables must be defined to specify their name
and type and set aside storage.
E.g.
int num;
¾When you define a variable, the compiler set side an appropriate amount of memory to store that variable.
¾Variable declarationspecify name and data type but doesn't set aside memory.
Data type Variablename
2 Each integer occupies 2 bytes of memory
An integer in memory
Each rectangle is one bytes
12
Rules for Constructing Variable Names
(a). The first character in the variable name must be an alphabet.
(c). No commas or blanks are allowed within a variable name.
(d). No special symbol other than an underscore can be used in a variable name. E.g.: Name, roll_no, fee
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler. The keywords are also called ‘Reserved words’. The keywords cannot be used as variables names.
For example: int, float, if, for, while, etc.
Unsigned Data types
The integer and character types also have unsigned versions
¾ For e.g.. Unsigned int type holds numbers from 0- 65,535 rather than
-32768 to 32767.
13
Exploring the data types
Floating point variable type float occupies four bytes in memory and can hold numbers from 1038 to 1038 with six digits of precision. Floating point variable type double, occupies eight bytes with 15 digits of precision, and can hold numbers from 10308 to 10-308.Long doubletype occupies 10 bytes
Floating Point
A Character variable occupies one byte in memory in which the characters constant are stored. The type name for a character is char.
Characters
The integer variable occupies two bytes in memory and can hold numbers from -32768 to 32767. The type name for the integer is int. Integer variable type long
or long int, occupies four bytes and can hold numbers from -2147, 483, 648 to 2147, 483, 647.
Integer
14 void main(void)
{
int event = 2; float time=23.45; char heat = ‘c’;
printf(“The winning time in heat %c”, heat); printf(“of event %d was %f”, event, time); }
Output:The winning time in heat C of event 2 was 23.45
Example
15
The scanf() Function
¾
C has a large collection of input and output functions
but printf() and scanf() are the most versatile and can
handle different types of variables and their
formatting.
¾
Argument on the left side is the string that contains
the format specifiers and on the right is the variable
name with the ampersand (&) preceding the variable
name.
16 void main(void)
{
int event; float time; char heat;
printf(“Type event number, heat letter and time”);
scanf(“%d %c %f”, &event, &heat, &time);
printf(“The winning time in heat %c”, heat); printf(“of event %d was %f”, event, time); }
Output:The winning time in heat B of event 4 was 36.34
17
How scanf() know we finished typing
¾
As we type three input values say ‘4’, ‘B’ and 36.34,
we separate them by spaces.
¾
scanf() matches each space we type with
corresponding space between the conversion type
characters in scanf() format string %d %c %f
¾
Spaces serves as delimiters because it matches the
space in the format string.
4 B 36.34
scanf(“%d %c %f”, &event, &heat, &time);
18
void main(void)
{
float years, days;
printf(“Enter age in years”);
scanf(“%f, &years);
days = years * 365;
printf(“you are %.1f days old”, days);
}
Output:
Enter age in years:10
19
Address Operator (&)
¾
C compiler requires the arguments to the scanf() to be
the addresses of variables rather than the variables
themselves
.
¾
Each variable occupies a certain location in memory
and its address is that of the 1
stbyte it occupies.
22 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 Address of integer variable
Address of variable
void main(void) { int n = 2;
n =2;
printf(“Value = %d,address=%d”, n, &n); }
Output: Value is 2, address is 2035
20
getch() and getche() Function
¾ getch() is a limited input function, only characters can be taken
as input where as scanf() is not limited.
¾ In scanf() you need to press [Return] before the function will
know what you have typed. If we want a function that reads a single character the instant it’s typed without waiting for [Return], then we use the getche() function.
¾ If any character is typed wrongly you can’t go back.
¾ It stores and writes the value at runtime i.e. the function
itself takes on or returns the value of each character typed.
¾ get – to get something (i/p function) ¾ ch – it gets a character
21