I have a little problem with the code below. This is a simple program that is read in 2 char and int arrays. Then it saves all the contents to another line and prints it.
#include <stdio.h>
#include <string.h>
int main ()
{
char string [50];
char first [11];
char last [16];
int age = 0;
printf("Please type in your first name: ");
scanf("%s", first);
printf("Please type in your last name: ");
scanf("%s", last);
printf("Please type in your age: ");
scanf("%d", &age);
sprintf(string, "Your name is %s %s and you are %d years old.", first, last, age);
puts(string);
getchar();
getchar();
return 0;
}
Now the program works fine, but when I close it, I get the following error: Runtime check error # 2 - Damage to the variable "string" was damaged. This is a bit confusing and I cannot figure out where the problem is. I would appreciate any advice.
source
share