Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 25th, 2008, 1:20 AM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
Re: printf Alignment

Quote:
Originally Posted by WaltP View Post
printf() returns the number of characters output.
Well, yes, but that returned number includes things like newlines, line feeds, etc and doesn't get reset for a newline. A tab is also counted as one character, even if it effectively moves several spaces to the right. This means the return value from printf() is not related in a neat manner to the resultant position in a line - unless the data passed to it is closely controlled (eg one printf() call per line).
grumpy is offline   Reply With Quote
Old Jan 25th, 2008, 10:42 AM   #12
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
Re: printf Alignment

Dunno if this helps:

The %n format specifier tells you how many characters have been output at the point %n is encountered in the formatting process. It also is a good place for hackers to attack code, but if your code is an unprivileged report process, it may be allowable.

Downside: It does not carry over position information from previous printf calls. If you are writing to a file rather than the console with fprintf() maybe, then ftell() is useful if you write all fixed length strings to the output file. Otherwise you will have to use OS dependent calls to find your current cursor position.
jim mcnamara is offline   Reply With Quote
Old Jan 27th, 2008, 7:07 PM   #13
WaltP
Programmer
 
Join Date: Oct 2007
Posts: 39
Rep Power: 0 WaltP is on a distinguished road
Re: printf Alignment

Quote:
Originally Posted by grumpy View Post
Well, yes, but that returned number includes things like newlines, line feeds, etc and doesn't get reset for a newline. A tab is also counted as one character, even if it effectively moves several spaces to the right. This means the return value from printf() is not related in a neat manner to the resultant position in a line - unless the data passed to it is closely controlled (eg one printf() call per line).
Yes, newlines are a character, so is a tab. But if you're trying to line up columns, it's foolish to use tabs at all and just as foolish to use newlines before you actually reach the end of the line. It's up to the programmer to reset the counter after every newline.

It's call "programmer control". The programmer has to be in contol of the code s/he's writing.
__________________
Testing 001 010 011 100....
WaltP is offline   Reply With Quote
Old Jan 27th, 2008, 8:18 PM   #14
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
Re: printf Alignment

Quote:
Originally Posted by WaltP View Post
It's call "programmer control". The programmer has to be in contol of the code s/he's writing.
Thanks for the lecture. If you check my earlier posts in this thread, you will find that is exactly my point.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
printf or cout? d_heyzie C++ 15 Apr 18th, 2006 3:57 AM
Printing escape sequences using printf()? bivhitscar C 5 Nov 3rd, 2005 10:08 PM
printing "%" with printf ivan C 15 Oct 20th, 2005 7:22 AM
How to say the size of an array using printf ? colt C 2 May 19th, 2005 3:25 PM
printf() Planet_EN C++ 1 May 3rd, 2005 4:04 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC