this is one of the k&r exercises that ive been stuck on. i was trying to do this manually and not using a stdio.h function. i wasnt aware this function existed. and may i add k&r doesnt assign exercises very well. i guess they expect you to already know whats out there. the use of pointers wasnt discussed and yet i had to figure out the basics of them and use it to do an exercise.
Code:
#include<stdio.h>
unsigned int htoi(char *s);
int main()
{
char s[10];
int i;
int c;
for (i = 0; i <= (10 -1) && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
//do i need to add a '\n' and a '\0' here? or will this suffice and put the '\0' at the end?
printf("%d\n",htoi(s));
return 0;
}
unsigned int htoi(char *s)
{
unsigned int n;
int r;
r = sscanf(s, "%x", &n);
return n;
}