Monday, November 28, 2022
HomeFun With ProgrammingDifference between scanf() and gets() in C And Other input methods in...

Difference between scanf() and gets() in C And Other input methods in c

Difference between scanf() & gets() in c ,Hello everyone how are you all? I hope you will be safe at your home. So friends today of in this article we will discuss what is scanf() and gets() in C. And we will also see the difference between scanf() and gets() in C.And in this article we will also cover the rest of the function of c programming like getch(),fgets(),getchar() and getline().

Then let’s start from what is the scanf() function in C Programming?

What is Scanf() In C – Difference between scanf() and gets() in c

so I tell you, friends,

actually, The scanf() function is an inbuilt library function in the C programming language that is available by default in the C library.

To use the scanf() function in c programming you have to compulsory include a header file in your C program. Because this function and related macros are declared in the header file.

  • Scanf() is used to read input from the user.
  • scanf() function is used to read input data character, string, numeric data from console
  • Syntax Of scanf() function is : scanf(“format string”,argument_list);

Example Of scanf() function

void main()
int a;
printf("enter the value of a\n");

As you can see in the above program,

I have taken a variable named “a” and to take the value of “a” from the keyboard I used the scanf() function.

What is gets() in C – Difference between scanf() and gets() in c?

A function named gets() has been provided in the C Library to meet the shortfall of the scanf() function.

Using this function, we can input the desired length String into any Array_Identifier. This function terminates when we press the Enter key of the keyboard after inputting the string.

Example Of gets() function

int main()
	/* code */
	char c[25];
	printf("Enter String\n");
	printf("The Entered String Is %s",c);
	return 0;

In this above-created function,

we have to give the name of the array in which we want to store the string inside the brackets.

you can see I highlighted the line in which I gave the name of the array in the gets() function.

So till now, you all have seen what is scanf() function and gets() function now let’s see the difference between scanf and gets.

Difference B/W Scanf() & gets()

The first difference is that whenever we use the scanf() function to read the value from the keyboard. It stops the reading from the console when it gets the whitespace and new line in the value.

On the other hand, whenever we use the gets() function to read the value from the keyboard it won’t stop the reading when it will get the whitespace.

Because it considers the whitespace as a string. It only stops reading when it will get the new line in the value.

The second difference between scanf() and gets() is that the scanf() function is used to read the input of any datatype whereas the gets() function is used to read the input of only string datatype.

Some Other Input Methods in C

What is getch() in C?

getch() is a single character input function defined in the conio.h header file. This is a single character input function that means it can take input of only one character but that character is not printed on the screen which means it is not visible.

This function also works the same way as getchar() does, but the character inputted in this function is not visible on the screen while inputting.

We can also use this function when we want to get the password from the user because the password is never appearing while inputting on the screen.


int main()
    /* code */
    char tocnvrt;
    printf("Enter the charater\n");
    tocnvrt = getch();
    printf("The Charcter Is %c",tocnvrt);
    return 0;

What Is getchar() in c

The getchar() function reads only one character received from the keyboard. This function keeps the characters coming from the keyboard in its buffer until we use the Enter key located on the keyboard.

getchar is a library function of the standard input/output <stdio.h> header file, so #include<stdio.h> preprocessor is necessary.

Syntax : getchar(void)


#include <stdio.h>

int main(){

char c;

printf("Enter a character :");
c = getchar();

printf( "You entered character is : ");

return 0;

What is fgets() in c?

fgets() : – This is a function of File Handling. This Function comes under <stdio.h> header file. Here fgets means to read the string of the file.

Syntax for fgets()

fgets(char_array, size_of_char, file_pointer) ;

  • Here char_array – This is a user-defined char data type variable. It stores the read string inside itself.
  • size_of_char – The number of characters to be taken from the file is written here.
  • file_pointer – File Pointer is used to open/close the file. The fopen function is written in the variable of the file pointer.

Example Of fgets() Function

int main()
    char storeFileContent[30];
    FILE *filepointer;
    filepointer = fopen("file.txt","r");
    printf("File Contents Is:\n%s",storeFileContent);
    return 0;
  • In above program of fgets() function first of all i took the one variable of char data type
  • that will store the readed content of the file .
  • Secondaly I took the file pointer for open the file.
  • then i used the fgets() function of c and pass the three parameter.
  • first is the the variable in which i want to store the conntent of the file,
  • second parameter is for the value specified that define how much value i have to read from the file.
  • and last one is the file pointer
  • After the i have printed the content of the file using printf statement.

I hope you all will be understood that what is scanf() and gets() or what is the difference between them in c. If you have any queries and suggestions regarding this topic you can ask us by commenting on the comments section.Thank you for visiting RKR Knowledge keep visiting and stay safe.

You can also read

Suhashini Mishra
Suhasini Mishra from India passionate UI UX Developer | Content Writer | Content Researcher | Ionic Developer


Please enter your comment!
Please enter your name here

- Advertisment -

Stay Connected


Most Popular

Recent Comments