Thread: 2 very noob questions

  1. #16
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    \n is just another character that printf() doesn't care about and doesn't do anything special with. What you may be confusing this with is how file are buffered. Output/Input can be delayed until the file is closed or until a "\n" is printed. stdio works this way, though I'm not sure if stdout does. (You can fflush(stdout); if needed...) Besides, there is no good reason for printf to specially treat or dislike a \n.

    You can always read the manual on printf, which never mentions \n. Nor does anything I can find.

    Code:
     if((string = strchr(firstname, '\n') != NULL)
    Should be obvious, but count your parentheses. You're one short.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #17
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    The 'failing to include '\n' in printf() could lead to undefined behavior' was a topic on comp.lang.c a long long time ago. I remember the professor from St Petersburg State University in Russia actually had given a URL (from the standard) where it stated this. Okay, I need to find the thread.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you sure it isn't something like this?
    Whether the last line requires a terminating new-line character is implementation-defined.
    http://web.archive.org/web/200502070...aft.html#4.9.2
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Dave,

    Yes, that smells like what the good professor was talking about at the time. Okay, since it is starting to become apparent that I make a fool out of myself in front of the smart people, combined with the fact my reading comprehension blows, do you wish to enlighten me (and the rest of us) on the whole '\n' with printf() thingy?

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'd rather not -- I've been a bit distracted lately. How about this?

    Basically I always try to ensure that the last output of a program ends in a newline, and that I fflush(stdout) when I want to see a prompt without a newline (aided by a preferred environment that does not display them otherwise).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually it seems better to not use printf unless you have an actual format string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Few more noob questions
    By SlpCtrl in forum C Programming
    Replies: 12
    Last Post: 10-14-2008, 04:32 PM
  2. A bunch of REALLY uber noob questions
    By mcmasterballer in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 01:43 PM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Noob Questions...
    By Firemagic in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2006, 03:57 PM
  5. just started using CPP, couple of arb NooB questions
    By sempuritoza in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 05:21 AM