I have recently started to learn programming in c language. I installed Mingw and vista studio code for the same. However, when I wrote my first program it just keeps showing error and won't run. I made a program to calculate simple interest. I have checked the syntax multiple times. Please help in pointing out the error.
#include <stdio.h>
int main()
{
int p, n;
float r, si;
printf("Enter principal\n Enter no. of years\n Enter rate\n");
scanf("%d, %d, %f", &p, &n, &r);
si = p * r * n / 100;
printf("%f\n", si);
return 0;
}
Picture of the error
The gcc executable is not on your PATH, and so the system can't find it (and thus not execute it)
Update the PATH environment variable so that the directory where gcc is installed is part of it.
See here for how to do that.
Related
I recently started learning C and have just started using VSC. I was previously using CodeBlocks. While testing out a simple array program, I realised that there is a difference in output. For reference, the array program code is shown below.
#include <stdio.h>
int main() {
int grades[10];
int count = 10;
long sum;
float average;
printf("\nPlease enter the 10 grades: \n");
//printf("%i", count);
for(int i = 0; i < count; i++) {
printf("%2u> ", i+1);
scanf("%d", &grades[i]);
sum += grades[i];
};
average = (float)sum/count;
printf("The average of the 10 grades are %.2f", average);
return 0;
}
I am able to run the for loop but the code after the for loop does not appear in the CMD like terminal and the program just quits after the for loop. However, the code after the for loop does appear in the VSC terminal. I am able to enter the ten values.
The VSC Terminal Output:
However, the CMD looking terminal did not manage to print the code after the for loop and instead exits the program.
Could it be because of this error: "The preLaunchTask 'C/C++:gcc.exe build active file' terminated with exit code -1." everytime I click 'Run > Start Debugging'.
Sorry for this messy format but thank you in advance for helping!
I can't properly run/debug my code in VS Code using the C language. I've installed C/C++ package on VSC, Mingw & applied the path for Mingw. All my files are running .c format as well.
Only the last part of my code keeps crashing in VSC, when I run this same code on website compilers, it works!
Here is my code:
#include <stdio.h>
int main(void) {
int num1;
int num2;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d ", num1 + num2);
return 0;
}
That last printf is where VSC just shuts down the output window, so I never get to see the end result of my code. Anyone have any solutions to fix this? It'd be greatly appreciated!
When you run your console program from Visual Studio, it opens a terminal window, runs the program and the terminal window closes automatically when the program exits. This is a classic problem with the Microsoft Windows platform that they do not seem to care about despite millions of newbie programmers like you experiencing the same problem.
If you open the terminal window yourself, by running the CMD command from the start menu, you will be able to run your program manually after changing the current directory to that of the program binary.
To prevent the terminal window from closing immediately when running directly from Visual Studio, you should add 2 getchar(); statements before returning from main() to wait for user input and get a chance to see the output. Just reading a single byte with getchar() will not suffice because it will just read the pending newline entered by the user in response to the second prompt.
Also note that it is preferable to output a trailing newline to ensure the output is properly flushed on some legacy systems:
printf("Answer: %d\n", num1 + num2);
Here is a modified program you can test:
#include <stdio.h>
int main(void) {
int num1 = 0, num2 = 0;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d\n", num1 + num2);
getchar(); // read the pending newline
getchar(); // read at least another byte from the user.
return 0;
}
run you program from the console:
In the search box type cmd
cd \path_to_your_executable
run your program.
I'm coding using Ubuntu virtual box and when I am trying to execute this following code it shows the segmentation fault (core dumped). Why this is happening? What should I do?
#include <stdio.h>
int main() {
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++) {
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
why this is happening?
There doesn't seem to be anything wrong with the code you show. If this code is indeed crashing, it must be because of something you didn't show.
If could be that your system is completely broken (does /bin/date work? does /bin/sleep 1 work?), or because you are running a different binary from the one you think you are, or ...
what should I do?
Learn how to debug. Build your program with -g flag, and run it under debugger (which will tell you where your program is crashing).
Here is a handy starting point.
I'm new to C. I was learning scanf() function, and the error that occured is that when I run the .exe file, it executes, gives correct output, and closes; but it then again executes on its own , asks for input and gives correct output and closes. Here's my code :
#include <stdio.h>
int main() {
int a;
printf("Enter a num\n");
scanf("%d", &a);
int b = 1601;
if (a == b) {
printf("Found a match!!");
} else {
printf("Match not found!");
}
return 0;
}
The code works well in Ubuntu 14.04. At MS Windows + MinGW there are some problem sometimes. If you want to develop a program with gcc you should use Unix Like Operating System (Linux, BSD, etc) If you want to still use MS Windows, then you should use MS Visual Studio for the best result.
so I've decided to install Eclipse to use for my C programming. I wanted to write a small program, just to test that everything works, however it seems that Eclipse won't let me scan in any C inputs. For any other program, that requires no input, it works fine but it seems for some reason Eclipse won't run any program that requires a use to input. I'm running the programs by going to Run->Run As-> Local C/C++ Applications. I've also tried running these programs through the command line, and they turn out fine. Any ideas?
Code:
#include <stdio.h>
int main(void) {
int length, width, height, volume, weight;
printf("Enter the length of box: ");
scanf("%d", &length);
printf("Enter the height of box: ");
scanf("%d", &height);
printf("Enter the width of box: ");
scanf("%d", &width);
volume = length * width * height;
weight = (volume+165)/166;
printf("Volume(cubic inches) %d\n", volume);
printf("Dimensional weight(pounds): %d\n", weight);
return 0;
}
Installed Packages:
After I try to run these programs, nothing appears in the console window, but after I press stop this is what comes out:
Here's a better pic: http://i.imgur.com/zgV1r.png
Try adding an fflush(stdout) after each of your printf() calls as suggested here.
Here is some more discussion of why fflush is required.