Running a simple C program using VS code - c

I recently started taking a C course and I am having some difficulties running a simple I/O program:
#include <stdio.h>
int main()
{
int age;
printf("Please enter your age: ");
scanf("%d\n", age);
printf("Your age is: %d", age);
return 0;
}
the problem is that the program does start running, but it never stops and it doesn't allow me to type in the age and doesn't even print out the "Please enter your age: " sentence.
I have a gcc, g++ and a gdb installed and everything works fine if I remove the "scanf".
I would really appreciate some help!
Thank you!

You need to add an & symbol before the variable name while scanning. Check why here.
scanf("%d\n", &age);

Working Code
#include <stdio.h>
int main()
{
int age;
printf("Please enter your age: ");
fflush(stdout); // Prints to screen or whatever your standard out is
scanf("%d", &age);
printf("Your age is: %d\n", age);
fflush(stdout); // Prints to screen or whatever your standard out is
return 0;
}
I run this and it is working as expected. If you are using linux os, please follow below command to compile and run.
gcc age.c
./a.out
Please visit here to know more about scanf function in c.

Related

My command prompt wont run the whole program

This code runs perfectly in my IDE; I even tried it in an online compiler just to be sure, but when I try to open the .exe it will only ask for the integer and automatically close. I tried it with another program from the school where I asked for like 15 numbers but right before a goodbye message it just closes. Any idea how to fix cmd?
#include <stdio.h>
int main()
{
int numberOfWidgets;
printf("Give me a number: ");
scanf("%d", &numberOfWidgets);
printf("You choose the number: %d", numberOfWidgets);
return 0;
}
You need to add two lines to the end of your program:
int main() {
...
getchar();
getchar();
return 0;
}
The first call to getchar will clear the return key you pressed when you entered the number, the second one will stop and wait for a key to be pressed.
That way, your program will not exit until you press a key, and you will be able to read the output.

How I run C progamme with Input in Netbeans

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;
}

My Visual Studio Code won't run my code properly

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.

why is printf not called when expected?

This is my Code [Note: I am using Eclipse for C/C++ on Windows Platform]
#include <stdio.h>
#include<stdlib.h>
int main(void) {
int num;
printf("Enter a number:\n");
scanf("%d",&num);
if(num%2==0)
printf("Number is Even");
else
printf("Number is Odd");
return EXIT_SUCCESS;
}
Here I have to enter an Integer first only then printf is called... I want to call printf first before I enter an Integer...What am I doing wrong here?
for example this is the output that I get
6
Enter a number:
Number is Even
and expected output is
Enter a number:
6
Number is Even
you can call fflush(stdout) after first printf to print the buffered output. But considering in future if you extend the program with more printfs then adding fflush after every printf will be an overhead. So you can add
setbuf(stdout, NULL)
just before all the printfs.
This will make sure no output is buffered and you will see the prints instantaneously.

Simple C program executed in the terminal with prompt alogside the final output

I have written a simple "hello, world" program from here http://www.cprogramming.com/tutorial/c/lesson1.html cprogramming: lesson 1, the output is shown then the terminal prompt shows up in the following one. When executing another program:
#include <stdio.h>
main()
{
int age;
printf("How old are you?");
scanf("%d", &age);
if (age <= 20)
{
printf("You are still young");
}
else if (age >= 20)
printf("You are not that young anymore!");
else if (age >= 30)
printf("Hello young man!\n");
getchar();
return 0;
}
The output is shown in the same line as the terminal prompt in Gnome Terminal 3.6.1 on Ubuntu 13.10. I just don't know if this a code issue or is it related to the terminal only.
Alignment can be done using escape sequences.
In your case u need to use \n for new line
Add a newline character after each of your printf's
example:
int age;
printf("How old are you?\n");
scanf("%d", &age);

Resources