C Compiled Executable Not Running - c

I wanted to run an Hello World program as I am starting in C :
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
I compile it with this command : gcc h4.c -o h4
It create the executable h4 and when I click on it the command prompt pop and get shut down directly its like its running but to quick for me to see before it shut down.

use the command ./h4 in your command prompt to run the program but keep the prompt open.
Clicking on the executable also does "runs" the program, but it closes the window after the execution is finished.
Seems like you are on Windows. In this case, you don't need the prefix ./,just simply h4.exe. Or, if you really want to, you can use .\.

If you are not running it on your command prompt
do this:
#include <stdio.h>
#include <conio.h>
int main(){
printf("Hello world\n");
getch();
return 0;
}
Assuming your on windows, you should just type h4 instead if you want to run it on your command prompt.

Related

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.

I'm starting coding and am stuck on hello world

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/.

C program discrepancy - windows 10 - running program in Sublime Text 3 and cmd

This is my program -
#include <stdio.h>
main()
{
printf("hi");
}
When I run it through Sublime Text, it shows it properly in the command prompt; but when I try to run it again in cmd, it says:
prog1.c:3:1: warning: return type defaults to 'int' [-Wimplicit-int]
main()
^~~~
So I changed the program to -
#include <stdio.h>
int main(void)
{
printf("hi");
}
When I run this, same way it runs correctly the first time, but when I run it again, it displays nothing!
This is the output of the first program.
This is for the second one.
The 'hi' in the first line of cmd is the output of the program, which runs correctly for the first time.
gcc doesn't "run" your program, it compiles it. The editor is configured to compile and run.
You then manually compile, but don't run. You can specify the output name, like:
gcc -o prog1.exe prog1.c
Then run prog1.exe if that succeeded. This is where a tool like make comes in handy to help automate this.

Cannot run C program from command prompt on windows 10

I am using Windows 10 OS. I installed MinGW for compiling C programs. I tried running my program using the gcc command on the Command Prompt. The file compiles and an executable file(.exe) is formed in the same folder as my source file. But when I try running this file, I keep getting the message 'Access is denied'. Also the .exe file vanishes after this. I do not know what is wrong. Please help me out.
P.S Another time I did the same thing mentioned above and the .exe file ran and I was able to see the output on the Command line. And this time the .exe file did not vanish either.
What was the command you put to compile the program? Also to compile and make an executable file, you have to put this command - "gcc -o nameofexecutablefile nameofsourcefile.c", to run this program just type the name of your executable file in cmd. And to stop the program's window from closing, put "system("pause");" right before the "return 0;" in your program, at the end of the program it will display "Press any key to continue..." and when you press any key, the window will close. Also check if you did any mistakes in your program. I'll also give an example -
------------CODE-----------
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
system("pause"); // this will pause the window
return 0;
}
-------OUTPUT--------
Hello World
Press any key to continue...

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