Question: Program 1: Your program must use only the POSIX system call write() (you may also use _exit() if you want). 1. The file functions.c should
Program 1: Your program must use only the POSIX system call write() (you may also use _exit() if you want).
1. The file functions.c should contain four functions. The first two functions are "single digit conversion" functions (between int and char) that are then used by the next two "multi digit conversion" functions. Below is the skeleton template for functions.c, please complete the rest of the code in C code in the areas directed:
/************************* * * * FILE functions.c * * * *************************/ /* This file should only contain four standalone functions and must not include any headers! Your code here should be pure C code that does not use any library function at all! */ /* Function 1: char idtoc(int d) { } - Takes an integer input - Requires the input to be between 0 and 15 (inclusive) - Returns the character for that int digit, assuming the maximum possible base of 16 (that is, hex) */ char idtoc(int d) { /* Write your code here */ } /* Function 2: int ctoid(int base, char c) { } - This is a kind of an inverse function of idtoc() - The first input parameter is an int specifying the conversion base which is required to be between 2 and 16 (inclusive) - The second parameter is any character - Return value: If the character c represents a valid digit in the given base, returns the int value of that digit, else returns -1. For bases between 11 and 16 (inclusive), allow both upper case and lower case characters as digits. */ int ctoid(int base, char c) { /* Write your code here */ } /* Function 3 - Convert a char digit-string to the int it represents int strtonum(int base, char *bufsrc, unsigned int *ndst) { } - The first input parameter is an int specifying the conversion base which is required to be between 2 and 16 (inclusive) - The second input parameter is a pointer to a character string, which is required to be null-terminated string - The third input parameter is a pointer to an unsigned int (for storing the converted integer) - Return value: If all inputs are valid and the conversion can be done successfully, then return 0. Otherwise return a negative integer whose value is informative. Example: If the character string pointed to by bufsrc contains an invalid char (i.e. any char other than the digit characters in the given base) then return -1, else if the string is a valid digit string in the given base but the integer it represents is too large to be stored as an unsigned int then return -2 (overflow check), etc. - As usual, the caller is responsible for valid storage at the locations pointed to by all the pointer parameters that are passed */ int strtonum(int base, char *bufsrc, unsigned int *ndst) { /* Write your code here */ } /* Function 4 - Convert an int to its char digit string in a given base: int numtostr(int base, unsigned int *nsrc, char *bufdst) { } - The first input parameter is an int specifying the conversion base which is required to be between 2 and 16 (inclusive) - The second input parameter is a pointer to the unsigned int (required to be non-negative) that needs to be converted to its representation as a string of digit characters in the given base - The third parameter is a pointer to (the start of) a buffer of chars; the caller is required to have allocated sufficient storage to this buffer for holding the converted character digit string including the terminating null character - Return value: On success, return the length of the computed digit string (without leading zero), and return -1 on any error */ int numtostr(int base, unsigned int *nsrc, char *bufdst) { /* Write your code here */ } /******************************* * END OF FILE functions.c * *******************************/ Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
