
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is the use of %n in printf()?
In C language, %n is a special format specifier. It cause printf() to load the variable pointed by corresponding argument. The loading is done with a value which is equal to the number of characters printed by printf() before the occurrence of %n.
Note − It does not print anything. Another printf() function is used to print the statement.
Here is an example of %n in C language,
Example
#include<stdio.h> int main() { int s; printf("The value of %ns : ", &s); printf("%d", s); getchar(); return 0; }
Output
The value of s : 13
Even if we give the value to the identifier, it will not consider the value given by us. It counts the characters used before the use of %n in the statement. It will not count %n as a character.
Here is an example if we pass the value,
Example
#include<stdio.h> int main() { int s; int m = 28; int val; printf("The value of %ns and %nm %nval : ", &s, &m, &val); printf("%d\t%d\t%d", s, m, val); return 0; }
Output
The value of s and m val : 131921
- Related Articles
- What is the use of `%p` in printf in C?
- What is the correct way to use printf to print a size_t in C/C++?
- How to use printf () in Android sqlite?
- What is the use of StrictMath class in Java?\n
- What is the difference between printf() and cout in C++?
- How to use formatting with printf() correctly in Java?
- What is the use of the Optional.stream() method in Java 9?\n
- What is the use of @JacksonInject annotation using Jackson in Java?\n
- How to print a formatted text using printf() method in Java?\n
- How to change the output of printf() in main()?
- printf() function in PHP
- Execution of printf with ++ operators in C
- What are printf conversion characters and their types?
- Return values of printf() and scanf() in C
- What is the use of "is" keyword in C#?

Advertisements