Programming in C++
Programming in C++
Function
PDF generated using the open source mwlib toolkit. See http://code.pediapress.com/ for more information.
PDF generated at: Thu, 14 Jan 2010 09:36:59 UTC
Contents
Articles
C++Programming/Code/Standard C Library 1
C++Programming/Code/Standard C Library/Functions 5
C++Programming/Code/Standard C Library/Functions/abort 9
C++Programming/Code/Standard C Library/Functions/asctime 9
C++Programming/Code/Standard C Library/Functions/assert 10
C++Programming/Code/Standard C Library/Functions/atexit 10
C++Programming/Code/Standard C Library/Functions/atof 11
C++Programming/Code/Standard C Library/Functions/atoi 11
C++Programming/Code/Standard C Library/Functions/atol 12
C++Programming/Code/Standard C Library/Functions/clearerr 12
C++Programming/Code/Standard C Library/Functions/clock 13
C++Programming/Code/Standard C Library/Functions/ctime 13
C++Programming/Code/Standard C Library/Functions/difftime 14
C++Programming/Code/Standard C Library/Functions/exit 14
C++Programming/Code/Standard C Library/Functions/fclose 15
C++Programming/Code/Standard C Library/Functions/feof 15
C++Programming/Code/Standard C Library/Functions/ferror 16
C++Programming/Code/Standard C Library/Functions/fflush 16
C++Programming/Code/Standard C Library/Functions/fgetc 17
C++Programming/Code/Standard C Library/Functions/fgetpos 17
C++Programming/Code/Standard C Library/Functions/fgets 18
C++Programming/Code/Standard C Library/Functions/fopen 18
C++Programming/Code/Standard C Library/Functions/fprintf 19
C++Programming/Code/Standard C Library/Functions/fputc 20
C++Programming/Code/Standard C Library/Functions/fputs 20
C++Programming/Code/Standard C Library/Functions/fread 21
C++Programming/Code/Standard C Library/Functions/freopen 21
C++Programming/Code/Standard C Library/Functions/fscanf 22
C++Programming/Code/Standard C Library/Functions/fseek 22
C++Programming/Code/Standard C Library/Functions/fsetpos 23
C++Programming/Code/Standard C Library/Functions/ftell 23
C++Programming/Code/Standard C Library/Functions/fwrite 24
C++Programming/Code/Standard C Library/Functions/getc 24
C++Programming/Code/Standard C Library/Functions/getchar 25
C++Programming/Code/Standard C Library/Functions/getenv 25
C++Programming/Code/Standard C Library/Functions/gets 26
C++Programming/Code/Standard C Library/Functions/localtime 26
C++Programming/Code/Standard C Library/Functions/mktime 27
C++Programming/Code/Standard C Library/Functions/perror 27
C++Programming/Code/Standard C Library/Functions/printf 28
C++Programming/Code/Standard C Library/Functions/putc 30
C++Programming/Code/Standard C Library/Functions/putchar 31
C++Programming/Code/Standard C Library/Functions/puts 31
C++Programming/Code/Standard C Library/Functions/rand 32
C++Programming/Code/Standard C Library/Functions/rewind 32
C++Programming/Code/Standard C Library/Functions/scanf 33
C++Programming/Code/Standard C Library/Functions/setbuf 34
C++Programming/Code/Standard C Library/Functions/setvbuf 35
C++Programming/Code/Standard C Library/Functions/sprintf 35
C++Programming/Code/Standard C Library/Functions/srand 36
C++Programming/Code/Standard C Library/Functions/sscanf 36
C++Programming/Code/Standard C Library/Functions/strerror 37
C++Programming/Code/Standard C Library/Functions/strftime 37
C++Programming/Code/Standard C Library/Functions/strtod 38
C++Programming/Code/Standard C Library/Functions/system 39
C++Programming/Code/Standard C Library/Functions/time 39
C++Programming/Code/Standard C Library/Functions/ungetc 40
References
Article Sources and Contributors 41
Article Licenses
License 43
C++ Programming/Code/Standard C Library 1
C++Programming/Code/Standard C Library
Standard C Library
The C standard library is a standardized collection of header files and library routines used to implement common
operations, such as input/output and string handling. It became part of the C++ Standard Library as the Standard C
Library in its ANSI C 89 form with some small modifications to make it work better with the C++ language.
For a more in depth look into the C programming language check the C Programming Wikibook but be aware of the
incompatibilities we have already covered on the Comparing C++ with C Section of this book.
Functions Descriptions
clock returns the amount of time that the program has been running
cos cosine
floor returns the largest integer not greater than a given value
ldiv returns the quotient and remainder of a division, in long integer form
sin sine
strpbrk finds the first location of any character in one string, in another string
tan tangent
vprintf, vfprintf, and vsprintf write formatted output with variable argument lists
vscanf, vfscanf, and vsscanf read formatted input with variable argument lists
These routines included on the Standard C Library can be sub divided into:
• Standard C I/O
• Standard C String & Character
• Standard C Math
• Standard C Time & Date
• Standard C Memory
• Other Standard C functions
C++ Programming/Code/Standard C Library/Functions 5
C++Programming/Code/Standard C Library/
Functions
All Standard C Library Functions
Functions Descriptions
clock returns the amount of time that the program has been running
cos cosine
floor returns the largest integer not greater than a given value
C++ Programming/Code/Standard C Library/Functions 6
ldiv returns the quotient and remainder of a division, in long integer form
sin sine
strpbrk finds the first location of any character in one string, in another string
tan tangent
vprintf, vfprintf, and vsprintf write formatted output with variable argument lists
vscanf, vfscanf, and vsscanf read formatted input with variable argument lists
These routines included on the Standard C Library can be sub divided into:
• Standard C I/O
• Standard C String & Character
• Standard C Math
• Standard C Time & Date
• Standard C Memory
• Other Standard C functions
C++ Programming/Code/Standard C Library/Functions/abort 9
C++Programming/Code/Standard C Library/
Functions/abort
abort
The function abort() terminates the current program. Depending on the implementation, the return from the function
can indicate a canceled (e.g. you used the signal() function to catch SIGABRT) or failed abort.
SIGABRT is sent by the process to itself when it calls the abort libc function, defined in cstdlib. The SIGABRT
signal can be caught, but it cannot be blocked; if the signal handler returns then all open streams are closed and
flushed and the program terminates (dumping core if appropriate). This means that the abort call never returns.
Because of this characteristic, it is often used to signal fatal conditions in support libraries, situations where the
current operation cannot be completed but the main program can perform cleanup before exiting. It is also used if an
assertion fails.
Related topics
assert - atexit - exit
C++Programming/Code/Standard C Library/
Functions/asctime
asctime
The function asctime() converts the time in the struct 'ptr' to a character string of the following format:
An example:
Related topics
clock - ctime - difftime - gmtime - localtime - mktime - time
C++ Programming/Code/Standard C Library/Functions/assert 10
C++Programming/Code/Standard C Library/
Functions/assert
assert
The assert() macro is used to test for errors. If exp evaluates to zero, assert() writes information to stderr and exits the
program. If the macro NDEBUG is defined, the assert() macros will be ignored.
Related topics
abort
C++Programming/Code/Standard C Library/
Functions/atexit
atexit
The function atexit() causes the function pointed to by func to be called when the program terminates. You can make
multiple calls to atexit() (at least 32, depending on your compiler) and those functions will be called in reverse order
of their establishment. The return value of atexit() is zero upon success, and non-zero on failure.
Related topics
abort - exit
C++ Programming/Code/Standard C Library/Functions/atof 11
C++Programming/Code/Standard C Library/
Functions/atof
atof
The function atof() converts str into a double, then returns that value. str must start with a valid number, but can be
terminated with any non-numerical character, other than "E" or "e". For example,
x = atof( "42.0is_the_answer" );
C++Programming/Code/Standard C Library/
Functions/atoi
atoi
The atoi() function converts str into an integer, and returns that integer. str should start with a whitespace or some
sort of number, and atoi() will stop reading from str as soon as a non-numerical character has been read. For
example:
int i;
i = atoi( "512" );
i = atoi( "512.035" );
i = atoi( " 512.035" );
i = atoi( " 512+34" );
i = atoi( " 512 bottles of beer on the wall" );
All five of the above assignments to the variable i would result in it being set to 512.
If the conversion cannot be performed, then atoi() will return zero:
Related topics
atof - atol
(Standard C I/O) sprintf
C++ Programming/Code/Standard C Library/Functions/atol 12
C++Programming/Code/Standard C Library/
Functions/atol
atol
The function atol() converts str into a long, then returns that value. atol() will read from str until it finds any
character that should not be in a long. The resulting truncated value is then converted and returned. For example,
x = atol( "1024.0001" );
C++Programming/Code/Standard C Library/
Functions/clearerr
clearerr
The clearerr function resets the error flags and EOF indicator for the given stream. If an error occurs, you can use
perror() or strerror() to figure out which error actually occurred, or read the error from the global variable errno.
Related topics
feof - ferror - perror - strerror
C++ Programming/Code/Standard C Library/Functions/clock 13
C++Programming/Code/Standard C Library/
Functions/clock
clock
The clock() function returns the processor time since the program started, or -1 if that information is unavailable. To
convert the return value to seconds, divide it by CLOCKS_PER_SEC.
Related topics
asctime - ctime - time
C++Programming/Code/Standard C Library/
Functions/ctime
ctime
The ctime() function converts the calendar time time to local time of the format:
asctime( localtime( tp ) );
Related topics
asctime - clock - gmtime - localtime - mktime - time
C++ Programming/Code/Standard C Library/Functions/difftime 14
C++Programming/Code/Standard C Library/
Functions/difftime
difftime
C++Programming/Code/Standard C Library/
Functions/exit
exit
The exit() function stops the program. exit_code is passed on to be the return value of the program, where usually
zero indicates success and non-zero indicates an error.
Related topics
abort - atexit - system
C++ Programming/Code/Standard C Library/Functions/fclose 15
C++Programming/Code/Standard C Library/
Functions/fclose
fclose
The function fclose() closes the given file stream, deallocating any buffers associated with that stream. fclose()
returns 0 upon success, and EOF otherwise.
Related topics
fflush - fopen - freopen - setbuf
C++Programming/Code/Standard C Library/
Functions/feof
feof
The function feof() returns TRUE if the end-of-file was reached, or FALSE otherwise.
Related topics
clearerr - ferror - getc - perror - putc
C++ Programming/Code/Standard C Library/Functions/ferror 16
C++Programming/Code/Standard C Library/
Functions/ferror
ferror
The ferror() function looks for errors with stream, returning zero if no errors have occurred, and non-zero if there is
an error. In case of an error, use perror() to determine which error has occurred.
Related topics
clearerr - feof - perror
C++Programming/Code/Standard C Library/
Functions/fflush
fflush
If the given file stream is an output stream, then fflush() causes the output buffer to be written to the file. If the given
stream is of the input type, the behavior of fflush() depends on the library being used (for example, some libraries
ignore the operation, others report an error, and others clear pending input).
fflush() is useful when either debugging (for example, if a program segfaults before the buffer is sent to the screen),
or it can be used to ensure a partial display of output before a long processing period.
By default, most implementations have stdout transmit the buffer at the end of each line, while stderr is flushed
whenever there is output. This behavior changes if there is a redirection or pipe, where calling fflush(stdout) can
help maintain the flow of output.
Related topics
fclose - fopen - fread - fwrite - getc - putc
C++ Programming/Code/Standard C Library/Functions/fgetc 17
C++Programming/Code/Standard C Library/
Functions/fgetc
fgetc
The fgetc() function returns the next character from stream, or EOF if the end of file is reached or if there is an error.
Related topics
fopen - fputc - fread - fwrite - getc - getchar - gets - putc
C++Programming/Code/Standard C Library/
Functions/fgetpos
fgetpos
The fgetpos() function stores the file position indicator of the given file stream in the given position variable. The
position variable is of type fpos_t (which is defined in cstdio) and is an object that can hold every possible position
in a FILE. fgetpos() returns zero upon success, and a non-zero value upon failure.
Related topics
fseek - fsetpos - ftell
C++ Programming/Code/Standard C Library/Functions/fgets 18
C++Programming/Code/Standard C Library/
Functions/fgets
fgets
The function fgets() reads up to num - 1 characters from the given file stream and dumps them into str. The string
that fgets() produces is always NULL-terminated. fgets() will stop when it reaches the end of a line, in which case str
will contain that newline character. Otherwise, fgets() will stop when it reaches num - 1 characters or encounters the
EOF character. fgets() returns str on success, and NULL on an error.
Related topics
fputs - fscanf - gets - scanf
C++Programming/Code/Standard C Library/
Functions/fopen
fopen
The fopen() function opens a file indicated by fname and returns a stream associated with that file. If there is an
error, fopen() returns NULL. mode is used to determine how the file will be treated (i.e. for input, output, etc)
The mode contains up to three characters. The first character is either "r", "w", or "a", which indicates how the file is
opened. A file opened for reading starts allows input from the beginning of the file. For writing, the file is erased.
For appending, the file is kept and writing to the file will start at the end. The second character is "b", is an optional
flag that opens the file as binary - omitting any conversions from different formats of text. The third character "+" is
an optional flag that allows read and write operations on the file (but the file itself is opened in the same way.
"r" Open a text file for reading "r+" Open a text file for read/write
"w" Create a text file for writing "w+" Create a text file for read/write
"a" Append to a text file "a+" Open a text file for read/write
"rb" Open a binary file for reading "rb+" Open a binary file for read/write
"wb" Create a binary file for writing "wb+" Create a binary file for
read/write
"ab" Append to a binary file "ab+" Open a binary file for read/write
An example:
int ch;
FILE *input = fopen( "stuff", "r" );
ch = getc( input );
C++ Programming/Code/Standard C Library/Functions/fopen 19
Related topics
fclose - fflush - fgetc - fputc - fread - freopen - fseek - fwrite - getc - getchar - setbuf
C++Programming/Code/Standard C Library/
Functions/fprintf
fprintf
The fprintf() function sends information (the arguments) according to the specified format to the file indicated by
stream. fprintf() works just like printf() as far as the format goes. The return value of fprintf() is the number of
characters outputted, or a negative number if an error occurs. An example:
Related topics
fputc - fputs - fscanf - printf - sprintf
C++ Programming/Code/Standard C Library/Functions/fputc 20
C++Programming/Code/Standard C Library/
Functions/fputc
fputc
The function fputc() writes the given character ch to the given output stream. The return value is the character, unless
there is an error, in which case the return value is EOF.
Related topics
fgetc - fopen - fprintf - fread - fwrite - getc - getchar - putc
C++Programming/Code/Standard C Library/
Functions/fputs
fputs
The fputs() function writes an array of characters pointed to by str to the given output stream. The return value is
non-negative on success, and EOF on failure.
Related topics
fgets - fprintf - fscanf - gets - getc - puts
C++ Programming/Code/Standard C Library/Functions/fread 21
C++Programming/Code/Standard C Library/
Functions/fread
fread
The function fread() reads num number of objects (where each object is size bytes) and places them into the array
pointed to by buffer. The data comes from the given input stream. The return value of the function is the number of
things read. You can use feof() or ferror() to figure out if an error occurs.
Related topics
fflush - fgetc - fopen - fputc - fscanf - fwrite - getc
C++Programming/Code/Standard C Library/
Functions/freopen
freopen
The freopen() function is used to reassign an existing stream to a different file and mode. After a call to this function,
the given file stream will refer to fname with access given by mode. The return value of freopen() is the new stream,
or NULL if there is an error.
Related topics
fclose - fopen
C++ Programming/Code/Standard C Library/Functions/fscanf 22
C++Programming/Code/Standard C Library/
Functions/fscanf
fscanf
The function fscanf() reads data from the given file stream in a manner exactly like scanf(). The return value of
fscanf() is the number of variables that are actually assigned values, including zero if there were no matches. EOF is
returned if there was an error reading before the first match.
Related topics
fgets - fprintf - fputs - fread - fwrite - scanf - sscanf
C++Programming/Code/Standard C Library/
Functions/fseek
fseek
The function fseek() sets the file position data for the given stream. The origin value should have one of the
following values (defined in cstdio):
Name Explanation
fseek() returns zero upon success, non-zero on failure. You can use fseek() to move beyond a file, but not before the
beginning. Using fseek() clears the EOF flag associated with that stream.
Related topics
fgetpos - fopen - fsetpos - ftell - rewind
C++ Programming/Code/Standard C Library/Functions/fsetpos 23
C++Programming/Code/Standard C Library/
Functions/fsetpos
fsetpos
The fsetpos() function moves the file position indicator for the given stream to a location specified by the position
object. fpos_t is defined in cstdio. The return value for fsetpos() is zero upon success, non-zero on failure.
Related topics
fgetpos - fseek - ftell
C++Programming/Code/Standard C Library/
Functions/ftell
ftell
The ftell() function returns the current file position for stream, or -1 if an error occurs.
Related topics
fgetpos - fseek - fsetpos
C++ Programming/Code/Standard C Library/Functions/fwrite 24
C++Programming/Code/Standard C Library/
Functions/fwrite
fwrite
The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the
number of objects written.
Related topics
fflush - fgetc - fopen - fputc - fread - fscanf - getc
C++Programming/Code/Standard C Library/
Functions/getc
getc
The getc() function returns the next character from stream, or EOF if the end of file is reached. getc() is identical to
fgetc(). For example:
int ch;
FILE *input = fopen( "stuff", "r" );
ch = getc( input );
while( ch != EOF ) {
printf( "%c", ch );
ch = getc( input );
}
Related topics
feof - fflush - fgetc - fopen - fputc - fgetc - fread - fwrite - putc - ungetc
C++ Programming/Code/Standard C Library/Functions/getchar 25
C++Programming/Code/Standard C Library/
Functions/getchar
getchar
The getchar() function returns the next character from stdin, or EOF if the end of file is reached.
Related topics
fgetc - fopen - fputc - putc
C++Programming/Code/Standard C Library/
Functions/getenv
getenv
The function getenv() returns environmental information associated with name, and is very implementation
dependent. NULL is returned if no information about name is available.
Related topics
system
C++ Programming/Code/Standard C Library/Functions/gets 26
C++Programming/Code/Standard C Library/
Functions/gets
gets
The gets() function reads characters from stdin and loads them into str, until a newline or EOF is reached. The
newline character is translated into a null termination. The return value of gets() is the read-in string, or NULL if
there is an error.
Related topics
fgetc - fgets - fputs - puts
C++Programming/Code/Standard C Library/
Functions/localtime
localtime
The function localtime() converts calendar time time into local time. Watch out for the static return.
Related topics
asctime - ctime - difftime - gmtime - strftime - time
C++ Programming/Code/Standard C Library/Functions/mktime 27
C++Programming/Code/Standard C Library/
Functions/mktime
mktime
The mktime() function converts the local time in time to calendar time, and returns it. If there is an error, -1 is
returned.
Related topics
asctime - ctime - gmtime - time
C++Programming/Code/Standard C Library/
Functions/perror
perror
The perror() function prints str and an implementation-defined error message corresponding to the global variable
errno. For example:
If the file called not_found.txt is not found, this code will produce the following output:
Related topics
clearerr - feof - ferror
C++ Programming/Code/Standard C Library/Functions/printf 28
C++Programming/Code/Standard C Library/
Functions/printf
printf
The printf() function prints output to stdout, according to format and other arguments passed to printf(). The string
format consists of two types of items - characters that will be printed to the screen, and format commands that define
how the other arguments to printf() are displayed. Basically, you specify a format string that has text in it, as well as
"special" characters that map to the other arguments of printf(). For example, this code
The %s means, "insert the first argument, a string, right here." The %d indicates that the second argument (an
integer) should be placed there. There are different %-codes for different variable types, as well as options to limit
the length of the variables and whatnot.
Control Explanation
Character
%c a single character
%d a decimal integer
%i an integer
%f a floating-point number
%o an octal number
%u an unsigned integer
%s a string
%x a hexadecimal number
%p a pointer
%n the argument shall be a pointer to an integer into which is placed the number of characters written so far
%% a percent sign
A field-length specifier may appear before the final control character to indicate the width of the field:
C++ Programming/Code/Standard C Library/Functions/printf 29
%012d
You can also include a precision modifier, in the form of a .N where N is some number, before the format command:
%012.4d
The precision modifier has different meanings depending on the format command being used:
• With %e, %E, and %f, the precision modifier lets you specify the number of decimal places desired. For example,
%12.6f will display a floating number at least 12 digits wide, with six decimal places.
• With %g and %G, the precision modifier determines the maximum number of significant digits displayed.
• With %s, the precision modifier simply acts as a maximum field length, to complement the minimum field length
that precedes the period.
All of printf()'s output is right-justified, unless you place a minus sign right after the % sign. For example,
%-12.4f
will display a floating point number with a minimum of 12 characters, 4 decimal places, and left justified. You may
modify the %d, %i, %o, %u, and %x type specifiers with the letter l and the letter h to specify long and short data
types (e.g. %hd means a short integer). The %e, %f, and %g type specifiers can have the letter l before them to
indicate that a double follows. The %g, %f, and %e type specifiers can be preceded with the character '#' to ensure
that the decimal point will be present, even if there are no decimal digits. The use of the '#' character with the %x
type specifier indicates that the hexidecimal number should be printed with the '0x' prefix. The use of the '#'
character with the %o type specifier indicates that the octal value should be displayed with a 0 prefix.
Inserting a plus sign '+' into the type specifier will force positive values to be preceded by a '+' sign. Putting a space
character ' ' there will force positive values to be preceded by a single space character.
You can also include constant escape sequences in the output string.
The return value of printf() is the number of characters printed, or a negative number if an error occurred.
Related topics
fprintf - puts - scanf - sprintf
C++ Programming/Code/Standard C Library/Functions/putc 30
C++Programming/Code/Standard C Library/
Functions/putc
putc
The putc() function writes the character ch to stream. The return value is the character written, or EOF if there is an
error. For example:
int ch;
FILE *input, *output;
input = fopen( "tmp.c", "r" );
output = fopen( "tmpCopy.c", "w" );
ch = getc( input );
while( ch != EOF ) {
putc( ch, output );
ch = getc( input );
}
fclose( input );
fclose( output );
C++Programming/Code/Standard C Library/
Functions/putchar
putchar
putchar( ch );
is the same as
The return value of putchar() is the written character, or EOF if there is an error.
Related topics
putc
C++Programming/Code/Standard C Library/
Functions/puts
puts
The function puts() writes str to stdout. puts() returns non-negative on success, or EOF on failure.
Related topics
fputs - gets - printf - putc
C++ Programming/Code/Standard C Library/Functions/rand 32
C++Programming/Code/Standard C Library/
Functions/rand
rand
The function rand() returns a pseudo-random integer between zero and RAND_MAX. An example:
srand( time(NULL) );
for( i = 0; i < 10; i++ )
printf( "Random number #%d: %d\n", i, rand() );
The rand() function needs to be seeded before it's first call with the srand() function - otherwise it will consistently
return the same numbers when the program is restarted.
Related topics
srand
C++Programming/Code/Standard C Library/
Functions/rewind
rewind
The function rewind() moves the file position indicator to the beginning of the specified stream, also clearing the
error and EOF flags associated with that stream.
Related topics
fseek
C++ Programming/Code/Standard C Library/Functions/scanf 33
C++Programming/Code/Standard C Library/
Functions/scanf
scanf
The scanf() function reads input from stdin, according to the given format, and stores the data in the other
arguments. It works a lot like printf(). The format string consists of control characters, whitespace characters, and
non-whitespace characters. The control characters are preceded by a % sign, and are as follows:
Control Explanation
Character
%c a single character
%d a decimal integer
%i an integer
%lf a double
%o an octal number
%s a string
%x a hexadecimal number
%p a pointer
%u an unsigned integer
%% a percent sign
scanf() reads the input, matching the characters from format. When a control character is read, it puts the value in the
next variable. Whitespace (tabs, spaces, etc) are skipped. Non-whitespace characters are matched to the input, then
discarded. If a number comes between the % sign and the control character, then only that many characters will be
converted into the variable. If scanf() encounters a set of characters, denoted by the %[] control character, then any
characters found within the brackets are read into the variable. The return value of scanf() is the number of variables
that were successfully assigned values, or EOF if there is an error.
This code snippet uses scanf() to read an int, float, and a double from the user. Note that the variable arguments to
scanf() are passed in by address, as denoted by the ampersand (&) preceding each variable:
int i;
float f;
double d;
Related topics
fgets - fscanf - printf - sscanf
C++Programming/Code/Standard C Library/
Functions/setbuf
setbuf
The setbuf() function sets stream to use buffer, or, if buffer is NULL, turns off buffering. This function expects that
the buffer be BUFSIZ characters long - since this function does not support specifying the size of the buffer, buffers
larger than BUFSIZ will be partly unused.
Related topics
fclose - fopen - setvbuf
C++ Programming/Code/Standard C Library/Functions/setvbuf 35
C++Programming/Code/Standard C Library/
Functions/setvbuf
setvbuf
The function setvbuf() sets the buffer for stream to be buffer, with a size of size. mode can be one of:
• _IOFBF, which indicates full buffering
• _IOLBF, which means line buffering
• _IONBF, which means no buffering
Related topics
fflush - setbuf
C++Programming/Code/Standard C Library/
Functions/sprintf
sprintf
The sprintf() function is just like printf(), except that the output is sent to buffer. The return value is the number of
characters written. For example:
char string[50];
int file_number = 0;
Note that sprintf() does the opposite of a function like atoi() -- where atoi() converts a string into a number, sprintf()
can be used to convert a number into a string.
For example, the following code uses sprintf() to convert an integer into a string of characters:
char result[100];
int num = 24;
sprintf( result, "%d", num );
This code is similar, except that it converts a floating-point number into an array of characters:
char result[100];
float fnum = 3.14159;
sprintf( result, "%f", fnum );
Related topics
C++ Programming/Code/Standard C Library/Functions/sprintf 36
fprintf - printf
(Standard C String and Character) atof - atoi - atol
C++Programming/Code/Standard C Library/
Functions/srand
srand
The function srand() is used to seed the random sequence generated by rand(). For any given seed, rand() will
generate a specific "random" sequence over and over again.
srand( time(NULL) );
for( i = 0; i < 10; i++ )
printf( "Random number #%d: %d\n", i, rand() );
Related topics
rand
(Standard C Time & Date functions) time
C++Programming/Code/Standard C Library/
Functions/sscanf
sscanf
The function sscanf() is just like scanf(), except that the input is read from buffer.
Related topics
fscanf - scanf
C++ Programming/Code/Standard C Library/Functions/strerror 37
C++Programming/Code/Standard C Library/
Functions/strerror
strerror
The function strerror() returns an implementation defined string corresponding to num. If an error occurred, the error
is located within the global variable errno.
Related topics
perror
C++Programming/Code/Standard C Library/
Functions/strftime
strftime
The function strftime() formats date and time information from time to a format specified by fmt, then stores the
result in str (up to maxsize characters). Certain codes may be used in fmt to specify different types of time:
Code Meaning
%p locale's equivalent of AM or PM
%U week of the year, (0-53), where week 1 has the first Sunday
%W week of the year, (0-53), where week 1 has the first Monday
C++ Programming/Code/Standard C Library/Functions/strftime 38
%% a percent sign
Related topics
gmtime - localtime - time
C++Programming/Code/Standard C Library/
Functions/strtod
strtod
The function strtod() returns whatever it encounters first in start as a double. end is set to point at whatever is left in
start after that double. If overflow occurs, strtod() returns either HUGE_VAL or -HUGE_VAL.
x = atof( "42.0is_the_answer" );
C++Programming/Code/Standard C Library/
Functions/system
system
The system() function runs the given command by passing it to the default command interpreter.
The return value is usually zero if the command executed without errors. If command is NULL, system() will test to
see if there is a command interpreter available. Non-zero will be returned if there is a command interpreter available,
zero if not.
Related topics
exit - getenv
C++Programming/Code/Standard C Library/
Functions/time
time
The function time() returns the current time, or -1 if there is an error. If the argument time is given, then the current
time is stored in time.
Related topics
asctime - clock - ctime - difftime - gmtime - localtime - mktime - strftime
(Other Standard C functions) srand
C++ Programming/Code/Standard C Library/Functions/ungetc 40
C++Programming/Code/Standard C Library/
Functions/ungetc
ungetc
C+ + Programming/ Code/ Standard C Library/ Functions Source: http://en.wikibooks.org/w/index.php?oldid=1532657 Contributors: Adrignola, Panic2k4, Sigma 7, Van der Hoorn
C+ + Programming/ Code/ Standard C Library/ Functions/ abort Source: http://en.wikibooks.org/w/index.php?oldid=1499577 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ asctime Source: http://en.wikibooks.org/w/index.php?oldid=1500208 Contributors: Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ assert Source: http://en.wikibooks.org/w/index.php?oldid=1500210 Contributors: Panic2k4, 1 anonymous edits
C+ + Programming/ Code/ Standard C Library/ Functions/ atexit Source: http://en.wikibooks.org/w/index.php?oldid=1501764 Contributors: Panic2k4, 1 anonymous edits
C+ + Programming/ Code/ Standard C Library/ Functions/ atof Source: http://en.wikibooks.org/w/index.php?oldid=1501765 Contributors: Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ atoi Source: http://en.wikibooks.org/w/index.php?oldid=1501772 Contributors: Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ atol Source: http://en.wikibooks.org/w/index.php?oldid=1501767 Contributors: Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ clearerr Source: http://en.wikibooks.org/w/index.php?oldid=1415366 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ clock Source: http://en.wikibooks.org/w/index.php?oldid=1532661 Contributors: Adrignola, Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ ctime Source: http://en.wikibooks.org/w/index.php?oldid=1532664 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ difftime Source: http://en.wikibooks.org/w/index.php?oldid=1532665 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ exit Source: http://en.wikibooks.org/w/index.php?oldid=1647105 Contributors: Adrignola, Jomegat, Panic2k4, 2 anonymous
edits
C+ + Programming/ Code/ Standard C Library/ Functions/ fclose Source: http://en.wikibooks.org/w/index.php?oldid=1415404 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ feof Source: http://en.wikibooks.org/w/index.php?oldid=1417343 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ ferror Source: http://en.wikibooks.org/w/index.php?oldid=1532671 Contributors: Adrignola, Panic2k4, Van der Hoorn
C+ + Programming/ Code/ Standard C Library/ Functions/ fflush Source: http://en.wikibooks.org/w/index.php?oldid=1415600 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ fgetc Source: http://en.wikibooks.org/w/index.php?oldid=1532672 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fgetpos Source: http://en.wikibooks.org/w/index.php?oldid=1532673 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fgets Source: http://en.wikibooks.org/w/index.php?oldid=1532674 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fopen Source: http://en.wikibooks.org/w/index.php?oldid=1417347 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ fprintf Source: http://en.wikibooks.org/w/index.php?oldid=1532677 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fputc Source: http://en.wikibooks.org/w/index.php?oldid=1532678 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fputs Source: http://en.wikibooks.org/w/index.php?oldid=1532679 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fread Source: http://en.wikibooks.org/w/index.php?oldid=1532680 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ freopen Source: http://en.wikibooks.org/w/index.php?oldid=1532681 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fscanf Source: http://en.wikibooks.org/w/index.php?oldid=1415761 Contributors: Panic2k4, Sigma 7, 1 anonymous edits
C+ + Programming/ Code/ Standard C Library/ Functions/ fseek Source: http://en.wikibooks.org/w/index.php?oldid=1532683 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fsetpos Source: http://en.wikibooks.org/w/index.php?oldid=1532684 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ ftell Source: http://en.wikibooks.org/w/index.php?oldid=1532685 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ fwrite Source: http://en.wikibooks.org/w/index.php?oldid=1532687 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ getc Source: http://en.wikibooks.org/w/index.php?oldid=1532688 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ getchar Source: http://en.wikibooks.org/w/index.php?oldid=1532689 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ getenv Source: http://en.wikibooks.org/w/index.php?oldid=1532690 Contributors: Adrignola, Panic2k4, 1 anonymous edits
C+ + Programming/ Code/ Standard C Library/ Functions/ gets Source: http://en.wikibooks.org/w/index.php?oldid=1532691 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ localtime Source: http://en.wikibooks.org/w/index.php?oldid=1532713 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ mktime Source: http://en.wikibooks.org/w/index.php?oldid=1532722 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ perror Source: http://en.wikibooks.org/w/index.php?oldid=1532724 Contributors: Adrignola, Panic2k4, 2 anonymous edits
C+ + Programming/ Code/ Standard C Library/ Functions/ printf Source: http://en.wikibooks.org/w/index.php?oldid=1410194 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ putc Source: http://en.wikibooks.org/w/index.php?oldid=1532726 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ putchar Source: http://en.wikibooks.org/w/index.php?oldid=1532727 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ puts Source: http://en.wikibooks.org/w/index.php?oldid=1532728 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ rand Source: http://en.wikibooks.org/w/index.php?oldid=1410878 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ rewind Source: http://en.wikibooks.org/w/index.php?oldid=1532731 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ scanf Source: http://en.wikibooks.org/w/index.php?oldid=1532733 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ setbuf Source: http://en.wikibooks.org/w/index.php?oldid=1417225 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ setvbuf Source: http://en.wikibooks.org/w/index.php?oldid=1415774 Contributors: Panic2k4, Sigma 7
Article Sources and Contributors 42
C+ + Programming/ Code/ Standard C Library/ Functions/ sprintf Source: http://en.wikibooks.org/w/index.php?oldid=1532737 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ srand Source: http://en.wikibooks.org/w/index.php?oldid=1532742 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ sscanf Source: http://en.wikibooks.org/w/index.php?oldid=1532743 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ strerror Source: http://en.wikibooks.org/w/index.php?oldid=1415400 Contributors: Panic2k4, Sigma 7
C+ + Programming/ Code/ Standard C Library/ Functions/ strftime Source: http://en.wikibooks.org/w/index.php?oldid=1532750 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ strtod Source: http://en.wikibooks.org/w/index.php?oldid=1532757 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ system Source: http://en.wikibooks.org/w/index.php?oldid=1532763 Contributors: Adrignola, Panic2k4, 2 anonymous edits
C+ + Programming/ Code/ Standard C Library/ Functions/ time Source: http://en.wikibooks.org/w/index.php?oldid=1532766 Contributors: Adrignola, Panic2k4
C+ + Programming/ Code/ Standard C Library/ Functions/ ungetc Source: http://en.wikibooks.org/w/index.php?oldid=1532772 Contributors: Adrignola, Panic2k4
Image Sources, Licenses and Contributors 43
License
Creative Commons Attribution-Share Alike 3.0 Unported
http:/ / creativecommons. org/ licenses/ by-sa/ 3. 0/