getopt
printf
Family of Functionsgetopt
Section 5.10 of K&R explains commandline arguments and explains how to write C
code to process them. This method works well for simple cases. For more complex commandline processing, use a library function specially written for the purpose.
GNU’s getopt
is a special library function very useful for commandline input processing. Its manual page has an example program fragment that you can modify to suit your need.
#include <time.h>
.
.
.
/*
Set 'month' (in the caller) to current month, and
set 'year' (in the caller) to current year.
*/
void getMonthYear(int *month, int *year) {
time_t timenow;
struct tm *current;
time(&timenow);
current = gmtime(&timenow);
*month = current->tm_mon+1;
*year = current->tm_year+1900;
}
.
.
.
int month, int year;
getMonthYear(&month, &year);
/* At this point month equals 9 and year equals 2016 */
.
.
.
printf
Family of Functionsfprintf
and sprintf
belong to the stdio
library. You have to have a line
#include <stdio.h>
in order to use them. They are similar to printf
but have one extra paramenter as the first parameter.
For fprintf
, the first parameter is a file descriptor (e.g., stderr
) and the effect of calling fprintf
is to print like printf
does but direct the output to the stream given as the first argument instead. E.g. the call
fprintf(stderr, "%3d%3d\n", 2, 3)
will print the NULL
-terminated string " 2 3" (without the quotes) to the standard error output stream.
For sprintf
, the first parameter is a string variable and the effect of calling sprintf
is to print like printf
does but direct the output to the string given as the first argument instead. E.g. the call
char s[80];
sprintf(s, "%3d%3d\n", 2, 3)
will create the NULL
-terminated string " 2 3" at the (first) location of s
. Make sure the char array s
has enough space to hold the NULL
-terminated string before calling sprintf
.
strlen
, strcmp
, strcpy
, strcat
, and strncat
are string library functions. You need to include the line
#include <string.h>
in order to use them.
strlen(s)
returns the length (as an unsigned
) of the NULL
-terminated string s
strcpy(t, cs)
copies the NULL
-terminated constant string cs
to string t
; return t
strncpy(t, cs, n)
copies at most n
characters of the NULL
-terminated constant string cs
to string t
; return t
. Pad with ‘\0’ if t
has fewer than n
characters.
strcat(t, cs)
appends the NULL
-terminated constant string s
to the end of string t
; return t
strncat(t, cs, n)
appends at most n
characters from the NULL
-terminated constant string s
to string t
; return t
strcmp(cs, ct)
compare constant string cs
to constant string ct
; return a negative integer if cs < ct
, 0 if cs = ct
, or positive integer of cs > ct
strcmp(cs, ct, n)
compare at most n
characters of constant string cs
to constant string ct
; return a negative integer if cs < ct
, 0 if cs = ct
, or positive integer of cs > ct
memset(s, c, n)
places character c
as the first n
characters of s
; return s
Declaring an array variable does not initialize its content with any special values. E.g. in the code
int main() {
char s[10];
...
}
at the point … you cannot assume elements of array s
contain any special value, i.e., they contain garbage.
Similarly, memory allocated dynamically contains no special value. E.g. in the code
int main() {
char *s = (char *) malloc(10);
...
}
at the point … you cannot assume the ten bytes starting at s
contain any special value, i.e., they contain garbage.
To initialize a chunk of consecutive memory to a specific value, you can use the library function memset
in string.h
. E.g. in the above codes, at point … you can write
memset(s, ' ', 10*sizeof(char));
to initialize all ten bytes of array s
to blanks.