L
lovecreatesbeauty
Could you talk something about the general rules on the interface
(function) design in C program that recognized publically? Or introduce
some good advice of yourself.
How do you think about some of those advices like following?
a. keep the interface clean and clear (What does clean or clear mean
and how to achieve that?).
b. avoid using static variables in local function if possible.
c. avoid using global variables for local function if possible.
d. avoid allocating dynamic memory in local function if possible.
....
And I write following the function to convert an integer to a string.
Your advices are welcome and appreciated.
Requirement: convert an integer to character string, put one blank char
among the string, for example: "1 2 3 4 0 9" for 123409.
/* s : hold the string presentation of an integer.
* num : an integer is to be gotten its string presentation.
*/
void itoa2(char *s, int num)
{
int len = 100;
char s1[len]; /* C99 features involved */
sprintf(s1, "%d", num);
int i = 0, j = 0;
for (i = 0; s1; ++i, ++j)
{
s[j] = s1;
s[++j] = ' ';
}
s[--j] = '\0';
}
How do you think about following choices of different interface design:
/* #2 self-contained or self-sufficient */
void itoa2(char *s, int num);
/* #3 global or static variables or dynamic memory may be required */
void itoa3(int num);
(function) design in C program that recognized publically? Or introduce
some good advice of yourself.
How do you think about some of those advices like following?
a. keep the interface clean and clear (What does clean or clear mean
and how to achieve that?).
b. avoid using static variables in local function if possible.
c. avoid using global variables for local function if possible.
d. avoid allocating dynamic memory in local function if possible.
....
And I write following the function to convert an integer to a string.
Your advices are welcome and appreciated.
Requirement: convert an integer to character string, put one blank char
among the string, for example: "1 2 3 4 0 9" for 123409.
/* s : hold the string presentation of an integer.
* num : an integer is to be gotten its string presentation.
*/
void itoa2(char *s, int num)
{
int len = 100;
char s1[len]; /* C99 features involved */
sprintf(s1, "%d", num);
int i = 0, j = 0;
for (i = 0; s1; ++i, ++j)
{
s[j] = s1;
s[++j] = ' ';
}
s[--j] = '\0';
}
How do you think about following choices of different interface design:
/* #2 self-contained or self-sufficient */
void itoa2(char *s, int num);
/* #3 global or static variables or dynamic memory may be required */
void itoa3(int num);