-2

in C language how would I do in just one printf to receive two variables for example: type your age and name and already receive the variable name and variable age, would that be it?

#include <stdio.h>
#include <stdlib.h>

main(void){
   char age, name;

   printf("Type your age and name: ");
   scanf("%s", &age, "%s", &name);

   return 0;

}

I tried to put both variables inside the same scanf:

printf("Type your age and name: ");
scanf("%s", &age, "%s", &name);

I also tried to put it like this:

printf("Type your age and name: ");
scanf("%s", &age, &name);

I tried to put two scanf and only one printf:

printf("Type your age and name: ");
scanf("%s", &age);
scanf("%s", &name);
  • 3
    C doesn't have a string class and that string class which C doesn't have is not `char`. You need to study arrays, then pointers, then strings, in that order. – Lundin Feb 28 '23 at 16:07
  • Does this answer your question? [Getting multiple values with scanf()](https://stackoverflow.com/questions/1412513/getting-multiple-values-with-scanf) – dotthor Feb 28 '23 at 16:08
  • 2
    `scanf("%s", &age, "%s", &name);` -> `scanf("%s%s", &age, &name);` – Jabberwocky Feb 28 '23 at 16:08
  • I wrote a newbie FAQ here: [Common string handling pitfalls in C programming](https://software.codidact.com/posts/284849). See FAQ #1 and #3. – Lundin Feb 28 '23 at 16:08

1 Answers1

1

Put both formats in the same string.

name should be a string, which is implemented using a character array, not a single character, and age should be an integer, not a string.

char name[50];
unsigned int age;

printf("Type your age and name: ");
scanf("%u %s", &age, name);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Pedantically, `name` should be a character array, inside which scanf then stores a C string: some text that ends with a null terminator. And well, you messed up `%d` and `%u`. – Lundin Feb 28 '23 at 16:12