0
#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...

3
  • are you sure it doesn't print the sum and then exit the program? Commented Apr 14, 2021 at 9:54
  • By "exit the program" do you mean the whole window closes? Commented Apr 14, 2021 at 9:54
  • end lines \r\n are missing Commented Apr 15, 2021 at 2:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.