0% found this document useful (0 votes)
2 views4 pages

3printf_scanf

The document provides an overview of the printf and scanf functions in C programming, detailing their usage for formatted input and output. It explains the components of format control strings, conversion specifiers, and the capabilities of both functions for displaying and inputting various data types. Additionally, it includes guidelines for proper usage and examples to illustrate the concepts.

Uploaded by

anshureddy0612
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)
2 views4 pages

3printf_scanf

The document provides an overview of the printf and scanf functions in C programming, detailing their usage for formatted input and output. It explains the components of format control strings, conversion specifiers, and the capabilities of both functions for displaying and inputting various data types. Additionally, it includes guidelines for proper usage and examples to illustrate the concepts.

Uploaded by

anshureddy0612
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
You are on page 1/ 4

Printf and Scanf Statements 2019

Input and Output Statements


- Formatted input and output.
Displaying Output using printf( )
 printf( ) is an output function in C used to display the content on the screen.
 For precise output formatting, every printf( ) call contains a format control string that describes
the output format.
 The format control string consists of:
 Conversion specifiers.
 Field widths.
 Precisions.
 Literal characters.
 Flags.

 Together with percent sign (%), these, form conversion specifications.


 Function printf() can perform the following formatting capabilities:
 Rounding floating-point values to an indicated number of decimal places.
 Aligning a column of numbers with decimal points appearing one above the other.
 Right-justification and left-justification of outputs.
 Inserting literal characters at precise locations in a line of output.
 Representing floating-point number in exponential format.
 Representing unsigned integers in octal and hexadecimal format.
 Displaying all types of data with fixed-size field widths and precisions
 There are various forms of printf statements.
 General form of printf( ) is: printf (format-control-string, other-arguments);
 The format-control-string describes the output format, and other-arguments (optional) correspond
to each conversion specification in the format-control-string.
 Each conversion specification begins with a percent sign (%) and ends with a conversion
specifier.
 Printing Integers - Integer is a whole number, such as 880 or –456, that contains no decimal
point.
Conversion Specifier Description
d Display a signed decimal integer
i Display a signed decimal integer (Note: Both are different in scanf() )
o Display a unsigned octal integer
u Display a unsigned decimal integer
X or x Display a unsigned hexa decimal integer

1 Mr. Sukruth Gowda M A, Assistant Professor


Printf and Scanf Statements 2019
h or l (letter l) Place before any integer conversion specifier to indicate that a short
or long integer.

 Printing Floating-point Numbers - Contains the decimal point such as 35.5 or 7456.945.

Conversion specifier Description


e or E Display a floating-point value in exponential notation.
f Display floating-point values.
Display a floating-point value in either the floating-point form f or the
g or G
exponential form e ( or E)
Place before any floating-point conversion specifier to indicate that a long
l
double floating-point value is displayed.

 Exponential notation is the computer equivalent of scientific notation used in mathematics.


For example, 150.2352 is represented in scientific notation as:
1.502352 x 102
And is represented in exponential notation as
1.502352E+02 by computer
So, 150.2352 = 1.502352 x 102 = 1.502352E+02
 E stand for exponent.
 e, E and f will output with 6 digits of precision to the right of the decimal point by default.

 Printing Strings & Characters - c and s conversion specifiers are used to print individual
characters and strings respectively.
 Conversion specifier c requires a char argument and s requires a pointer to char as an argument.
 s causes characters to be printed until a terminating NULL (‘\0’) character is encountered.

Note: % - Display the percentage character.


#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Printing a %% in a format control string\n");
}

Output:
Printing a % in a format control string

2 Mr. Sukruth Gowda M A, Assistant Professor


Printf and Scanf Statements 2019
 Printing With Field Widths And Precisions - A field width determines the exact size of a field
in which data is printed.
 If the field width is larger than the data being printed, the data will normally be right-justified
within that field.
 An integer representing the field width is inserted between the percent sign (%) and the
conversion specifier in the conversion specification.
 Function printf( ) also provides the ability to specify the precision with which data is printed.
 Precision has different meanings for different data types.
Note:
1. String can also b printed directly -
printf(“ literal string”);
 Literal string may be any character or content. Whatever the content included within the
double quotes will be displayed on the output screen
 Example: printf(“Welcome to India”);
Output: Welcome to India

2. Guidelines for printf


 printf should contain control string or format string in quotes.
 Control string may or may not be followed by some variables or expressions.
 To print a value it needs convention specification or access specifier to be included.
 When printf statement is executed the convention specifier or access specifier will be
replaced by its value.
 Additional characters included between the access specifiers will be maintained as it is on the
output screen.

3 Mr. Sukruth Gowda M A, Assistant Professor


Printf and Scanf Statements 2019
Inputting Values Using scanf
 Used for precise input formatting & to enter the input through the input devices like keyboard.
 Every scanf( ) function contains a format control string that describes the format of the data to be
input.
 The format control string consists of conversion specifications and literal characters.
 Function scanf() has the following input formatting capabilities
 Inputting all types of data.
 Inputting specific characters from an input stream.
 Skipping specific characters in the input stream.
 General form of scanf( ): scanf (format-control-string, other-arguments);

For example:
scanf("%e%f%g", &a, &b, &c);
 The format-control-string describes the formats of the input.
 The other-arguments are pointers to variables in which the input will be stored (i.e..,
address of the variable where input values to be stored).

 Guidelines for scanf


1. No escape sequences or additional blank spaces should be specified in the format specifiers.
Ex: scanf(“%d %f”,&a,&b); //invalid
scanf(“%d\n%f”,&a,&b); //invalid
2. & symbol is must to read the values, if not the entered value will not be stored in the variable
specified.
Ex: scanf(“%d%f”,a,b); //invalid.

4 Mr. Sukruth Gowda M A, Assistant Professor

You might also like