Thread: introducing printf causes wrong behaviour of a previously working program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    44

    Question introducing printf causes wrong behaviour of a previously working program

    Here is the base code (the program displays the longest line inputted by user):
    Code:
    #include <stdio.h>
    #define MAXLINE 100 
    int getlne(char line[], int maxline);
    void copy(char to[], char from[]);
    
    int main()
    {
        int len;
        int max;
        char line[MAXLINE];
        char longest[MAXLINE];
        
        max = 0;
        while ((len = getlne(line, MAXLINE)) > 0)
        
            if (len > max) {
                
                max = len;
                copy(longest, line);
            }
        if (max > 0)
            
            printf("the longest is %s", longest);
            
        return 0;
    }
    int getlne(char s[], int lim)
    {
        int c, i;
        for (i = 0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
            s[i] = c;
        if (c == '\n') {
            s[i] = c;
            ++i;
        }
        s[i] = '\0';
        return i;
    }
    void copy(char to[], char from[])
    {
        int i;
        i = 0;
        while ((to[i] = from[i]) != '\0')
            ++i;
    }
    However, when I try to add such a printf
    Code:
    printf("length %d", len);
    here
    Code:
    max = 0;
        while ((len = getlne(line, MAXLINE)) > 0)
        printf("length %d", len);
            if (len > max) {
                
                max = len;
    The printf showing longest string stops working.
    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    This most likely is due to the fact that you aren't ever using newline characters ('\n') at the end of your printf calls, and stdout won't always display new data until the buffer is flushed. You can flush the buffer with fflush, but usually a newline is used for convenience and clarity of output.

    Also, you should use Valgrind or a similar tool to make sure you aren't accidentally overwriting anything. If you are, seemingly random bugs can occur that can be incredibly difficult to find.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The code you show with the printf("length...") statement is missing braces.
    That's what's causing your problem.
    Code:
        max = 0;
        while ((len = getlne(line, MAXLINE)) > 0)
        {
            printf("length %d", len);
            if (len > max) {
                max = len;
                copy(longest, line);
            }
        }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Champaign, Illinois, United States
    Posts
    27
    Quote Originally Posted by oogabooga View Post
    The code you show with the printf("length...") statement is missing braces.
    That's what's causing your problem.
    Code:
        max = 0;
        while ((len = getlne(line, MAXLINE)) > 0)
        {
            printf("length %d", len);
            if (len > max) {
                max = len;
                copy(longest, line);
            }
        }
    To avoid this problem I ALWAYS use braces. Even if the loop or if statement only applies to one line. It makes it more clear to the next person who is going to look at my code what I mean, and then if I go back to do some debugging like that then I don't have to debug why my debug printf statements just completely broke my program. Either way is acceptable though, I just am putting in my suggestion, take what you will.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf behaviour
    By raju.b41 in forum C Programming
    Replies: 19
    Last Post: 07-17-2011, 11:03 AM
  2. strange behaviour of printf after stdout redirection
    By printfede in forum C Programming
    Replies: 14
    Last Post: 03-02-2011, 07:25 AM
  3. Strange behaviour of printf
    By pshirishreddy in forum C Programming
    Replies: 5
    Last Post: 08-29-2009, 11:46 PM
  4. problem with previously working internet code
    By abachler in forum Windows Programming
    Replies: 11
    Last Post: 04-10-2009, 04:11 AM
  5. Replies: 0
    Last Post: 07-26-2007, 09:55 AM