Working on a simple C program using basic I/O. printf and scanf to get started.
Using Visual Studio 2019 and Eclipse with MinGW.
The program has several printf statements followed by scanf_s to get an input. I was expecting that printf statements would be executed first and then scanf_s would read the keyboard input.
How in Eclipse that's not what happens. Seems that scanf_s is executed first and then printf statements.
#include <stdio.h>
int main(void)
{
int s;
printf("** Service Ticket Center **\n");
printf(" Choose from the following options.\n");
printf(" 1. Create new ticket.\n");
printf(" 2. Update ticket status.\n");
printf(" 3. Delete ticket.\n");
printf(" 4. Find ticket.");
scanf_s("%d", &s);
return 0;
Ran same code in Visual Studio and printf statements were executed first followed by scanf_s as expected. It seems like Eclipse is the issue.
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 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.
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.
#include <stdio.h>
int main()
{
int answer;
printf("Please insert your desired budget :"); //normal printf functions.
printf(" $_____\b\b\b\b"); //This should move the curser back 4 spaces.
//The program outputs the line followed with 4 inverted question marks.
scanf("%d", &answer);
printf("So your budget is %d", answer);
return 0;
}
How come the output is 4 inverted question marks? I am using xcode on mac, could that the problem?
You need to run it in a terminal environment that supports \b escape sequences. The console in Xcode must not understand them.
If you run it in the Terminal app, it should be fine.
There is an example in the book [C Primer Plus] 6th Page.91
list3.10 escape.c
/* escape.c -- uses escape characters */
#include <stdio.h>
int main(void)
{
float salary;
printf("\aEnter your desired monthly salary:");/* 1 */
printf(" $_______\b\b\b\b\b\b\b"); /* 2 */
scanf("%f", &salary);
printf("\n\t$%.2f a month is $%.2f a year.", salary,salary * 12.0); /* 3 */
return 0;
}
The author says :
The actual behavior could be different. For instance, XCode 4.6 displays the \a, \b, and \r characters as upside down question marks!
They shows Xcode didn't support the \b
and the Apple Apple Support Communitiesalso shows the situation.
In my Xcode 5.0,it has the same situation.