How to write printf in C

Prev NextC printf and scanf functions:printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.These func

How to write printf in C

Prev  Next

C printf and scanf functions:

  • 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.

1. printf() function in C language:

  • 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 %dformat specifier to display the value of an integer variable.
  • Similarly%cis used to display character,%ffor float variable,%sfor string variable,%lffor double and%xfor 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:C1234567891011121314151617#include <stdio.h>int main(){ char ch = 'A'; char str[20] = "fresh2refresh.com"; float flt = 10.234; int no = 150; double dbl = 20.123456; 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 %lf \n", dbl); printf("Octal value is %o \n", no); printf("Hexadecimal value is %x \n", no); return 0;}

Output:Character is A
String is fresh2refresh.com
Float value is 10.234000
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),
  • %cgot replaced by value of a character variable (ch),
  • %fgot replaced by value of a float variable (flt),
  • %lfgot replaced by value of a double variable (dbl),
  • %sgot replaced by value of a string variable (str),
  • %ogot replaced by a octal value corresponding to integer variable (no),
  • %xgot replaced by a hexadecimal value corresponding to integer variable
  • \ngot replaced by a newline.

2. scanf function in C language:

  • In C programming language, scanf() function is 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 variablestr and then displayed.

Example program for C printf and scanf functions in C programming language:C123456789101112#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 )
hai
Entered string is hai

  • 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 isjust like in a pointer which is used to point to the variable.For more information about how pointer works, pleaseclick here.

KEY POINTS TO REMEMBER IN C PRINTF() AND SCANF():

  1. printf() is used to display the output and scanf() is used to read the inputs.
  2. printf() and scanf() functions are declared in stdio.h header file in C library.
  3. All syntax in C language including printf() and scanf() functions are case sensitive.

Prev  Next

Like it? Please Spread the word!

Video liên quan