why does printf output include a trailing 'D'

This is a discussion on why does printf output include a trailing 'D' within the C Programming forums, part of the General Programming Boards category; I am a newbie C coder. I am wondering why the following program's printf output show a trailing 'D' after ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    why does printf output include a trailing 'D'

    I am a newbie C coder. I am wondering why the following program's printf output show a trailing 'D' after the decimal integer. Note the trailing D after the 8 in the output snippet below.

    compiler: gcc version 2.95.4 20020320 [FreeBSD]


    here is the output:

    ---- output ----

    % ./count.exe
    123
    123
    8D
    ^
    this is the 'D' I am talking about.

    ---- snip ----


    Code:
    #include <stdio.h>
    
    /* count characters in input; 1st version */
    main()
    {
    long nc;
    
    nc=0;
    while (getchar() != EOF)
    ++nc;
    printf("%ld\n", nc);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because you have a 'd' in the line. So your output is actually "8d", not "8D" as you have in your example. IE: That isn't your actual output, it's your illustration of it, which has a 'D' that really is a 'd' in the above code.


    Quzah
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    Actually the %ld tells printf that hte correcsponding argument is a long integer.

    let me get out a different Program that does not use the %ld specification. it is still strange because the output count displays as '2D'

    here is the output:

    --- output ---

    hello
    there
    2D

    --- output ---


    Code:
    #include <stdio.h>
    
    /* count lines in input */
    main()
    {
      int c, nl;
      
      nl=0;
      while ((c = getchar()) != EOF)
         if (c == '\n')
           ++nl;
      printf("%d\n", nl);
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, don't mind me. I have no idea what I was thinking.

    They say just waking up is like being drunk. This isn't entirely true, because when you're just waking up, you want to be asleep. Where as when you're drunk, you're usually just fine being the way you are.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You are terminating the program by hitting control-D, right? So, the terminal is echo'ing part of that as a "D". It would probably echo ^D if not for the fact that you overwrite the ^ with a digit. You'll see the D disappears too if you get nl to 10 or above (2 digits).

    The D doesn't appear if you tell the same program to read stdin from a file, ie foo < somefile.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    okay CWR - thats it. thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 08:38 AM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. to #include or not to #include
    By krygen in forum C++ Programming
    Replies: 9
    Last Post: 12-25-2004, 12:06 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21