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 to make use of these printf() and scanf() library functions in C program.
#include <stdio.h>
 
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:
 
%c        character
%d        decimal (integer) number (base 10)
%ld       Long integer
%e        exponential floating-point number
%f        floating-point number
%lf       double number
%i        integer (base 10)
%o       octal number (base 8)
%s       a string of characters
%u       unsigned decimal (integer) number
%x       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. 



Formatting floating point numbers with printf
Here are several examples showing how to format floating-point numbers with printf:

printf string formatting 
Here are several examples that show how to format string output with printf:


printf special characters 
The following character sequences have a special meaning when used as printf format specifiers:

\a
Audible alert
\b
Backspace
\f
Form feed
\r
Carriage return
\?
Question mark
\n
New Line
\t
Tab
\v
Vertical tab
\\
Back slash
\0
ASCII null character
\’
Single quotes
\””
Double quotes
\o
Octal Constant
\x
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:



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 different data types


int x; scanf(“%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);
 
“ * ” in scanf:
Sometimes, you may need to exclude some character or number from user input. Suppose, the input is 30/01/2018 and you want to get day, month, year separately as integer. You can achieve this, using “ * ” in string format.int day,month,year;
 
scanf("%d%*c%d%*c%d", &day, &month, &year);
 
Here, %c is character type specifier and %*c means one character would be read from the console but it wouldn’t be assigned to any variable. Here we excluded two “/” from the input.

%*c would exclude one character. Remember, ‘/n’ and ‘/t’ are single characters.
%*d would exclude one integer.
%*f would exclude one float.
%*s would exclude one word.

Number in scanf:
Sometimes, we need to limit the number of digit in integer or float, number of character in string. We can achieve this by adding an integer(>0) in the string format.
 
int a;scanf("%5d", &a);
The code above, would take just first 5 digit as input. Any digits after 5th one would be ignored. If the integer has less that 5 digits, then it would be taken as it is.
 
%5c would take exact 5 characters as input. Remember, if you take 5 character as input and want to store in an array, then the array size must not be less than 6. This means, the extra position is reserved for a null character `\0`. Otherwise, you may face some unexpected difficulties.
 
%5d would take an integer of at most 5 digits.
 
%5f would take a float type number of at most 5 digits or 4 digits and one “ . ” as input.
 
%5s would take a string of at most 5 non white-space characters as input.

[set] in scanf: 
You can achieve regular expression like flavor in scanf using [set] in string format. This method does not work with integer or float type. Using [set], you can only take character type input.
 
char str[100];scanf("%[^\n]", str);
 
%[^\n] would take all characters in a single line as input. You may know that, whenever we press Enter in keyboard, then “\n” character is written in the console. Here, “^” means up-to and “\n” means new line and finally, [^\n] means take input up-to new line.
 
Similarly, %[^5] would take all characters as input up-to 5. If you write “abcde fghk5k zaq” in the console, then just “abcde fghk” would be taken as input.
 
%[0–9] would take just number characters as input. If you write “1234abc567” in the console, then just first numbers “1234” would be taken as input.
 
Similarly, %[a–z] would take just small letter alphabets as input. If you write “abc12ijkl” in the console, then just first small letters “abc” would be taken as input.
 
%[123] would just take any combination of 1, 2, 3 as input. Anything other than 1,2,3 would be ignored. If you write “1123390123” in the console then just first “11233” would be taken as input.

for more details of various input output functions visit https://en.cppreference.com/w/c/io/fscanf

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n ...

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Devel...