2

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.

TryinHard
  • 4,078
  • 3
  • 28
  • 54
Manariba
  • 376
  • 6
  • 16
  • 1
    It's just not working :D (sorry for the bad English) – Manariba Aug 20 '15 at 07:51
  • 2
    What's the exact input? Is the name made up of sevelar words ("Alice Watson")? The `%d` format may "block" if the found input doesn't translate to a decimal value. – M Oehm Aug 20 '15 at 07:51
  • I've just tested it and it works for me. – rodrigo Aug 20 '15 at 07:52
  • 1
    What is your input? Why not skip the newline (which I assume is what you want to remove with the `"%*c"` sequence) by telling the *next* `scanf` to skip leading whitespace (which is done automatically for just about all formats anyway). You might want to read e.g. [this `scanf` (and family) reference](http://en.cppreference.com/w/c/io/fscanf). – Some programmer dude Aug 20 '15 at 07:52
  • @rodrigo It depends on the input. Try a string with a whitespace in it. – A. Abramov Aug 20 '15 at 07:54
  • @A.Abramov Yeah, well, it does not parse the integer, but it returns an error and does not block. That is still working for me. – rodrigo Aug 20 '15 at 07:57
  • @rodrigo OP has poor english, by blocking he meant an error as specified by his comment :) – A. Abramov Aug 20 '15 at 07:57
  • 1
    "It's not working" is a very bad problem description, can you please be more verbose? Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? And include (in the question) the actual input you enter, plus telling us what `scanf` returns (if it does return), actual and expected output, and just about as much details as you can. – Some programmer dude Aug 20 '15 at 08:01

5 Answers5

3

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

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
2
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

Olayinka
  • 2,813
  • 2
  • 25
  • 43
2

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);
}

OUTPUT Refer under the image

0

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.

cwfighter
  • 502
  • 1
  • 5
  • 20
-3

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);
}