11 Replies Latest reply: Jan 18, 2013 4:13 AM by Wesley212
johnfromgilroy Level 1 Level 1 (0 points)

Here's my program, written in vim.

 

#include <stdio.h>

 

 

int main(void)

 

 

{

  printf("Hello world!\n");

  return 0;

}

 

I enter:   gcc hello.c

and I get:

 

hello.c:1:19: error: stdio.h: No such file or directory

hello.c: In function ‘main’:

hello.c:6: warning: incompatible implicit declaration of built-in function ‘printf’

 

Why doesn't this work? and How do I get going to writing programs in C?

 

Thanks,

JfromG


iMac, Mac OS X (10.7.1)
  • 1. Re: gcc not working?
    Jeffrey Jones2 Level 6 Level 6 (8,510 points)

    How did you install gcc? The correct way is in Xcode, Preferences > Downloads > Components > Command Line Tools. If you did it any other way, it is unlikely that gcc is properly installed.

  • 2. Re: gcc not working?
    VikingOSX Level 5 Level 5 (5,490 points)

    Once you have followed Jeffrey's advice, you will need the following:

     

    #include <stdio.h>

    #include <stdlib.h>

     

    int main(int argc, char * argv[])

    {

     

        return 0;

    }

  • 3. Re: gcc not working?
    johnfromgilroy Level 1 Level 1 (0 points)

    Ah, yes. Need stdlib-eration.

  • 4. Re: gcc not working?
    woodmeister50 Level 4 Level 4 (3,745 points)

    VikingOSX wrote:

     

    Once you have followed Jeffrey's advice, you will need the following:

     

    #include <stdio.h>

    #include <stdlib.h>

     

    int main(int argc, char * argv[])

    {

     

        return 0;

    }

    #include <stdlib.h> not needed for printf, but just good

    a good practice to get into adding it to include list.

     

    The stuff within the parenthesis in int main(int argc, char * argv[])

    is also not needed/required unless the function will have command line

    arguments.  (void) is sufficient in this case.

     

    Ran the code just as he had it on my system without issue.

    His issue is an incomplete or bad install of Xcode.

  • 5. Re: gcc not working?
    steve359 Level 6 Level 6 (12,040 points)

    Two thoughts to help you ...

     

    There is an excellent (in my not-so-humble-but-so-what opinion) book for C.  "The C Programming Language" written by Dennis Ritchie and Brian Kernighan (the authors of C), ISBN 0-13-110362-8.  $45 on Amazon, $60 through bookstores.  Excellent refrence for C.

     

    It will also explain why these two declarations of "main" are both valid:

     

    main (int argc, char *argv[])

    main (int argc, char **argv)

  • 6. Re: gcc not working?
    VikingOSX Level 5 Level 5 (5,490 points)

    Suspected Xcode issue. Yes, we all have some coding pecadillo(s) in our closet. Mine are stdlib and formal main (command-line) syntax.

  • 7. Re: gcc not working?
    VikingOSX Level 5 Level 5 (5,490 points)

    The 2012 edition is in its 49th printing. I still have the 1978 original version.

  • 8. Re: gcc not working?
    steve359 Level 6 Level 6 (12,040 points)

    Mine is the 1988 printing ... you win!

  • 9. Re: gcc not working?
    woodmeister50 Level 4 Level 4 (3,745 points)

    I have the latest printing of K&R because my copy of

    the original was literally disintegrating and was becoming

    a mass of loose pages.

  • 10. Re: gcc not working?
    steve359 Level 6 Level 6 (12,040 points)

    It is sad that the newer ones are 50% fatter because the page-stock is heavier.

     

    Or maybe that thin page stock is the reason for yours falling apart.

     

    Mine is very close to losing pages and needing replacement ...

  • 11. Re: gcc not working?
    Wesley212 Level 1 Level 1 (0 points)

    Has this been solved? If not, would you provide an update of what you have tried?