Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Extra blank lines from printf.

Reply
Thread Tools

Re: Extra blank lines from printf.

 
 
bartc
Guest
Posts: n/a
 
      03-24-2010

"Robbie Hatley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
>
> Perhaps I'm making some newbie, bonehead mistake here, but
> I can't see what it is. I wrote a simple program which
> writes a table of celsius-to-fahrenheit conversions.
> I've included the entire program at the bottom of this post
> for reference, but the guts of the program are the following
> lines:
>
> printf(" Fahr Cels\n");
>
> for (Fahr = FahrMin; Fahr <= FahrMax; Fahr += FahrInc)
> {
> Cels = (((Fahr-32)*100.0)/180.0);
> printf("%10.3f %10.3f\n", Fahr, Cels);
> }
>
> When I run "CelsFahr 40 41 0.2, I expected it to print:
>
> Cels Fahr
> 40.000 104.000
> 40.200 104.360
> 40.400 104.720
> 40.600 105.080
> 40.800 105.440
>
> but what it *actually* prints is:
>
> Cels Fahr
> 40.000 104.000
>
> 40.200 104.360
>
> 40.400 104.720
>
> 40.600 105.080
>
> 40.800 105.440
>
> Why the extra blank lines? printf() doesn't automatically
> print a newline, does it? Don't you have to put those in
> manually? So I don't see why I'm getting extra blank lines.
> Am I missing something obvious?


What happens when you leave out the "\n"?

What about just doing printf ("abc\ndef\n"); ?

Or puts("abc"); puts("def"); ?

(I'm assuming the display width is more than about 24 characters.)
--
Bartc

 
Reply With Quote
 
 
 
 
bartc
Guest
Posts: n/a
 
      03-24-2010

"Robbie Hatley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
>
> "bartc" wrote:
>
>> What happens when you leave out the "\n"?
>>
>> What about just doing printf ("abc\ndef\n"); ?
>>
>> Or puts("abc"); puts("def"); ?


> Ok, I'll test those. Here's a simple program:


> printf("abcd");
> printf("efgh");
> printf("ijkl");

....
> OUTPUT:
>
> abcdefghijkl
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> abc
> def
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> abc
> def
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> printf() isn't automatically inserting newlines.
> I didn't think it would, as other programs I've written
> and compiled on same compiler don't put them in.


OK, so printf() works as it should. So now, starting either with a simple,
working printf and adding to it, or the other way, try and see where the
problem creeps in.

These are suggestions (or perhaps put all of them in the loop; after which
one is there an extra newline?):

printf("<>\n");
printf("<%d>\n", (int)Fahr);
printf("<%10d>\n", (int)Fahr);
printf("<%10.3f>\n", Fahr);
printf("<%10.3f %10.3f>\n", Fahr, Cels);

Then that might give a clue as to what's causing the problem (as I can't
recreate it). Maybe a hidden control character in the format string (I
didn't notice one)?

--
Bartc

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get rid of extra, blank lines via html parsing? David Ainley Ruby 3 08-04-2010 07:28 PM
Chrome Inserts Extra Blank Lines in Drop-Down list OccasionalFlyer Javascript 1 04-22-2010 06:14 AM
Re: Extra blank lines from printf. Eric Sosman C Programming 1 03-24-2010 08:42 PM
How to copy text in Outlook without extra blank lines yong321@yahoo.com Computer Support 0 02-01-2007 07:15 AM
extra blank lines appear when rendering <textarea> from xslt Andy Fish XML 1 12-21-2004 10:52 AM



Advertisments