Is it possible to read a character and an integer in one time? Like this:
char name[32];
int age;
printf("Give name and age: ");
scanf("%s%d%*c", name, &age);
It blocks all the time.
Is it possible to read a character and an integer in one time? Like this:
char name[32];
int age;
printf("Give name and age: ");
scanf("%s%d%*c", name, &age);
It blocks all the time.
UPDATED: You only accept input but not printing any thing. See the below code ( working and tested )
#include <stdio.h>
int main(void) {
char name[32];
int age;
printf("Give name and age: ");
scanf("%31s%d", name, &age);
printf("Your name is %s and age is %d",name,age);
}
intput: Shaz 30
Output : Your name is Shaz and age is 30
char name[32];
int age;
printf("Give name and age: ");
scanf("%31s%d", name, &age);
If the input string
is longer than 31 characters, the value read for age
will be affected, this is where fgets
comes in handy.
Do not read both the name and age on the same line input. Read the name first to force the user to enter a new line so you can handle the entire line as the input name.
Read fgets
It is one of the method
CODE
#include<stdio.h>
char name[32],age;
int main()
{
printf("Enter name and age: ");
scanf("%s%d", name, &age);
printf("\nNAME : %s AGE : %d",name,age);
}
I have tested your code, there is no error. Maybe you should only add printf()
to print the answer.You will find you can get the same answer with what you have printed.
EDIT: If input is Colin 23
(the space is necessary), you should use printf("name is %31s, age is %d", name, age)
. Then you will get output Colin 23
.
You can do it like this:
#include <stdio.h>
int main(void)
{
char name[32];
int age;
printf("Give name and age: ");
scanf("%s%d", name, &age);
printf("%s\n",name);
printf("%d",age);
}