Input and Output functions in C

In this tutorial, we will learn to use scanf() and printf() to take input and display the output from the user.

So let’s discuss input and output functions in C without wasting any time because there is a saying that “Time is money”.

Input and Output functions in C

C Input

  • In the C programming language, scanf() is used to take input from the user.
  • The scanf() function reads input given using the keyboards.

Example 1: Let’s see how to take Integer input/output

#include <stdio.h>
int main()
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &a);  
    printf("Number = %d",a);
    return 0;
}

Output

Enter an integer: 10
Number = 10

Code Explanation

  • %d is a format specifier inside the scanf() function to take int input from the user.
  • The integer number entered by the user is stored in a variable n.
  • &- This symbol is called ampersand and is also known as the address of operator.
  • &n inside scanf() gets the address of variable n and the value entered by the user is stored in that address.

Example 2: Let’s see how to take Float and Double Input/Output

#include <stdio.h>
int main()
{
    float x;
    double y;

    printf("Enter a number: ");
    scanf("%f", &x);
    printf("Enter another number: ");
    scanf("%lf", &y);

    printf("num1 = %f\n", x);
    printf("num2 = %lf", y);

    return 0;
}

Output

Enter a number: 19.523
Enter another number: 18.2
x = 19.523000
y = 18.200000

Note:- Here we have used %f and %lf format specifier for float and double respectively.


Example 3: Character I/O in C

#include <stdio.h>
int main()
{
    char ch;
    printf("Enter a character: ");
    scanf("%c",&ch);     
    printf("Entered character is %c.", ch);  
    return 0;
}   

Output

Enter a character: A
Entered character is A

Try the above code here with different characters.

Code Explanation

  • Character data type is denoted by char in input and output function in c programming.
  • Character entered by the user is not stored itself, instead, an integer value (ASCII value) is stored.
  • %c text format specifier is used to display character.
  • Instead of character if we want to display it’s ASCII value we have to use %d
  • In example 4 below, let’s see how to print ASCII value.

Example 4: ASCII Value

#include <stdio.h>
int main()
{
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);     

    printf("Entered character is %c.\n",ch); // Here we are printing the character

    printf("ASCII value of %c %d.",ch, ch);// Here we are printing the ASCII  
                                     // Value 
    return 0;
}

Output

Enter a character: a
Entered character is a
ASCII value of a is 97

You can have a full list of ASCII values here.

Try the above code here with different values.

Till now we have taken a single input of one type only. Let’s see how can we give multiple inputs of different data types in a single statement.

Input and Output functions in C for Multiple Values

Let’s write a code snippet to illustrate multiple I/O values.

#include <stdio.h>
int main()
{
    int a;
    float b;

    printf("Enter any integer and a float values: ");
    scanf("%d%f", &a, &b);

    printf("Entered values are %d and %f", a, b);  
    return 0;
}

Output

Enter any integer and a float values:9
5.6
Entered values are 9 and 5.600000

I/O Format Specifiers

Let’s summarize what we have learnt from the above examples.

  • %d for int
  • %f for float
  • %lf for double
  • %c for char

Let’s have a look at commonly used C data types and their format specifiers.

Data TypeFormat Specifier
int%d
char%c
float%f
double%lf
short int%hd
unsigned int%u
long int%li
long long int%lli
unsigned long int%lu
unsigned long long int%llu
signed char%c
unsigned char%c
long double%Lf

C Output

Now in this block, as the heading suggests we are going to discuss how to print/display the outputs in C programming.

printf() is one of the main output functions that sends formatted output to the screen.

Example 1: Output

#include <stdio.h>    
int main()
{ 

    printf("Welcome to the World of C Programming");
    return 0;
}

Output

Welcome to the World of C Programming

How does this program work?

  • All C programs must contain the function main().
  • The code execution of every C program start from the main() function.
  • printf() is a library function to send output to the screen, prints everything as it is when entered in double quotes(” “) and terminated by semi colon (;).
  • We must include stdio.h header file using the  #include<stdio.h> statement for using printf() function.
  • The return 0; statement is the “Return status” of the program and it is not mandatory i.e., It’s optional.

3 thoughts on “Input and Output functions in C”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Please Reload After Disabling Ad\'s