Friday, July 12, 2019

Format Specifiers Printf ,Scanf in C Programming

Format Specifiers Printf ,Scanf in C

 Programming

Format Specifiers Printf ,Scanf in C  Programming

In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. We use printf() function with %d format specifiers to display the value of an integer variable, %s for string variable, %If for double and %x for hexadecimal variable.
To generate a newline, we use “\n” in C printf() statement.

Note:

C Language is case sensitive.

 For example, printf() and scanf() are different from printf() and scanf(). All characters in printf() and scanf() functions must be in lower case.

Example program for C printf() function:

#include<stdio.h>
int main()
{
char ch= ‘A’;
char str[20]= “pocketedu.com”;
float flt= 10.234;
int no= 150;
double dbl= 20.123456;
octal o=226;
hexadecimal x=96;
printf(“character is %c\n”,ch);
printf(“string is %s\n”,str);
printf(“float value is %f\n”,flt);
printf(“Integer value is %d\n”,no);
printf(“double value is %If\n”,dbl);
printf(“octal value is %o\n”,dbl);
printf(“hexadecimal value is %x\n”,dbl);q
printf(“Octal value is %o\n”,no);
printf(“Hexadecimal value is %x\n”,no);
return 0;
}

Output:

Character is A
Sring is pocketedu.com
float value is 10.234         
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96
You can see the output with the same data which are placed within the double quotes of printf statement in the program except
%d got replaced by value of an integer variable (no),
%c got replaced by value of a character variable (ch),
%f got replaced by value of a float variable (flt),
%If got replaced by value of a double variable (dbl),
%s got replaced by value of a string variable(str),
%o got replaced by a octal value corresponding to integer variable  (no),
%x got replaced by a hexadecimal value corresponding to integer variable
\n got replaced by a newline.

scanf() function in C language:

In C programming  language, scanf() functionis used to read character, string, numeric data from keyboard. Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed. Then, user enters a string and this value is assigned to the variable “str” and then displayed.

Example program for print() and scanf() functions in C programming language:

#include <stdio.h>
int main()
{
char ch;
char str[100];
printf(“Enter any character\n”);
scanf(“%c”,&ch);
printf(“Entered character is %c\n”,ch);
printf(“Enter any string (upto 100 character)\n”);
scanf(“%s”,&str);
printf(“Entered string is %s\n”,str);
}

Output:

Enter any character
a
Entered character is a
Enter any string (upto 100 character)
Hiii
Entered string is Hiii

The format specifier %d is used in scanf() statement. So that, the value entered is received as an integer and %s for string. Ampersand is used before variable name “ch” in scanf() statement as &ch. It is just like in a pointer which is used to point to the variable.

Labels: