char name;
Reserves just one byte for the name, so when you enter the name it creates a buffer overflow that produce a memory violation error.
Use a char array instead:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("HI whats your name? ");
char name[256]; //Note we are reserving space for up to 255 chars,
//because the 256th must be reserved for the ending null.
scanf("%s",name); //Note also that we don't use the & because an array is already a pointer
printf("So your name is %s ", name);
return 0;
}
A buffer overflow is a process where you try to stuff in a buffer more items than its size can contain. In your case you start to store the name characters in the location of the variable name
and continue storing in the following memory cells eventually overwriting the variables that follows it.
Moreover, because the variable name
is an automatic variable, it resides on the stack. When you store the input character will overwrite also the return address, that in most systems is saved on the same stack, with the result that when returning from the function the processor jumps to a wrong address.
When opportunely crafted a buffer overflow can voluntarily change the return address to force processor to execute malicius code. The so called "buffer overflow exploit".