a new line terminates a statement when the last token on the line is(1) an identifier,(2) a numeric, character, or string literal,(3) one of the keywords break, continue,fallthrough, or return, (4) one of ++, --, ), ], or }

Functions return values such as EOF, NULL, or -1 to indicate error. Some functions return the value of errno. In some cases errors are not distinguished from end-of-file. The functions ferror() and feof() can be used to test a file handle.

The type of error is stored in errno. strerror(errno) or the thread safe strerror_r(errno, buf, buflen) convert the errors code to a string and perror() writes its argument to stderr with sterror(errno).

In all languages on this sheet the && and || operators short circuit: i.e. && will not evaluate the 2nd argument if the 1st argument is false, and || will not evaluate the 2nd argument if the 1st argument is true. If the 2nd argument is not evaluated, side-effects that it contains are not executed.

Returns 1, 0, or -1 depending upon whether the first string is lexicographically greater, equal, or less than the second. The variants strncmp, strcasecmp, and strncasecmp can perform comparisons on the first n characters of the strings or case insensitive comparisons.

The stdarg.h library supports variable length functions, but provides no means for the callee to determine how many arguments were provided. Two techniques for communicating the number of arguments to the caller are (1) devote one of the non-variable arguments for the purpose as illustrated in the table above, or (2) set the last argument to a sentinel value as illustrated below. Both techniques permit the caller to make a mistake that can cause the program to segfault. printf uses the first technique, because it infers the number of arguments from the number of format specifiers in the format string.

An if statement is a series of blocks, each guarded by a conditional expression. The first conditional expression statement which evaluates to true determines the block which executes. An optional else block executes if none of the of the conditional expressions are true.

The switch statement has a switch expression which is evaluated against one or more case values. The first case value which is equal to the switch expression determines the block which executes. An optional default block executes if none of the case values are equal.

Note that in some languages, execution "falls through" to the next block in a switch unless prevented by a break statement.

go:

Case values can be expressions.

The switch statement can lack a switch expression, in which case the first case value which evaluates to true determines the block to execute. Such a switch statement differs little from an if statement, but note that fallthrough cannot be used in an if statement.

It is possible to switch on type of an expression, in which case the case values are types:

A while loop is a conditional expression and a block. The conditional expression is evaluated before each execution of the block. The block is executed iteratively as long as the conditional expression is true.

c:

C has a do-while statement. The block is always executed at least once. The conditional expression is evaluated before the second and subsequent iterations to determine if the block is executed again.

int i = 0;
do {
print("%d\n", ++i);
} while (i < 10);

If the body of a while loop consists of a single statement the curly braces are optional:

A for loop has four parts: the initialization which executes at the outset, the condition which is evaluated before each iteration, the body which is executed if the condition is true, and the afterthought which is executed after each execution of the body.

POSIX systems provide processes with the ability to open multiple files and manipulate them with via integers called file descriptors. Normally the integers 0, 1, and 2 refer to standard input, standard output, and standard error. The header <unistd.h> defines the macros STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO for these file descriptors.

System calls take file descriptors as arguments, but the C standard library provides an alternate set functions for buffered I/O. The standard library functions use FILE structs to identify streams and open files.

first argument

c:

The first argument is the pathname to the executable. Whether the pathname is absolute or relative depends on how the executable was invoked. If the executable was invoked via a symlink, then the first argument is the pathname of the symlink, not the executable the symlink points to.

On Linux and Mac OS X the value returned to the parent is exit_arg & 0377. If the process exited because of a signal, the kernel sets the exit status to 128 plus the signal number. The signals are numbered starting from 1, leaving exit status values from 1 to 127 and perhaps 128 available for other failure conditions.

The C standard library defines the values EXIT_SUCCESS and EXIT_FAILURE as an aid for writing code which is portable to systems which do not use 0 to indicate success.

Because C integer types don't have well defined sizes, typedef is sometimes employed to as an aid to writing portable code. One might include the following in a header file:

typedef int int32_t;

The rest of the code would declare integers that need to be 32 bits in size using int32_t and if the code needed to be ported to a platform with a 16 bit int, only a single place in the code requires change. In practice the typedef abstraction is leaky because functions in the standard library such as atoi, strtol, or the format strings used by printf depend on the underlying type used.

Enums were added to the C standard when the language was standardized by ANSI in 1989.

An enum defines a family of integer constants. If an integer value is not explicitly provided for a constant, it is given a value one greater than the previous constant in the list. If the first constant in the list is not given an explicit value, it is assigned a value of zero. it is possible for constants in a list to share values. For example, in the following enum, a and c are both zero and b and d are both one.

enum { a=0, b, c=0, d };

A typedef can be used to make the enum keyword unnecessary in variable declarations:

struct definition

A struct provides names for elements in a predefined set of data and permits the data to be accessed directly without the intermediation of getters and setters. C++, Java, and C# classes can be used to define structs by making the data members public. However, public data members violates the uniform access principle.

struct declaration

struct initialization

c:

The literal format for a struct can only be used during initialization. If the member names are not provided, the values must occur in the order used in the definition.

struct member assignment

struct member access

c:

The period operator used for member access has higher precedence than the pointer operator. Thus parens must be used to get at the member of a struct referenced by a pointer: