C - Print How Many Inputs are taken from Keyboard using Scanf in C program

In this C program, we will learn how to get total number of inputs taken from scanf() in c program.

Since, we know that scanf() returns total number of input using the return value of scanf(), we can count the total number of inputs.

In this program, we are reading multiple numbers and storing them into an integer array, at every input scanf() is returning the number of inputs and we are adding them into count.

Print Number of Inputs taken from Keyboard using Scanf in C Program

/*C - Print How Many Inputs are Taken from 
Keyboard using Scanf in C Progra.*/
 
#include <stdio.h>
 
int main(){
    int count=0;
    int num; 
    int arr[100],i=0;
     
    while(num!=-1){
        printf("Enter an integer number (-1 to exit): ");
        count+=scanf("%d",&num);
        arr[i++]=num;
    }
     
    printf("\nTotal Inputs (including 0) are: %d\n",count);
    printf("Entered numbers are:\n");
    for(i=0;i<count;i++){
        printf("%d ",arr[i]);
    }
     
    printf("\n");
    return 0;       
}

Output

    Enter an integer number (-1 to exit): 10
    Enter an integer number (-1 to exit): 20
    Enter an integer number (-1 to exit): 30
    Enter an integer number (-1 to exit): 40
    Enter an integer number (-1 to exit): -1

    Total Inputs (including 0) are: 5
    Entered numbers are:
    10 20 30 40 -1 

C Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.