I have installed the MinGW and added the binary folder to the path in system variables. When I just print "Hello world", It prints normally on the screen. But when I try to get user input using scanf() (example given below), it compiles, but when I try to run it, it says "access is denied". Can someone please help me with this?
#include<stdio.h>
int main(void){
int num = 0;
printf("enter a number: ");
scanf("%d", &num);
printf("%d", num * 2);
return 0;
}
Thanks for the support. I finally figured it out. It was the antivirus software blocking the execution of the code. When I uninstalled it, the code ran perfectly.
Related
I'm testing c program on Visual Studio Code in Windows. I have installed the extensions in the Visual Studio Code: C/C++ from Microsoft; & Code Runner.
I have written a c program that will take two numbers as input from the user and print the sum as output.
C program code:
# include<stdio.h>
void main(){
float n1, n2;
printf("Enter 1st number: ");
scanf("%f",&n1);
printf("Enter 2nd number: ");
scanf("%f",&n2);
printf("Sum of %f + %f = %0.2f",n1,n2,n1+n2);
return;
}
When I'm trying to execute this code on Visual Studio Code, I'm not getting any output(not even the statement "Enter 1st number") or error message. But, when I'm trying to run the code directly using the command prompt, the code is running successfully.
Can anyone please help me to understand, what can be the possible issue that the code is not running on Visual Studio Code, and how can I fix that issue?
I try This code
#include <stdio.h>
int main() {
int testInteger;
printf("Enter an integer: \n");
scanf("%d", &testInteger);
return 0;
}
But in Netbeans, I can't Run this code because It only blank output
Like this
I have same err in VS code but I fix it with run in terminal
Can you help me
This problem is about Netbean's internal terminal/console section. The internal console is not able to run scanf function. So use an external terminal for your project. To do this:
first right click on your project, and select properties.
In that window select "Run" tab at the bottom.
in there, there is "Console Type", change this console type from "internal terminal" to "external terminal".
That is all.
try this code
Add a space in scanf
#include <stdio.h>
int main() {
int testInteger;
printf("Enter an integer: \n");
scanf(" %d", &testInteger);
return 0;
}
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.
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.
In the following simple program after the user inputs an integer the command line remains a flashing prompt. When I exit the program the answer is then printed out. Why is this and how can I fix it?
//powers of 2
#include <stdio.h>
int main(void){
int a,b=1,i;
printf("What power of 2?\n");
scanf("%i\n",&a);
for (i=0; i<a;i++)
{
b=b*2;
}
printf("the answer is: %i\n",b);
return 0;
}
Try remove the \n in your scan :
scanf("%i",&a);
Remove \n from scanf. After that I compiled your program and it worked properly.