scanf() Function In C and C++ To Read Input From Command Line – POFTUT

scanf() Function In C and C++ To Read Input From Command Line


scanf() function is used to read input from the console or standard input of the application in C and C++ programming language. scanf() function can read different data types and assign the data into different variable types. The input data can be read in different formats by using format specifiers. The standard input can be also called as stdin in C and C++ terminology. The scanf()  function is provided by the stdio.h library or header.

scanf() Function Syntax

As a function scanf() accepts different parameters. It has the following syntax and parameter meaning. We can also read multiple parameters with a single scanf() function. For single-parameter, only VARIABLE1 will be specified.

int scanf(const char *FORMAT, VARIABLE1, VAARIABLE2, ...)
  • `int` is the return data type which is an integer after the reading input data. This will provide the status of the reading operation.
  • `scanf` is the function name.
  • `const char *FORMAT` is the format specifier of the input.
  • `VARIABLE1`, `VARIABLE2`, …  are the variable name in which value will be set from the user input.

scanf() Return Value

The scanf() function will return an integer value which is not related to the input value. If the scanf() function is completed successfully the return value will be the input parameter count. If there is an error like matching failure, reading error or reach the end of file the return value will be less than zero which value is related to the error type.

scanf() Function Format Specifiers

Before starting the scanf() example we will provide the format specifiers. Format specifiers are used to specifying the input data format which will be set for the given variable. The format should be the same as the given variable. There are different format specifiers for different variable types like below.

  • `%c` is used to input character input  for `char` variable type
  • `%d` is used to input integer input for `int` variable type
  • `%e`,`%E`,`%f`,`%g`,`%G` are used to input floating number for `float` variable type
  • `%o` is used to input integer for `int` variable type in octal format
  • `%s` is used to input string for `char[]` variable type in string or char array format.
  • `%x`,`%X` are used to input integer for `int` variable type in hexadecimal format.
LEARN MORE  How To Sleep PowerShell with Start-Sleep Command-let Tutorial with Examples?

Format specifiers also used for format visually the input box to set some space or formatting. We can specify the character count of the input by providing the character count between % and format specifier like s,c,d,f etc.

//5 digit integer input
scanf("%5d",&age);

//30  characters input
scanf("%30s",&name);

Read/Input Single Character From Console/Standard Input

We will start with a simple example where we will read a character from the standard input or console. We will use the %c format specifier for the char array type. In this example, we will accept a char input from the user to select a car model from the given list.

/* scanf example */
#include <stdio.h>

int main ()
{
   char selection;

   printf("Please select one option:\n");
   printf("a) Ferrari\n");
   printf("b) Bugatti\n");
   printf("c) Porsche\n");


   scanf ("%c",&selection);

   printf("Your selection is %c\n",selection);
   return 0;
}
Read/Input Single Character From Console/Standard Input
Read/Input Single Character From Console/Standard Input

Read/Input Multiple Characters or String From Console/Standard Input

We can also read multiple characters or character array or a string by using scanf() function. We will use %s format specifier in order to accept a char array which is multiple characters or string which is the same with the char array. In this example, we will read the user name and favorite web site from the console as a string using %s format specifier.

/* scanf example */
#include <stdio.h>

int main ()
{
   char name[30];
   char website[50];

   printf("What is your name?\n");
   scanf ("%s",name);


   printf("What is your favourite web site?\n");
   scanf ("%s",website);


   printf("Your name is %s\n",name);
   printf("Your favourite web site is %s\n",website);
   return 0;
}
Read/Input Multiple Characters or String From Console/Standard Input
Read/Input Multiple Characters or String From Console/Standard Input

Read/Input Integer From Console/Standard Input

We can also read the user input as an integer value from the command line. We can use %d format specifier. We will read the user age in this example. If the provided value is not in integer format the application will crash because of an improper data type.

/* scanf example */
#include <stdio.h>

int main ()
{

   int age;

   printf("What is your age?\n");
   scanf ("%d",&age);

   printf("Your age is %d\n",age);

   return 0;
}
Read/Input Integer From Console/Standard Input
Read/Input Integer From Console/Standard Input

Read/Input Float or Double From Console/Standard Input

Float is another type of data that can be read from input. We can use %e,%E,%f,%g,%G format specifiers. Generally %f is used for float and %g is used for double data types. We will use %f in order to get the percentage of car usage for transportation.

/* scanf example */
#include <stdio.h>

int main ()
{

   float usage;

   printf("What is your car usage percentage?\n");
   scanf ("%f",&usage);

   printf("Your car usage is %f\n",usage);

   return 0;
}
Read/Input Float or Double From Console/Standard Input
Read/Input Float or Double From Console/Standard Input

Read/Input Hexadecimal From Console/Standard Input

Hexadecimal format is used to get input from the user in the hexadecimal base. We will use the %x in order to get user input in hexadecimal format.

/* scanf example */
#include <stdio.h>

int main ()
{

   float hex;

   printf("What is your age?\n");
   scanf ("%x",&hex);

   printf("Your car usage is %x\n",hex);

   return 0;
}

Read Multiple Values/Input with A Single scanf() Function

As stated previously we can read multiple values or input with a single scanf() function. We will just provide multiple format specifiers and add the variables to set by delimiting with commas. Also, these values or inputs can be in different data types or formats. In this example, we will read the user name and age with a single scanf() function.

/* scanf example */
#include <stdio.h>

int main ()
{

   char name[30];
   int age;

   printf("What is your name and age?\n");
   scanf ("%s %d",name,&age);

   printf("Your name is %s and age is %d\n",name,age);

   return 0;
}

Leave a Comment