I'm starting coding and am stuck on hello world - c

I'm new to coding and doing the first CS50 course exercise where we are taught to code using C and doing the "hello world" activity. I input the code:
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
and when I type in the terminal make hello and ./hello it says hello is a directory instead of doing the command.
What should I do?

C is a compiled language, so once you compile the code, it will output an executable that unless specified by -o [executable-name], will be called a.out. Your hello world program should look like this:
#include <stdio.h>
int main() {
printf("hello, world\n");
}
Don't forget the closing bracket at the end. To compile this code, make sure you have GCC installed and run gcc [program-file-name].c, and replace the second argument with the name of the C file. Once the code is compiled, you can see that a file named a.out has appeared in your current directory. You can then run ./a.out to run the program.
I'm assuming your system comes with GCC, but if it doesn't, there are many resources on Google to help, including https://gcc.gnu.org/install/.

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.

fatal error: studio.h: no such file or directory

I'm trying to learn how to program in C.
I'm simultaneously learning C, C++, & Java. I have also coded in html and javascript successfully making rich websites.
I'm following video lessons on skillshare. Through VirtualBox I've set up a ubuntu installation, created lesson001.c, and attempted to compile it by entering "gcc lesson001.c"
The program:
#include <studio.h>
int main(){
printf("hello, world!\n");
return 0;
}
The error:
lesson001.c:1:10 fatal error: studio.h: no such file or directory.
The instructor is walking through the coding lesson on a pre-configured linux system, so he does have the same errors. It is frustrating that a comprehensive paid lesson set does not include critical setup parameters.
additional info: "gcc -v" returns about 20 lines of information on gcc 9.3.0, so I believe it is installed correctly.
Thank you
Change the #include <studio.h> declaration to #include <stdio.h>. A header file named studio.h does not exist in the standard library.
stdio stands for "standard input/output," and has nothing to do with "studio"! 😀
It should be stdio instead of studio.
stdio stands for Standard Input Output
Correctly formatted code :
#include <stdio.h>
int main(){
printf("hello, world!\n");
return 0;
}

C shell script for gcc compiler

I would like to make a script for testing my c program but I could not figure out why it does not work
I tested it with a easy code so that I am sure that the problem is not because of the C file.
My C Code is:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
return 0;
}
And my Shell Script is:
gcc -o main main.c
echo "Hello world"
If I execute the script I get the error message on my console
: No such file or directory
gcc: fatal error: no input files
compilation terminated
This is the Error I get if I want to execute the script
https://imgur.com/a/zIl55
In the image you can see my problem
If I compile the C file “per hand” it has no problem but if I execute the script which contains the same statement it does not work.
If I just want to compile and only write the command for compiling in my script it works but as soon as I ad an echo or any other command it will not work.
I am using an Ubuntu Shell under Windows.
Any help would be very appreciated.
My guess: you wrote your script with a Windows-only editor such as Notepad, so it used Windows newlines (\r\n AKA CRLF). bash passes main.c\r as argument to gcc, which cannot find it. Printing out the error, the terminal interpret \r as carriage return character, so, it goes in column 1 and prints the rest of the message, which results in the bizarre thing you are seeing.
You can check if this is the case by running dos2unix over your script, if now it works correctly it's as I suspected.
Solution: use a more serious editor and/or make sure it writes Unix newlines (plain \n, AKA LF).

Nothing working when trying to run C programs?

I have been trying to use Cygwin64 to compile and run C programs. I have been trying to run a simple Hello World program as follows:
#include <stdio.h>
int main()
{
printf("Hello world!");
}
And in Cygwin, I have been typing the following command:
gcc -o hello hello.c
Followed by
./hello.exe
After that, there is simply no output, and I receive a new prompt.
Does anyone have a solution to this issue?
Gcc don't give .exe extention for output executable file. You are giving gcc -o hello hello.c command then it generates executable with name hello (not hello.exe). If you don't give any name for output file, it generates executable file with name a.out
I would guess that the prompt printed after the execution of the program is printed by first returning the cursor to column 0. This is then overwriting the "Hello World!" message you've printed. You should probably add a "\r\n" to your printf call as follows:
#include <stdio.h>
int main()
{
printf("Hello world!\r\n");
}
This will move the cursor to the next line before exiting, so that the prompt is printed on the next line instead.
I guess you just need to pause your program.
Can be done with a get(), scanf()
Also you should do a return 0; at the end of your code

Resources