Skip to content

Instantly share code, notes, and snippets.

@Silva97
Last active November 3, 2022 13:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Silva97/0e864b9a3baf40dd8f4d13137400e9f7 to your computer and use it in GitHub Desktop.
Save Silva97/0e864b9a3baf40dd8f4d13137400e9f7 to your computer and use it in GitHub Desktop.
Maneira segura de pegar o input do usuário
/********************
* Developed by Luiz Felipe.
*
* GitHub: https://github.com/Silva97
* Facebook: https://www.facebook.com/B4.0E.B0.48.CD.10.B0.69.CD.10.C3
********************/
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
int lscanf(char *text, const char *format, ...);
int main(){
char str[6];
int x, y, z;
lscanf("Valor de STR: ", "%5s", str);
lscanf("Valor de X: ", "%d", &x);
lscanf("Valor de Y: ", "%d", &y);
lscanf("Valor de Z: ", "%d", &z);
printf("str = %s\n", str);
printf("x = %d\n", x);
printf("y = %d\n", y);
printf("z = %d\n", z);
return 0;
}
int lscanf(char *text, const char *format, ...){
char buff[MAX_INPUT];
int ret;
va_list args;
if(text)
fputs(text, stdout);
fgets(buff, sizeof buff, stdin);
va_start(args, format);
ret = vsscanf(buff, format, args);
va_end(args);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment