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
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
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.
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
I used to be able to compile C programs, but now I can't:
$ cat helloworld.c
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
$ gcc helloworld.c
helloworld.c:1:19: error: no include path in which to search for stdio.h
Yes, I do have /usr/include/stdio.h . Yes, build-essentials is installed.
This problem began after I modified my ~/.bashrc to run a program installed in my user directory. I know this is what's wrong because if I remove ~/.bashrc, it works.
What environment variable would be shadowing /usr/include as an include path?
The problem was that I had another GCC in my PATH:
$ which gcc
/home/joey/gcc4ti/bin/gcc
When I was trying to compile "Hello World", it was running a compiler for a 68000, not my system compiler :D
I had this in my ~/.bashrc:
export PATH="/home/joey/gcc4ti/bin:$PATH"
Because paths are scanned in order, the gcc in /home/joey/gcc4ti/bin is seen first. I changed it to:
export PATH="$PATH:/home/joey/gcc4ti/bin"