Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE) PDF Download

Introduction

 

The slides cover the following topics:

  1. Streams
  2. Formatting Output with printf
  3. Printing Integers
  4. Printing Floating-Point Numbers
  5. Printing Strings and Characters
  6. Other Conversion Specifiers
  7. Printing with Field Widths and Precision
  8. Printing with Field Widths and Precision

 

C Formatted Input/Output-----------------------------------------------Next slide

 

OBJECTIVES

 

In this chapter you will learn:

•To use input and output streams.
•To use all print formatting capabilities.
•To use all input formatting capabilities.
•To print with field widths and precisions.
•To use formatting flags in the printf format control string.
•To output literals and escape sequences.
•To format input using scanf.
 
C Formatted Input/Output-----------------------------------------------Next slide
 
Outline
 

9.1  Introduction

9.2  Streams

9.3  Formatting Output with printf

9.4  Printing Integers

9.5  Printing Floating-Point Numbers

9.6  Printing Strings and Characters

9.7  Other Conversion Specifiers

9.8  Printing with Field Widths and Precision

9.9  Using Flags in the printf Format Control String

9.10  Printing Literals and Escape Sequences

9.11  Reading Formatted Input with scanf

 

C Formatted Input/Output-----------------------------------------------Next slide

 

9.1 Introduction

 

In this chapter
Presentation of results
scanf and printf
Streams (input and output)
-gets, puts, getchar, putchar (in <stdio.h>)
 
C Formatted Input/Output-----------------------------------------------Next slide
 
9.2 Streams
 
Streams
  • Sequences of characters organized into lines
  • Each line consists of zero or more characters and ends with newline character
  • ANSI C must support lines of at least 254 characters

 

Performs all input and output
 
Can often be redirected
  • Standard input keyboard
  • Standard output screen
  • Standard error screen
  • More in Chapter 11
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
9.3 Formatting Output with printf
 
printf
Precise output formatting
-Conversion specifications: flags, field widths, precisions, etc.
Can perform rounding, aligning columns, right/left justification, inserting literal characters, exponential format, hexadecimal format, and fixed width and precision
 
Format
printf( format-control-string, other-arguments );
Format control string: describes output format
Other-arguments: correspond to each conversion specification in format-control-string
-Each specification begins with a percent sign(%), ends with conversion specifier
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
Common Programming Error 9.1
 

Forgetting to enclose a format-control-string in quotation marks is a syntax error.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Good Programming Practice 9.1

 

Format outputs neatly for presentation to make program outputs more readable and reduce user errors.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

9.4 Printing Integers

 

Integer
Whole number (no decimal point):  25, 0, -9
Positive, negative, or zero
Only minus sign prints by default (later we will change this)

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

Fig. 9.1 | Integer conversion specifiers.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Common Programming Error 9.2

 

Printing a negative value with a conversion specifier that expects an unsigned value.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

9.5 Printing Floating-Point Numbers

 

Floating Point Numbers
Have a decimal point (33.5)
Exponential notation (computer's version of scientific notation)
-150.3 is 1.503 x 10² in scientific
-150.3 is 1.503E+02 in exponential
 
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

Fig. 9.3 | Floating-point conversion specifiers.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Error-Prevention Tip 9.1

 

 

When outputting data, be sure that the user is aware of situations in which data may be imprecise due to formatting (e.g., rounding errors from specifying precisions).

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

C Formatted Input/Output-----------------------------------------------Next slide

 

9.6 Printing Strings and Characters

 

c
Prints char argument
Cannot be used to print the first character of a string
 
s
Requires a pointer to char as an argument
Prints characters until NULL ('') encountered
Cannot print a char argument
 
Remember
Single quotes for character constants ('z')
Double quotes for strings "z" (which actually contains two characters, 'z' and '')
 
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
Common Programming Error 9.3
 

Using %c to print a string is an error. The conversion specifier %c expects a char argument. A string is a pointer to char (i.e., a char *).

 

C Formatted Input/Output-----------------------------------------------Next slide
 
 
Common Programming Error 9.4
 

Using %s to print a char argument, on some systems, causes a fatal execution-time error called an access violation. The conversion specifier %s expects an argument of type pointer to char.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Common Programming Error 9.5

 

Using single quotes around character strings is a syntax error. Character strings must be enclosed in double quotes.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Common Programming Error 9.6

 

Using double quotes around a character constant creates a pointer to a string consisting of two characters, the second of which is the terminating null. A character constant is a single character enclosed in single quotes.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C Formatted Input/Output-----------------------------------------------Next slide

 

9.7 Other Conversion Specifiers

 

p
Displays pointer value (address)
 
n
Stores number of characters already output by current printf statement
Takes a pointer to an integer as an argument
Nothing printed by a %n specification
Every printf call returns a value
-Number of characters output
-Negative number if error occurs
 
%
Prints a percent sign
%%
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
Portability Tip 9.1
 

The conversion specifier p displays an address in an implementation-defined manner (on many systems, hexadecimal notation is used rather than decimal notation).

 

C Formatted Input/Output-----------------------------------------------Next slide

 

 

Common Programming Error 9.7

 

Trying to print a literal percent character using % rather than %% in the format control string. When % appears in a format control string, it must be followed by a conversion specifier.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

Fig. 9.6 | Other conversion specifiers.

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C Formatted Input/Output-----------------------------------------------Next slide

 

Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

C Formatted Input/Output-----------------------------------------------Next slide

 

9.8 Printing with Field Widths and Precision

 

Field width
Size of field in which data is printed
If width larger than data, default right justified
-If field width too small, increases to fit data
-Minus sign uses one character position in field
Integer width inserted between % and conversion specifier
%4d field width of 4
 
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)
 
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)
 
 
C Formatted Input/Output-----------------------------------------------Next slide
 
 
Common Programming Error 9.8
 

