Skip to main content

Input Output-printf() and scanf()

printf()  and scanf() function in C:
  • printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.These functions are declared and related macros are defined in “stdio.h” which is a header file in C language.
  • We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions in C language.
printf() function in C:
  • In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
    Here’s a quick summary of the available printf format specifiers:
decimal (integer) number (base 10)
Long integer
exponential floating-point number
floating-point number
integer (base 10)
octal number (base 8)
a string of characters
unsigned decimal (integer) number
number in hexadecimal (base 16)
print a percent sign
print a percent sign

Controlling integer width with printf
The %3d specifier is used with integers, and means a minimum width of three spaces, which, by default, will be right-justified:
Left-justifying printf integer output
To left-justify integer output with printf, just add a minus sign (-) after the % symbol,
printf integer zero fill option
To zero-fill your printf integer output, just add a zero (0) after the % symbol,
printf integer formatting
As a summary of printf integer formatting, here’s a little collection of integer formatting examples. Several different options are shown, including a minimum width specification, left-justified, zero-filled, and also a plus sign for positive numbers.
Description Code Result
At least five wide printf("'%5d'", 10); ' 10'
At least five-wide, left-justified printf("'%-5d'", 10); '10 '
At least five-wide, zero-filled printf("'%05d'", 10); '00010'
At least five-wide, with a plus sign printf("'%+5d'", 10); ' +10'
Five-wide, plus sign, left-justified
printf("'%-+5d'", 10);
'+10 '

formatting floating point numbers with printf
Here are several examples showing how to format floating-point numbers with printf:
Description Code Result
Print one position after the decimal printf("'%.1f'", 10.3456); '10.3'
Two positions after the decimal printf("'%.2f'", 10.3456); '10.35'
Eight-wide, two positions after the decimal printf("'%8.2f'", 10.3456); ' 10.35'
Eight-wide, four positions after the decimal printf("'%8.4f'", 10.3456); ' 10.3456'
Eight-wide, two positions after the decimal, zero-filled printf("'%08.2f'", 10.3456); '00010.35'
Eight-wide, two positions after the decimal, left-justified printf("'%-8.2f'", 10.3456); '10.35 '
Printing a much larger number with that same format printf("'%-8.2f'", 101234567.3456); '101234567.35'

printf string formatting
Here are several examples that show how to format string output with printf:
Description Code Result
A simple string printf("'%s'", "Hello"); 'Hello'
A string with a minimum length printf("'%10s'", "Hello"); ' Hello'
Minimum length, left-justified printf("'%-10s'", "Hello"); 'Hello '

printf special characters
The following character sequences have a special meaning when used as printf format specifiers:
Audible alert
Form feed
Carriage return
Question mark
New Line
Vertical tab
Back slash
ASCII null character
Single quotes
Double quotes
Octal Constant
Hexadecimal constant

As you can see from that last example, because the backslash character itself is treated specially, you have to print two backslash characters in a row to get one backslash character to appear in your output.
Here are a few examples of how to use these special characters:
Description Code Result
Insert a tab character in a string printf("Hello\tworld"); Hello world
Insert a newline character in a string printf("Hello\nworld"); Hello
Typical use of the newline character printf("Hello world\n"); Hello world
A DOS/Windows path with
backslash characters
printf("C:\\Windows\\System32\\"); C:\Windows\System32\

scanf() function in C language:
  • In C programming language, scanf() function is used to read character, string, numeric data from keyboard
The examples show various syntax for reading diffrent data types
int x; sacnf(“%i”,&x); or scanf(“%d”,&x);
float x;scanf(“%f”,&x);
char x; scanf(“%c”,&c);
long int x; scanf(“%ld”,&x);
char str[100];scanf(“%s”,str);


Popular posts from this blog

KTU C programming question paper and evaluation scheme

PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark

2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …