0% found this document useful (0 votes)
75 views21 pages

Lecture4 - Input and Output Operations PDF

The document discusses formatted input and output operations in C programming. It describes how the printf() and scanf() functions work, including control strings that define the format for displaying or reading data. Specific format specifiers are provided for different data types like integers, floats, strings, and characters. Examples are given to demonstrate printing aligned values with fields and reading user input with field widths.

Uploaded by

gye so
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views21 pages

Lecture4 - Input and Output Operations PDF

The document discusses formatted input and output operations in C programming. It describes how the printf() and scanf() functions work, including control strings that define the format for displaying or reading data. Specific format specifiers are provided for different data types like integers, floats, strings, and characters. Examples are given to demonstrate printing aligned values with fields and reading user input with field widths.

Uploaded by

gye so
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 5

Input and Output


Operations

Department of C programming
Mechanical Engineering
Formatted Output: printf
• We have seen the use of printf function for printing captions and
numerical results.

• The printf statement provides certain features that can be


effectively exploited to control the alignment and spacing of print-
outs on the terminals.

• Control string consists of three types of items :


1. Characters that will be printed on the screen as they appear.
2. Format specifications that define the output format for display of
each item.
3. Escape sequence characters such as \n, \t, and \b.
Department of C programming
Mechanical Engineering
Formatted Output: printf
#include <stdio.h>

int main(void)
{
printf("I like programming \n");
printf("I love puppy! \n");
printf("I am so happy \n");

printf("He said \"I am a student in Mechanical Eng.\" And.. \n");


return 0;
}

Department of C programming
Mechanical Engineering
Formatted Output: printf

Table of Escape Sequence

Department of C programming
Mechanical Engineering
Formatted Output: printf
• Example of formatted output

#include <stdio.h>

int main(void)
{
int age = 12;
printf("%d years in decimal, \n %x years in hexadecimal \n", age, age);
return 0;
}

Department of C programming
Mechanical Engineering
Formatted Output: printf

Format Specifier Data Type Output form


%d char, short, int Decimal integer
%ld long Decimal integer
%lld long long Decimal integer
%u unsigned int Unsigned decimal integer
%o unsigned int Unsigned octal integer
%x, %X unsigned int Unsigned hexadecimal integer
%f float, double Floating point value without exponent
%Lf long double Floating point value without exponent
%e, %E float, double Floating point value in exponent
Floating point value either e-type or f-type
%g, %G float, double
depending on the number
%c char, short, int Single character
%s char * string
%p void * Pointer address

Department of C programming
Mechanical Engineering
Formatted Output: printf
• Output of Integer Numbers
#include <stdio.h>

int main(void)
{
int num1 = 7, num2 = 13;
printf("%o %#o \n", num1, num1);
printf("%x %#x \n", num2, num2);
return 0;
}

• Output of Real Numbers


#include <stdio.h>

int main(void)
{
printf("%f \n", 0.1234);
printf("%e \n", 0.1234);
printf("%f \n", 0.12345678);
printf("%e \n", 0.12345678);
return 0;
}

Department of C programming
Mechanical Engineering
Formatted Output: printf
• Output of Real Numbers

#include <stdio.h>

int main(void)
{
double d1 = 1.23e-3; // 0.00123
double d2 = 1.23e-4; // 0.000123
double d3 = 1.23e-5; // 0.0000123
double d4 = 1.23e-6; // 0.00000123

printf("%g \n", d1);


printf("%g \n", d2);
printf("%g \n", d3);
printf("%g \n", d4);
return 0;
}

Department of C programming
Mechanical Engineering
Formatted Output: printf
%wd
Format Output

printf (“%d”, 9876) 9 8 7 6

printf (“%6d”, 9876) 9 8 7 6

printf (“%2d”, 9876) 9 8 7 6

printf (“%-6d”, 9876) 9 8 7 6

printf (“%06d”, 9876) 0 0 9 8 7 6

Department of C programming
Mechanical Engineering
Formatted Output: printf
%w.pf y = 98.7654
Format Output
printf (“%7.4f”, y) 9 8 . 7 6 5 4

printf (“%7.2f”, y) 9 8 . 7 7

printf (“%-7.2f”, y) 9 8 . 7 7

printf (“%f”, y) 9 8 . 7 6 5 4

printf (“%10.2e”, y) 9 . 8 8 e + 0 1

printf (“%11.4e”, -y) ㅡ 9 . 8 7 6 5 e + 0 1

printf (“%-10.2e”, y) 9 . 8 8 e + 0 1

