-4

(I am very new to C by btw) My current code

#include <stdio.h>

char main()
{
    char x;
    printf("Please enter your first initial:",x);
    scanf("%c",&x);

    char string[100];

    char y;
    printf("Please enter your last name:",y);
    scanf("%s",&y);

    printf("Hello I am %c %s. Nice to meet you.",x,y);

    return 0;
}
Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
  • 1
    http://idownvotedbecau.se/imageofcode – Barmar Oct 22 '21 at 19:57
  • `%s` is used to read a string. You can't use it for a single `char`. – Barmar Oct 22 '21 at 19:58
  • Adding to the on-point answer below, some minor items. The first two `printf` statements have a second parameter (`x` and `y`) that are not actually needed because there are no `%` tokens for them to interpolate; remove them. Also, `main` should not have type `char` - use `int` instead. – Steve Friedl Oct 22 '21 at 21:11

1 Answers1

1

You can't use a char value with %s scanf formatting. That reads a null-terminated string, and char doesn't have a place for the null terminator.

Read the last name into the string variable, and use that.

char string[100];
printf("Please enter your last name:)";
scanf("%99s", string);

printf("Hello I am %c %s. Nice to meet you.", x, string);
Barmar
  • 741,623
  • 53
  • 500
  • 612