B
bryanvick
I just started learning C, and wrote this small program to play around
with the printf function in stdio.h. At the console, I am able to type
input at the first 2 getchar() calls, but when I call it again at big =
getchar(), I never get to type anything, it just reads ASCII 10 from
the stdin for some reason. Can someone explain why it is reading the
stdin stream at that point even though I am not typing, and how I would
get this to work.
CODE:
#include <stdio.h>
int main(){
int per1;
int per2;
int sumper;
printf("Type in two characters: \n");
per1 = getchar();
per2 = getchar();
sumper = per1 + per2;
printf("ASCII first number = %i\n",per1);
printf("ASCII second number = %i\n",per2);
printf("ASCII sum = %i\n",sumper);
int big;
printf("Type in a large number for scientific notation: \n");
big = getchar();
printf("Scientific Notation: %e\n", big);
printf("big = %i\n", big);
return 0;
}
RESULT:
Type in two characters:
50 /*I can type this in*/
ASCII first number = 53
ASCII second number = 48
ASCII sum = 101
Type in a large number for scientific notation:
Scientific Notation: 1.253353e-308 /*I don't get to type this in*/
big = 10
with the printf function in stdio.h. At the console, I am able to type
input at the first 2 getchar() calls, but when I call it again at big =
getchar(), I never get to type anything, it just reads ASCII 10 from
the stdin for some reason. Can someone explain why it is reading the
stdin stream at that point even though I am not typing, and how I would
get this to work.
CODE:
#include <stdio.h>
int main(){
int per1;
int per2;
int sumper;
printf("Type in two characters: \n");
per1 = getchar();
per2 = getchar();
sumper = per1 + per2;
printf("ASCII first number = %i\n",per1);
printf("ASCII second number = %i\n",per2);
printf("ASCII sum = %i\n",sumper);
int big;
printf("Type in a large number for scientific notation: \n");
big = getchar();
printf("Scientific Notation: %e\n", big);
printf("big = %i\n", big);
return 0;
}
RESULT:
Type in two characters:
50 /*I can type this in*/
ASCII first number = 53
ASCII second number = 48
ASCII sum = 101
Type in a large number for scientific notation:
Scientific Notation: 1.253353e-308 /*I don't get to type this in*/
big = 10