Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Extra blank lines from printf.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Robbie Hatley  
View profile  
 More options Mar 24 2010, 3:37 pm
Newsgroups: comp.lang.c
From: "Robbie Hatley" <see.my.signat...@for.my.contact.info>
Date: Wed, 24 Mar 2010 12:37:58 -0700
Local: Wed, Mar 24 2010 3:37 pm
Subject: Extra blank lines from printf.

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?

Entire program, for reference:

/********************************************************************\
 * Program name:  FahrCelsTable                                     *
 * Description:   Fahrenheit-to-Celsius Table Generator             *
 * File name:     fahrcelstable.c                                   *
 * Source for:    fahrcelstable.exe                                 *
 * Author:        Robbie Hatley                                     *
 * Date written:  Sat May 01, 2004                                  *
 * Inputs:        Three CLI args: begin, end, increment.            *
 * Outputs:       Sends chart to cout.  Can be redir'ed to a file.  *
 * To make:       No dependencies.                                  *
 * Edit history:                                                    *
 *   Thu Nov 23, 2006: Split FahrCelsTable from FahrCels.           *
\********************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void Help (void);

int main(int Fred, char* Ethel[])
{
   double FahrMin=0, FahrMax=0, FahrInc=0, Fahr=0, Cels=0;

   if ( 2 == Fred && ( 0 == strcmp(Ethel[1], "-h") || 0 == strcmp(Ethel[1], "--help") ) )
   {
      Help();
      return 777;
   }

   if (4 != Fred)
   {
      printf("FahrCelsTable must have 3 args.:\n");
      printf("  Fahrenheit Min\n");
      printf("  Fahrenheit Max\n");
      printf("  Fahrenheit Increment\n");
      return 666;
   }

   FahrMin = atof(Ethel[1]);
   FahrMax = atof(Ethel[2]);
   FahrInc = atof(Ethel[3]);

   if (FahrMin > FahrMax - 0.001)
   {
      printf
      (
         "Error: maximum must be at least 0.001 greater than minimum.\n"
      );
      return 666;
   }

   if (FahrInc < 0.001)
   {
      printf
      (
         "Error: increment must be at least 0.001\n"
      );
      return 666;
   }

   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);
   }

   return 0;

}

void Help (void)
{
   printf
   (
      "FahrCelsTable must have 3 arguments:\n"
      "  Fahrenheit Min\n"
      "  Fahrenheit Max\n"
      "  Fahrenheit Increment\n"
      "Max must be at least 0.001 greater than Min,\n"
      "and Increment must be at least 0.001\n"
      "FahrCelsTable will then print a table of Fahrenheit-to-Celsius conversions\n"
      "for the range and increment you specified."
   );
   return;

}

--
Cheers,
Robbie Hatley
lonewolf at well dot com
www dot well dot com slant tilde lonewolf slant

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bartc  
View profile  
 More options Mar 24 2010, 3:54 pm
Newsgroups: comp.lang.c
From: "bartc" <ba...@freeuk.com>
Date: Wed, 24 Mar 2010 19:54:36 -0000
Local: Wed, Mar 24 2010 3:54 pm
Subject: Re: Extra blank lines from printf.

"Robbie Hatley" <see.my.signat...@for.my.contact.info> wrote in message

news:Z8udnQ3wFv5n9DfWnZ2dnUVZ_oSdnZ2d@giganews.com...

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robbie Hatley  
View profile  
 More options Mar 24 2010, 4:17 pm
Newsgroups: comp.lang.c
From: "Robbie Hatley" <see.my.signat...@for.my.contact.info>
Date: Wed, 24 Mar 2010 13:17:54 -0700
Local: Wed, Mar 24 2010 4:17 pm
Subject: Re: Extra blank lines from printf.

"bartc" wrote:
> 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.)

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

#include <stdio.h>
#include <stdlib.h>
int main (void)
{
   printf("abcd");
   printf("efgh");
   printf("ijkl");

   printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

   printf("abc\ndef\n");

   printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

   puts("abc"); puts("def");

   printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

   return 0;

}

Compiled with same compiler (gcc).

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.

puts() *is* inserting newlines, but then, it's supposed to.

So none of the above explains the extra newlines in my program.

--
Cheers,
Robbie Hatley
lonewolf at well dot com
www dot well dot com slant tilde lonewolf slant


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ImpalerCore  
View profile  
 More options Mar 24 2010, 4:17 pm
Newsgroups: comp.lang.c
From: ImpalerCore <jadil...@gmail.com>
Date: Wed, 24 Mar 2010 13:17:18 -0700 (PDT)
Local: Wed, Mar 24 2010 4:17 pm
Subject: Re: Extra blank lines from printf.
On Mar 24, 3:37 pm, "Robbie Hatley"

Are you sure that this is really the line of code, and not

printf("%10.3f   %10.3f\n\n", Fahr, Cels);

If you have one '\n' newline character, and you have a standard 80
char line output, there must be a typo somewhere.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Oops, wrong program source!" by Robbie Hatley
Robbie Hatley  
View profile  
 More options Mar 24 2010, 4:30 pm
Newsgroups: comp.lang.c
From: "Robbie Hatley" <see.my.signat...@for.my.contact.info>
Date: Wed, 24 Mar 2010 13:30:51 -0700
Local: Wed, Mar 24 2010 4:30 pm
Subject: Oops, wrong program source!

I inadvertantly included the source for my "Fahrenheit-to-Celsius"
program, not the "Celsius-to-Fahrenheit" one.  Now I see why the
two programs behave differently!  Too many files open in NoteTab;
I grabbed the wrong one.  Sorry about the false alarm.

As is obvious from the source, the problem is an extra \n :

"Celsius-to-Fahrenheit":

   printf("    Cels        Fahr   \n");

   for (Cels = CelsMin; Cels <= CelsMax; Cels += CelsInc)
   {
      Fahr = (((Cels*180.0)/100.0)+32);
      printf("%10.3f   %10.3f\n\n", Cels, Fahr);         /* Extra \n */
   }