Not providing a sufficiently large field width to handle a value to be printed can offset other data being printed and can produce confusing outputs. Know your data!

 

C Formatted Input/Output-----------------------------------------------Next slide

 

9.8 Printing with Field Widths and Precision

 

Precision
Meaning varies depending on data type
Integers (default 1)
-Minimum number of digits to print

If data too small, prefixed with zeros

Floating point
-Number of digits to appear after decimal (e and f)

For g maximum number of significant digits

Strings
-Maximum number of characters to be written from string
Format
-Use a dot (.) then precision number after %

  %.3f

The document Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

FAQs on Chapter - C Formatted Input/Output, PPT, PF, Semester, Engineering - Computer Science Engineering (CSE)

1. What is the purpose of formatted input/output in C?
Ans. Formatted input/output in C allows us to control the way data is displayed or read from the console or files. It provides a way to specify the format of the data being input or output, such as the number of decimal places to display or the width of the field.
2. How can I format the output to display a floating-point number with a specific number of decimal places in C?
Ans. To format the output of a floating-point number with a specific number of decimal places in C, you can use the printf() function with the format specifier "%.nf", where 'n' is the desired number of decimal places. For example, to display a floating-point number with 2 decimal places, you would use "%.2f".
3. What is the difference between scanf() and printf() in C?
Ans. scanf() and printf() are both functions used for formatted input/output in C, but they have different purposes. scanf() is used for reading input from the user or a file, while printf() is used for displaying output to the console or a file. scanf() uses format specifiers to specify the type of input to be read, while printf() uses format specifiers to specify the type and format of output to be displayed.
4. How can I read a string with spaces using scanf() in C?
Ans. By default, scanf() stops reading input as soon as it encounters a space. To read a string with spaces using scanf() in C, you can use the format specifier "%[^\n]". This tells scanf() to read all characters until it encounters a newline character (\n), allowing you to read a string with spaces.
5. Can I use printf() to print a variable's memory address in C?
Ans. Yes, you can use printf() to print a variable's memory address in C. To do this, you can use the format specifier "%p" and pass the address of the variable as an argument. This will display the memory address of the variable in hexadecimal format. For example, printf("%p", &variable) will print the memory address of the variable.
Download as PDF

Top Courses for Computer Science Engineering (CSE)

Related Searches

Semester

,

PF

,

Free

,

Engineering - Computer Science Engineering (CSE)

,

shortcuts and tricks

,

Semester

,

pdf

,

ppt

,

Extra Questions

,

Chapter - C Formatted Input/Output

,

Objective type Questions

,

Viva Questions

,

MCQs

,

past year papers

,

Important questions

,

video lectures

,

mock tests for examination

,

Summary

,

practice quizzes

,

Engineering - Computer Science Engineering (CSE)

,

Semester Notes

,

PPT

,

PF

,

PPT

,

PPT

,

Previous Year Questions with Solutions

,

Semester

,

Engineering - Computer Science Engineering (CSE)

,

Exam

,

Sample Paper

,

study material

,

PF

,

Chapter - C Formatted Input/Output

,

Chapter - C Formatted Input/Output

;