Null Blake
Null Blake

Reputation: 1

Program accepting input but does not give output

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

Upvotes: 0

Views: 64

Answers (2)

DarkAtom
DarkAtom

Reputation: 3161

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.

Upvotes: 1

klutt
klutt

Reputation: 31389

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.

Upvotes: 2

Related Questions