Syntax error near unexpected token `(' no matter what code I run - c

I'm getting this error for all my codes, even the ones that were testing properly before.
Even the most basic codes like this gives me that error. Did I change my emacs setting
somehow? I have no idea what's going on...
./te.c: line 3: syntax error near unexpected token '('
./te.c: line 3: `int main ()'
#include <stdio.h>
int main () {
printf ("Welcome to C!\n"); return 0;
}
Please help!!!

You don't run ./te.c
That is your source code.
You are supposed to run ./a.out which is the executable output of your compiler!
Recommend you read up on Example 1: Compiling a simple program

Related

Trying to run hello world on C and the output throwing this error on Mac on VS : zsh: parse error near `&&'

I'm trying to run a simple C Program - Hello World on VSC on my mac.
It throws up this error everytime on the output screen, please understand that I'm new to coding and well uh that's that.
Here's the screenshot :
The error
#include <stdio.h>
int main()
{
printf("%s", "Hello World");
return 0;
}
Output : zsh: parse error near `&&'
Your source code itself is correct. Assuming that is the only source code in your current project, The problem either lies with your non-portable filename, or with a misconfigured or corrupted compiler. Change your filename to something that describes the contained source code. A filename like #include <stdio.h> doesn't just make debugging difficult, but is not even allowed in some operating systems. While it does appear to be technically supported in your implementation, it could still be causing zsh to behave improperly.

Syntax error while running the c program despite having no problem during compiling

I am trying to run a basic program 'hello world'using this code
#include<stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
There is no error while compiling but when i try to run the code the error is:
./hello.c: line 2: syntax error near unexpected token'('
./hello.c: line 2: 'int main()'
How can i solve this because ive tried everything and i dont think there is a syntax error.
You don't run the .c file from the shell. After you compile the program, you run the executable that was created.
So if you compiled it like:
gcc -o hello hello.c
Then you run it with
./hello
When you use ./hello.c, the shell tries to run your C source code as a shell script. That won't work because they're completely different languages.

(First C Program / Hello World) Keep getting Syntax Error

This is my current code:
#include <stdio.h>
void print_hello() {
printf("Hello n10321234, welcome to BSB211");
}
int main() {
print_hello();
return 0;
}
However I keep getting the following errors when I compile and run the .exe:
./print_hello: line 3: syntax error near unexpected token ('
./print_hello: line 3: `void print_hello(){'
For compiling I used gcc print_hello.c -o print_hello and to run I use ./print_hello
Your code is correct. I compiled and run it successfully.
What happened is you've accidentally copied print_hello.c to print_hello. You're running this file as a script and get a script error. please make sure you compile correctly before running print_hello.
You can try to delete print_hello then run your gcc again.
This is what happens when I try to run the code as a script(exactly the same error)
>./hello.c
./hello.c: line 3: syntax error near unexpected token `('
./hello.c: line 3: `void print_hello() {'
Your code seems to be correct. I ran it on my machine and works fine.
I also ran it on ideone and it works fine.
Try reinstalling/updating your gcc. Maybe that will help.
Alternatively, you can create an Ubuntu Virtual Machine and try it there.

Error in compilation of "Hello World" program

I am trying to run a simple program below on my mac using CodeBlocks IDE:
1. #include <stdio.h>
2. int main()
3. {
4. // printf() displays the string inside quotation
5. printf("Hello, World!");
6. return 0;
7. }
but I get the following error:
error: expected identifier or '('
As the machine is trying to compile, I think gcc compiler is working fine.
Remove the line numbers, that's the problem.
try in different IDE and find out where the problem is whether in Codeblocks IDE configuration or with the compiler itself.

Beginner: syntax error before int main ()

I'm trying to run a Hello World program but am getting the error
./ex1.c: line 3: syntax error near unexpected token `(`
./ex1.c: line 3: `int main (int argc, char *argv[])'
or
./ex1.c: 3: ./ex1.c: Syntax error: "(" unexpected
or
./ex1.c:3: unknown file attribute: i
./ex1.c:4: parse error near `}'
The weird thing is I've run this same program before and had no issues.
Not sure if these issues are related but the problem happened after I installed Valgrind to run exercise 4 in Learn C The Hard Way. I received an error that said permission denied which I fixed using chmod +x. Then all my .c files needed permission which they had not before. I then did chmod -R 0777 for the directory with all of my .c practice files. So the permission problem is fixed but then the error above started. They may be completed unrelated but wanted to include just in case.
You can't run a .c file just by using ./ex1.c; you have to compile it into a runnable program first.
Assuming you have a Linux/OS X machine, use gcc -Wall ex1.c -o ex1 to compile it (or, more simply, make ex1). Then you can ./ex1 to run the program.
After you compile the program by using make "your program name" (like make mario in this case), then just use ./"your program name" (this case ./mario). DO NOT add .c when running the program.

Resources