C Functions – 5/5/14

In this post, I want to list several useful C functions that I don’t know before. Some are from my homework or classes, others may from my own programming experience.

getopt

NAME

getopt, optarg, optind, opterr, optopt

SYNOPSIS

DESCRIPTION

  • argc and argv are the argument count and argument array as passed to main(). * optstring is a string of recognized option characters. Any option that requires a parameter alongside it is suffixed by a colon (‘:’). e.g. df:mps:, optionf and s both need a argument alongside them.
  • optind is the index of the next element of the argv[] vector to be processed. It shall be initialized to 1 by the system. getopt() shall update it when it finished with each element of argv[], and optind will point to the first argument which is not a option argument.
  • If getopt() encounters an option that’s not in the list given to it, it returns a (‘?’) character to signify an error. If it detects a missing option argument, it will return the colon character (‘:’).
  • In either case, getopt() shall set the variable optopt to the option character that causes the error.
  • If there are no more command-line options found then getopt() return -1.
  • We usually program class to getopt() in a while loop with a switch statement within with case for each option.

Example

NOTE

Here are two resources to learn the getopt function:

boolstr

NAME

strbool

SYNOPSIS

DESCRIPTION

A short and useful method from my professor. When I checked the value of a boolean type variable, I used to print out 1 or 0. With this function, I can print out string “true” or “false”.

Example

basename

NAME

base name – return the last component of a pathname

SYNOPSIS

DESCRIPTION

  • The basename() function shall take the pathname pointed to by path and return a pointer to the final component of the pathname, deleting any trailing ‘/’ characters.
  • If the string pointed to by path consists entirely of the ‘/’ character, basename() shall return a pointer to the string “/”. If the string pointed to by path is exactly “//”, it is implementation-defined whether ‘/’ or “//” is returned.
  • If path is a null pointer or points to an empty string, basename() shall return a pointer to the string “.”.
  • I noticed that my professor uses this function several times in his C program. He used this function to get the function name. For C programs, in the command line, they are usually called by ./PROGRAM_NAME. By using the function basename, a programmer can easily remove ./ and get the PROGRAM_NAME.

RETURN VALUE

A pointer to the finial component of path.

Example

Input String Output String
“/usr/lib” “lib”
“/usr/” “usr”
“/” “/”
“///” “/”
“//usr//lib//” “lib”

strcasestr

  • This function is used to check whether a string contains a specific pattern. strcasestr is really similar to strstr, but strcasestr is case insensitive.
  • As specified in the corresponding manage, since strcasestr is a nonstandard extension, #define _GNU_SOURCE need to be specified before any other #include.

Example

Interesting C Array

This is a block of easy C code, right? This will print out 4. This block of code can also be written as the following:

a can be placed inside the square brackets. This syntax is correct! The output will be 4 as well!