Lecture 4 - Format Specifiers and Escape Sequences


Format specifiers
Format specifiers are the special characters which govern the formatting of the variables on the screen.
Format Specifier
For (Data Type)
Format Specifier
For (Data Type)
%d
Signed Decimal Integer
%s
String
%f
Floating Point (decimal notation)
%x
Unsigned Hexadecimal Integer
%c
Character
%o
Unsigned Octal Integer
%e
Floating Point (exponential notation)
l
Prefix, e.g. %ld for long integer
%u
Un-signed Decimal Integer
Table 4– Format Specifiers
/* This program demonstrates:-
Declaration of
Constants and Variables
Use of
Constant
Variables
Variable initialization
Format Specifier
Expression
Function call                                       */
# include <stdio.h>
Output
Area of Circle = 78.54
# include <conio.h>
#define PI 3.1416                              // Constant
void main()
{
int r;                                                                                                       // variable declaration
r=5;                                                                                                        // variable initialization
float area=0.0;                                                                                  // variable declaration and initialization
clrscr();                                                                                                 // clears the screen, for your program’s output.
area = PI * r * r;
printf(“Area of Circle = %f”, area);                                            // function call to printf, with 2 parameters
getch();
}
Example 1 – Using ONE Format Specifier




/* This program demonstrates:-
Declaration of
Constants
Variables
Use of
Constant
Variables
Variable initialization
2- Format Specifiers
Expression
Function call
*/
# include <stdio.h>
Output
Radius = 5     Area of Circle = 78.54
# include <conio.h>
#define PI 3.1416                              // Constant
void main()
{
int r;                                                                                                       // variable declaration
r=5;                                                                                                        // variable initialization
float area=0.0;                                                                                  // variable declaration and initialization
clrscr();                                                                                                 // clears the screen, for your program’s output.
area = PI * r * r;
printf(“Radius = %d      Area of Circle = %f”, r,  area);
getch();
}
Example 2– Using TWO Format Specifiers
In above example, the placement of the variable is very important. The first variable will be displayed on the first format specifiers position and the next on the next position and so on.

Escape sequence

These are the commands for the compiler, which are embedded in a formatted string. Normally, these commands are for aesthetics only, i.e. for formatted output.
Escape Sequence
Used for
Escape Sequence
Used For
\n
New Line
\f
From Feed
\t
Tab Space
\’
For printing single quote
\b
Backspace
\”
For printing double quote
\a
Alarm (Beep from PC Speaker)
\\
For printing backslash
\r
Carriage Return


Table 5– Escape Sequences
// This program demonstrates the use of printf function.
Output
My Name is Hammad Naqvi
# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();                                                                                                 // clears the screen, for your program’s output.
printf(“My Name is Hammad Naqvi”);                                   // function call to printf
getch();
}
Example 3 – My First C Program
Steps for getting above program to work
Step 1 – Go to C:\TC\BIN
Step 2 – Double Click TC.EXE
Step 3 – Type above program on as-is basis
Step 4 – Press Ctrl + F9 keys, they will compile and run your program
You will see My Name is Hammad Naqvi on the screen
Remember whatever you write in printf within double quotes. It will be displayed as-it-is on the screen, except format specifiers and escape sequences.
Consider a scenario where you want to write like
My Name is Hammad Naqvi
I am a Lecturer in Computer Science at APS&C
Now we can achieve above output in two ways
1.       By using multiple (2) printf
2.       By using escape sequence

Using escape sequence

// This program demonstrates the use of escape sequence.
Output
My Name is Hammad Naqvi
I am a Lecturer in Computer Science at APS&C

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();                                                                                                
printf(“My Name is Hammad Naqvi\nI am a Lecturer in Computer Science at APS&C”);
getch();
}
Example 4 – Using Escape Sequence


// This program demonstrates the use of escape sequence.
Output
My Name is        Hammad Naqvi
# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();                                                                                                
printf(“My Name is \tHammad Naqvi”);
getch();
}
Example 5 – Using Escape Sequence (TAB)

// This program demonstrates the use of escape sequence.
Output
Hammad Naqvi
# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();                                                                                                
printf(“My Name is \rHammad Naqvi”);
getch();
}
Example 6 – Using Escape Sequence  (Return)

// This program demonstrates the use of escape sequence.
Output
My Name iHammad Naqvi
# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();                                                                                                
printf(“My Name is\bHammad Naqvi”);
getch();
}
Example 7 – Using Escape Sequence

 

Input from the user

So far we have not written any interactive program. We shall use scanf for taking input from the user. Let’s do that. For startup let’s just modify example 2



Output
Please enter radius of the circle : 5
Radius = 5     Area of Circle = 78.54
# include <stdio.h>
# include <conio.h>
#define PI 3.1416                              // Constant
void main()
{
int r;                                                                                                       // variable declaration
float area=0.0;                                                                                  // variable declaration and initialization
clrscr();                                                                                                 // clears the screen, for your program’s output.
printf(“\nPlease enter the radius of the circle :”);
scanf(“%d”, &r);
area = PI * r * r;
printf(“\nRadius = %d      Area of Circle = %f”, r,  area);
getch();
}
Example 8 – Taking input from the user


Output
Please enter the length and width of rectangle: 5, 5
Area of the Rectangle = 25
# include <stdio.h>
# include <conio.h>

void main()
{
float length, width, area;                                                             // variable declaration
clrscr();                                                                                                 // clears the screen, for your program’s output.
printf(“\nPlease enter the length and width of the rectangle:”);
scanf(“%f”,” “%f”, &length, &width);                                     // input in multiple variable
area = length * width;
printf(“\nArea of the Rectangle  = %f”,  area);
getch();
}
Example 9- Using scanf to input multiple values

 

 

 

 

Comments

Popular posts from this blog

Lecture 17 – Functions (continued)

Lecture 6 – Operators in C (continued)

Lecture 10 – Decisions (if-else-if)