Printf conversion specifiers


 
Thread Tools Search this Thread
Top Forums Programming Printf conversion specifiers
# 1  
Old 01-23-2014
Printf conversion specifiers

Hello, this is one examples that I always panic with C printf format specifier.
1) I did read the manpage with
Code:
 man 3 printf
...... One can also specify explicitly which argument is taken, at each place where an argument  is  required,  by  writing
       "%m$"  instead  of  '%'  and "*m$" instead of '*', where the decimal integer m denotes the position in the argument list of the desired
       argument, indexed starting from 1.  Thus,
           printf("%*d", width, num);
       and
           printf("%2$*1$d", width, num);
       are equivalent.  The second style allows repeated references to the same argument.

1) Can you please explain this example on width and num for me.
2) To have further understanding I used following code from "A book on C by Al Kelley/Ira Pohl, page 503" to test,
Code:
#include <stdio.h>

int main()
{
    int i;
    char c;
    char string[15];
    printf("Please input a line:\n"); 
    
   scanf("%d , %*s %% %c %5s %s", &i, &c, string, &string[5]);
    //input: 45 , ignore_this, % C read_in_this**
    
    printf("%d , %*s %% %c %5s %c", &i, &c, string, &string[5]);
    return 0;
}

but I met two problems. 1) A bunch of warnings as I mentioned in my last threads:
Code:
pg503.c: In function ‘main’:
pg503.c:13:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
pg503.c:13:2: warning: field width specifier ‘*’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat]
pg503.c:13:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 5 has type ‘char *’ [-Wformat]
pg503.c:13:2: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat]
pg503.c:13:2: warning: format ‘%c’ expects a matching ‘int’ argument [-Wformat]

2) when I tried:
Code:
./a.out 
Please input a line:
45 , ignore_this, % C read_in_this**

Note: Spaces after 45, two asterisks were put at the end on purpose, and I was expecting to see:
Code:
45
, 
read_
in_this**

The program went infinite, nothing was printed. Thank you!

Last edited by yifangt; 01-23-2014 at 02:45 PM.. Reason: indentation
# 2  
Old 01-23-2014
You are expecting newline where is \n which is not in your printf statement.
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 01-23-2014
You give printf and scanf identical command strings, but %* means something very different to printf and scanf. It means "print this type to [format] characters wide" in printf, and "don't store this value" in scanf.

Also, you are giving printf all the same arguments too. scanf needs &i because scanf needs i's location, not its contents -- but printf needs its contents, not its location!

So, my best guess at what happened is that printf's %d prints part of a memory address (since you are on a 64-bit system, and %d expects a 32-bit integer). Next, %*s tries to get a width, and gets the other half of that memory address -- a very, very high number! So it prints several billion leading spaces before it actually gets to the string.

Last edited by Corona688; 01-23-2014 at 03:15 PM..
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 01-23-2014
Sorry I put the '\n' in printf(), forgot to change it when I posted it.
Code:
printf("%d\n,\n%*s\n%%\n%c\n%5s\n%c", &i, &c, string, &string[5]);
printf("%ld\n,\n%*s\n%%\n%c\n%5s\n%c", &i, &c, string, &string[5]);

However, the same problem still there.
Thank you both!
# 5  
Old 01-23-2014
I'm not sure what you're trying to do with that printf. Putting the same command string in printf as scanf does not make sense here.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 01-23-2014
How stupid to use the scanf format for printf!------Thanks, Corona!
I got it now:
Code:
printf("%d\n%%\n%c\n%5s\n%c\n%s\n\n", i, c, string, string[5], string+5);

$ ./a.out
Please input a line:
45 , ignore_this, % C read_in_this**

45
%
C
read_in_this**
i
in_this**

Quote:
Also, you are giving printf all the same arguments too. scanf needs &i because scanf needs i's location, not its contents -- but printf needs its contents, not its location!
If print string, use the address of the string array(cf: string+5); if print the char of the string, use the subscription (string[5]) explicitly.

Last edited by yifangt; 01-23-2014 at 04:18 PM.. Reason: quote reply
This User Gave Thanks to yifangt For This Post:
# 7  
Old 01-23-2014
From our previous discussions, I assume that you understand that the call:
Code:
printf("%*d\n", 5, 1);

prints 1 as a right-justified decimal integer in a field 5 characters wide.
This can be restated as printf() "prints the 2nd argument following the format string as a decimal integer in a field whose width is specified by the 1st argument following the format string". Note that the numbers in red match the second call you had to printf:
Code:
printf("%2$*%1d\n", 5, 1);

The n$ forms to reference argument positions are most frequently used when dealing with differences in output based on localization. (For example, in the US, dates are often printed in the form MM/DD/YYYY, but in Europe, dates are frequently printed in the form DD-MM-YYYY.) If you have your format strings in a message catalog based on your current locale settings, a printf() call to print a date in the US or Europe might be:
Code:
printf(format, m, d, y);

