Difference between scanf() and gets() in C Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report scanf()It is used to read the input(character, string, numeric data) from the standard input(keyboard).It is used to read the input until it encounters a whitespace, newline or End Of File(EOF). For example see the following code: C // C program to see how scanf() // stops reading input after whitespaces #include <stdio.h> int main() { char str[20]; printf("enter something\n"); scanf("%s", str); printf("you entered: %s\n", str); return 0; } Here the input will be provided by the user and output will be as follows: Input: Geeks for Geeks Output: Geeks Input: Computer science Output: ComputergetsIt is used to read input from the standard input(keyboard).It is used to read the input until it encounters newline or End Of File(EOF). C // C program to show how gets() // takes whitespace as a string. #include <stdio.h> int main() { char str[20]; printf("enter something\n"); gets(str); printf("you entered : %s\n", str); return 0; } Here input will be provided by user as follows Input: Geeks for Geeks Output: Geeks for Geeks Input: Computer science Output: Computer science The main difference between them is: scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it encounters whitespace instead it takes whitespace as a string.scanf can read multiple values of different data types whereas gets() will only get character string data. The difference can be shown in tabular form as follows: scanf()gets()when scanf() is used to read string input it stops reading when it encounters whitespace, newline or End Of Filewhen gets() is used to read input it stops reading input when it encounters newline or End Of File. It does not stop reading the input on encountering whitespace as it considers whitespace as a string.It is used to read input of any datatypeIt is used only for string input. Its syntax is -: scanf(const char *format, ...) Its syntax is -: char *gets(char *str) It is fast to take input.It takes one parameter that is the pointer to an array of charsFormat specifiers is used inside scanf to take input.Its return value is str on success else NULL.How to read complete sentence from user using scanf() Actually we can use scanf() to read entire string. For example we can use %[^\n]s inside scanf() to read entire string. C // C program to show how to read // entire string using scanf() #include <stdio.h> int main() { char str[20]; printf("Enter something\n"); // Here \n indicates that take the input // until newline is encountered scanf("%[^\n]s", str); printf("%s", str); return 0; } The above code reads the string until it encounters newline. Examples: Input: Geeks for Geeks Output: Geeks for Geeks Input: Computer science Output: Computer science Comment More infoAdvertise with us Next Article What is the difference between printf, sprintf and fprintf? V vaibhav_arora Follow Improve Article Tags : Difference Between C Language c-input-output Similar Reads Difference between return and printf in C In C programming, return and print serve fundamentally different purposes and they are used in distinct contexts to achieve specific tasks. Let's see each of them and see what are their functions and differences. 1. Return Statement In C, return is a statement used within a function to terminate the 2 min read Difference between getc(), getchar(), getch() and getche() In C, getc(), getchar(), getch(), and getche() are all functions that is used to read input character in different contexts. Although these functions are used for similar purposes and have similar names, they have different behaviours and use cases.The below table lists the primary differences betwe 3 min read Difference between %d and %i format specifier in C language A format specifier is a special character or sequence of characters used to define the type of data to be printed on the screen or the type of data to be scanned from standard input. A format specifier begins with a '%' character followed by the sequence of characters for different types of data. In 3 min read What is the difference between printf, sprintf and fprintf? The printf() function is used as a standard method for output operations and C also provides some different versions of this function such as sprintf() and fprintf(). These functions are also used for output operations but in different contexts.The below table lists the primary differences between t 3 min read Difference between while(1) and while(0) in C language Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break 3 min read Difference between Sentinel and Counter Controlled Loop in C Sentinel Controlled Loop A sentinel controlled loop is also called an indefinite repetition loop because the number of iterations is not known before the loop starts executing. In a sentinel controlled loop, a special value called sentinel value is used to change the loop control expression from tru 3 min read Like