Reputation: 689
I'm trying to code to refresh my memory preparing myself for a course.
int main(){
int x;
for( x = 0;x < 10; x++){
printf("Hello world\n");
}
return 0;
}
But when I tried to run this I get Too few arguments
I compiled the code above using gcc -o repeat file.c
Then to run this I just type repeat
Sorry if this was a stupid question, it has been a while since I took the introduction class.
Upvotes: 1
Views: 2654
Reputation: 263557
UPDATE :
Based on more recent comments, the problem is that the executable is named repeat
, and you're using csh or tcsh, so repeat
is a built-in command.
Type ./repeat
rather than repeat
.
And when asking questions, don't omit details like that; copy-and-paste your source code, any commands you typed, and any messages you received.
The executable is named file
, which is also a command.
To run your own program, type
./file
EDIT :
The above was an educated guess, based on the assumption that:
gcc file.c -o file
or gcc -o file file.c
; andfile
command (man file
for information) would produce that error message if you invoke it without arguments.The question originally said that the compilation command was gcc file.c
; now it says gcc -o filename file.c
. (And the file
command prints a different error message if you run it without arguments).
The correct way to do this is:
gcc file.c -o filename && ./filename
(I'd usually call the executable file
to match the name of the source file, but you can do it either way.)
The gcc
command, if it succeeds, gives you an executable file in your current directory named filename
. The &&
says to execute the second command only if the first one succeeds (no point in trying to run your program if it didn't compile). ./filename
explicitly says to run the filename
executable that's in the current directory (.
); otherwise it will search your $PATH
for it.
If you get an error message Too few arguments
, it's not coming from your program; you won't see that message unless something prints it explicitly. The explanation must be that you're running some other program. Perhaps there's already a command on your system called filename
.
So try doing this:
gcc file.c -o filename && ./filename
and see what happens; it should run your program. If that works, try typing just
filename
and see what that does. If that doesn't run your program, then type
type -a filename
or
which filename
to see what you're actually executing.
And just to avoid situations like this, cultivate the habit of using ./whatever
to execute a program in the current directory.
Upvotes: 0
Reputation: 283793
When you type
filename
at a prompt, your OS searches the path. By default, Linux doesn't include the current directory in the path, so you end up running something like /bin/filename
, which complains because it wants arguments. To find out what file you actually ran, try
which filename
To run the filename
file gcc created in the working directory, use
./filename
Upvotes: 2
Reputation: 27214
Your code compiles fine. Try:
gcc -o helloworld file.c
./helloworld
Upvotes: 1