The C Library Reference Guide
The C Library Reference Guide
by Eric Huss
© Copyright 1997 Eric Huss
Release 1
Introduction
1. Language
1.1 Characters
1.1.1 Trigraph Characters
1.1.2 Escape Sequences
1.1.3 Comments
1.2 Identifiers
1.2.1 Keywords
1.2.2 Variables
1.2.3 Enumerated Tags
1.2.4 Arrays
1.2.5 Structures and Unions
1.2.6 Constants
1.2.7 Strings
1.2.8 sizeof Keyword
1.3 Functions
1.3.1 Definition
1.3.2 Program Startup
1.4 References
1.4.1 Pointers and the Address Operator
1.4.2 Typecasting
1.5 Operators
1.5.1 Postfix
1.5.2 Unary and Prefix
1.5.3 Normal
1.5.4 Boolean
1.5.5 Assignment
1.5.6 Precedence
1.6 Statements
1.6.1 if
1.6.2 switch
1.6.3 while
1.6.4 do
1.6.5 for
1.6.6 goto
1.6.7 continue
1.6.8 break
1.6.9 return
1.7 Preprocessing Directives
1.7.1 #if, #elif, #else, #endif
1.7.2 #define, #undef, #ifdef, #ifndef
1.7.3 #include
1.7.4 #line
1.7.5 #error
1.7.6 #pragma
1.7.7 Predefined Macros
2. Library
2.1 assert.h
2.1.1 assert
2.2 ctype.h
2.2.1 is... Functions
2.2.2 to... Functions
2.3 errno.h
2.3.1 EDOM
2.3.2 ERANGE
2.3.3 errno
2.4 float.h
2.4.1 Defined Values
2.5 limits.h
2.5.1 Defined Values
2.6 locale.h
2.6.1 Variables and Definitions
2.6.2 setlocale
2.6.3 localeconv
2.7 math.h
2.7.1 Error Conditions
2.7.2 Trigonometric Functions
2.7.2.1 acos
2.7.2.2 asin
2.7.2.3 atan
2.7.2.4 atan2
2.7.2.5 cos
2.7.2.6 cosh
2.7.2.7 sin
2.7.2.8 sinh
2.7.2.9 tan
2.7.2.10 tanh
2.7.3 Exponential, Logarithmic, and Power Functions
2.7.3.1 exp
2.7.3.2 frexp
2.7.3.3 ldexp
2.7.3.4 log
2.7.3.5 log10
2.7.3.6 modf
2.7.3.7 pow
2.7.3.8 sqrt
2.7.4 Other Math Functions
2.7.4.1 ceil
2.7.4.2 fabs
2.7.4.3 floor
2.7.4.4 fmod
2.8 setjmp.h
2.8.1 Variables and Definitions
2.8.2 setjmp
2.8.3 longjmp
2.9 signal.h
2.9.1 Variables and Definitions
2.9.2 signal
2.9.3 raise
2.10 stdarg.h
2.10.1 Variables and Definitions
2.10.2 va_start
2.10.3 va_arg
2.10.4 va_end
2.11 stddef.h
2.11.1 Variables and Definitions
2.12 stdio.h
2.12.1 Variables and Definitions
2.12.2 Streams and Files
2.12.3 File Functions
2.12.3.1 clearerr
2.12.3.2 fclose
2.12.3.3 feof
2.12.3.4 ferror
2.12.3.5 fflush
2.12.3.6 fgetpos
2.12.3.7 fopen
2.12.3.8 fread
2.12.3.9 freopen
2.12.3.10 fseek
2.12.3.11 fsetpos
2.12.3.12 ftell
2.12.3.13 fwrite
2.12.3.14 remove
2.12.3.15 rename
2.12.3.16 rewind
2.12.3.17 setbuf
2.12.3.18 setvbuf
2.12.3.19 tmpfile
2.12.3.20 tmpnam
2.12.4 Formatted I/O Functions
2.12.4.1 ...printf Functions
2.12.4.2 ...scanf Functions
2.12.5 Character I/O Functions
2.12.5.1 fgetc
2.12.5.2 fgets
2.12.5.3 fputc
2.12.5.4 fputs
2.12.5.5 getc
2.12.5.6 getchar
2.12.5.7 gets
2.12.5.8 putc
2.12.5.9 putchar
2.12.5.10 puts
2.12.5.11 ungetc
2.12.7 Error Functions
2.12.7.1 perror
2.13 stdlib.h
2.13.1 Variables and Definitions
2.13.2 String Functions
2.13.2.1 atof
2.13.2.2 atoi
2.13.2.3 atol
2.13.2.4 strtod
2.13.2.5 strtol
2.13.2.6 strtoul
2.13.3 Memory Functions
2.13.3.1 calloc
2.13.3.2 free
2.13.3.3 malloc
2.13.3.4 realloc
2.13.4 Environment Functions
2.13.4.1 abort
2.13.4.2 atexit
2.13.4.3 exit
2.13.4.4 getenv
2.13.4.5 system
2.13.5 Searching and Sorting Functions
2.13.5.1 bsearch
2.13.5.2 qsort
2.13.6 Math Functions
2.13.6.1 abs
2.13.6.2 div
2.13.6.3 labs
2.13.6.4 ldiv
2.13.6.5 rand
2.13.6.6 srand
2.13.7 Multibyte Functions
2.13.7.1 mblen
2.13.7.2 mbstowcs
2.13.7.3 mbtowc
2.13.7.4 wcstombs
2.13.7.5 wctomb
2.14 string.h
2.14.1 Variables and Definitions
2.14.2 memchr
2.14.3 memcmp
2.14.4 memcpy
2.14.5 memmove
2.14.6 memset
2.14.7 strcat
2.14.8 strncat
2.14.9 strchr
2.14.10 strcmp
2.14.11 strncmp
2.14.12 strcoll
2.14.13 strcpy
2.14.14 strncpy
2.14.15 strcspn
2.14.16 strerror
2.14.17 strlen
2.14.18 strpbrk
2.14.19 strrchr
2.14.20 strspn
2.14.21 strstr
2.14.22 strtok
2.14.23 strxfrm
2.15 time.h
2.15.1 Variables and Definitions
2.15.2 asctime
2.15.3 clock
2.15.4 ctime
2.15.5 difftime
2.15.6 gmtime
2.15.7 localtime
2.15.8 mktime
2.15.9 strftime
2.15.10 time
Appendix A
ASCII Chart
Index
Index
Example:
printf("No???/n");
translates into:
printf("No?\n");
Escape
Name Meaning
Sequence
\a Alert Produces an audible or visible alert.
\b Backspace Moves the cursor back one position (non-destructive).
\f Form Feed Moves the cursor to the first position of the next page.
\n New Line Moves the cursor to the first position of the next line.
Carriage
\r Moves the cursor to the first position of the current line.
Return
Horizontal
\t Moves the cursor to the next horizontal tabular position.
Tab
Vertical
\v Moves the cursor to the next vertical tabular position.
Tab
\' Produces a single quote.
\" Produces a double quote.
\? Produces a question mark.
\\ Produces a single backslash.
\0 Produces a null character.
Defines one character by the octal digits (base-8 number).
Multiple characters may be defined in the same escape
\ddd
sequence, but the value is implementation-specific (see
examples).
Defines one character by the hexadecimal digit (base-16
\xdd
number).
Examples:
printf("\12");
Produces the decimal character 10 (x0A Hex).
printf("\xFF");
Produces the decimal character -1 or 255 (depending on sign).
printf("\x123");
Produces a single character (value is undefined). May cause errors.
printf("\0222");
Produces two characters whose values are implementation-specific.
1.1.3 Comments
Comments in the source code are ignored by the compiler. They are
encapsulated starting with /* and ending with */. According to the ANSI
standard, nested comments are not allowed, although some implementations
allow it.
Single line comments are becoming more common, although not defined in the ANSI
standard. Single line comments begin with // and are automatically terminated at the end
of the current line.
Previous Section | Table of Contents | Index | Next Section
1.1 Characters 1.3 Functions
1.2.1 Keywords
The following keywords are reserved and may not be used as an identifier for
any other purpose.
1.2.2 Variables
A variable may be defined using any uppercase or lowercase character, a
numerical digit (0 through 9), and the underscore character (_). The first
character of the variable may not be a numerical digit or underscore. Variable
names are case sensitive.
Access to variables outside of their file scope can be made by using linkage.
Linkage is done by placing the keyword extern prior to a variable declaration.
This allows a variable that is defined in another source code file to be accessed.
Variables defined within a function scope have automatic storage duration.
The life of the variable is determined by the life of the function. Space is
allocated at the beginning of the function and terminated at the end of the
function. Static storage duration can be obtained by placing the
keyword static in front of the variable declaration. This causes the variable's
space to be allocated when the program starts up and is kept during the life of
the program. The value of the variable is preserved during subsequent calls to
the function that defines it. Variables with file scope are automatically static
variables.
Here are the maximum and minimum sizes of the type-specifiers on most
common implementations. Note: some implementations may be different.
Examples:
int bob=32;
Creates variable "bob" and initializes it to the value 32.
char loop1,loop2,loop3='\x41';
Creates three variables. The value of "loop1" and "loop2" is undefined. The
value of loop3 is the letter "A".
typedef char boolean;
Causes the keyword "boolean" to represent variable-type "char".
boolean yes=1;
Creates variable "yes" as type "char" and sets its value to 1.
enum identifier{enumerator-list};
Identifier is a handle for identification, and is optional.
Enumerator-list is a list of variables to be created. They will be constant
integers. Each variable is given the value of the previous variable plus 1. The
first variable is given the value of 0.
Examples:
enum {joe, mary, bob, fran};
Creates 4 variables. The value of joe is 0, mary is 1, bob is 2, and fran is 3.
1.2.4 Arrays
Arrays create single or multidimensional matrices. They are defined by
appending an integer encapsulated in brackets at the end of a variable name.
Each additional set of brackets defines an additional dimension to the array.
When addressing an index in the array, indexing begins at 0 and ends at 1 less
than the defined array. If no initial value is given to the array size, then the size
is determined by the initializers. When defining a multidimensional array,
nested curly braces can be used to specify which dimension of the array to
initialize. The outermost nest of curly braces defines the leftmost dimension,
and works from left to right.
Examples:
int x[5];
Defines 5 integers starting at x[0], and ending at x[4]. Their values are
undefined.
char str[16]="Blueberry";
Creates a string. The value at str[8] is the character "y". The value at str[9] is
the null character. The values from str[10] to str[15] are undefined.
char s[]="abc";
Dimensions the array to 4 (just long enough to hold the string plus a null
character), and stores the string in the array.
int y[3]={4};
Sets the value of y[0] to 4 and y[1] and y[2] to 0.
int joe[4][5]={
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
};
The first row initializes joe[0], the second row joe[1] and so forth. joe[3] is
initialized to 5 zeros.
variables,...
} structure-variables,...;
Structure-name is optional and not needed if the structure variables are defined.
Inside it can contain any number of variables separated by semicolons. At the
end, structure-variables defines the actual names of the individual structures.
Multiple structures can be defined by separating the variable names with
commas. If no structure-variables are given, no variables are
created. Structure-variables can be defined separately by specifying:
To access a variable in the structure, you must use a record selector (.).
Unions work in the same way as structures except that all variables are
contained in the same location in memory. Enough space is allocated for only
the largest variable in the union. All other variables must share the same
memory location. Unions are defined using the union keyword.
Examples:
struct my-structure {
int fred[5];
char wilma, betty;
float barny=1;
};
This defines the structure my-structure, but nothing has yet been done.
struct my-structure account1;
This creates account1 and it has all of the variables from my-structure.
account1.barny contains the value "1".
union my-union {
char character_num;
int integer_num;
long long_num;
float float_num;
double double_num;
} number;
This defines the union number and allocates just enough space for the variable
double_num.
number.integer_num=1;
Sets the value of integer_num to "1".
number.float_num=5;
Sets the value of float_num to "5".
printf("%i",integer_num);
This is undefined since the location of integer_num was overwritten in the
previous line by float_num.
1.2.6 Constants
Constants provide a way to define a variable which cannot be modified by any
other part in the code. Constants can be defined by placing the
keyword const in front of any variable declaration. If the keyword volatile is
placed after const, then this allows external routines to modify the variable
(such as hardware devices). This also forces the compiler to retrieve the value
of the variable each time it is referenced rather than possibly optimizing it in a
register.
Hexadecimal constant:
hexadecimal digits...
0x
Where hexadecimal digits is any digit or any letter A through F or a through f.
Decimal constant:
Octal constant:
Floating constant:
L or l:
Examples:
const float PI=3.141;
Causes the variable PI to be created with value 3.141. Any subsequent attempts
to write to PI are not allowed.
const int joe=0xFFFF;
Causes joe to be created with the value of 65535 decimal.
const float penny=7.4e5;
Causes penny to be created with the value of 740000.000000.
1.2.7 Strings
Strings are simply an array of characters encapsulated in double quotes. At the
end of the string a null character is appended.
Examples:
or
The sizeof keyword returns the number of bytes of the given expression or
type.
size_t is an unsigned integer result.
Example:
printf("The number of bytes in an int is %d.\n",sizeof(int));
return-type is the variable type that the function returns. This can not be an
array type or a function type. If not given, then int is assumed.
return-type function-name(paramater-type-list);
Examples:
int joe(float, double, int);
This defines the prototype for function joe.
int joe(float coin, double total, int sum)
{
/*...*/
}
int (*peter)();
Defines peter as a pointer to a function with no parameters specified. The value
of peter can be changed to represent different functions.
int (*aaron(char *(*)(void)) (long, int);
Defines the function aaron which returns a pointer to a function. The function
aaron takes one argument: a pointer to a function which returns a character
pointer and takes no arguments. The returned function returns a type int and has
two parameters of type long and int.
Note that they do not have to be called argc or argv, but this is the common
naming system.
argc is a nonnegative integer. If argc is greater than zero, then the string
pointed to by argv[0] is the name of the program. If argc is greater than one,
then the strings pointed to by argv[1] through argv[argc-1] are the parameters
passed to the program by the system.
Example:
#include<stdio.h>
int main(int argc, char *argv[])
{
int loop;
if(argc>0)
printf("My program name is %s.\n",argv[0]);
if(argc>1)
{
for(loop=1;loop<argc;loop++)
printf("Parameter #%i is %s.\n",loop,argv[loop]);
}
}
The address operator causes the memory address for a variable to be returned.
It is written with an ampersand sign before the variable name.
Causes my_pointer to contain the address of barny. Now the pointer can be use
indirection to reference the variable it points to. Indirection is done by prefixing
an asterisk to the pointer variable.
*my_pointer=3;
This causes the value of barny to be 3. Note that the value of my_pointer is
unchanged.
Then modifying the pointer will correctly increment the pointer so as to point
to the next element in the array.
*(some_pointer+1)=3;
This defines homer_pointer to point to the structure homer. Now, when you use
the pointer to reference something in the structure, the record selector now
becomes -> instead of a period.
homer_pointer->an_element=5;
1.4.2 Typecasting
Typecasting allows a variable to act like a variable of another type. The method
of typecasting is done by prefixing the variable type enclosed by parenthesis
before the variable name. The actual variable is not modified.
Example:
float index=3; int loop=(int)index;
1.5.1 Postfix
Postfix operators are operators that are suffixed to an expression.
operand++;
This causes the value of the operand to be returned. After the result is obtained,
the value of the operand is incremented by 1.
operand--;
This is the same but the value of the operand is decremented by 1.
Examples:
int joe=3;
joe++;
++operand;
This causes the value of the operand to be incremented by 1. Its new value is
then returned.
--operand;
This is the same but the value of the operand is decremented by 1.
!operand
Returns the logical NOT operation on the operand. A true operand returns false,
a false operand returns true. Also known as the bang operand.
~operand
Returns the compliment of the operand. The returned value is the operand with
its bits reversed (1's become 0's, 0's become 1's).
Examples:
int bart=7;
printf("%i",--bart);
This outputs the number 6. The value of bart is now 6.
int lisa=1;
printf("%i",!lisa);
This outputs 0 (false).
1.5.3 Normal
There are several normal operators which return the result defined for each:
expression1+ expression
The result of this is the sum of the two expressions.
expression1- expression2
The result of this is the value of expression2 subtracted from expression1.
expression1* expression2
The result of this is the value of expression1 multiplied by expression2.
expression1/ expression2
The result of this is the value of expression1 divided by expression2.
expression1% expression2
The result of this is the value of the remainder after
dividing expression1 by expression2. Also called the modulo operator.
expression1& expression2
Returns a bitwise AND operation done on expression1 and expression2. The
result is a value the same size as the expressions with its bits modified using the
following rules: Both bits must be 1 (on) to result in 1 (on), otherwise the result
is 0 (off).
e1 e2 Result
0 0 0
0 1 0
1 0 0
1 1 1
expression1| expression2
Returns a bitwise OR operation done on expression1 and expression2. The
result is a value the same size as the expressions with its bits modified using the
following rules: Both bits must be 0 (off) to result in 0 (off), otherwise the
result is 1 (on).
e1 e2 Result
0 0 0
0 1 1
1 0 1
1 1 1
expression1^ expression2
Returns a bitwise XOR operation done on expression1 and expression2. The
result is a value the same size as the expressions with its bits modified using the
following rules: If both bits are the same, then the result is 0 (off), otherwise
the result is 1 (on).
e1 e2 Result
0 0 0
0 1 1
1 0 1
1 1 0
expression1>> shift_value
Returns expression1 with its bits shifted to the right by the shift_value. The
leftmost bits are replaced with zeros if the value is nonnegative or unsigned.
This result is the integer part of expression1 divided by 2 raised to the power
of shift_value. If expression1 is signed, then the result is implementation
specific.
expression1<< shift_value
Returns expression1 with its bits shifted to the left by the shift_value. The
rightmost bits are replaced with zeros. This result is the value
of expression1 multiplied by the value of 2 raised to the power of shift_value.
If expression1 is signed, then the result is implementation specific.
1.5.4 Boolean
The boolean operators return either 1 (true) or 0 (false).
expression1&& expression2
Returns the logical AND operation of expression1 and expression2. The result
is 1 (true) if both expressions are true, otherwise the result is 0 (false).
e1 e2 Result
0 0 0
0 1 0
1 0 0
1 1 1
expression1|| expression2
Returns the logical OR operation of expression1 and expression2. The result is
0 (false) if bother expressions are false, otherwise the result is 1 (true).
e1 e2 Result
0 0 0
0 1 1
1 0 1
1 1 1
expression1< expression2
Returns 1 (true) if expression1 is less than expression2, otherwise the result is 0
(false).
expression1> expression2
Returns 1 (true) if expression1 is greater than expression2, otherwise the result
is 0 (false).
expression1<= expression2
Returns 1 (true) if expression1 is less than or equal to expression2, otherwise
the result is 0 (false).
expression1>= expression2
Returns 1 (true) if expression1 is greater than or equal to expression2,
otherwise the result is 0 (false).
expression1== expression2
Returns 1 (true) if expression1 is equal to expression2, otherwise the result is 0
(false).
expression1!= expression2
Returns 1 (true) if expression1 is not equal to expression2, otherwise the result
is 0 (false).
1.5.5 Assignment
An assignment operator stores the value of the right expression into the left
expression.
expression1= expression2
The value of expression2 is stored in expression1.
expression1*= expression2
The value of expression1 times expression2 is stored in expression1.
expression1/= expression2
The value of expression1 divided by expression2 is stored in expression1.
expression1%= expression2
The value of the remainder of expression1 divided by expression2 is stored
in expression1.
expression1+= expression2
The value of expression1 plus expression2 is stored in expression1.
expression1-= expression2
The value of expression1 minus expression2 is stored in expression1.
expression1<<= shift_value
The value of expression1's bits are shifted to the left by shift_value and stored
in expression1.
expression1>>= shift_value
The value of expression1's bits are shifted to the right by shift_value and stored
in expression1.
expression1&= expression2
The value of the bitwise AND of expression1 and expression2 is stored
in expression1.
e1 e2 Result
0 0 0
0 1 0
1 0 0
1 1 1
expression1^= expression2
The value of the bitwise XOR of expression1 and expression2 is stored
in expression1.
e1 e2 Result
0 0 0
0 1 1
1 0 1
1 1 0
expression1|= expression2
The value of the bitwise OR of expression1 and expression2 is stored
in expression1.
e1 e2 Result
0 0 0
0 1 1
1 0 1
1 1 1
1.5.6 Precedence
The operators have a set order of precedence during evaluation. Items
encapsulated in parenthesis are evaluated first and have the highest precedence.
The following chart shows the order of precedence with the items at the top
having highest precedence.
Operator Name
! Logical NOT. Bang.
++ -- Increment and decrement operators.
*/% Multiplicative operators.
+- Additive operators.
<< >> Shift operators.
< > <= >= Inequality comparators.
== != Equality comparators
& Bitwise AND.
^ Bitwise XOR.
| Bitwise OR.
&& Logical AND.
|| Logical OR.
?: Conditional.
= op= Assignment.
Examples:
17 * 5 + !(1+1) && 0
Evaluates to 0 (false).
5+7<4
Evaluates to 1 (true).
a<b<c
Same as (a<b)<c.
1.6.1 if
The if statement evaluates an expression. If that expression is true, then a
statement is executed. If an else clause is given and if the expression is false,
then the else's statement is executed.
Syntax:
or
Examples:
if(loop<3) counter++;
if(x==y)
x++;
else
y++;
if(z>x)
{
z=5;
x=3;
}
else
{
z=3;
x=5;
}
1.6.2 switch
A switch statement allows a single variable to be compared with several
possible constants. If the variable matches one of the constants, then a
execution jump is made to that point. A constant can not appear more than
once, and there can only be one default expression.
Syntax:
switch ( variable)
{
case const:
statements...;
default:
statements...;
}
Examples:
switch(betty)
{
case 1:
printf("betty=1\n");
case 2:
printf("betty=2\n");
break;
case 3:
printf("betty=3\n");
break;
default:
printf("Not sure.\n");
}
If betty is 1, then two lines are printed: betty=1 and betty=2. If betty is 2, then
only one line is printed: betty=2. If betty=3, then only one line is printed:
betty=3. If betty does not equal 1, 2, or 3, then "Not sure." is printed.
1.6.3 while
The while statement provides an iterative loop.
Syntax:
while(counter<5)
{
printf("counter=%i",counter);
counter++;
}
1.6.4 do
The do...while construct provides an iterative loop.
Syntax:
Examples:
do {
betty++;
printf("%i",betty);
} while (betty<100);
1.6.5 for
The for statement allows for a controlled loop.
Syntax:
Examples:
for(loop=0;loop<1000;loop++)
printf("%i\n",loop);
1.6.6 goto
The goto statement transfers program execution to some label within the
program.
Syntax:
goto label;
....
label:
Examples:
goto skip_point;
printf("This part was skipped.\n");
skip_point:
printf("Hi there!\n");
1.6.7 continue
The continue statement can only appear in a loop body. It causes the rest of the
statement body in the loop to be skipped.
Syntax:
continue;
Examples:
for(loop=0;loop<100;loop++)
{
if(loop==50)
continue;
printf("%i\n",loop);
}
Each number from 0 to 999 is printed 100 times except for the number 500
which is not printed at all.
1.6.8 break
The break statement can only appear in a switch body or a loop body. It causes
the execution of the current enclosing switch or loop body to terminate.
Syntax:
break;
Examples:
switch(henry)
{
case 1: print("Hi!\n");
break;
case 2: break;
}
1.6.9 return
The return statement causes the current function to terminate. It can return a
value to the calling function. A return statement can not appear in a function
whose return type is void. If the value returned has a type different from that of
the function's return type, then the value is converted. Using the return
statement without an expression creates an undefined result. Reaching the } at
the end of the function is the same as returning without an expression.
Syntax:
return expression;
Examples:
int alice(int x, int y)
{
if(x<y)
return(1);
else
return(0);
}
Syntax:
#if constant_expression
#else
#endif
or
#if constant_expression
#elif constant_expression
#endif
The compiler only compiles the code after the #if expression if
the constant_expression evaluates to a non-zero value (true). If the value is 0
(false), then the compiler skips the lines until the next #else, #elif, or #endif.
If there is a matching #else, and the constant_expression evaluated to 0 (false),
then the lines between the #else and the #endif are compiled. If there is a
matching #elif, and the preceding #if evaluated to false, then
the constant_expression after that is evaluated and the code between
the #elif and the #endif is compiled only if this expression evaluates to a non-
zero value (true).
Examples:
int main(void)
{
#if 1
printf("Yabba Dabba Do!\n");
#else
printf("Zip-Bang!\n");
#endif
return 0;
}
Only "Yabba Dabba Do!" is printed.
int main(void)
{
#if 1
printf("Checkpoint1\n");
#elif 1
printf("Checkpoint2\n");
#endif
return 0;
}
Only "Checkpoint1" is printed. Note that if the first line is #if 0, then only
"Checkpoint2" would be printed.
#if OS==1
printf("Version 1.0");
#elif OS==2
printf("Version 2.0");
#else
printf("Version unknown");
#endif
Syntax:
#undef identifier
#ifdef identifier
#else or #elif
#endif
#ifndef identifier
#else or #elif
#endif
Examples:
#define PI 3.141
printf("%f",PI);
#define DEBUG
#ifdef DEBUG
printf("This is a debug message.");
#endif
#define ADD(x, y) x + y
z=3 * ADD(5,6)
This evaluates to 21 due to the fact that multiplication takes precedence over
addition.
#define ADD(x,y) (x + y)
z=3 * ADD(5,6)
1.7.3 #include
The #include directive allows external header files to be processed by the
compiler.
Syntax:
#include <header-file>
or
#include "source-file"
When enclosing the file with < and >, then the implementation searches the
known header directories for the file (which is implementation-defined) and
processes it. When enclosed with double quotation marks, then the entire
contents of the source-file is replaced at this point. The searching manner for
the file is implementation-specific.
Examples:
#include <stdio.h>
#include "my_header.h"
1.7.4 #line
The #line directive allows the current line number and the apparent name of
the current sourcecode filename to be changed.
Syntax:
Note that if the filename is not given, then it stays the same. The line number
on the current line is one greater than the number of new-line characters (so the
first line number is 1).
Examples:
#line 50 user.c
#line 23
1.7.5 #error
The #error directive will cause the compiler to halt compiling and return with
the specified error message.
Syntax:
#error message
Examples:
#ifndef VERSION
#error Version number not specified.
#endif
1.7.6 #pragma
This #pragma directive allows a directive to be defined. Its effects are
implementation-defined. If the pragma is not supported, then it is ignored.
Syntax:
#pragma directive
2.1 assert.h
The assert header is used for debugging purposes.
Macros:
assert();
External References:
NDEBUG
2.1.1 assert
Declaration:
Example:
#include<assert.h>
int main(void)
{
open_record(NULL);
}
2.2 ctype.h
The ctype header is used for testing and converting characters. A control
character refers to a character that is not part of the normal printing set. In the
ASCII character set, the control characters are the characters from 0 (NUL)
through 0x1F (US), and the character 0x7F (DEL). Printable characters are
those from 0x20 (space) to 0x7E (tilde).
Functions:
isalnum();
isalpha();
iscntrl();
isdigit();
isgraph();
islower();
isprint();
ispunct();
isspace();
isupper();
isxdigit();
tolower();
toupper();
Declarations:
Conditions:
Declarations:
Conditions:
Example:
#include<ctype.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
int loop;
char string[]="THIS IS A TEST";
for(loop=0;loop<strlen(string);loop++)
string[loop]=tolower(string[loop]);
printf("%s\n",string);
return 0;
}
2.3 errno.h
The errno header is used as a general error handler.
Macros:
EDOM
ERANGE
Variables:
errno
2.3.1 EDOM
Declaration:
EDOM is an identifier macro declared with #define. Its value represents a domain
error which is returned by some math functions when a domain error occurs.
2.3.2 ERANGE
Declaration:
ERANGE is an identifier macro declared with #define. Its value represents a range
error which is returned by some math functions when a range error occurs.
2.3.3 errno
Declaration:
int errno;
The errno variable has a value of zero at the beginning of the program. If an
error occurs, then this variable is given the value of the error number.
2.4 float.h
The float header defines the minimum and maximum limits of floating-point
number values.
Where sign is plus or minus, value is the value of the number, and exponent is
the value of the exponent.
The following values are defined with the #define directive. These values are
implementation-specific, but may not be any lower than what is given here.
Note that in all instances FLT refers to type float, DBL refers to double,
and LDBL refers to long double.
-1 indeterminable
FLT_ROUNDS 0 toward zero
1 to nearest
2 toward positive infinity
3 toward negative infinity
Defines the base (radix) representation of the exponent (i.e.
FLT_RADIX 2 base-2 is binary, base-10 is the normal decimal
representation, base-16 is Hex).
FLT_MANT_DIG
DBL_MANT_DIG
Defines the number of digits in the number (in
LDBL_MANT_DIG the FLT_RADIX base).
FLT_DIG 6
DBL_DIG 10
The maximum number decimal digits (base-10) that can be
LDBL_DIG 10 represented without change after rounding.
FLT_MIN_EXP
DBL_MIN_EXP
The minimum negative integer value for an exponent in base
LDBL_MIN_EXP FLT_RADIX.
FLT_MIN_10_EXP -
37
DBL_MIN_10_EXP - The minimum negative integer value for an exponent in base
37 10.
LDBL_MIN_10_EXP
-37
FLT_MAX_EXP
DBL_MAX_EXP
The maximum integer value for an exponent in base
LDBL_MAX_EXP FLT_RADIX.
FLT_MAX_10_EXP
+37
DBL_MAX_10_EXP
+37 The maximum integer value for an exponent in base 10.
LDBL_MAX_10_EXP
+37
FLT_MAX 1E+37
DBL_MAX 1E+37 Maximum finite floating-point value.
LDBL_MAX 1E+37
FLT_EPSILON 1E-5
DBL_EPSILON 1E-9
LDBL_EPSILON 1E- Least significant digit representable.
9
FLT_MIN 1E-37
DBL_MIN 1E-37 Minimum floating-point value.
LDBL_MIN 1E-37
2.5 limits.h
The limits header defines the characteristics of variable types.
The following values are defined with the #define directive. These values are
implementation-specific, but may not be any lower than what is given here.
2.6 locale.h
The locale header is useful for setting location specific information.
Variables:
struct lconv
Macros:
NULL
LC_ALL
LC_COLLATE
LC_CTYPE
LC_MONETARY
LC_NUMERIC
LC_TIME
Functions:
localeconv();
setlocale();
The lconv structure contains the following variables in any order. The use of
this structure is described in 2.6.3 localeconv.
char *decimal_point;
char *thousands_sep;
char *grouping;
char *int_curr_symbol;
char *currency_symbol;
char *mon_decimal_point;
char *mon_thousands_sep;
char *mon_grouping;
char *positive_sign;
char *negative_sign;
char int_frac_digits;
char frac_digits;
char p_cs_precedes;
char p_sep_by_space;
char n_cs_precedes;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
The LC_ macros are described in 2.6.2 setlocale. NULL is the value of a null
pointer constant.
2.6.2 setlocale
Declaration:
LC_NUMERIC
Affects decimal-point formatting and the information provided
by localeconv function.
LC_TIME Affects the strftime function.
A value of "C" for locale sets the locale to the normal C translation
environment settings (default). A null value ("") sets the native environment
settings. A null pointer (NULL) causes setlocale to return a pointer to the string
associated with this category for the current settings (no changes occur). All
other values are implementation-specific.
Example:
#include<locale.h>
#include<stdio.h>
int main(void)
{
char *old_locale;
old_locale=setlocale(LC_ALL,"C");
printf("The preivous setting was %s.\n",old_locale);
return 0;
}
2.6.3 localeconv
Declaration:
struct lconv *localeconv(void);
The string pointers in the structure may point to a null string ("") which
indicates that the value is not available. The char types are nonnegative
numbers. If the value is CHAR_MAX, then the value is not available.
lconv variables:
char
*decimal_point Decimal point character used for non-monetary values.
char Thousands place separator character used for non-
*thousands_sep monetary values.
A string that indicates the size of each group of digits in
non-monetary quantities. Each character represents an
char *grouping integer value which designates the number of digits in the
current group. A value of 0 means that the previous value
is to be used for the rest of the groups.
A string of the international currency symbols used. The
char first three characters are those specified by ISO 4217:1987
*int_curr_symbol and the fourth is the character which separates the
currency symbol from the monetary quantity.
char
*currency_symbol The local symbol used for currency.
char
*mon_decimal_point The decimal point character used for monetary values.
char The thousands place grouping character used for monetary
*mon_thousands_sep values.
A string whose elements define the size of the grouping of
digits in monetary values. Each character represents an
char *mon_grouping integer value which designates the number of digits in the
current group. A value of 0 means that the previous value
is to be used for the rest of the groups.
char
*positive_sign The character used for positive monetary values.
char
*negative_sign The character used for negative monetary values.
char Number of digits to show after the decimal point in
int_frac_digits international monetary values.
char frac_digits
Number of digits to show after the decimal point in
monetary values.
If equal to 1, then the currency_symbol appears before a
char p_cs_precedes
positive monetary value. If equal to 0, then
the currency_symbol appears after a positive monetary
value.
If equal to 1, then the currency_symbol is separated by a
char space from a positive monetary value. If equal to 0, then
p_sep_by_space there is no space between the currency_symbol and a
positive monetary value.
If equal to 1, then the currency_symbol precedes a
char n_cs_precedes negative monetary value. If equal to 0, then
the currency_symbol succeeds a negative monetary value.
If equal to 1, then the currency_symbol is separated by a
char space from a negative monetary value. If equal to 0, then
n_sep_by_space there is no space between the currency_symbol and a
negative monetary value.
char p_sign_posn
Represents the position of the positive_sign in a positive
monetary value.
char n_sign_posn
Represents the position of the negative_sign in a negative
monetary value.
Example:
#include<locale.h>
#include<stdio.h>
int main(void)
{
struct lconv locale_structure;
struct lconv *locale_ptr=&locale_structure;
locale_ptr=lcoaleconv();
printf("Decimal point: %s",locale_ptr-
>decimal_point);
printf("Thousands Separator: %s",locale_ptr-
>thousands_sep);
printf("Grouping: %s",locale_ptr->grouping);
printf("International Currency Symbol: %s",locale_ptr-
>int_curr_symbol);
printf("Currency Symbol: %s",locale_ptr-
>currency_symbol);
printf("Monetary Decimal Point: %s",locale_ptr-
>mon_decimal_point);
printf("Monetary Thousands Separator: %s",locale_ptr-
>mon_thousands_sep);
printf("Monetary Grouping: %s",locale_ptr-
>mon_grouping);
printf("Monetary Positive Sign: %s",locale_ptr-
>positive_sign);
printf("Monetary Negative Sign: %s",locale_ptr-
>negative_sign);
printf("Monetary Intl Decimal Digits: %c",locale_ptr-
>int_frac_digits);
printf("Monetary Decimal Digits: %c",locale_ptr-
>frac_digits);
printf("Monetary + Precedes: %c",locale_ptr-
>p_cs_precedes);
printf("Monetary + Space: %c",locale_ptr-
>p_sep_by_space);
printf("Monetary - Precedes: %c",locale_ptr-
>n_cs_precedes);
printf("Monetary - Space: %c",locale_ptr-
>n_sep_by_space);
printf("Monetary + Sign Posn: %c",locale_ptr-
>p_sign_posn);
printf("Monetary - Sign Posn: %c",locale_ptr-
>n_sign_posn);
}
2.7 math.h
The math header defines several mathematic functions.
Macros:
HUGE_VAL
Functions:
acos();
asin();
atan();
atan2();
ceil();
cos();
cosh();
exp();
fabs();
floor();
fmod();
frexp();
ldexp();
log();
log10();
modf();
pow();
sin();
sinh();
sqrt();
tan();
tanh();
In the case that the argument passed to the function exceeds the range of that
function, then the variable errno is set to EDOM. The value that the function
returns is implementation specific.
In the case that the value being returned is too large to be represented in a
double, then the function returns the macro HUGE_VAL, and sets the
variable errno to ERANGE to represent an overflow. If the value is too small to be
represented in a double, then the function returns zero. In this case whether or
not errno is set to ERANGE is implementation specific.
Note that in all cases when it is stated that there is no range limit, it is implied
that the value is limited by the minimum and maximum values of type double.
2.7.2.1 acos
Declaration:
Range:
The value x must be within the range of -1 to +1 (inclusive). The returned value
is in the range of 0 to pi (inclusive).
2.7.2.2 asin
Declaration:
Range:
2.7.2.3 atan
Declaration:
double atan(double x);
Range:
The value of x has no range. The returned value is in the range of -pi/2 to +pi/2
(inclusive).
2.7.2.4 atan2
Declaration:
Returns the arc tangent in radians of y/x based on the signs of both values to
determine the correct quadrant.
Range:
Both y and x cannot be zero. The returned value is in the range of -pi to +pi
(inclusive).
2.7.2.5 cos
Declaration:
Range:
2.7.2.6 cosh
Declaration:
2.7.2.7 sin
Declaration:
Range:
2.7.2.8 sinh
Declaration:
Range:
2.7.2.9 tan
Declaration:
Range:
Declaration:
Range:
2.7.3.1 exp
Declaration:
Range:
2.7.3.2 frexp
Declaration:
Range:
Declaration:
Range:
2.7.3.4 log
Declaration:
Range:
2.7.3.5 log10
Declaration:
Range:
2.7.3.6 modf
Declaration:
Range:
2.7.3.7 pow
Declaration:
Range:
2.7.3.8 sqrt
Declaration:
Range:
2.7.4.1 ceil
Declaration:
2.7.4.2 fabs
Declaration:
Range:
There is no range limit on the argument. The return value is always positive.
2.7.4.3 floor
Declaration:
Range:
2.7.4.4 fmod
Declaration:
Range:
There is no range limit on the return value. If y is zero, then either a range error
will occur or the function will return zero (implementation-defined).
Previous Section | Table of Contents | Index | Next Section
2.7 math.h 2.9 signal.h
2.8 setjmp.h
The setjmp header is used for controlling low-level calls and returns to and
from functions.
Macros:
setjmp();
Functions:
longjmp();
Variables:
typedef jmp_buf
The variable type jmp_buf is an array type used for holding information
for setjmp and longjmp.
2.8.2 setjmp
Declaration:
2.8.3 longjmp
Declaration:
Example:
#include<setjmp.h>
#include<stdio.h>
void some_function(jmp_buf);
int main(void)
{
int value;
jmp_buf environment_buffer;
value=setjmp(environment_buffer);
if(value!=0)
{
printf("Reached this point from a longjmp with value=%d.\n",value);
exit(0);
}
printf("Calling function.\n");
some_function(environment_buffer);
return 0;
}
2.9 signal.h
The signal header provides a means to handle signals reported during a
program's execution.
Macros:
SIG_DFL
SIG_ERR
SIG_IGN
SIGABRT
SIGFPE
SIGILL
SIGINT
SIGSEGV
SIGTERM
Functions:
signal();
raise();
Variables:
typedef sig_atomic_t
The SIG macros are used to represent a signal number in the following
conditions:
SIGABRT Abnormal termination (generated by the abort function).
SIGFPE
Floating-point error (error caused by division by zero, invalid
operation, etc.).
SIGILL Illegal operation (instruction).
SIGINT Interactive attention signal (such as ctrl-C).
SIGSEGV Invalid access to storage (segment violation, memory violation).
SIGTERM Termination request.
2.9.2 signal
Declaration:
Controls how a signal is handled. sig represents the signal number compatible
with the SIG macros. func is the function to be called when the signal occurs. If
func is SIG_DFL, then the default handler is called. If func is SIG_IGN, then the
signal is ignored. If func points to a function, then when a signal is detected the
default function is called (SIG_DFL), then the function is called. The function
must take one argument of type int which represents the signal number. The
function may terminate with return, abort, exit, or longjmp. When the function
terminates execution resumes where it was interrupted (unless it was
a SIGFPE signal in which case the result is undefined).
If the call to signal is successful, then it returns a pointer to the previous signal
handler for the specified signal type. If the call fails, then SIG_ERR is returned
and errno is set appropriately.
2.9.3 raise
Declaration
Example:
#include<signal.h>
#include<stdio.h>
void catch_function(int);
int main(void)
{
if(signal(SIGINT, catch_function)==SIG_ERR)
{
printf("An error occured while setting a signal handler.\n");
exit(0);
}
2.10 stdarg.h
The stdarg header defines several macros used to get the arguments in a
function when the number of arguments is not known.
Macros:
va_start();
va_arg();
va_end();
Variables:
typedef va_list
The va_list type is a type suitable for use in accessing the arguments of a
function with the stdarg macros.
A function of variable arguments is defined with the ellipsis (,...) at the end
of the parameter list.
2.10.2 va_start
Declaration:
Initializes ap for use with the va_arg and va_end macros. last_arg is the last
known fixed argument being passed to the function (the argument before the
ellipsis).
Note that va_start must be called before using va_arg and va_end.
2.10.3 va_arg
Declaration:
typeva_arg(va_list ap, type);
Expands to the next argument in the paramater list of the function with
type type. Note that ap must be initialized with va_start. If there is no next
argument, then the result is undefined.
2.10.4 va_end
Declaration:
Allows a function with variable arguments which used the va_start macro to
return. If va_end is not called before returning from the function, the result is
undefined. The variable argument list ap may no longer be used after a call
to va_end without a call to va_start.
Example:
#include<stdarg.h>
#include<stdio.h>
int main(void)
{
sum("The sum of 10+15+13 is %d.\n",3,10,15,13);
return 0;
}
va_start(ap,num_args);
for(loop=0;loop<num_args;loop++)
sum+=va_arg(ap,int);
printf(string,sum);
va_end(ap);
}
2.11 stddef.h
The stddef header defines several standard definitions. Many of these
definitions also appear in other headers.
Macros:
NULL
offsetof();
Variables:
typedef ptrdiff_t
typedef size_t
typedef wchar_t
offsetof(type, member-designator)
This results in a constant integer of type size_t which is the offset in bytes of a
structure member from the beginning of the structure. The member is given
by member-designator, and the name of the structure is given in type.
Example:
#include<stddef.h>
#include<stdio.h>
int main(void)
{
struct user{
char name[50];
char alias[50];
int level;
};
2.12 stdio.h
The stdio header provides functions for performing input and output.
Macros:
NULL
_IOFBF
_IOLBF
_IONBF
BUFSIZ
EOF
FOPEN_MAX
FILENAME_MAX
L_tmpnam
SEEK_CUR
SEEK_END
SEEK_SET
TMP_MAX
stderr
stdin
stdout
Functions:
clearerr();
fclose();
feof();
ferror();
fflush();
fgetpos();
fopen();
fread();
freopen();
fseek();
fsetpos();
ftell();
fwrite();
remove();
rename();
rewind();
setbuf();
setvbuf();
tmpfile();
tmpnam();
fprintf();
fscanf();
printf();
scanf();
sprintf();
sscanf();
vfprintf();
vprintf();
vsprintf();
fgetc();
fgets();
fputc();
fputs();
getc();
getchar();
gets();
putc();
putchar();
puts();
ungetc();
perror();
Variables:
typedef size_t
typedef FILE
typedef fpos_t
Text streams are composed of lines. Each line has zero or more characters and
are terminated by a new-line character which is the last character in a line.
Conversions may occur on text streams during input and output. Text streams
consist of only printable characters, the tab character, and the new-line
character. Spaces cannot appear before a newline character, although it is
implementation-defined whether or not reading a text stream removes these
spaces. An implementation must support lines of up to at least 254 characters
including the new-line character.
Binary streams input and output data in an exactly 1:1 ratio. No conversion
exists and all characters may be transferred.
When a program begins, there are already three available streams: standard
input, standard output, and standard error.
Files are associated with streams and must be opened to be used. The point of
I/O within a file is determined by the file position. When a file is opened, the
file position points to the beginning of the file unless the file is opened for an
append operation in which case the position points to the end of the file. The
file position follows read and write operations to indicate where the next
operation will occur.
2.12.3.1 clearerr
Declaration:
void clearerr(FILE *stream);
Clears the end-of-file and error indicators for the given stream. As long as the
error indicator is set, all stream operations will return an error
until clearerr or rewind is called.
2.12.3.2 fclose
Declaration:
2.12.3.3 feof
Declaration:
Tests the end-of-file indicator for the given stream. If the stream is at the end-
of-file, then it returns a nonzero value. If it is not at the end of the file, then it
returns zero.
2.12.3.4 ferror
Declaration:
Tests the error indicator for the given stream. If the error indicator is set, then it
returns a nonzero value. If the error indicator is not set, then it returns zero.
2.12.3.5 fflush
Declaration:
Flushes the output buffer of a stream. If stream is a null pointer, then all output
buffers are flushed.
If successful, it returns zero. On error it returns EOF.
2.12.3.6 fgetpos
Declaration:
Gets the current file position of the stream and writes it to pos.
If successful, it returns zero. On error it returns a nonzero value and stores the
error number in the variable errno.
2.12.3.7 fopen
Declaration:
Opens the filename pointed to by filename. The mode argument may be one of
the following constant strings:
If the file is opened with append mode (a), then all write operations occur at the
end of the file regardless of the current file position.
If the file is opened in the update mode (+), then output cannot be directly
followed by input and input cannot be directly followed by output without an
intervening fseek, fsetpos, rewind, or fflush.
2.12.3.8 fread
Declaration:
Reads data from the given stream into the array pointed to by ptr. It
reads nmemb number of elements of size size. The total number of bytes read is
(size*nmemb).
2.12.3.9 freopen
Declaration:
Associates a new filename with the given open stream. The old file in stream is
closed. If an error occurs while closing the file, the error is ignored. The mode
argument is the same as described in the fopen command. Normally used for
reassociating stdin, stdout, or stderr.
2.12.3.10 fseek
Declaration:
int fseek(FILE *stream, long int offset, int whence);
Sets the file position of the stream to the given offset. The
argument offset signifies the number of bytes to seek from the given whence
position. The argument whence can be:
On a text stream, whence should be SEEK_SET and offset should be either zero or
a value returned from ftell.
2.12.3.11 fsetpos
Declaration:
Sets the file position of the given stream to the given position. The
argument pos is a position given by the function fgetpos. The end-of-file
indicator is cleared.
2.12.3.12 ftell
Declaration:
Returns the current file position of the given stream. If it is a binary stream,
then the value is the number of bytes from the beginning of the file. If it is a
text stream, then the value is a value useable by the fseek function to return the
file position to the current position.
On success the current file position is returned. On error a value of -1L is
returned and errno is set.
2.12.3.13 fwrite
Declaration:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
Writes data from the array pointed to by ptr to the given stream. It
writes nmemb number of elements of size size. The total number of bytes
written is (size*nmemb).
2.12.3.14 remove
Declaration:
Deletes the given filename so that it is no longer accessible (unlinks the file). If
the file is currently open, then the result is implementation-defined.
2.12.3.15 rename
Declaration:
On success zero is returned. On error a nonzero value is returned and the file is
still accessible by its old filename.
2.12.3.16 rewind
Declaration:
void rewind(FILE *stream);
Sets the file position to the beginning of the file of the given stream. The error
and end-of-file indicators are reset.
2.12.3.17 setbuf
Declaration:
Defines how a stream should be buffered. This should be called after the stream
has been opened but before any operation has been done on the stream. Input
and output is fully buffered. The default BUFSIZ is the size of the buffer. The
argument buffer points to an array to be used as the buffer. If buffer is a null
pointer, then the stream is unbuffered.
2.12.3.18 setvbuf
Declaration:
Defines how a stream should be buffered. This should be called after the stream
has been opened but before any operation has been done on the stream. The
argument mode defines how the stream should be buffered as follows:
_IOFBF Input and output is fully buffered. If the buffer is empty, an input
operation attempts to fill the buffer. On output the buffer will be
completely filled before any information is written to the file (or the
stream is closed).
_IOLBF Input and output is line buffered. If the buffer is empty, an input
operation attempts to fill the buffer. On output the buffer will be flushed
whenever a newline character is written.
_IONBF Input and output is not buffered. No buffering is performed.
Declaration:
FILE *tmpfile(void);
Creates a temporary file in binary update mode (wb+). The tempfile is removed
when the program terminates or the stream is closed.
2.12.3.20 tmpnam
Declaration:
Generates and returns a valid temporary filename which does not exist. Up
to TMP_MAX different filenames can be generated.
If the argument str is a null pointer, then the function returns a pointer to a
valid filename. If the argument str is a valid pointer to an array, then the
filename is written to the array and a pointer to the same array is returned. The
filename may be up to L_tmpnam characters long.
Declarations:
These functions take the format string specified by the format argument and
apply each following argument to the format specifiers in the string in a left to
right fashion. Each character in the format string is copied to the stream except
for conversion characters which specify a format specifier.
The string commands (sprintf and vsprintf) append a null character to the end
of the string. This null character is not counted in the character count.
A conversion specifier begins with the % character. After the % character come
the following in this order:
Flags:
Width:
The width of the field is specified here with a decimal value. If the value is not
large enough to fill the width, then the rest of the field is padded with spaces
(unless the 0 flag is specified). If the value overflows the width of the field,
then the field is expanded to fit the value. If a * is used in place of the width
specifer, then the next argument (which must be an int type) specifies the
width of the field. Note: when using the * with the width and/or precision
specifier, the width argument comes first, then the precision argument, then the
value to be converted.
Precision:
The precision begins with a dot (.) to distinguish itself from the width specifier.
The precision can be given as a decimal value or as an asterisk (*). If a * is
used, then the next argument (which is an int type) specifies the precision.
Note: when using the * with the width and/or precision specifier, the width
argument comes first, then the precision argument, then the value to be
converted. Precision does not affect the c type.
[.precision] Result
(none) Default precision values:
1 for d, i, o, u, x, X types. The minimum number of digits to appear.
6 for f, e, E types. Specifies the number of digits after the decimal
point.
For g or G types all significant digits are shown.
For s type all characters in string are print up to but not including
the null character.
. or .0 For d, i, o, u, x, X types the default precision value is used unless
the value is zero in which case no characters are printed.
For f, e, E types no decimal point character or digits are printed.
For g or G types the precision is assumed to be 1.
.n For d, i, o, u, x, X types then at least n digits are printed (padding
with zeros if necessary).
For f, e, E types specifies the number of digits after the decimal
point.
For g or G types specifies the number of significant digits to print.
For s type specifies the maximum number of characters to print.
Modifier:
A modifier changes the way a conversion specifier type is interpreted.
[type] Output
d, i Type signed int.
o Type unsigned int printed in octal.
u Type unsigned int printed in decimal.
x Type unsigned int printed in hexadecimal as dddd using a, b, c, d, e, f.
X Type unsigned int printed in hexadecimal as dddd using A, B, C, D, E,
F.
f Type double printed as [-]ddd.ddd.
e, E Type double printed as [-]d.ddde[+-]dd where there is one digit printed
before the decimal (zero only if the value is zero). The exponent
contains at least two digits. If type is E then the exponent is printed with
a capital E.
g, G Type double printed as type e or E if the exponent is less than -4 or
greater than or equal to the precision. Otherwise printed as type f.
Trailing zeros are removed. Decimal point character appears only if
there is a nonzero decimal digit.
c Type char. Single character is printed.
s Type pointer to array. String is printed according to precision (no
precision prints entire string).
p Prints the value of a pointer (the memory location it holds).
n The argument must be a pointer to an int. Stores the number of
characters printed thus far in the int. No characters are printed.
% A % sign is printed.
Declarations:
These functions take input in a manner that is specified by the format argument
and store each input field into the following arguments in a left to right fashion.
Each input field is specified in the format string with a conversion specifier
which specifies how the input is to be stored in the appropriate variable. Other
characters in the format string specify characters that must be matched from the
input, but are not stored in any of the following arguments. If the input does not
match then the function stops scanning and returns. A whitespace character
may match with any whitespace character (space, tab, carriage return, new line,
vertical tab, or formfeed) or the next incompatible character.
Assignment suppressor:
Causes the input field to be scanned but not stored in a variable.
Width:
The maximum width of the field is specified here with a decimal value. If the
input is smaller than the width specifier (i.e. it reaches a nonconvertible
character), then what was read thus far is converted and stored in the variable.
Modifier:
A modifier changes the way a conversion specifier type is interpreted.
[type] Input
d Type signed int represented in base 10. Digits 0 through 9 and the
sign (+ or -).
i Type signed int. The base (radix) is dependent on the first two
characters. If the first character is a digit from 1 to 9, then it is base
10. If the first digit is a zero and the second digit is a digit from 1 to
7, then it is base 8 (octal). If the first digit is a zero and the second
character is an x or X, then it is base 16 (hexadecimal).
o Type unsigned int. The input must be in base 8 (octal). Digits 0
through 7 only.
u Type unsigned int. The input must be in base 10 (decimal). Digits 0
through 9 only.
x, X Type unsigned int. The input must be in base 16 (hexadecimal).
Digits 0 through 9 or A through Z or a through z. The characters 0x
or 0X may be optionally prefixed to the value.
e, E, f, g, G Type float. Begins with an optional sign. Then one or more digits,
followed by an optional decimal-point and decimal value. Finally
ended with an optional signed exponent value designated with an e
or E.
s Type character array. Inputs a sequence of non-whitespace
characters (space, tab, carriage return, new line, vertical tab, or
formfeed). The array must be large enough to hold the sequence plus
a null character appended to the end.
[...] Type character array. Allows a search set of characters. Allows
input of only those character encapsulated in the brackets (the
scanset). If the first character is a caret (^), then the scanset is
inverted and allows any ASCII character except those specified
between the brackets. On some systems a range can be specified
with the dash character (-). By specifying the beginning character, a
dash, and an ending character a range of characters can be included
in the scanset. A null character is appended to the end of the array.
c Type character array. Inputs the number of characters specified in
the width field. If no width field is specified, then 1 is assumed. No
null character is appended to the array.
p Pointer to a pointer. Inputs a memory address in the same fashion of
the %p type produced by the printf function.
n The argument must be a pointer to an int. Stores the number of
characters read thus far in the int. No characters are read from the
input stream.
% Requires a matching % sign from the input.
On success the number of input fields converted and stored are returned. If an
input failure occurred, then EOF is returned.
2.12.5.1 fgetc
Declaration:
int fgetc(FILE *stream);
Gets the next character (an unsigned char) from the specified stream and
advances the position indicator for the stream.
2.12.5.2 fgets
Declaration:
Reads a line from the specified stream and stores it into the string pointed to
by str. It stops when either (n-1) characters are read, the newline character is
read, or the end-of-file is reached, whichever comes first. The newline
character is copied to the string. A null character is appended to the end of the
string.
2.12.5.3 fputc
Declaration:
Writes a character (an unsigned char) specified by the argument char to the
specified stream and advances the position indicator for the stream.
On success the character is returned. If an error occurs, the error indicator for
the stream is set and EOF is returned.
2.12.5.4 fputs
Declaration:
2.12.5.5 getc
Declaration:
Gets the next character (an unsigned char) from the specified stream and
advances the position indicator for the stream.
2.12.5.6 getchar
Declaration:
int getchar(void);
2.12.5.7 gets
Declaration:
Reads a line from stdin and stores it into the string pointed to by str. It stops
when either the newline character is read or when the end-of-file is reached,
whichever comes first. The newline character is not copied to the string. A null
character is appended to the end of the string.
On success a pointer to the string is returned. On error a null pointer is
returned. If the end-of-file occurs before any characters have been read, the
string remains unchanged.
2.12.5.8 putc
Declaration:
Writes a character (an unsigned char) specified by the argument char to the
specified stream and advances the position indicator for the stream.
On success the character is returned. If an error occurs, the error indicator for
the stream is set and EOF is returned.
2.12.5.9 putchar
Declaration:
On success the character is returned. If an error occurs, the error indicator for
the stream is set and EOF is returned.
2.12.5.10 puts
Declaration:
Writes a string to stdout up to but not including the null character. A newline
character is appended to the output.
Declaration:
Pushes the character char (an unsigned char) onto the specified stream so that
the this is the next character read. The functions fseek, fsetpos,
and rewind discard any characters pushed onto the stream.
Multiple characters pushed onto the stream are read in a FIFO manner (first in,
first out).
2.12.7.1 perror
Declaration:
Prints a descriptive error message to stderr. First the string str is printed
followed by a colon then a space. Then an error message based on the current
setting of the variable errno is printed.
2.13 stdlib.h
The stdlib header defines several general operation functions and macros.
Macros:
NULL
EXIT_FAILURE
EXIT_SUCCESS
RAND_MAX
MB_CUR_MAX
Variables:
typedef size_t
typedef wchar_t
struct div_t
struct ldiv_t
Functions:
abort();
abs();
atexit();
atof();
atoi();
atol();
bsearch();
calloc();
div();
exit();
free();
getenv();
labs();
ldiv();
malloc();
mblen();
mbstowcs();
mbtowc();
qsort();
rand();
realloc();
srand();
strtod();
strtol();
strtoul();
system();
wcstombs();
wctomb();
2.13.2.1 atof
Declaration:
2.13.2.2 atoi
Declaration:
int atoi(const char *str);
The string pointed to by the argument str is converted to an integer (type int).
Any initial whitespace characters are skipped (space, tab, carriage return, new
line, vertical tab, or formfeed). The number may consist of an optional sign and
a string of digits. Conversion stops when the first unrecognized character is
reached.
2.13.2.3 atol
Declaration:
2.13.2.4 strtod
Declaration:
The argument endptr is a pointer to a pointer. The address of the character that
stopped the scan is stored in the pointer that endptr points to.
On success the converted number is returned. If no conversion can be made,
zero is returned. If the value is out of range of the type double, then HUGE_VAL is
returned with the appropriate sign and ERANGE is stored in the variable errno. If
the value is too small to be returned in the type double, then zero is returned
and ERANGE is stored in the variable errno.
2.13.2.5 strtol
Declaration:
If the base (radix) argument is zero, then the conversion is dependent on the
first two characters. If the first character is a digit from 1 to 9, then it is base 10.
If the first digit is a zero and the second digit is a digit from 1 to 7, then it is
base 8 (octal). If the first digit is a zero and the second character is an x or X,
then it is base 16 (hexadecimal).
If the base argument is from 2 to 36, then that base (radix) is used and any
characters that fall outside of that base definition are considered unconvertible.
For base 11 to 36, the characters A to Z (or a to z) are used. If the base is 16,
then the characters 0x or 0X may precede the number.
The argument endptr is a pointer to a pointer. The address of the character that
stopped the scan is stored in the pointer that endptr points to.
2.13.2.6 strtoul
Declaration:
unsigned long int strtoul(const char *str, char **endptr, int base);
The string pointed to by the argument str is converted to an unsigned long
integer (type unsigned long int). Any initial whitespace characters are skipped
(space, tab, carriage return, new line, vertical tab, or formfeed). The number
may consist of an optional sign and a string of digits. Conversion stops when
the first unrecognized character is reached.
If the base (radix) argument is zero, then the conversion is dependent on the
first two characters. If the first character is a digit from 1 to 9, then it is base 10.
If the first digit is a zero and the second digit is a digit from 1 to 7, then it is
base 8 (octal). If the first digit is a zero and the second character is an x or X,
then it is base 16 (hexadecimal).
If the base argument is from 2 to 36, then that base (radix) is used and any
characters that fall outside of that base definition are considered unconvertible.
For base 11 to 36, the characters A to Z (or a to z) are used. If the base is 16,
then the characters 0x or 0X may precede the number.
The argument endptr is a pointer to a pointer. The address of the character that
stopped the scan is stored in the pointer that endptr points to.
2.13.3.1 calloc
Declaration:
Allocates the requested memory and returns a pointer to it. The requested size
is nitems each size bytes long (total memory requested is nitems*size). The
space is initialized to all zero bits.
2.13.3.2 free
Declaration:
void free(void *ptr);
No value is returned.
2.13.3.3 malloc
Declaration:
Allocates the requested memory and returns a pointer to it. The requested size
is size bytes. The value of the space is indeterminate.
2.13.3.4 realloc
Declaration:
Attempts to resize the memory block pointed to by ptr that was previously
allocated with a call to malloc or calloc. The contents pointed to by ptr are
unchanged. If the value of size is greater than the previous size of the block,
then the additional bytes have an undeterminate value. If the value of size is
less than the previous size of the block, then the difference of bytes at the end
of the block are freed. If ptr is null, then it behaves like malloc. If ptr points to a
memory block that was not allocated with calloc or malloc, or is a space that
has been deallocated, then the result is undefined. If the new space cannot be
allocated, then the contents pointed to by ptr are unchanged. If size is zero, then
the memory block is completely freed.
2.13.4.1 abort
Declaration:
void abort(void);
No return is possible.
2.13.4.2 atexit
Declaration:
2.13.4.3 exit
Declaration:
No return is possible.
2.13.4.4 getenv
Declaration:
Searches for the environment string pointed to by name and returns the
associated value to the string. This returned value should not be written to.
If the string is found, then a pointer to the string's associated value is returned.
If the string is not found, then a null pointer is returned.
2.13.4.5 system
Declaration:
If string is a null pointer and the command processor exists, then zero is
returned. All other return values are implementation-defined.
Declaration:
The array must be arranged so that elements that compare less than key are
first, elements that equal key are next, and elements that are greater than key
are last.
2.13.5.2 qsort
Declaration:
Sorts an array. The beginning of the array is pointed to by base. The array
is nitems long with each element in the array size bytes long.
The elements are sorted in ascending order according to the compar function.
This function takes two arguments. These arguments are two elements being
compared. This function must return less than zero if the first argument is less
than the second. It must return zero if the first argument is equal to the second.
It must return greater than zero if the first argument is greater than the second.
If multiple elements are equal, the order they are sorted in the array is
unspecified.
No value is returned.
Example:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
char string_array[10][50]={"John",
"Jane",
"Mary",
"Rogery",
"Dave",
"Paul",
"Beavis",
"Astro",
"George",
"Elroy"};
return 0;
}
Declaration:
Returns the absolute value of x. Note that in two's compliment that the most
maximum number cannot be represented as a positive number. The result in
this case is undefined.
2.13.6.2 div
Declaration:
Where quot is the quotient and rem is the remainder. In the case of inexact
division, quot is rounded down to the nearest integer. The value numer is equal
to quot * denom + rem.
Declaration:
Returns the absolute value of x. Not that in two's compliment that the most
maximum number cannot be represented as a positive number. The result in
this case is undefined.
2.13.6.4 ldiv
Declaration:
Where quot is the quotient and rem is the remainder. In the case of inexact
division, quot is rounded down to the nearest integer. The value numer is equal
to quot * denom + rem.
2.13.6.5 rand
Declaration:
int rand(void);
2.13.6.6 srand
Declaration:
void srand(unsigned int seed);
This function seeds the random number generator used by the function rand.
Seeding srand with the same seed will cause rand to return the same sequence
of pseudo-random numbers. If srand is not called, rand acts as if srand(1) has
been called.
No value is returned.
2.13.7.1 mblen
Declaration:
If str is a null pointer, then zero is returned if multibyte characters are not state-
dependent (shift state). Otherwise a nonzero value is returned if multibyte
character are state-dependent.
If str is not null, then the number of bytes that are contained in the multibyte
character pointed to by str are returned. Zero is returned if str points to a null
character. A value of -1 is returned if str does not point to a valid multibyte
character.
2.13.7.2 mbstowcs
Declaration:
Converts the string of multibyte characters pointed to by the argument str to the
array pointed to by pwcs. It stores no more than n values into the array.
Conversion stops when it reaches the null character or n values have been
stored. The null character is stored in the array as zero but is not counted in the
return value.
2.13.7.3 mbtowc
Declaration:
Examines the multibyte character pointed to by the argument str. The value is
converted and stored in the argument pwc if pwc is not null. It scans at
most n bytes.
If str is a null pointer, then zero is returned if multibyte characters are not state-
dependent (shift state). Otherwise a nonzero value is returned if multibyte
character are state-dependent.
If str is not null, then the number of bytes that are contained in the multibyte
character pointed to by str are returned. Zero is returned if str points to a null
character. A value of -1 is returned if str does not point to a valid multibyte
character.
2.13.7.4 wcstombs
Declaration:
Converts the codes stored in the array pwcs to multibyte characters and stores
them in the string str. It copies at most n bytes to the string. If a multibyte
character overflows the n constriction, then none of that multibyte character's
bytes are copied. Conversion stops when it reaches the null character or n bytes
have been written to the string. The null character is stored in the string, but is
not counted in the return value.
Declaration:
If str is a null pointer, then zero is returned if multibyte characters are not state-
dependent (shift state). Otherwise a nonzero value is returned if multibyte
character are state-dependent.
If str is not null, then the number of bytes that are contained in the multibyte
character wchar are returned. A value of -1 is returned if wchar is not a valid
multibyte character.
2.14 string.h
The string header provides many functions useful for manipulating strings
(character arrays).
Macros:
NULL
Variables:
typedef size_t
Functions:
memchr();
memcmp();
memcpy();
memmove();
memset();
strcat();
strncat();
strchr();
strcmp();
strncmp();
strcoll();
strcpy();
strncpy();
strcspn();
strerror();
strlen();
strpbrk();
strrchr();
strspn();
strstr();
strtok();
strxfrm();
Declaration:
Searches for the first occurrence of the character c (an unsigned char) in the
first n bytes of the string pointed to by the argument str.
2.14.3 memcmp
Declaration:
Compares the first n bytes of str1 and str2. Does not stop comparing even after
the null character (it always checks n characters).
Returns zero if the first n bytes of str1 and str2 are equal. Returns less than zero
or greater than zero if str1 is less than or greater than str2 respectively.
2.14.4 memcpy
Declaration:
Copies n characters from str2 to str1. If str1 and str2 overlap the behavior is
undefined.
2.14.5 memmove
Declaration:
2.14.6 memset
Declaration:
Copies the character c (an unsigned char) to the first n characters of the string
pointed to by the argument str.
2.14.7 strcat
Declaration:
Appends the string pointed to by str2 to the end of the string pointed to by str1.
The terminating null character of str1 is overwritten. Copying stops once the
terminating null character of str2 is copied. If overlapping occurs, the result is
undefined.
2.14.8 strncat
Declaration:
Appends the string pointed to by str2 to the end of the string pointed to
by str1 up to n characters long. The terminating null character of str1 is
overwritten. Copying stops once n characters are copied or the terminating null
character of str2 is copied. A terminating null character is always appended
to str1. If overlapping occurs, the result is undefined.
Declaration:
Searches for the first occurrence of the character c (an unsigned char) in the
string pointed to by the argument str. The terminating null character is
considered to be part of the string.
2.14.10 strcmp
Declaration:
Returns zero if str1 and str2 are equal. Returns less than zero or greater than
zero if str1 is less than or greater than str2 respectively.
2.14.11 strncmp
Declaration:
Compares at most the first n bytes of str1 and str2. Stops comparing after the
null character.
Returns zero if the first n bytes (or null terminated length) of str1 and str2 are
equal. Returns less than zero or greater than zero if str1 is less than or greater
than str2 respectively.
2.14.12 strcoll
Declaration:
Returns zero if str1 and str2 are equal. Returns less than zero or greater than
zero if str1 is less than or greater than str2 respectively.
2.14.13 strcpy
Declaration:
Copies the string pointed to by str2 to str1. Copies up to and including the null
character of str2. If str1 and str2 overlap the behavior is undefined.
2.14.14 strncpy
Declaration:
2.14.15 strcspn
Declaration:
Finds the first sequence of characters in the string str1 that does not contain any
character specified in str2.
Returns the length of this first sequence of characters found that do not match
with str2.
2.14.16 strerror
Declaration:
Searches an internal array for the error number errnum and returns a pointer to
an error message string.
2.14.17 strlen
Declaration:
Computes the length of the string str up to but not including the terminating
null character.
2.14.18 strpbrk
Declaration:
Finds the first character in the string str1 that matches any character specified
in str2.
Example:
#include<string.h>
#include<stdio.h>
int main(void)
{
char string[]="Hi there, Chip!";
char *string_ptr;
while((string_ptr=strpbrk(string," "))!=NULL)
*string_ptr='-';
The output should result in every space in the string being converted to a dash
(-).
2.14.19 strrchr
Declaration:
Searches for the last occurrence of the character c (an unsigned char) in the
string pointed to by the argument str. The terminating null character is
considered to be part of the string.
2.14.20 strspn
Declaration:
Finds the first sequence of characters in the string str1 that contains any
character specified in str2.
Returns the length of this first sequence of characters found that match
with str2.
Example:
#include<string.h>
#include<stdio.h>
int main(void)
{
char string[]="7803 Elm St.";
return 0;
}
2.14.21 strstr
Declaration:
Finds the first occurrence of the entire string str2 (not including the terminating
null character) which appears in the string str1.
Returns a pointer to the first occurrence of str2 in str1. If no match was found,
then a null pointer is returned. If str2 points to a string of zero length, then the
argument str1 is returned.
2.14.22 strtok
Declaration:
Breaks string str1 into a series of tokens. If str1 and str2 are not null, then the
following search sequence begins. The first character in str1 that does not occur
in str2 is found. If str1 consists entirely of characters specified in str2, then no
tokens exist and a null pointer is returned. If this character is found, then this
marks the beginning of the first token. It then begins searching for the next
character after that which is contained in str2. If this character is not found,
then the current token extends to the end of str1. If the character is found, then
it is overwritten by a null character, which terminates the current token. The
function then saves the following position internally and returns.
Subsequent calls with a null pointer for str1 will cause the previous position
saved to be restored and begins searching from that point. Subsequent calls
may use a different value for str2 each time.
Returns a pointer to the first token in str1. If no token is found then a null
pointer is returned.
Example:
#include<string.h>
#include<stdio.h>
int main(void)
{
char search_string[]="Woody Norm Cliff";
char *array[50];
int loop;
array[0]=strtok(search_string," ");
if(array[0]==NULL)
{
printf("No test to search.\n");
exit(0);
}
for(loop=1;loop<50;loop++)
{
array[loop]=strtok(NULL," ");
if(array[loop]==NULL)
break;
}
for(loop=0;loop<50;loop++)
{
if(array[loop]==NULL)
break;
printf("Item #%d is %s.\n",loop,array[loop]);
}
return 0;
}
This program replaces each space into a null character and stores a pointer to
each substring into the array. It then prints out each item.
2.14.23 strxfrm
Declaration:
Transforms the string str2 and places the result into str1. It copies at
most n characters into str1 including the null terminating character. The
transformation occurs such that strcmp applied to two separate converted
strings returns the same value as strcoll applied to the same two strings. If
overlapping occurs, the result is undefined.
Returns the length of the transformed string (not including the null character).
Previous Section | Table of Contents | Index | Next Section
2.14 string.h Index
2.15 time.h
The time header provides several functions useful for reading and converting
the current time and date. Some functions behavior is defined by
the LC_TIME category of the location setting.
Macros:
NULL
CLOCKS_PER_SEC
Variables:
typedef size_t
typedef clock_t
typedef time_t
struct tm
Functions:
asctime();
clock();
ctime();
difftime();
gmtime();
localtime();
mktime();
strftime();
time();
Note that tm_sec may go as high as 61 to allow for up to two leap seconds.
2.15.2 asctime
Declaration:
Returns a pointer to a string which represents the day and time of the
structure timeptr. The string is in the following format:
DDD MMM dd hh:mm:ss YYYY
DDD Day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat)
MMM
Month of the year (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov,
Dec)
dd Day of the month (1,...,31)
hh Hour (0,...,23)
mm Minute (0,...,59)
ss Second (0,...,59)
YYYY Year
The string is terminated with a newline character and a null character. The
string is always 26 characters long (including the terminating newline and null
characters).
A pointer to the string is returned.
Example:
#include<time.h>
#include<stdio.h>
int main(void)
{
time_t timer;
timer=time(NULL);
printf("The current time is %s.\n",asctime(localtime(&timer)));
return 0;
}
2.15.3 clock
Declaration:
clock_t clock(void);
Example:
#include<time.h>
#include<stdio.h>
int main(void)
{
clock_t ticks1, ticks2;
ticks1=clock();
ticks2=ticks1;
while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC)<1)
ticks2=clock();
Declaration:
Returns a string representing the localtime based on the argument timer. This is
equivalent to:
asctime(locatime(timer));
DDD Day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat)
MMM
Month of the year (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov,
Dec)
dd Day of the month (1,...,31)
hh Hour (0,...,23)
mm Minute (0,...,59)
ss Second (0,...,59)
YYYY Year
The string is terminated with a newline character and a null character. The
string is always 26 characters long (including the terminating newline and null
characters).
2.15.5 difftime
Declaration:
Declaration:
2.15.7 localtime
Declaration:
The value of timer is broken up into the structure tm and expressed in the local
time zone.
Example:
#include<time.h>
#include<stdio.h>
int main(void)
{
time_t timer;
timer=time(NULL);
printf("The current time is %s.\n",asctime(localtime(&timer)));
return 0;
}
2.15.8 mktime
Declaration:
Example:
#include<time.h>
#include<stdio.h>
int main(void)
{
struct tm time_struct;
char days[7][4]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_struct.tm_year=2001-1900;
time_struct.tm_mon=0;
time_struct.tm_mday=1;
time_struct.tm_sec=0;
time_struct.tm_min=0;
time_struct.tm_hour=0;
time_struct.tm_isdst=-1;
if(mktime(&time_struct)==-1)
{
printf("Error getting time.\n");
exit(0);
}
2.15.9 strftime
Declaration:
size_t strftime(char *str, size_t maxsize, const char *format, const
struct tm *timeptr);
All characters in the format string are copied to the str string, including the
terminating null character, except for conversion characters. A conversion
character begins with the % sign and is followed by another character which
defines a special value that it is to be replaced by.
Conversion
What it is replaced by
Character
%a abbreviated weekday name
%A full weekday name
%b abbreviated month name
%B full month name
%c appropriate date and time representation
%d day of the month (01-31)
%H hour of the day (00-23)
%I hour of the day (01-12)
%j day of the year (001-366)
%m month of the year (01-12)
%M minute of the hour (00-59)
%p AM/PM designator
%S second of the minute (00-61)
%U
week number of the year where Sunday is the first day of week 1
(00-53)
%w weekday where Sunday is day 0 (0-6)
%W
week number of the year where Monday is the first day of week 1
(00-53)
%x appropriate date representation
%X appropriate time representation
%y year without century (00-99)
%Y year with century
%Z
time zone (possibly abbreviated) or no characters if time zone
isunavailable
%% %
Returns the number of characters stored into str not including the terminating
null character. On error zero is returned.
2.15.10 time
Declaration:
Calculates the current calender time and encodes it into time_t format.
The time_t value is returned. If timer is not a null pointer, then the value is also
stored into the object it points to. If the time is unavailable, then -1 is returned.
[ # | __ | _ | A | B | C | D | E | F | G | H | I | J | L | M | N | O | P | Q | R | S | T | U |
V|W]
Table of Contents
_IOFBF stdio.h
_IOLBF stdio.h
_IONBF stdio.h
abort() stdlib.h
abs() stdlib.h
acos() math.h
asctime() time.h
asin() math.h
assert() assert.h
atan() math.h
atan2() math.h
atexit() stdlib.h
atof() stdlib.h
atoi() stdlib.h
atol() stdlib.h
auto Identifiers
break Statements
bsearch() stdlib.h
BUFSIZ stdio.h
calloc() stdlib.h
ceil() math.h
char Identifiers
clearerr() stdio.h
clock() time.h
clock_t time.h
CLOCKS_PER_SEC time.h
const Identifiers
continue Statements
cos() math.h
cosh() math.h
ctime() time.h
difftime() time.h
div() stdlib.h
div_t stdlib.h
do Statements
double Identifiers
EDOM errno.h
enum Identifiers
EOF stdio.h
ERANGE errno.h
errno errno.h
exit() stdlib.h
EXIT_FAILURE stdlib.h
EXIT_SUCCESS stdlib.h
exp() math.h
extern Identifiers
fabs() math.h
fclose() stdio.h
feof() stdio.h
ferror() stdio.h
fflush() stdio.h
fgetc() stdio.h
fgetpos() stdio.h
fgets() stdio.h
FILE stdio.h
FILENAME_MAX stdio.h
float Identifiers
floor() math.h
fmod() math.h
fopen() stdio.h
FOPEN_MAX stdio.h
for Statements
fpos_t stdio.h
fprintf() stdio.h
fputc() stdio.h
fputs() stdio.h
fread() stdio.h
free() stdlib.h
freopen() stdio.h
frexp() math.h
fscanf() stdio.h
fseek() stdio.h
fsetpos() stdio.h
ftell() stdio.h
fwrite() stdio.h
getc() stdio.h
getchar() stdio.h
getenv() stdlib.h
gets() stdio.h
gmtime() time.h
goto Statements
HUGE_VAL math.h
if Statements
isalnum() ctype.h
isalpha() ctype.h
iscntrl() ctype.h
isdigit() ctype.h
isgraph() ctype.h
islower() ctype.h
isprint() ctype.h
ispunct() ctype.h
isspace() ctype.h
isupper() ctype.h
isxdigit() ctype.h
jmp_buf setjmp.h
L_tmpnam stdio.h
labs() stdlib.h
LC_ALL locale.h
LC_COLLATE locale.h
LC_CTYPE locale.h
LC_MONETARY locale.h
LC_NUMERIC locale.h
LC_TIME locale.h
ldexp() math.h
ldiv() stdlib.h
ldiv_t stdlib.h
linkage Identifiers
localeconv() locale.h
localtime() time.h
log() math.h
log10() math.h
long Identifiers
longjmp() setjmp.h
malloc() stdlib.h
MB_CUR_MAX stdlib.h
mblen() stdlib.h
mbstowcs() stdlib.h
mbtowc() stdlib.h
memchr() string.h
memcmp() string.h
memcpy() string.h
memmove() string.h
memset() string.h
mktime() time.h
modf() math.h
NDEBUG assert.h
NULL time.h string.h stdlib.h stdio.h stddef.h locale.h
offsetof() stddef.h
perror() stdio.h
pow() math.h
printf() stdio.h
ptrdiff_t stddef.h
putc() stdio.h
putchar() stdio.h
puts() stdio.h
qsort() stdlib.h
raise() signal.h
rand() stdlib.h
RAND_MAX stdlib.h
realloc() stdlib.h
register Identifiers
remove() stdio.h
rename() stdio.h
return Statements
rewind() stdio.h
scanf() stdio.h
scope Identifiers
SEEK_CUR stdio.h
SEEK_END stdio.h
SEEK_SET stdio.h
setbuf() stdio.h
setjmp() setjmp.h
setlocale() locale.h
setvbuf() stdio.h
short Identifiers
sig_atomic_t signal.h
SIG_DFL signal.h
SIG_ERR signal.h
SIG_IGN signal.h
SIGABRT signal.h
SIGFPE signal.h
SIGILL signal.h
SIGINT signal.h
signed Identifiers
SIGSEGV signal.h
SIGTERM signal.h
signal() signal.h
sin() math.h
sinh() math.h
size_t time.h string.h stdlib.h stdio.h stddef.h
sizeof Identifiers
sprintf() stdio.h
sqrt() math.h
srand() stdlib.h
sscanf() stdio.h
static Identifiers
stderr stdio.h
stdin stdio.h
stdout stdio.h
strcat() string.h
strncat() string.h
strchr() string.h
strcmp() string.h
strncmp() string.h
strcoll() string.h
strcpy() string.h
strncpy() string.h
strcspn() string.h
strerror() string.h
strftime() time.h
strlen() string.h
strpbrk() string.h
strrchr() string.h
strspn() string.h
strstr() string.h
strtod() stdlib.h
strtok() string.h
strtol() stdlib.h
strtoul() stdlib.h
struct Indentifiers
strxfrm() string.h
switch Statements
system() stdlib.h
tan() math.h
tanh() math.h
time() time.h
tm time.h
TMP_MAX stdio.h
tmpfile() stdio.h
tmpnam() stdio.h
tolower() ctype.h
toupper() ctype.h
typedef Identifiers
ungetc() stdio.h
unions Identifiers
va_arg() stdarg.h
va_end() stdarg.h
va_list stdarg.h
va_start() stdarg.h
vfprintf() stdio.h
void Identifiers
vprintf() stdio.h
vsprintf() stdio.h
wcstombs() stdlib.h
wctomb() stdlib.h
wchar_t stdlib.h stddef.h
while Statements