top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Use of %n format specifier in C

+2 votes
1,154 views

What does the format specifier %n of printf function do?

posted Sep 24, 2014 by Aarti Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

5 Answers

+2 votes
 
Best answer

Unlike other specifiers, the argument associated with the %n specifier will be treated as a int* and filled with the number of total characters printed at that point in the printf.
See the below example.

#include<stdio.h>
int main()
{
  int count;
  printf("good %n morning ", &count);
  printf("%d", count);

  return 0;
}

The above program prints good morning 5.
The first printf() prints “good morning”.
The second printf() prints 5 as there are 5 characters printed before %n in the first printf.

answer Sep 24, 2014 by Ranjith
Ranjith.. thanks for your answer.. but I have one more doubt.. like could you please explain me behaviour of %n in scanf..
The %n specifier instructs scanf() to store the number of characters read from the input so far by this scanf call. No input is consumed, and the return value is not incremented.
+1 vote

%n will be used to write number of characters are printed before %n format specified in printf
for exp:

#include <stdio.h>

int main()
{
    int val = 0;

    printf("count %n written count to val\n", &val);

    printf("val = %d\n", val);

    return 0;

}

output :

 countwritten count to val
 val = 5  (before %n number of characters were 5 i.e count )
answer Sep 24, 2014 by Bheemappa G
+1 vote

It stores the number of character printed so far.

int n;
printf("Hemant %n Patel", &n);
printf("%d", n);

Output:
Hemant Patel
7

answer Sep 24, 2014 by anonymous
0 votes

It doesn't print anything. It is used to figure out how many characters got printed before %n appeared in the format string.

Use of %n with printf() and scanf():

int main()
{
    int temp = 0;
    int x = -1;
    printf("Testing %n\n",  &temp);
    //It will store the no of char got printed i.e Testing + one space char total 8 to "temp"

    scanf("%n", &x);  //Here not a single char is given .. to it store 0 to x
    printf("x = %d\n", x);

    scanf("%d%n", &temp, &x); // Here it will wait for user to give input and store its no of digit to x.
    printf("x = %d\n", x);

    return 0;
 }
answer Sep 24, 2014 by Arshad Khan
0 votes

In Case of printf- filled with the number of total characters printed at that point in the printf...
And in case of scanf -new line from previous input is being considered for consecutive scanfs

For eg:

 #include < stdio.h >
int main()
{
   int temp = 0;
   int x = -1;
   printf("Testing %n\n",  &temp);
   //It will store the no of char got printed i.e Testing + one space char total 8 to "temp"

   scanf("%d%n", &temp,&x);  //Here not a single char is given .. to it store 0 to x
   printf("x = %d\n", x);

   scanf("%d%n", &temp, &x); // Here it will wait for user to give input and store its no of digit to x.
   printf("x = %d\n", x);

   scanf("%d%n", &temp,&x);  //Here not a single char is given .. to it store 0 to x
   printf("x = %d\n", x);
   return 0;
} 

o/p:
Testing 
12
x = 2
123
x = 4
1234
x = 5
answer Sep 24, 2014 by Aarti Jain
Similar Questions
+1 vote

I have written a simple program and stored it with .c file,
When i compiled it with g++ it is getting compiled and giving the proper output when i run that,
but when i compile the same using gcc then it is throwing error.

Below is the sample .c file
#include<stdio.h>
struct A{
        private:
        int a;
        public:
        int sum(){
        a=10;
        }
        void print()
        {
        printf("%d\n",a);
        }
        A()
        {
                printf("Constructor\n");
        }
        A(int b)
        {
                printf("Copy Constructor\n");
        }
        ~A()
        {
                printf("Destructor\n");
        }
};
main()
{
        struct A a(10);
        a.sum();
        a.print();
}
+6 votes

How can we change the width in the printf() ? Can we also use negative width? What will be the impact of that?

0 votes

Write a C program to convert date from 24 hrs format to 12 hrd format?
Ex: 23:10 = 11:10PM

+1 vote

Here's a small piece of code:

 static struct {
 const char fmt[10];
 } const x = { "%dn" };
 static const char * const fmt = "%dn";
 printf(fmt, 1);
 printf(x.fmt, 1);

The second printf produces a warning:

 test.c:105:10: warning: format string is not a string literal
[-Wformat-nonliteral]
 printf(x.fmt, 1);
 ^~~~~

From my point of view both format strings are identical. Why can't gcc deduce the format string in the second case?

...