Reputation: 471
I'm new to using C in visual studio. I have this code and I can't figure out why it's acting the way it is. I put the getchar() to stop the console window from disappearing. It still disappears though unless I have the second getchar(). Why does it do this and why does a second stop it?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(){
int nums[10];
int number;
int i = 0;
printf("Enter a number: ");
scanf("%i", &number);
printf("%i", number);
srand((unsigned)time(NULL));
for (i; i < 10; i++){
nums[i] = rand() % 50;
printf("nums[%d] = %d\n", i, *(nums + i));
}
getchar();
getchar();
return 0;
}
Upvotes: 0
Views: 56
Reputation: 75062
The first getchar()
should read '\n'
which is not read by scanf
, so it will return immediately.
The second getchar()
doesn't have anything to read, so it will wait for input.
Upvotes: 1