3

SI am a beginner in programming, and I haven't ever used command line arguments before, so I'm just trying to get the hang of them now. In this, program, it compiles fine, but when I try to run it it gives me the error "Syntax Error near unexpected token('`". I'm not sure what I'm doing wrong. Here's the code...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ( int argc, char *argv[] )
{
    printf("%s\n %s\n", argv[0], argv[1]);
    return 0;
}

All I want this program to do is print the command and the first argument. When I get the error, it says it is on line 5, which is the one that contains "int main...". By the way, I realize I included a lot of libraries but that's because I plan on turning this program into a much bigger one once I fix the bugs.

2
  • Does it compile if you remove the libraries? Commented Mar 5, 2014 at 5:44
  • "By the way, I realize I included a lot of libraries but that's because I plan on turning this program into a much bigger one once i fix the bugs." Good on you though for breaking down your project enough to where you know you have a problem early, while there's still only a little bit of code. This definitely beats grinding out all of the code at once and only finding afterward that there's bugs. Commented Mar 5, 2014 at 6:24

1 Answer 1

5

You have to run the compiled program not the source code. I think you are executing the .c file instead of the created program file.

gcc -o main main.c
./main helloworld
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, try to run that C source file did give a similar error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.