Windows 10 can compile C programs with gcc but will not run - c

I installed gcc on my Windows 10 PC and can compile C code into an exe from the command prompt eg.
gcc HelloWorld.c -o HelloWorld
however when I try to run using
HelloWorld
the program will run however nothing will be output and it won't do anything
This is the C program that I compile and run that does nothing which doesn't have any errors that I can see
#include <stdio.h>
int main(void) {
printf("Hello from C!");
return 0;
}
Can someone point out whats happening?

Compile your source code by:
gcc HelloWorld.c -o ./HelloWorld.exe
and then run it by:
./HelloWorld.exe
If it still fails, you can run dir to see what's in your current directory.

Related

Can't get output in Notepad++

I am new to learning programming. Ive set up MinGW and have added the plugin.
I tried running this basic code
#include <stdio.h>
int main(void)
{
puts("Hello, world!");
return 0;
}
but it doesnt seem to return anything. This is what the console log shows
cc -o hello.exe hello.c
gcc -o hello.exe hello.c
Process started (PID=14948) >>>
<<< Process finished (PID=14948). (Exit code 0)
What am i doing wrong?
Your code does not return anything because C is a compiled language, you just compiled it, for it to return something you need to run it.
To run it open the folder where you compiled it and 2 clicks on the file hello.exe

How to compile C program

I am learning C and I have a simple hello world program that I am trying to run on Windows 10. Here is the code:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
I have installed GCC compiler and I tried the following in order to run it in the command prompt:
gcc hello.c
a
I also tried:
gcc hello.c
./a.exe
and:
gcc hello.c
./a
and:
gcc hello.c -o hello
./hello
The program does not run displaying hello, world and it gives the following error:
bash: a.exe: command not found
What am I doing wrong and how can I run the program after the compilation?
It appears that your compilation succeeded successfully.
See if there is an a.out or a.exe file present, as you didn't indicate a non-default executable name.
Note that running a alone typically won't do anything, because it is highly unlikely that your executable is on the bash PATH. This means you need to run ./a.out or ./a (depending on base operating system).
Binary executables under windows typically must have .exe extension to be recognized as such.
I am not sure if gcc under windows adds the right extension automaticaly when outputting executables.
I would try:
gcc hello.c -o hello.exe
./hello.exe

Cygwin gcc compiling and running, but doesn't printf anything

I'm just starting with C and installed Cygwin with GCC compiler on Windows. I tried running this Hello World program.
#include <stdio.h>
int main(void)
{
//fflush(stdout);
//setlinebuf(stdout);
//setbuf(stdout, 0);
printf("Hello World!\n");
return 0;
}
The code compiles fine but when I try running it with ./ there is no output. I have tried to fix it using the commented lines (obviously I uncommented before running) but still had no output.
Name your source code file hello.c. In Cygwin bash shell, go to the directory where the source file hello.c is. Run gcc -o hello.exe hello.c. This will produce the executable hello.exe in the same directory. Then run ./hello.
Hope this helps.
this may be due to dll missing
add the cygwin dlls in your path, i.e.
d:\cygwin1.7.9[1]\cygwin\bin\

How can I run a C program on Mac OS X using Terminal?

I am new to C. Here is my "Hello, World!" program.
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
After I try to run it using Terminal it says:
/Users/macbook/Desktop/peng/Untitled1
-bash: /Users/macbook/Desktop/peng/Untitled1: Permission denied
Why?
First save your program as program.c.
Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools. How can you find App Store? Do a "Spotlight Search" by typing ⌘Space and start typing App Store and hit Enter when it guesses correctly.
App Store looks like this:
Xcode looks like this on App Store:
Then you need to install the command-line tools in Terminal. How can you start Terminal? You need to do another "Spotlight Search", which means you type ⌘Space and start typing Terminal and hit Enter when it guesses Terminal.
Now install the command-line tools like this:
xcode-select --install
Then you can compile your code with by simply running gcc as in the next line without having to fire up the big, ugly software development GUI called Xcode:
gcc -Wall -o program program.c
Note: On newer versions of OS X, you would use clang instead of gcc, like this:
clang program.c -o program
Then you can run it with:
./program
Hello, World!
If your program is C++, you'll probably want to use one of these commands:
clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp
First make sure you correct your program:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n"); //printf instead of pintf
return 0;
}
Save the file as HelloWorld.c and type in the terminal:
gcc -o HelloWorld HelloWorld.c
Afterwards, just run the executable like this:
./HelloWorld
You should be seeing Hello, World!
A "C-program" is not supposed to be run. It is meant to be compiled into an "executable" program which then can be run from your terminal. You need a compiler for that.
Oh, and the answer to your last question ("Why?") is that the file you are trying to execute doesn't have the executable rights set (which a compiler usually does automatically with the binary, which let's infer that you were trying to run the source code as a script, hence the hint at compiling.)
This is Working in 2019
By default, you can compile your name.c using the terminal:
cc name.c
And if you need to run, just write
./name.out
To do this:
Open the terminal
Type in the terminal: nano ; which is a text editor available for the terminal. When you do this, something like this would appear.
Here you can type in your C program
Type in Ctrl + X → which means to exit.
save the file by typing in Y to save the file
Type the file name; e.g., helloStack.c (don't forget to add .c)
When this appears, type in gcc helloStack.c
And then ./a.out: this should give you your result!
For compiling a C program on your latest macOS, just type the following in the terminal after saving the file with a .c extension and on reaching the path where the file is saved:
cc yourfilename.c
Once you have checked all the errors after compilation (if any), type the following for executing the code:
./a.out
These commands are tested on macOS v10.14 (Mojave) and are working perfectly fine.
To compile a C program in macOS, simply follow the below steps
Using the cd command in terminal, go to your C program location and then type the command present below:
make filename
then type
./filename
The answer is chmod 755 hello - it makes the file executable... I had same problem on macOS, which is now solved.
nano hello.c
make hello
chmod 755 hello
Then you run it by ./hello
clang --version
Output:
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Nothing was installed. nano make (clang) chmod - all inside macOS already.
On Mac, GCC (executable gcc) is installed by default in /usr/local/bin.
To run C:
gcc -o tutor tutor.c
First you need to install a GCC compiler for Mac (google it and install it from the Internet)
Remember the path where you are storing the C file
Go to Terminal and set the path
E.g., if you have saved in a new folder ProgramC in the Document folder.
Then type this in Terminal:
cd Document
cd ProgramC
Now you can see that you are in folder where you have saved your C program (let you saved your program as Hello.c)
Now compile your program
make Hello
./hello

Unable to run c Program through cmd2

I have a C file names Test1.c on my desktop, and the code is as follows:
#include <stdio.h>
main ()
{
printf ("I'm a C program\n");
}
Although when I follow what this guide tells me to do in cmd I still can't
run it:
host% gcc test1.c -o test1
host% ./test1
I'm a C program
host%
There are two reasons why it's not working-
The pathname of the directory where this c code is, is not defined in environment variable path.
To compile the code, user must have installed the 'gcc' compiler on machine. If it's not installed, you can't compile the c code above.
Thanks.
First make sure you set path of gcc in Environment Variable and then change directory to where your c program is using cd command .
After this when you are done-
To compile -
gcc Test1.c
after successful compilation a.exe will be created .
Run that a.exe in cmd using -
a.exe
And you program will be executed.

Resources