-2

I'm new to coding and doing the first CS50 course exercise where we are taught to code using C and doing the "hello world" activity. I input the code:

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");

and when I type in the terminal make hello and ./hello it says hello is a directory instead of doing the command.

What should I do?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I take it that you are using UNIX of some kind. Do you know how to use `ls` and `cat`? Can you verify that you put that code into a file? What is the exact name of the file? – Beta Oct 22 '22 at 03:18
  • 2
    the error is obvious, you already have a folder named `hello` in the current directory, just remove it – phuclv Oct 22 '22 at 03:19
  • 2
    ... after checking to make sure there's nothing in there you want to keep. – user3386109 Oct 22 '22 at 03:21
  • 1
    @drescherjm No, `make` has implicit rules and can work without Makefile. – dimich Oct 22 '22 at 03:30
  • @dimich Thanks for the correction. I learned something today..: [https://stackoverflow.com/questions/15745241/using-the-make-command-without-makefiles](https://stackoverflow.com/questions/15745241/using-the-make-command-without-makefiles) – drescherjm Oct 22 '22 at 03:32
  • Please add directory contents (`ls -l` or `ls -alR` if that is not too long) and output of `make hello`. As a guess, you were going to use directory `hello`, and created it, but forgot to change to that directiry, so now you have `hello.c` and `hello` subdirectory in same directory. – hyde Oct 22 '22 at 04:08
  • 1
    So, solution you want is probably, `cd hello` then `mv ../hello.c .` then `make hello` then `./hello`. It is _imporrtant_ to read and try to understand the output of these commands. I have hard time believing `make` didn't already give you an error... – hyde Oct 22 '22 at 04:12

1 Answers1

2

C is a compiled language, so once you compile the code, it will output an executable that unless specified by -o [executable-name], will be called a.out. Your hello world program should look like this:

#include <stdio.h>

int main() {
    printf("hello, world\n");
}

Don't forget the closing bracket at the end. To compile this code, make sure you have GCC installed and run gcc [program-file-name].c, and replace the second argument with the name of the C file. Once the code is compiled, you can see that a file named a.out has appeared in your current directory. You can then run ./a.out to run the program.

I'm assuming your system comes with GCC, but if it doesn't, there are many resources on Google to help, including https://gcc.gnu.org/install/.

tsoa
  • 131
  • 7