2 votes

Write 2 values in a 1st scanf and the 2nd scanf is not skipped in C

If in the first scanf I put two example values "Marco Antonio" separated by a space automatically, the 2nd scanf does not ask me anything and takes the "Antonio" as the value entered.

How do I at least avoid that?

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

int main(void)
    char apellido[30], 

    printf("Nombre: ");
    scanf("%30s", nombre);
    printf("Apellido: ");
    scanf("%30s", apellido);

    printf("Mi nombre es %s y mi apellido es %s. \n", nombre, apellido);

    return 0;


PaperBirdMaster Points 24910

Change the reading using scanf for a reading using fgets :

 printf("Nombre: ");
fgets(nombre, 30, stdin);
printf("Apellido: ");
fgets(apellido, 30, stdin);

Note that the end-of-line character will also be read to you.


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: