Thread: %n specifier use in C

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    8

    %n specifier use in C

    Can you help me why %n specifier is used for in this programming example that i get from deitels book.
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    
    int main()
    {
      int a[10] = { 0 };            /* random integers from 1 to 1000 */
      int i;                        /* loop counter */
      int count;                    /* number of characters in current value */
      int totalCount = 0;           /* total characters in array */
    
      srand(time(NULL));
    
      /* fill the array with random numbers */
      for (i = 0; i <= 9; i++) {
        a = 1 + rand() % 1000;
      }                             /* end for */
    
      /* print table headers */
      printf("%s\t%s\n", "Value", "Total characters");
    
      /* loop through 10 elements */
      for (i = 0; i <= 9; i++) {
        printf("%d%n", a, &count);
        totalCount += count;        /* update totalCount */
        printf("\t%d\n", totalCount);
      }
    }
    Last edited by Salem; 11-04-2020 at 11:30 PM. Reason: Removed crayola - use copy/paste as text

  2. #2
    Registered User
    Join Date
    Oct 2020
    Posts
    1
    The percent symbol is a wildcard. The percent symbol tells the compiler where to substitute the value from the variable in the string.
    In your case


    printf ("% s \ t% s \ n", "Value", "Total characters");


    you say you want to print two string values. But I assume the output will be integers, so you should use the following:
    pro integer
    printf ("% i \ t% i \ n", "Value", "Total characters");


    or
    or a number of the double data type.
    printf ("% d \ t% d \ n", "Value", "Total characters");

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    164
    From "man fprintf":


    n The number of characters written so far is stored into the integer pointed to by the corresponding argument. That
    argument shall be an int *, or variant whose size matches the (optionally) supplied integer length modifier. No
    argument is converted. (This specifier is not supported by the bionic C library.) The behavior is undefined if
    the conversion specification includes any flags, a field width, or a precision.
    Never knew that....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About the Extern specifier...
    By Jony in forum C Programming
    Replies: 2
    Last Post: 08-26-2011, 11:42 AM
  2. Conversion Specifier
    By Nextstopearth in forum C Programming
    Replies: 4
    Last Post: 09-12-2008, 08:31 PM
  3. got confused which specifier to use ?!
    By Masterx in forum C Programming
    Replies: 12
    Last Post: 12-22-2007, 08:27 PM
  4. Format Specifier
    By babu in forum C Programming
    Replies: 2
    Last Post: 06-27-2007, 11:17 PM
  5. Conversion specifier
    By babu in forum C Programming
    Replies: 1
    Last Post: 06-25-2007, 12:09 AM

Tags for this Thread