Week3 Lec5 6 Formatted Input Output Ch3 Fa24
Week3 Lec5 6 Formatted Input Output Ch3 Fa24
Week 3
Lecture 5-6
Fall 2024
Sheikh Usama Khalid
Department of Computer Science
100 Building, Clifton, Room 301
Usama.khalid@szabist.edu.pk
Lecture Plan
• Chapter 3: Formatted Input / Output
Fundamentals of Programming
• Programming Review
Fundamentals of Programming
#include <stdio.h>
int main(void)
{
printf("To C, or not to C: ");
printf("that is the question.\n");
return 0;
}
The printf Function
• The first argument in a call of printf is a string, which may contain both
ordinary characters and conversion specifications, which begin with the %
symbol.
Fundamentals of Programming
int i, j;
float x, y;
printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y);
The printf Function
• Warning: C compilers don’t check that the number of conversion specifications
in a format string matches the number of output items:
Fundamentals of Programming
• .p (optional) specifies:
• Number of digits after the decimal point (for a floating point number)
• Minimum number of digits to print (for an integer)
Conversion Specifications
• X is one of the following letters:
Fundamentals of Programming
int i;
float f;
i = 40;
f = 839.21;
printf("|%d|%5d|%-5d|%5.3d|\n", i, i, i, i);
printf("|%10.3f|%10.3e|%-10g|\n", f, f, f);
return 0;
}
• Output:
|40| 40|40 | 040|
| 839.210| 8.392e+02|839.21 |
Escape Sequences
• String literals sometimes need to contain control characters (such as tab and
newline) or characters that have a special meaning in C (such as "). These
characters can be represented by escape sequences.
Fundamentals of Programming
• Writing the \n escape causes the cursor to move to the beginning of the next
Fundamentals of Programming
line.
• Writing the \t escape causes the cursor to move to the next tab stop.
• The \" escape allows us to print the double quote character: printf("\"Hello!\"");
• Warning: Forgetting to put the & symbol in front of a variable will have
unpredictable — and possibly disastrous — results.
The scanf Function
• scanf format strings are similar to printf format strings, but usually contain only
conversion specifications:
Fundamentals of Programming
int i, j;
float x, y;
scanf("%d%d%f%f", &i, &j, &x, &y);
• Note: When used with scanf, the e, f, and g conversions are identical: each
causes scanf to read a floating-point number. A sign, exponent, and decimal
point may or may not be present.
How scanf works?
scanf ignores white-space characters (blanks, tabs, and new-line characters)
when searching for an input item. If scanf is used in the following way:
Fundamentals of Programming
If the input is
1-20.3-4.0e3z
will read
5/ 28/ 2002
but not
5 / 28 / 2002
The scanf Format String
• A white-space character in a format string matches zero or more white-space
characters in the input. The call
Fundamentals of Programming
will read
5 / 28 / 2002
and all similar inputs, regardless of the amount of space before and after each /
symbol.
The scanf Format String
• Warning: Putting a new-line character at the end of a scanf format string is
usually a bad idea:
Fundamentals of Programming
scanf("%d\n", &i);
• P4: Take an integer input n and print n, its square (n2) and its cube (n3).
• P5: Calculate and print the surface area and volume for a box with dimensions (length,
width, and height) entered by the user. [ Use float data type for all variables ]
• P6: Take gross salary of an employee (e.g. 50000) and tax rate (e.g. 5.5) as input and
print net salary as output.