Printf conversion specifiers


 
Thread Tools Search this Thread
Top Forums Programming Printf conversion specifiers
# 8  
Old 01-23-2014
Thanks Don!
So that 1$ as the first argv, here width; 2$ as 2nd argv as 1, 3rd -> 2, 4th->3 and 5th->4. The order of the argv was switched on purpose for the 3rd printf in main() function.
On my system (Ubuntu 13.10), the second style does not print the leading 0s between 1 and 2, that does not matter to me.
Anyway, How can I, or how long will it take to, get all those tricks?!
# 9  
Old 01-24-2014
You're digging deeper into printf than most people bother to... Smilie You're teaching me new stuff too now Smilie
# 10  
Old 01-24-2014
Don Cragun :

Your explanation was awesome, and it's crystal clear as always, actually when I was in college even our lecturers didn't teach/explain us any programming language / any single statement in depth as clear as you guys here teaching us.

I have noticed in many threads in forum especially You, Corona, Scrutinizer and RudiC explained so clearly that even beginner who just knows alphabets would be able to create sentence.

Thanks you so much for your valuable time.

As I always say Unix.com is well organized, and got prompt support compare any forum which one can search in any search engine. Smilie

Regards,
Akshay
These 3 Users Gave Thanks to Akshay Hegde For This Post:
# 11  
Old 01-24-2014
Quote:
Originally Posted by yifangt
Thanks Don!
So that 1$ as the first argv, here width; 2$ as 2nd argv as 1, 3rd -> 2, 4th->3 and 5th->4. The order of the argv was switched on purpose for the 3rd printf in main() function.
On my system (Ubuntu 13.10), the second style does not print the leading 0s between 1 and 2, that does not matter to me.
Anyway, How can I, or how long will it take to, get all those tricks?!
Yes, you have it.

Please, stop calling them tricks! I haven't used any tricks in any of the code I've shown you. (I know some tricks that I could use because I have seen (and written) some of the code that implements various utilities, library routines, and kernel features. But, tricks can disappear or start working differently at any time.) Everything I have used in the examples I've shown you are features documented in the man pages. I know that some implementations take greater care in producing quality man pages than some other implementations, but a good set of man pages (or a copy of the C Standard or the POSIX Standard) is your usually your best reference as long as you can figure out which man page to look for. In the old days, the manuals were available as printed documents and you could read them from cover to cover to get an idea of what was available even if you didn't need it at that time. (Just reading the table of contents gives you more information than you might think.) I reread the (currently 3,906 page) POSIX standard every time it is revised (every 5 to 10 years). There is a bunch of stuff in there I have never needed to use, a bunch of stuff I seldom use, and a bunch of stuff I use every day. After you've read a few dozen man pages, you may get to a point where you notice the symmetry between interfaces and you develop a feel for how things are supposed to work. Ken and Dennis had a great plan when they designed and built their first UNIX System. There has been a lot of unnecessary bloat added to UNIX and Linux systems since then, but the simple, basic principles are still hidden in those manuals.

Enjoy learning. It is a lifelong process!

The stuff you're getting into here isn't stuff that I use regularly. I reread the man pages and played around with code to try to create an example that would help you understand what the words on the man page meant. (And, as you may have noticed, I got something to work on my laptop that didn't use the format specified by the standards. That may have been why it didn't work on Ubuntu. In case you didn't notice, my original posting had the 0 fill flag in the wrong place. My original example had:
Code:
                printf("%2$*1$d%03$*1$d%4$*1$d%5$*1$d\n",
                        width, 1, 2, 3, 4);
                printf("%4$*1$d%05$*1$d%2$*1$d%3$*1$d\n\n",
                        width, 3, 4, 1, 2);

but has been fixed in message #7 in this thread to follow the form specified by the standards:
Code:
                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);

Both happen to work on OS X, but the standards specify that flags are supposed to follow the n$ that specifies which argument is to be processed and before the optional minimum field width, precision, and length modifiers, and the mandatory conversion specifier character. If you used the original form and it didn't work on Ubuntu, you may want to try using the corrected format strings.)
This User 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