C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Input Output operation using scanf and printf functions

C Programming

In this tutorial we will learn to handle input output operations in C programming language using the scanf and printf function.

In the previous tutorial we learned to handle single character input output operation using the getchar() and putchar() functions.

scanf function

We use the scanf() function to get input data from keyboard.

Format of scanf() function.

scanf("control string", &variable1, &variable2, ...);

The control string holds the format of the data being received. variable1, variable2, ... are the names of the variables that will hold the input value.

The & ampersand symbol is the address operator specifying the address of the variable where the input value will be stored.

Lets explore some code to see scanf in action.

In the following example we will take two integer numbers as input from the keyboard and then print the sum.

#include <stdio.h>
int main(void)
{
  int a, b, sum;
  printf("Enter two integer numbers:\n");
  scanf("%d %d", &a, &b);
  sum = a + b;
  printf("Sum: %d\n", sum);
  return 0;
}

Output

Enter two integer numbers:
10
20
Sum: 30

Format code for scanf function

Format CodeDescription
%cRead a character
%dRead a decimal integer
%ldRead a long integer
%hdRead a short integer
%eRead a floating point value
%fRead a floating point value
%lfRead a double number
%LfRead a long double number
%gRead a floating point value
%hRead a short integer
%iRead a decimal, hexadecimal or octal integer
%oRead an octal integer
%sRead a string
%uRead an unsigned decimal integer
%xRead a hexadecimal integer
%[..]Read a string of word(s)

printf function

We use the printf() function to print output.

The format of printf function to print output string.

printf("some-output-text");

In the following example we will print "Hello World" string using the printf function.

#include <stdio.h>
int main(void)
{
  printf("Hello World");
  return 0;
}

Format of printf function to output result using format code.

printf("some-output-text format-code", variable1, variable2, ...);

In the following example we will take an integer, double and a character value as input from the user and will output the entered data.

#include <stdio.h>
int main(void)
{
  //declare variables
  char ch;
  int i;
  double d;

  //take input from user
  printf("Enter an integer, a character and a double value: ");
  scanf("%d %c %lf", &i, &ch, &d);

  //output
  printf("Entered data\n");
  printf("Integer: %d\n", i);
  printf("Character: %c\n", ch);
  printf("Double: %lf\n", d);
  
  return 0;
}

Output

Enter an integer, a character and a double value: 10 a 12.34
Entered data
Integer: 10
Character: a
Double: 12.340000

Format code for printf function

Format CodeDescription
%cPrint a single character
%dPrint a decimal integer
%ldPrint a long integer
%hdPrint a short integer
%ePrint a floating point value in exponent form
%fPrint a floating point value without exponent
%lfPrint a double number
%LfPrint a long double number
%gPrint a floating point value either e-type or f-type depending on condition.
%iPrint a signed decimal integer
%oPrint an octal integer, without leading zero
%sPrint a string
%xPrint a hexadecimal integer, without leading 0x