Error in compilation of "Hello World" program - c

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.

Related

Why does the terminal keep returning a zsh parse error no matter my input?

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.

How to create a C console application in Visual Studio Code

I haven't found any extension, or tutorial to make and execute a C console application in the VSCode Terminal.
A simple program like
int main()
{
printf("Hello World!");
return 0;
}
And have the output in the VSCode Terminal.
Does someone know how to realize this? And/or are there solutions?
Thanks in advance
Regards
There actually is an extension for c/c++ on VSCode:
When you click the arrow in the top right (to run the file) you will be asked which compiler you want to use. In our case we can use the gcc compiler:
Then you can paste your code into a .c file and run it with the compiler. It should automatically also execute the binary and print your output into the debug-console:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Hello World!
You even have a debugger, if you set certain breakpoints!
Extra:
Make sure that you have the correct OS set in the bottom right (in the status bar), so your c code compiles for your machine specifically.

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.

Resources