j.beach
j.beach

Reputation: 53

Asking for Name and Displaying input

I relatively new to the C language and StackOverflow. I'm trying to write a simple C code that will prompt user for their name and then display it.

#include <stdio.h>

int main (void)
{
  char name;

 printf("Let's do this, please enter your name:");
 scanf("%s", &name);
 printf("Your name is %s", name);

 return 0;
}

The code complies but after inputting the name, it displays Segmentation fault (core dumped).

Thank you for any help.

Upvotes: 1

Views: 9303

Answers (5)

HarshitMadhav
HarshitMadhav

Reputation: 5069

You need to declare a string as because a single letter is stored in char and multiple characters along with a null character at the end forms an String which can be anything like names, etc.

In your code you have taken char which stores only a single character but in order to store your name (which is a string) you will have to take char array along with the size of the array.

Replace char with char[size] where size is the size for the string that you need.

Here are the changes I made to your code:

#include <stdio.h>

int main (void)
{
  char name[30];

 printf("Let's do this, please enter your name:");
 scanf("%s", name);
 printf("Your name is %s", name);

 return 0;
}

Upvotes: 1

chetan kankotiya
chetan kankotiya

Reputation: 106

It is because your name variable can only store single character and you are trying to store more than one character which will lead to unpredictable behaviour of program (e.g. segmentation fault).

If you know max length of name, declare variable name as array of character such as

char name[20]; 

Here you can store name with max length of 19 character. You can decide length of array as per your requirement.

Upvotes: 1

Harshith Rai
Harshith Rai

Reputation: 3066

A very small mistake @Jackie_Legs. In the declaration of the variable name, You have declared it as a char. so it holds just one character.

The solution: choose an arbitrary size for your name, say 10 or 15 characters. and declare it as an array of the size.

char name[15];

No changes in any other part of the program. Also, you should omit the & symbol in the scanf for strings.

So just one change and your code should work.

Here is the updated code which would work:

#include <stdio.h>

int main (void)
{
  char name[15];

  printf("Let's do this, please enter your name:");
  scanf("%s", name);
  printf("Your name is %s\n", name);

  return 0;
}

Upvotes: 1

AsthaUndefined
AsthaUndefined

Reputation: 1109

It is throwing error because of char name; char data type in C contain 1 character only in a variable. But your input is a string. You need to change the declaration of the variable from char to char array.

char name[50];
 printf("Let's do this, please enter your name:");
 scanf("%s", name);
 printf("Your name is %s", name);

Upvotes: 0

P.W
P.W

Reputation: 26800

name is declared as char, which means it can contain only one character. It is not sufficient to contain a string of characters (which a name usually consists of). You need to declare a char array (an array of characters).

The size of the array should be at least 1 more than the largest name you want to read in. The extra byte is to contain the null terminating character '\0'.

char name[SizeOfLargestName + 1];

And when you use scanf, you need not use & because now name points to the first byte of the array.

 scanf("%s", name);

Upvotes: 0

Related Questions