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.
Related
I am incredibly new to the C language: I am trying to run sample C programs from codecademy, such as the hello world command below:
#include <stdio.h>
int main() {
// output a line
printf("Hello World!\n");
}
Bottom line is, every time I try to run any code in my macOS terminal, I always get the following error:
zsh: parse error near `\n'
What can I do to resolve this problem?
c is a language where you need to compile the code you've written. You do that by starting a C compiler and give the file containing your C code as input.
Example:
File: myfirstcprogram.c
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
}
Then at the zsh prompt, invoke the compiler:
cc myfirstcprogram.c -o myfirstcprogram
-o myfirstcprogram is here an argument to the compiler telling it what to call the final program.
cc may be clang or gcc or any other compiler you've got installed if cc isn't already linked to the proper compiler.
When the compilation is done, the executable myfirstcprogram should have been created. You can now run it from your zsh prompt:
./myfirstcprogram
You can run it without recompiling it as many times as you like. Only when you change the source code (myfirstcprogram.c) do you need to compile the program into an executable again.
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.
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.
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
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.