Cannot run C program from command prompt on windows 10 - c

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

Related

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

My helloworld for C is compiling but the .exe file isn't running?

I coded this simple C Hello World program, but i don't know why it's not working in command line? I'm using MinGW C compiler which I downloaded per site instructions and I'm using Sublimetext for my text editor. I compiled the program fine it seems b/c the .exe file shows up, but when I try to run that file it prints
the problem seems pretty basic, my bad if it the question is amateur, just started trying to learn C.
Here's my code from Sublime text for it.
#include <stdio.h>
int main (){
printf("Hello World\n");
return 0;
}
You need to specify the directory of the executable within PowerShell. Your alternative is to launch the program through cmd.exe.
.\helloworld.exe
or
C:\Users\Zanel\OneDrive\documents\code\C\helloworld.exe
. represents current directory.

A program in C language made by CodeBlocks IDE runs with a double-click in Windows but not in Ubuntu. why?

I am learning C. I am using Ubuntu as main OS but I also have Windows 7 for testing in another partition. I have made a program in C in both OSes using Code Blocks. When I double click the compiled file of program in windows it runs, but when I do the same in Ubuntu it does not run. I have also created .desktop file for it, but even then it doesn't run. But using the command.someone told me code GUI in.so how can i code GUI in it? also why it runs on windows?
./addition
makes it run in terminal. But I want to run it using GUI. I am clicking on its icon but its not opening.Do i need to code GUI for it?
source code is
#include <stdio.h>
int main(){
int a,s,d;
printf("type the values u want to add and give tab between them\n");
scanf("%d %d",&a,&s);
d=a+s;
printf("addition is %d",d);
system("read -p 'Press Enter to EXIT...' var");
return 0;
}
in linux if you run a shell program from the desktop, it does not have a std input or output attached. For this reason you cannot see anything about it.
A quick solution would be to open a terminal and run it from there.
Two easy options if you don't know how to run the program from terminal:
A) Drag the icon of the program into the terminal, it will automatically build the full path to run it
B) Move to the program's home folder and run it from there "./programName"
I hope this helps mate.

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

Linux GCC Compiler Output in Mint: What to do with the file?

I wrote a simple c program. saved it with "c" suffix. Went to the terminal (in Linux Mint), typed in
gcc -o outputfile inputfile.c
In the directory containing inputfile.c a file called outputfile appeared, as I expected. I want to run the program, but clicking on the file does nothing.
What am I doing wrong?
The program is running, but you're not seeing its output because the terminal isn't open. Open up your terminal and run it with ./outputfile
If your program opened a window, you would see it by clicking on the file.

Resources