-2

I am trying to print the out put of name and phone. Keeps showing this two errors in terminal

format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’

and

format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’

my code:

int main(void)
{
    char name,phone;
    int age;


    printf("Kindly input your name, age and phone number");
    scanf("%s %s %d", &name, &phone, &age);
    printf("%s %s %d\n", name, phone, age);
    return 0;
}
James Z
  • 12,209
  • 10
  • 24
  • 44
wanicedude
  • 17
  • 1

2 Answers2

0

The error message means that in this call of printf

printf("%s %s %d\n", name, phone, age);

argument expressions name and phone having the type char are promoted to the type int and there is used the conversion specification %s designed to output strings instead of integers.

Instead of declaring these variables as having the scalar type char you need to declare them as having a character array type to be able to store strings.

And you should change the prompt

printf("Kindly input your name, age and phone number");

like

printf("Kindly input your name, phone number and age: ");

For example

char name[20], phone[20];
int age;


printf("Kindly input your name, phone number and age: ");
scanf("%19s %19s %d", name, phone, &age);
printf("%s %s %d\n", name, phone, age);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Your sample code is likely to fault .. Actually, you are storing an array of characters (string) into a single char memory location. You should rather do this:

/* Note the array definition `[]`.
 * Note that 32 is an arbitrary length, change it as per your needs.
 */
char name[32], phone[32];

Then, as an array is already a pointer to memory location, you can use it without the &.

scanf("%31s %31s %d", name, phone, &age);

EDIT: as suggested in comment, you can restrict the maximum input size. Knowing a string is NULL-terminated, in the example the maximum total string length becomes 31.

Jib
  • 1,334
  • 1
  • 2
  • 12
  • 2
    `scanf("%s ...` is a bad as [`gets()`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Better to use a _width_ `scanf("%31s ...`. – chux - Reinstate Monica Jul 03 '23 at 16:25