Formatting functions in C Language

Similar documents
C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

BSM540 Basics of C Language

Input/Output: Advanced Concepts

Unit 4. Input/Output Functions

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Course organization. Course introduction ( Week 1)

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

M1-R4: Programing and Problem Solving using C (JULY 2018)

Chapter 3 Basic Data Types. Lecture 3 1

Reserved Words and Identifiers

Fundamentals of Programming

Fundamental of Programming (C)

Introduction to Computing Lecture 03: Basic input / output operations

AWK - PRETTY PRINTING

Programming in C Quick Start! Biostatistics 615 Lecture 4

Input / Output Functions

C Programming Language Review and Dissection III

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING PROGRAMMING TECHNIQUES I EE271

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Advanced C Programming Topics

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Stream Model of I/O. Basic I/O in C

Binghamton University. CS-220 Spring Includes & Streams

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

Input/Output and the Operating Systems

Data Types and Variables in C language

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

EL2310 Scientific Programming

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

A Fast Review of C Essentials Part I

Lecture 3. More About C

First of all, it is a variable, just like other variables you studied

File Handling in C. EECS 2031 Fall October 27, 2014

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

BLM2031 Structured Programming. Zeyneb KURT

Introduction to C An overview of the programming language C, syntax, data types and input/output

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Basic Types and Formatted I/O

CSC 1107: Structured Programming

CSC 1107: Structured Programming

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

System Software Experiment 1 Lecture 7

Number Systems, Scalar Types, and Input and Output

BSM540 Basics of C Language

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

THE FUNDAMENTAL DATA TYPES

Library Functions. General Questions

H192 Midterm 1 Review. Tom Zajdel

Fundamentals of C Programming

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Basics of Programming

CpSc 1111 Lab 4 Formatting and Flow Control

VARIABLES AND CONSTANTS

Machine Arithmetic 8/31/2007

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

ANSI C Programming Simple Programs

Beginning C Programming for Engineers

Ch 6-2. File Input / Output

Digital Computers and Machine Representation of Data

Standard C Library Functions

Chapter 3. Fundamental Data Types

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Computers Programming Course 5. Iulian Năstac

File I/O. Arash Rafiey. November 7, 2017

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Dept. of CSE, IIT KGP

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Technical Questions. Q 1) What are the key features in C programming language?

Operators and Expressions:

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Programing in C. Pierre-Alain FOUQUE

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Fundamentals of C. Structure of a C Program

C - Basic Introduction

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

CSC 1107: Structured Programming

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Chapter 7. Basic Types

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Data Type Fall 2014 Jinkyu Jeong

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Computer Organization and Assembly Language. Lab Session 4

As stated earlier, the declaration

Transcription:

Formatting functions in C Language Formatting means display data in different format, within given set of columns, show specified set of decimal and align the data to left or right along with zero fill options. Format Specifiers Meaning %d or i Decimal format (signed integer) %o Octal format %x or X Hexadecimal format %u Unsigned Integer %c Character format [Single Character Only] %s String format %f Fixed Float %e or E Exponent Float or Scientific Float %g or G General Float %lf %ld double and long double long integer %p Pointer Address %% Print % sign C provides various functions to format the data under stdio.h header file printf() cprintf() sprintf()

fprintf() C provides pre-defined set of codes to display data in different format, align the data and define the decimal points in the output along with zero fill option. Syntax is % flag width.decimals format The flag can be + (default) - 0 to pad with leading zeros # to pad 0 for octal and 0x for hexa decimal numbers

Difference between %d and %i %d and %i are same in case of printf() since they consider base 10 decimal output by default. But while getting input using scanf() they are different. %d always consider base 10 number while %i automatically detects the base, based on 0 for Octal, 0x or 0X for Hexadecimal. %d cannot understand 0x or 0X prefix while %i can understand it.

How we can prove that double takes 8 bytes of memory without using sizeof()? Answer: Using %p or %u format Specifiers to view the address of variables

How to print %d is used for decimal input and output?

How to define columns and decimal points while displaying some output? Standard Output Screen has 25 rows and 80 columns per row. We can divide the 80 columns to display different fields in different columns. By default all data is right aligned. Use hyphen (-) to make it left align.

Decimal point can also be used to extract some characters from a string starting from first position. Example

What is mean by zero fill? While printing the output we can automatically fill zeros in the left side of the number to avoid to write or print data in left side.

What is mean by formatting in scanf() function? We can also format data while getting input For Example WAP to input some date in ddmmyyyy format Now we need to break first 2 digits as day number, next 2 digits as month number and next 4 digits as year.

What is suppression character? Why and how to use it? Special character in C which can be used to avoid (suppress) a character input. It is written as %*c Example WAP to input date of birth in any of format like dd-mm-yyyy dd/mm/yyyy dd:mm:yyyy dd mm yyyy etc.

Here we need to suppress 3 rd character and 6 th character What is mean by cprintf()? The function cprintf() is used to print in color Use textcolor() function to define the text color and textbackground() to define the background color. These functions are provided in conio.h header file. All color names are constants and written in capital. We can use the color name or its value. Example WAP to print "C is a beautiful language in Yellow color on Green background with blinking text

What is sprintf()? The function sprintf() is used to print formatted data into a string and use that date later on. Syntax sprintf(char buffer[],char *format, argumentlist) Example WAP to input two numbers and store the product of those two numbers into a string and print that string on the monitor in Green color. The function sprintf() is best suitable to return some formatted result from a function. Example

WAP having a function which take a year and returns a message as given year is leap year or given year is not a leap year. What is fprintf()? The function fprintf() is used to write some contents to the file. Syntax fprintf(file *fp, format", argumentlist) Example WAP to input two numbers and save product of those numbers into a file.

How to get multiline input using scanf()? You can get the multiline input by defining the terminating character other than \n while getting input with scanf() function Example

With best wishes Prof.(Dr.) B P Sharma ABES Engineering College, Ghaziabad +91 98108 49501