"Fahrenheit-to-Celsius":

   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);           /* NO extra \n */
   }

DOH!!!

--
Oops!
Robbie Hatley
lonewolf at well dot com
www dot well dot com slant tilde lonewolf slant


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Extra blank lines from printf." by Robbie Hatley
Robbie Hatley  
View profile  
 More options Mar 24 2010, 4:40 pm
Newsgroups: comp.lang.c
From: "Robbie Hatley" <see.my.signat...@for.my.contact.info>
Date: Wed, 24 Mar 2010 13:40:05 -0700
Local: Wed, Mar 24 2010 4:40 pm
Subject: Re: Extra blank lines from printf.

"ImpalerCore" wrote:
> ... must be a typo somewhere ...

Close.  I copy-n-pasted source code for wrong program altogether.
Sorry about the confusion. (See my post "Oops, wrong program source!").

--
Cheers,
Robbie Hatley
lonewolf at well dot com
www dot well dot com slant tilde lonewolf slant


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Mar 24 2010, 4:40 pm
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@ieee-dot-org.invalid>
Date: Wed, 24 Mar 2010 16:40:27 -0400
Local: Wed, Mar 24 2010 4:40 pm
Subject: Re: Extra blank lines from printf.
On 3/24/2010 3:37 PM, Robbie Hatley wrote:

> Perhaps I'm making some newbie, bonehead mistake here, but
> I can't see what it is.[...]

     Here's something I noticed:

> but what it *actually* prints is:

>      Cels        Fahr
>      40.000      104.000

>      40.200      104.360

... contrasted with the source line

>     printf("    Fahr        Cels\n");

Note that the column headings are desrever.  Also, note
that while 40 Celsius is 104 Fahrenheit, 40 Fahrenheit is
4.44 Celsius, not 104: The direction of conversion is
also desrever.  The output is not from some other program,
not from the source code you showed.

--
Eric Sosman
esos...@ieee-dot-org.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bartc  
View profile  
 More options Mar 24 2010, 4:41 pm
Newsgroups: comp.lang.c
From: "bartc" <ba...@freeuk.com>
Date: Wed, 24 Mar 2010 20:41:06 -0000
Local: Wed, Mar 24 2010 4:41 pm
Subject: Re: Extra blank lines from printf.

"Robbie Hatley" <see.my.signat...@for.my.contact.info> wrote in message

news:zt6dnfJU_J_W7jfWnZ2dnUVZ_jadnZ2d@giganews.com...

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Mar 24 2010, 4:42 pm
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@ieee-dot-org.invalid>
Date: Wed, 24 Mar 2010 16:42:36 -0400
Local: Wed, Mar 24 2010 4:42 pm
Subject: Re: Extra blank lines from printf.
On 3/24/2010 4:40 PM, Eric Sosman wrote:

> [...]
> Note that the column headings are desrever. Also, note
> that while 40 Celsius is 104 Fahrenheit, 40 Fahrenheit is
> 4.44 Celsius, not 104: The direction of conversion is
> also desrever. The output is not from some other program,
> not from the source code you showed.

     Too much editing, not enough proofreading: That final
sentence should have been "The output is from some other
program, not from the source code you showed."

--
Eric Sosman
esos...@ieee-dot-org.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »