Skip to main content

C – printf and scanf



  • 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 %d format specifier to display the value of an integer variable.
  • Similarly %c is used to display character, %f for float variable, %s for string variable, %lf 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:

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),
  • %c got replaced by value of a character variable  (ch),
  • %f got replaced by value of a float variable  (flt),
  • %lf 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.

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 variable “str” and then displayed.

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

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 is just like in a pointer which is used to point to the variable. For more information about how pointer works, please click 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.

Comments

Popular posts from this blog

C – Data Types

C data types are defined as the data storage format that a variable can store a data to perform a specific operation. Data types are used to define a variable before to use in a program. Size of variable, constant and array are determined by data types. C – data types: There are four data types in C language. They are, Types Data Types Basic data types int, char, float, double Enumeration data type enum Derived data type pointer, array, structure, union Void data type void 1. Basic data types in C language: 1.1. Integer data type: Integer data type allows a variable to store numeric values. “int” keyword is used to refer integer data type. The storage size of int data type is 2 or 4 or 8 byte. It varies depend upon the processor in the CPU that we use.  If we are using 16 bit processor, 2 byte  (16 bit) of memory will be allocated for int dat...

Write a program to do the following operations:

6. Write a program to do the following operations: --Read any two positive integer operands (say op1 & op2) and one character                type operator (say opr). Note that opr is any mathematical operator. --Depending upon the operator, do the appropriate operation. e.g. if opr is '+'                 then the display the value obtained by evaluating the expression (op1 + op2) #include<stdio.h> #include<conio.h> void main() {         int a,b;         char n;         clrscr(); printf(“enter the value of a and b=”);         scanf(“%d%d”,&a,&b);         printf(“enter the operation to perform=”); ...

C – Programming Basics

If you are looking for C programs, please click here “C programs”. This C programming basics section explains a simple “Hello World” C program. Also, it covers below basic topics as well, which are to be known by any C programmer before writing a C program. C programming basic commands to write a C program A simple C program with output and explanation Steps to write C programs and get the output Creation, Compilation and Execution of a C program * How to install C compiler and IDE tool to run C programming codes Basic structure of a C program * Example C program to compare all the sections * Description for each section of the C program C programs ( Click here for more C programs )  with definition and output – C program for Prime number, Factorial, Fibonacci series, Palindrome, Swapping 2 numbers with and without temp variable, sample calculator program and sam...