2

I was trying to make this simple Hello World program, however I receive the following errors. Someone can explain me why?

Program:

#include <cs50.h>
#include <stdio.h>

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

Errors:

    ~/workspace/ $ make hello
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wno-deprecated-declarations -Wshadow    hello.c  -lcs50 -lm -o hello
hello.c:1:9: error: expected ';' after top level
      declarator
int main
        ^
        ;
1 error generated.
make: *** [hello] Error 1
~/workspace/ $ ls
hello.c  pset1/
~/workspace/ $ make hello
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wno-deprecated-declarations -Wshadow    hello.c  -lcs50 -lm -o hello
hello.c:1:9: error: expected ';' after top level
      declarator
int main
        ^
~/workspace/ $ cd
~/workspace/ $ make hello
make: *** No rule to make target `hello'.  Stop.
~/workspace/ $ make hello
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wno-deprecated-declarations -Wshadow    hello.c  -lcs50 -lm -o hello
hello.c:1:9: error: expected ';' after top level
      declarator
int main
        ^
        ;
1 error generated.
make: *** [hello] Error 1
~/workspace/ $ clea
bash: clea: command not found
~/workspace/ $ clear
~/workspace/ $ make hello
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wno-deprecated-declarations -Wshadow    hello.c  -lcs50 -lm -o hello
hello.c:1:9: error: expected ';' after top level declarator
int main
        ^
        ;
1 error generated.
make: *** [hello] Error 1

Thanks in advance.

UPDATE:

Hello,first of all thanks for your fast answers.

I have cheked to be in the rigth directory, and also that there is no copy of the file in other in different directories, but I still recive the same error.

Now I have deleted the file and created a new one also named hello.c inside the pset1 directory.

~/workspace/pset1/ $ ls
hello.c  hello.txt

Again , I've tried to "make" the file but I still recive the same error, in fact, doing several tests, I have seen to recive this same error in every program that I do,independently of the code, as, I deduce that the problem is not the the sintax of the code.

~/workspace/pset1/ $ make hello
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wno-deprecated-declarations -Wshadow    hello.c  -lcs50 -lm -o hello
hello.c:1:9: error: expected ';' after top level declarator
int main
        ^
        ;
1 error generated.
make: *** [hello] Error 1
~/workspace/pset1/ $ 

On the other hand i've seen that in the CS50 videos, inside the terminal window, instead of having ~/workspace they have the name of the user infront of the ~/, is this a problem, or just an update of the CS50 IDE. By the way,can be a problem to use the cloud based CS50 IDE, instead of the CS50 Appliance.

If you have any idea of where is the problem, I would really aperciate it, as now I'm completly stuck in this low level.

UPDATE2:

This is the result of the commands that you suggested:

~/workspace/ $ cd ~
~/ $ ls
bin/  lib/  sessions/  workspace/
~/ $ cat hello.c
cat: hello.c: No such file or directory
~/ $ cd workspace
~/workspace/ $ ls
pset1/
~/workspace/ $ cat hello.c
cat: hello.c: No such file or directory
~/workspace/ $ ls pset1
hello.c  hello.txt
~/workspace/ $ find . -name hello.c
./pset1/hello.c
./.c9/metadata/workspace/hello.c
./.c9/metadata/workspace/pset1/hello.c

And this is the result of: for i in $(find ~ -name hello.c ! -path "*/.c9/*"); do echo $i:; cat $i; echo; done

~/workspace/ $ for i in $(find ~ -name hello.c ! -path "*/.c9/*"); do echo $i:; cat $i; echo; done
/home/ubuntu/workspace/pset1/hello.c:
#include<stdio.h>

main()
{
    printf("Hello World");

}
~/workspace/ $ 

And finally this is the code "hello world" that I'm working with:

#include<stdio.h>

int main(void)
{
    printf("Hello World");

}

@NullityNull @Matt Obert

1
  • I'm glad that you have checked that you are in the correct directory and that there is only one copy of the hello.c file -- at least, to your own satisfaction. However, what you're typing doesn't help us to see that. I recommend sharing the output of exactly the commands I pasted in my answer below. The cat hello.c command will allow us to see that the code in that file actually matches what you pasted above. The find . -name hello.c command will search for copies, in such a way that we can tell by the output whether any others are found, without needing to take your word for it. Commented Aug 10, 2016 at 0:52

2 Answers 2

2

First of all, welcome to CS50! :)

To add to NullityNull's good advice, it looks to me like you probably have more than one copy of hello.c, probably in different directories. The one you are trying to compile is ~/workspace/hello.c. Do you have another copy of this file in the ~/workspace/pset1 directory? You can help us to see this by sharing the output of the following commands:

cd ~
ls
cat hello.c
ls pset1
find . -name hello.c

More generally, since you are just starting out, you might want to make sure that you take some time setting up the directory structure for each module and pset, using mkdir to create directories, cd to change your working directory, and ls to list contents of directories, as recommended in the class materials. That should give you some practice in saving files where you can find them later!

EDIT:

This answer from the archives might help you out.

It looks like the compiler error you are seeing would be caused by attempting to compile a hello.c file which starts like this:

int main
#include <stdio.h>
#include <cs50.h>

I can tell that this is the case because your compiler error message says:

hello.c:1:9: error: expected ';' after top level
      declarator
int main
        ^
        ;

The hello.c:1:9 at the beginning means that you will find the cause of the compiler error in the file hello.c on line 1, character 9.

That means that it is not possible that the code which you pasted at the top of your question actually matches the code in the file you are trying to compile, despite what you tell us above.

Therefore, we will need to see the code you are actually trying to compile if we are going to be able to help you. You can help us to see this by sharing the output of the following command:

for i in $(find ~ -name hello.c ! -path "*/.c9/*"); do echo $i:; cat $i; echo; done

Please execute this exact command (no typos allowed!) and paste the results into your question above so that we can look at them.

(Never fear! Here's what the cryptic command does: It will search your entire home directory for files named hello.c, excluding hidden .c9 directories which are used by Cloud 9 IDE for configuration stuff. If it finds any hello.c files, it will print their location to the Terminal using echo, then print their contents using cat, then echo a blank line.)

EDIT 2:

Thanks for running the long command. Its output clearly shows that there are no other copies of the hello.c file hiding in your workspace -- and if there were, you have deleted them! Let's look at the evidence together:

~/workspace/ $ for i in $(find ~ -name hello.c ! -path "*/.c9/*"); do echo $i:; cat $i; echo; done
/home/ubuntu/workspace/pset1/hello.c:
#include<stdio.h>

main()
{
    printf("Hello World");

}

The good news is that /home/ubuntu/workspace/pset1/hello.c is right where you expect it to be.

The bad news is that, as I suspected, the contents of that file do not actually match the code you claim to be compiling.

Can you spot the differences?

Hint: edit the line that says main() and make certain that it says int main(void) instead. Then try to compile that file, and let me know what happens.

Hint2: If you want to print a newline character after Hello World, try using printf("Hello World\n"); instead of printf("Hello World"); and let me know what happens.

My hunch is that you are looking at some code in the c9 editor which you have not yet saved to the file.

After you get the code looking the way you want it to, make sure to look at its tab in the c9 IDE to see whether there is an asterisk (*) next to the file name. If there is, then you have not saved your changes to the disk, and the program you see in the editor exists only in memory!

Make certain that you always hit Ctrl-S to save your work in progress before trying to compile your code with make or clang!

BY THE WAY, if this helps to answer your question, please accept the answer by clicking on the green check mark. Otherwise, this question will forever be marked as unanswered, and will continually haunt the forums with all the other zombie unanswered questions.

1
  • 1
    Yes! Finally it's working,as you said the problem was that the file was not saved. For sure I have learned the lesson. Thankyou for all.
    – Simon
    Commented Aug 10, 2016 at 22:50
2

First, make sure you are in the same directory that the program is in and you can check if so by typing 'ls' into the console. Another thing to do is make sure you named the file 'hello.c'. that little file extension is crucial in determining whether the IDE should use the C compiler on it. If you checked yes to any of those, then I don't know what to tell you. There is little room for error in such a simple program like this. Your code isn't to blame, it looks like you're in the same directory, and you've named the file correctly. From here everything looks perfectly fine since I can't perceive more than what you can give me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .