#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
printf("enter the 2 numbers: ");
scanf("%d %d",&a,&b);
c=a+b;
printf("the sum is : %d ",c);
return(0);
}
this is a simple program to add 2 numbers.
my program would let me input the value..but it would not print the sum ,nor would it print the next line.
it would run till the scanf() and as i press enter, it would jst exit the program.
can you please tell me whats wrong. I am a beginner programmer...
There are two things you should think of here.
End printouts with a newline character, because stdout is often line buffered. Do printf("the sum is : %d \n",c); instead. Or call fflush(stdout); expliticly after the printout. This will ensure everything gets printed.
Add some input code in the end. Like an extra scanf("%d", &a); This is basically a small hack to prevent the window from closing before you can see the final output. Another alternative is to add sleep(3); to sleep for 3 seconds. A third alternative here is to see if there are some settings that controls the closing of the window in your IDE.
Your program works correctly, but it exits right after printing the output, giving you no time to look at it.
Consider adding some input before return(0);, such as 2 getchar(); calls. You need 2, because the first character read will be the \n that you typed after the numbers.
Related
I'm new to programming in C and I'm trying to build and execute my first programs. My first program was Hello, World printed in the CMD like many of you. It worked great. Now it's onto bigger and better projects though and I'm having a weird issue.
I'm trying to make a basic addition calculator using standard IO operations (scanf and printf) and it won't function properly.
My program asks for 2 numbers to be input by the user and it will then display the output of said calculation. The program executes flawlessly until the scanf expression comes into play. After I input my 2 numbers to be added the CMD prompt just closes without warning and never spits out an answer or the text I have to be displayed afterward.
I tried multiple solutions to fix this problem including copying and pasting the source code directly from the website I was learning from, even their perfect code nets the same outcome..just a crash before an output is displayed. I'm posting today because I'm wondering where my issue is coming from because I'm just not sure why this program won't execute as it's supposed to. Thanks in advance and heres the code I'm working with:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
c = a + b;
printf("Sum of the numbers = %d\n", c);
return 0;
}
Once console application returns from main method, the associated console window closes automatically. I assume you are using Windows OS. In that case add a system("pause"); before your return 0; statement.
For platform independent solution you can just show a prompt to user and wait for a key press before returning from main. As #chux pointed out in comment any character remaining in input buffer (enter from scanf in this case) must be cleared.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
c = a + b;
printf("Sum of the numbers = %d\n", c);
//clear input buffer
int d;
while ((d = getchar()) != '\n' && d != EOF) { }
printf("Press ENTER key to Continue\n");
getchar();
return 0;
}
CMD prompt just closes without warning and never spits out an answer
Seems like you are opening a new terminal on Windows machine. I compiled the code and it works. Your program closes just after printing the answer so you simply cannot see it. Stop it artificially before the end. To prove it add the following line directly before return:
scanf("%c", &a);
This will result in stopping the program and waiting for input. You will need to enter another number which will essentially be ignored but will stop the program so you can see the output. This is not a good target solution though due to you need to enter some characters to go on and exit but it proves the point :)
I am programming in C and i have a problem when i run a program in the cmd terminal. here is the code i use:
#include <stdio.h>
int main() {
int num;
printf("enter a number: ");
scanf("%i\n", &num);
for(int n = 1; n < num + 1; n++){
printf("%i\n", n);
}
return 0;
}
Generally, everything works like it should, exept for one thing.
when I enter a number, nothing happens. there is no output, until I write anything and press Enter, and only then the number appear.
this is a screenshot of what it looks like.
here is enter the number (and press enter) but nothing happens: http://prntscr.com/deum9a
and this is how it looks like after i entered something random nad all the numbers popped up: http://prntscr.com/deumyn
if anyone knows how to fix this, please tell me (:
Remove the \n from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf() will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf() call.
Generally, scanf() is considered bad for input reading. So, considering using fgets() and parsing the input using sscanf().
See: Why does everyone say not to use scanf? What should I use instead?
I am using Code::Blocks for programming and yeah i am a beginner but everytime i write a program it pauses in IDE but does not pause while executing directly.
What is the possible reason ? Can anyone explain me ?
My code goes as follows :
#include <stdio.h>
void main()
{
float length,breadth,Area;
printf("Enter the value of length and breadth \n\n");
scanf("%f %f",&length,&breadth);
printf("You entered length=%f and breadth=%f \n\n",length,breadth);
Area= length * breadth;
printf("The area of the rectangle is %f\n\n",Area);
return 0;
}
You have to tell your program to wait for input at the end otherwise it will execute, do exactly what you wrote in your code and exit. The "good" way would be to execute it from a terminal (cmd if you are on windows)
#include <stdio.h>
int main()
{
float length,breadth,Area;
printf("Enter the value of length and breadth \n\n");
scanf("%f %f",&length,&breadth);
getchar(); // catch the \n from stdin
printf("You entered length=%f and breadth=%f \n\n",length,breadth);
Area= length * breadth;
printf("The area of the rectangle is %f\n\n",Area);
getchar(); // wait for a key
return 0;
}
Why do you need a getchar() after your scanf()?
When you enter your numbers you finish it with a press of enter. Let's see what you are reading: a float whitespaces and another float. The \n is not consumed by scanf(), but left in the input buffer (stdin). The next time you use a function that reads from stdin, the first sign this function sees is a \n (Enter). To remove this \n from the input buffer you have to call getchar() after scanf() which reads 1 character from the input buffer. I'm sure you will encounter this behaviour more often in future.
As a really, REALLY bad practise, you can just use a getch call to stop the excecution (or any function that generates a small pause, getch is not a standard function).
A program is not supposed to pause after execution and this is a feature added by the IDE. If you want execution to pause and wait for some input you should instruct it to do so. For instance if you are on windows you can add a line:
system("pause");
Right before return 0;. This is not advisable, but may help you for debugging in some cases. Also the standard requires that your main function is int, not void. So you better get used to writing int main instead of void main.
You need to use getch(); at the end of program (Before return statement)
Getch holds down the output screen.
I have wrote a set of code that scan in values and use them to test out central limit theorem. However when I run my program after I input all the values using scanf my program is not proceeding to the next lines of code : the problem looks like this:
printf("*** DEMONSTRATION OF CENTRAL LIMIT THEOREM ***");
printf("Enter parameters for the distribution [a b] ==> ");
scanf("%f %f",&a,&b);
printf("Enter distribution to display [1=data, 2=mean] ==> ");
scanf("%d",&option);
printf("Enter number in each group ==> ");
scanf("%d",&group);
printf("Enter number of samples of groups ==> ");
scanf("%f",×);
printf("are we here yet");
after these printf and scanf the program starts to do the calculations. But When I run the program after I compile(successfully). It seems my code is stuck after the scanf("%f",×);
the line "are we here yet" never gets printed, meaning the program did not get past the scanf. I have not done much C programming this seemed really strange to me can someone figure out why the program is not excuting past the line scanf("%f",×); I really appericate it
Input/output at the terminal is line-buffered in C, and output is not going to display until you output a newline character, or you call fflush(stdout), or your program terminates normally and all the buffers are flushed anyway. Change:
printf("are we here yet");
to:
printf("are we here yet\n");
or:
printf("are we here yet");
fflush(stdout);
and you should see your output.
I have a simple program from a C programming book, and it's supposed to ask for two integers and then add them together and show the sum. I'm able to enter the two numbers, but the output doesn't show up until the very end of the program.
#include <stdlib.h>
#include <stdio.h>
/* Addition Program*/
main()
{
int integer1, integer2, sum;
printf("Enter first integer\n");
scanf("%d", &integer1);
printf("Enter second integer\n");
scanf("%d", &integer2);
sum = integer1 + integer2;
printf("Sum is %d\n", sum);
return 0;
}
The output looks like this:
2
6
Enter first integer
Enter second integer
Sum is 8
Any help would be greatly appreciated, thanks!
It is possible that the output is not being flushed automatically. You can add fflush(stdout) after each printf() and see if that helps.
Which environment are you using to build and run this program?
Further to the above, printf will only automatically flush it's buffer if it reaches a newline.
If you are running on windows, a newline is \r\n instead of \n.
Alternatively you can do:
fflush(stdout);
Another alternative is to turn off buffering by calling:
setbuf(stdout, NULL);
EDIT:
Just found this similar(but not the same) question:
Why does printf not flush after the call unless a newline is in the format string?