Skip to content

Instantly share code, notes, and snippets.

@rscarrera27
Created January 26, 2018 09:35
Show Gist options
  • Save rscarrera27/1ed55063779a20a4f3a5cfb09eb819c7 to your computer and use it in GitHub Desktop.
Save rscarrera27/1ed55063779a20a4f3a5cfb09eb819c7 to your computer and use it in GitHub Desktop.
myscanf
#include <stdio.h>
#include <stdlib.h>
int myscanf(char type[2], void *var);
int gets(char *address);
int main(void){
/*
char temp[16];
myscanf("%c", temp);
printf("%s", temp);
short temp = 0;
myscanf("%hd", &temp);
printf("%d\n", temp);
*/
int temp = 0;
myscanf("%d", &temp);
printf("%d\n", temp);
getchar();
return 0;
}
int myscanf(char type[2], void *address) {
if (*type == '%') {
type++;
if (*type == 'c') {
gets((char *)address);
return 1;
}
else if (*type == 'd') {
char temp[10];
gets(temp);
*(int *)address = atoi(temp);
return 2;
}
else if (*type == 'h' && *(++type) == 'd') {
char temp[5];
gets(temp);
*(short *)address = atoi(temp);
return 3;
}
else {
return 0;
}
}
else {
return 0;
}
}
int gets(char *address) {
while ((*address = getc(stdin)) != '\n') address++;
*address = 0;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment