Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 31, 2020 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/eeb7340819835ad264a27c70c07fed6e to your computer and use it in GitHub Desktop.
Save parzibyte/eeb7340819835ad264a27c70c07fed6e to your computer and use it in GitHub Desktop.
/*
https://parzibyte.me/blog
*/
#include <stdio.h>
#include <string.h> // Para strlen
#define MAXIMA_LONGITUD_CADENA 50
void consumirNuevaLinea(void)
{
int c;
do
{
c = getchar();
} while (c != EOF && c != '\n');
}
int main(int argc, char const *argv[])
{
int edad;
printf("Escribe tu edad: ");
scanf("%d", &edad);
// Consumir nueva línea
consumirNuevaLinea();
char nombre[MAXIMA_LONGITUD_CADENA];
printf("Ingresa tu nombre completo: ");
fgets(nombre, MAXIMA_LONGITUD_CADENA, stdin);
// Remover salto de línea
if ((strlen(nombre) > 0) && (nombre[strlen(nombre) - 1] == '\n'))
{
nombre[strlen(nombre) - 1] = '\0';
}
printf("Tu nombre es %s", nombre);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment