Thread: Hello world error. (compiler/ide problem)

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    7

    Hello world error. (compiler/ide problem)

    Hehe, I'm getting an error on the main saying it must return "int".

    Code:
    #include <stdio.h>
    
    void main() 
    {
        printf("Hello world!\n");
    }
    I copied the code straight from a book, so I'm guessing it has something to do with my compiler/IDE. I'm using Dev-C++. Anyone have an idea?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    change your code to:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("Hello world\n");
        return 0;;
    }
    Void main is wrong, read the FAQ.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    Wouldn't have thought it would be in the FAQ. Don't see how a book can be published when the hello world program isn't even right.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Hooligan, don't worry, most books uses void main(void), luckly compilers are getting smarter and starting to tell people that the correct is int main(), so from now, just use int main()

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    Thanks for the help

    Well one more question while im still the biggest newbie on the boards. Is there a reason why the command prompt closes almost right after the program runs?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Yes hooligan, this happens a lot, you have some options, at the main( ) function, before the 'return 0' statement, use one of the following statements:
    Code:
    getchar();
    system("pause");
    You have a lot more, some of them are platform specific. One example before I leave you
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("hello world\n");
        printf("Press enter to continue...");
        getchar(); //this should 'pause' 
       
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    Seems pretty uneffeciant. I skipped a few pages through my book and tried this program.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char me[20];
        
        printf("What is your name?");
        scanf("%s",&me);
        printf("Darn glad to meet you, %s!\n",me);
        getchar();
        getchar();
        
        return 0;
    }
    Had to put two getchar() methods in order for it to not close. I'm guessing I would have ended up needing to put getchar() for each input I had.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    >>scanf("%s",&me);
    That's not pretty efficient too If you still want to use this, remove the '&' operator, you dont need it here. Also, for getting strings (%s) use the fgets() function, but this is a little advanced for you right now. Anyway, if getchar(); doesn't work, system("pause"); always work, at least for me

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    Get a compile error - " `system' undeclared (first use this function)"

    Got that in the past programs too.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("%15s","right\n");
        printf("%-15s","left\n");
        system("pause");
    }
    Thanks for all your help though, really helped me get started

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Of course don't forget to include the 'stdlib.h' header file
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        printf("%15s","right\n");
        printf("%-15s","left\n");
        system("pause");
    }

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    yes, pause is platform specific, but for me as I told it always work, it works with linux, dos and windows.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    yes I tried before:
    Code:
    [serruya@programming Desktop]$ uname -a
    Linux programming 2.4.18-3 #1 Thu Apr 18 07:37:53 EDT 2002 i686 unknown
    [serruya@programming Desktop]$ cat file.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
            printf("Hello world\n");
            system("pause");
            return 0;
    }[serruya@programming Desktop]$ ./file
    Hello world
    Press any key to continue...Q
    [serruya@programming Desktop]$

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    I'm using College Linux, ohh you really tried in Debian and it didn't work? damn, no more system("pause")'s.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. Hello World Problem
    By khotwadi in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2005, 01:04 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM