Sei sulla pagina 1di 6

decodeschool info@decodeschool.

com

Interesting Format Specifier Examples


in C Programming
decodeschool info@decodeschool.com

Format Specifiers in C – Intersting Examples

In C printf() and scanf() functions are more important in getting input and writting output. “f” in
both scanf() and printf() repsents format. It means you can get input and write output in format like
character, integer, floating, octal, hexadecimal and so on.

We can achieve all the above and other formats using the format specifier (one or more character
prefixed by %) mentioned inside the printf() and scanf() function. In this document we have shown
five examples using format specifiers for various operations. You could see more number of
interesting C Programming Examples. These Example programs are very basic and also very
interesting. Happy Learning.

Example 1:

Question:
Get a line from user and display the same.
Input:
I am from india
Output:
I am from india

Solution:
//Program to print the given word
#include<stdio.h>

int main()

char msg[50];

scanf("%[^\n]s",&msg);

printf("%s",msg);

return 0;

Explanation:
Scanf is a function available(predefined) in c library to get input from user via
keyboard and stores the same in a variable. Here it reads input string and stores it in a variable
str1Format Specifier "%[^\\t]s" reads input as string and terminates read operation while entering
tab space or enter button.
decodeschool info@decodeschool.com

printf is a function available(pre defined) in C library which is used to print the specified content in
Monitor. Here it prints the value of the variable str1.Format Specifier "%s" prints value as String.

Example 2:

Question:
Get a number from user and display the corresponding hexadecimal value
Input:
11
Output:
B

Solution:
//Program to print the given Integer in Hexa decimal format
#include<stdio.h>

int main()

int num;

printf("Enter an Integer Number:");

scanf("%d",&num);

printf("Equivalent Hex Value: %x",num);

return 0;

Explanation:
Scanf is a function available(predefined) in c library to get input from user via
keyboard and stores the same in a variable. Here it reads input number and stores it in a variable
num.Format Specifier "%d" reads input as integer number. (d for decimal number system)

printf is a function available(pre defined) in C library which is used to print the specified content in
Monitor. Here it prints the value of the variable num.Format Specifier "%x" prints value as Hexa
decimal number.
decodeschool info@decodeschool.com

Example 3:

Question:
Get a number from user and display the corresponding Octal value Input:
Input:
11
Output:
13

Solution:
//Program to print the given Integer in Octal format
#include<stdio.h>

int main()

int num;

printf("Enter a number: ");

scanf("%d",&num);

printf("\nEquivalent octal value: %o",num);

return 0;

Explanation:
scanf is a function available(predefined) in c library to get input from user via
keyboard and stores the same in a variable. Here it reads input number and stores it in a variable
num.Format Specifier "%d" reads input as integer number. (d for decimal number system)

printf is a function available(pre defined) in C library which is used to print the specified content in
Monitor. Here it prints the value of the variable num.Format Specifier "%o" prints value as Octal
number.

Example4:
Question:
Get a ASCII value from user (0 to 255) and display the corresponding character
Input :
75
Output:
K
decodeschool info@decodeschool.com

Solution:
//Program to print the character of given ASCII value
#include<stdio.h>

int main()

{
int num;

printf("Enter a ASCII value(0 to 255)");

scanf("%d",&num);

printf("Equivalent Character: %c",num);

return 0;

Explanation:
scanf is a function available(predefined) in c library to get input from user via keyboard and stores
the same in a variable. Here it reads input number and stores it in a variable num.Format Specifier
"%d" reads Input as integer number.

printf is a function available(pre defined) in C library which is used to print the specified content in
Monitor. Here it prints the value of the variable num.Format Specifier "%c" prints value as
character (ASCII Converted to Character).

Example5:

Question:
Get a fractional number from user and display the same in two digit precision
Input:
56.3426
Output:
56.34

Solution:
//Program to print the given float (fractional) number in 2digit decimal format
#include<stdio.h>

int main()
decodeschool info@decodeschool.com

float num;

printf("Enter a fractional number:");

scanf("%f",&num);

printf("\nEntered number in 2digit precision: %.2f",num);

return 0;

Explanation:
scanf is a function available(predefined) in c library to get input from user via
keyboard and stores the same in a variable. Here it reads input number and stores it in a variable
num.Format Specifier "%f" reads input as floating number. (fractional number)

printf is a function available(pre defined) in C library which is used to print the specified content in
Monitor. Here it prints the value of the variable num.Format Specifier "%2f" prints value as
Floating Point Number with 2digit Precision(.00).

Potrebbero piacerti anche