Created
January 26, 2018 09:35
-
-
Save rscarrera27/1ed55063779a20a4f3a5cfb09eb819c7 to your computer and use it in GitHub Desktop.
myscanf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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