In C, %n is a special format specifier. In the case of printf() function the %n assign the number of characters printed by printf(). When we use the %n specifier in scanf() it will assign the number of characters read by the scanf() function until it occurs.
- It is an edit conversion code.
- The edit conversion code %[^\n] can be used as an alternative of gets.
- C supports this format specification with a scanf() function.
- This edit conversion code can be used to read a line containing characters like variables and even white spaces.
- In general scanf() function with format specification like %s and specification with the field width in the form of %ws can read-only strings till non-white space part.
- It means they cannot be used for reading a text containing more than one word, especially with white space.
Below is the program to demonstrate the working of %n in scanf():
Below is the input to the above code:
Below is the output to the above code:
scanf() function assigns the 10 and 20 in a and b respectively and 5 in variable check because there are 5 characters read by scanf function(including spaces).
- Command line arguments example in C
- tellg() function in C++ with example
- Difference between Public and Private in C++ with Example
- Life cycle of Objects in C++ with Example
- Difference between sizeof(int *) and sizeof(int) in C/C++
- Runtime and Compile-time constants in C++
- Efficient ways to compare a variable with multiple values
- C Program to check if two given strings are isomorphic to each other
- C Program to reverse the digits of a number using recursion
- C program to find the frequency of characters in a string
- Overloading the Comma Operator
- Examination Management System in C
- C program to check whether a given number is even or odd
- Custom Jumble Word Game
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.