Thread: Codeblock - wrong output!

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    1

    Codeblock - wrong output!

    Hello,

    in onlinegdb this program works, but in codeblock the output is not correct - any idea?
    Thank you very much!
    The output in codeblock is with weird characters.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char name[32];
        char buffer[64];
        char last[]= "passed the challenge!\n";
        int ch, a;
    
    
        printf("What is your name? ");
    
    
        a=0;
    
    
        while ((ch = getchar()) != '\n')
              {
                  name[a] = ch;
                  a++;
                  if(a==31)
                    break;
    
    
    
    
              }
        strcpy(buffer,name);
        strcat(buffer," ");
        strcat(buffer,last);
    
    
        a=0;
        while(putchar(buffer[a++]))
              ;
    
    
        return(0);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well your input loop fails to add a proper \0 string termination to the string you input.

    At which point, it's a crap-shoot as to what happens next.

    I guess you got unlucky on onlinegdb because it made you believe your faulty program was good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Code:
    while(putchar(buffer[a++]))
        ;
    I don't think this is a good idea. If putchar fails for some reason and returns EOF it will loop forever since EOF is guaranteed to be negative and therefore "true". Also, the '\0' is sent to putchar, and I'm not sure what putchar is allowed to do in that situation. Is it specifically defined to do nothing on input of '\0'?

    Perhaps it could be
    Code:
    while (buffer[a] && putchar(buffer[a++]) != EOF)
        ;
    That's a little overkill for an EOF that's expected to be unlikely, so something like this is more usual, which still mitigates the infinite loop problem on EOF.
    Code:
        while (buffer[a]) putchar(buffer[a++]);
    Last edited by john.c; 02-16-2022 at 07:13 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC CodeBlock?
    By Dmy in forum C++ Programming
    Replies: 0
    Last Post: 05-30-2017, 03:55 AM
  2. clrscr function in CodeBlock
    By shaswat in forum C Programming
    Replies: 2
    Last Post: 05-01-2014, 06:38 AM
  3. code fail to compile in mvs but run in codeblock
    By khoavo123 in forum C Programming
    Replies: 1
    Last Post: 01-19-2012, 02:32 AM
  4. CodeBlock
    By sandyrockzs in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2011, 08:26 AM
  5. compiling on codeblock
    By h_howee in forum C++ Programming
    Replies: 11
    Last Post: 12-24-2005, 02:55 PM

Tags for this Thread