printf (“%e”, y) 9 . 8 7 6 5 4 0 e + 0 1
Department of C programming
Mechanical Engineering
Formatted Output: printf
• Printing of a Single Character

– A single character can be displayed in a desired position using the


format :

– The character will be displayed right-justified in the field of w columns.

– We can make the display left-justified by placing a minus sign before


the integer w.

Department of C programming
Mechanical Engineering
Formatted Output: printf
• Printing of Strings

– The format specification for outputting strings is similar to that of real


numbers.

– where w specifies the field width for display and p instructs that only
the first p characters of the string are to be displayed.

– The display is right-justified.

– Ex) printing a string “NEW DELHI 110001”

Department of C programming
Mechanical Engineering
Formatted Output: printf

Specification Output
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
%s N E W D E L H I 1 1 0 0 0 1

%20s N E W D E L H I 1 1 0 0 0 1

%20.10s N E W D E L H I

%.5s N E W D

%-20.10s N E W D E L H I

%5s N E W D E L H I 1 1 0 0 0 1

Department of C programming
Mechanical Engineering
Formatted Output: printf

#include <stdio.h>

int main(void)
{
printf("%-8s %16s %5s \n", "name", "Department", "Year");
printf("%-8s %16s %5d \n", "James", "Mechanical Eng.", 3);
printf("%-8s %16s %5d \n", "Tomas", "Computer Eng.", 2);
printf("%-8s %16s %5d \n", "Phillip", "Chemical Eng.", 4);
return 0;
}

Department of C programming
Mechanical Engineering
Formatted Output: printf
• Mixed Data Output

– It is permitted to mix data types in one printf statement.

printf(“%d %f %s %c”, a, b, c, d);

– As pointed out earlier, printf uses its control string to decide how
many variables to be printed and what their types are.

– If there are not enough variables or if they are of the wrong type, the
output results will be incorrect.

Department of C programming
Mechanical Engineering
Formatted Input: scanf
15.74 123 John : How to read?

• The control string specifies the field format in which the data is to
be entered and the arguments arg1, arg2, …., argn specify the
address of locations where the data is stored.
• Control string (also known as format string) contains field
specifications, which direct the interpretation of input data.

• It may include :
• Field (or format) specifications, consisting of the conversion character %, a
data type character (or type specifier), and an optional number, specifying
the field width.
• Blanks, tabs, of newlines.

Department of C programming
Mechanical Engineering
Formatted Input: scanf
• Inputting Integer Numbers
– The field specification for reading an integer number is:
%wd
– The percentage sign (%) indicates that a conversion specification
follows.

– w is an integer number that specifies the field width of the number to


be read and d, known as data type character, indicates that the
number to be read is in integer mode.

Department of C programming
Mechanical Engineering
Formatted Input: scanf
• Example:
scanf (“%2d %5d”, &num1, &num2);
Input : 50 31426
Output : num1 = 50, num2 = 31426

• Suppose the input data is as follows :


Input : 31426 50
Output : num1 = 31 (because of %2d),
num2 = 426 (unread part of 31426)

• This kind of errors may be eliminated if we use the field


specifications without the field width specifications.
scanf (“%d %d”, &num1, &num2);

Department of C programming
Mechanical Engineering
Formatted Input: scanf
• What happens if we enter a floating point number instead of an
integer? The fractional part may be stripped away! Also, scanf may
skip reading further input.

• An input field may be skipped by specifying *in the place of field


width.
scanf(“%d %*d %d”, &a, &b)

Input : 123 456 789


Output : 123 to a
456 skipped (because of *)
789 to b

Department of C programming
Mechanical Engineering
Formatted Input: scanf
• Inputting Real Numbers
– Unlike integer numbers, the field width of real numbers is not to be
specified and therefore scanf reads real numbers using the simple
specification %f for both the notations, namely, decimal point
notation and exponential notation.

scanf(“%f %f %f”, &x, &y, &z);

– If the number to be read is of double type, then the specification


should be %lf instead of simple %f.

Department of C programming
Mechanical Engineering
Formatted Input: scanf_s
#include <stdio.h>

int main(void)
{
float num1;
double num2;
long double num3;

printf("Real number input 1(e format): ");


scanf_s("%f", &num1);
printf("Input number: %f \n", num1);

printf("Real number input 2(e format): ");


scanf_s("%lf", &num2);
printf("Input number: %f \n", num2);

printf("Real number input 3(e format): ");


scanf_s("%Lf", &num3);
printf("Input number: %Lf \n", num3);
return 0;
}

Department of C programming
Mechanical Engineering

You might also like