where format would be:
Code:
%02d/%02d/%d

to print in US format, and would be:
Code:
%2$02d-%1$02d-%3$d

to print in European format. (But, using message catalogs to control which format string is used is another level of complexity that you don't need to worry about for now.)

The statement "The second style allows repeated references to the same argument." may be better explained with a different example. Suppose that you want to print more that one decimal value with a printf statement and you want a variable to control the field width, but you want the field width to be the same for all of the values printed. Try building the following sample program:
Code:
#include <stdio.h>
void
ruler(int w) {
    printf("Following output produced with width set to %d\n", w);
    printf("         1111111111222222222233333333334444444444555555555566666\n");
    printf("1234567890123456789012345678901234567890123456789012345678901234\n");
    return;
}

int
main() {
        int     width;  // field width

        for(width = 1; width <= 16; width += width) {
                ruler(width);
                printf("%*d%0*d%*d%*d\n",
                        width, 1, width, 2, width, 3, width, 4);
                printf("%2$*1$d%3$0*1$d%4$*1$d%5$*1$d\n",
                        width, 1, 2, 3, 4);
                printf("%4$*1$d%5$0*1$d%2$*1$d%3$*1$d\n\n",
                        width, 3, 4, 1, 2);
        }
        return;
}

and examine the format strings used in the printf() calls in main(). Do you understand why the width parameter has to be supplied four times in the first call, but only supplied once in the other two calls?

The output the above program produces is:
Code:
Following output produced with width set to 1
         1111111111222222222233333333334444444444555555555566666
1234567890123456789012345678901234567890123456789012345678901234
1234
1234
1234

Following output produced with width set to 2
         1111111111222222222233333333334444444444555555555566666
1234567890123456789012345678901234567890123456789012345678901234
 102 3 4
 102 3 4
 102 3 4

Following output produced with width set to 4
         1111111111222222222233333333334444444444555555555566666
1234567890123456789012345678901234567890123456789012345678901234
   10002   3   4
   10002   3   4
   10002   3   4

Following output produced with width set to 8
         1111111111222222222233333333334444444444555555555566666
1234567890123456789012345678901234567890123456789012345678901234
       100000002       3       4
       100000002       3       4
       100000002       3       4

Following output produced with width set to 16
         1111111111222222222233333333334444444444555555555566666
1234567890123456789012345678901234567890123456789012345678901234
               10000000000000002               3               4
               10000000000000002               3               4
               10000000000000002               3               4

Update:
On some systems (including MAC OS/X), flags in the %n$ can come before or after the n$; the standards say the flags should follow the n$. The above examples have been updated to use the standard form.

Last edited by Don Cragun; 01-23-2014 at 07:20 PM.. Reason: Fix incorrect placement of flags...
These 5 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf statement for currency conversion

hi all, I had my script as a=qw b=rter c=fdfd curency=1000 printf"${curency} $a $b $c" > filename can i have printf statement that can change the currency from 1000 to 1,000 like it should convert the number to currency format ..?(i.e for any number) (14 Replies)
Discussion started by: hemanthsaikumar
14 Replies

2. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

3. Shell Programming and Scripting

Printf command

Hi, I a sequance number from 1-999 and i want asing the value like 001,002..999 Exp: file_001 file_002 file_003... file_999 How can i disaplay the sequnace number as mention above. (3 Replies)
Discussion started by: koti_rama
3 Replies

4. Programming

error: expected declaration specifiers or '...' before syslog

When I compile this i get the following error "error: expected declaration specifiers or '...' before syslog" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define __LIBRARY__ #include <linux/unistd.h> /* define the system call, to override the library... (6 Replies)
Discussion started by: dragonpoint
6 Replies

5. Shell Programming and Scripting

Printf problem

I am having a major problem with printf, The more I pad it, the less I see :( The problem is in the first function, report Am I ruining output somewhere? I wont print out the names propely, it cuts them off or deletes them completely :( #!/bin/bash report() { printf "%-10s" STUD# ... (2 Replies)
Discussion started by: L0ckz0r
2 Replies

6. UNIX for Dummies Questions & Answers

The meaning of %s in printf

I have this command like that has %s in it, I know %s calls a column, but I am not sure I understand which column (I mean for my case I can check the input file, but I want to know how is this %s used, how comes tha same symbo; gives different columns in one command line: {printf "grep %s... (22 Replies)
Discussion started by: cosmologist
22 Replies

7. Shell Programming and Scripting

AWK printf help

Target file contains short text (never more than 1 line) and filenames. The format is, e.g.,: TEXT1 filename1 TEXT2 TEXT3 filename3dddd filename3dddd TEXT4 filename4 TEXT5 filename5dddd filename5dddd filename5 where dddd is a random 4-digit whole number. Desired output: (4 Replies)
Discussion started by: uiop44
4 Replies

8. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

9. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

10. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies
Login or Register to Ask a Question