📄 all.html
字号:
<div class="name-format"> rand </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <stdlib.h> int rand( void );</pre> <p>The function rand() returns a pseudorandom integer between zero and RAND_MAX. An example:</p> <pre class="example-code"> srand( time(<strong>NULL</strong>) ); for( i = 0; i < 10; i++ ) printf( "Random number #%d: %d\n", i, rand() ); </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="srand.html">srand</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> setjmp </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <setjmp.h> int setjmp( jmp_buf envbuf );</pre> <p>The setjmp() function saves the system stack in <em>envbuf</em> for use by a later call to <a href="longjmp.html">longjmp</a>(). When you first call setjmp(), its return value is zero. Later, when you call <a href="longjmp.html">longjmp</a>(), the second argument of <a href="longjmp.html">longjmp</a>() is what the return value of setjmp() will be. Confused? Read about <a href= "longjmp.html">longjmp</a>().</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="longjmp.html">longjmp</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> signal </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <signal.h> void ( *signal( int signal, void (* func) (int)) ) (int);</pre> <p>The signal() function sets <em>func</em> to be called when <em>signal</em> is recieved by your program. <em>func</em> can be a custom signal handler, or one of these macros (defined in signal.h):</p> <table class="code-table"> <tr> <th class="code-table-th">Macro</th> <th class="code-table-th">Explanation</th> </tr> <tr> <td class="code-table-td">SIG_DFL</td> <td class="code-table-td">default signal handling</td> </tr> <tr> <td class="code-table-td">SIG_IGN</td> <td class="code-table-td">ignore the signal</td> </tr> </table> <p>Some basic signals that you can attach a signal handler to are:</p> <table class="code-table"> <tr> <th class="code-table-th">Signal</th> <th class="code-table-th">Description</th> </tr> <tr> <td class="code-table-td">SIGTERM</td> <td class="code-table-td">Generic stop signal that can be caught.</td> </tr> <tr> <td class="code-table-td">SIGINT</td> <td class="code-table-td">Interrupt program, normally ctrl-c.</td> </tr> <tr> <td class="code-table-td">SIGQUIT</td> <td class="code-table-td">Interrupt program, similar to SIGINT.</td> </tr> <tr> <td class="code-table-td">SIGKILL</td> <td class="code-table-td">Stops the program. Cannot be caught.</td> </tr> <tr> <td class="code-table-td">SIGHUP</td> <td class="code-table-td">Reports a disconnected terminal.</td> </tr> </table> <p>The return value of signal() is the address of the previously defined function for this signal, or SIG_ERR is there is an error.</p> <div class="related-examples-format"> Example code: </div> <div class="related-examples"> <p>The following example uses the signal() function to call an arbitrary number of functions when the user aborts the program. The functions are stored in a vector, and a single "clean-up" function calls each function in that vector of functions when the program is aborted:</p> <pre class="example-code">void f1() { cout << "calling f1()..." << endl;} void f2() { cout << "calling f2()..." << endl;} typedef void(*endFunc)(void);vector<endFunc> endFuncs; void cleanUp( int dummy ) { for( unsigned int i = 0; i < endFuncs.size(); i++ ) { endFunc f = endFuncs.at(i); (*f)(); } exit(-1);} int main() { // connect various signals to our clean-up function signal( SIGTERM, cleanUp ); signal( SIGINT, cleanUp ); signal( SIGQUIT, cleanUp ); signal( SIGHUP, cleanUp ); // add two specific clean-up functions to a list of functions endFuncs.push_back( f1 ); endFuncs.push_back( f2 ); // loop until the user breaks while( 1 ); return 0;} </pre> </div> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="raise.html">raise</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> srand </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <stdlib.h> void srand( unsigned seed );</pre> <p>The function srand() is used to seed the random sequence generated by <a href="rand.html">rand</a>(). For any given <em>seed</em>, rand() will generate a specific "random" sequence over and over again.</p> <pre class="example-code"> srand( time(<strong>NULL</strong>) ); for( i = 0; i < 10; i++ ) printf( "Random number #%d: %d\n", i, rand() ); </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="rand.html">rand</a><br> (Standard C Date & Time) <a href= "../stddate/time.html">time</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> system </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <stdlib.h> int system( const char *command );</pre> <p>The system() function runs the given <em>command</em> by passing it to the default command interpreter.</p> <p>The return value is usually zero if the command executed without errors. If <em>command</em> is <strong>NULL</strong>, 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.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="exit.html">exit</a><br> <a href="getenv.html">getenv</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> va_arg </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <stdarg.h> type va_arg( <strong>va_list</strong> argptr, type ); void va_end( <strong>va_list</strong> argptr ); void va_start( <strong>va_list</strong> argptr, last_parm );</pre> <p>The va_arg() macros are used to pass a variable number of arguments to a function.</p> <ol> <li>First, you must have a call to va_start() passing a valid <strong>va_list</strong> and the mandatory first argument of the function. This first argument can be anything; one way to use it is to have it be an integer describing the number of parameters being passed.</li> <li>Next, you call va_arg() passing the <strong>va_list</strong> and the type of the argument to be returned. The return value of va_arg() is the current parameter.</li> <li>Repeat calls to va_arg() for however many arguments you have.</li> <li>Finally, a call to va_end() passing the <strong>va_list</strong> is necessary for proper cleanup.</li> </ol> <p>For example:</p> <pre class="example-code"> int sum( int num, ... ) { int answer = 0; <strong>va_list</strong> argptr; va_start( argptr, num ); for( ; num > 0; num-- ) { answer += va_arg( argptr, int ); } va_end( argptr ); return( answer ); } int main( void ) { int answer = sum( 4, 4, 3, 2, 1 ); printf( "The answer is %d\n", answer ); return( 0 ); } </pre> <p>This code displays 10, which is 4+3+2+1.</p> <p>Here is another example of variable argument function, which is a simple printing function:</p> <pre class="example-code"> void my_printf( char *format, ... ) { <strong>va_list</strong> argptr; va_start( argptr, format ); while( *format != '\0' ) { // string if( *format == 's' ) { char* s = va_arg( argptr, char * ); printf( "Printing a string: %s\n", s ); } // character else if( *format == 'c' ) { char c = (char) va_arg( argptr, int ); printf( "Printing a character: %c\n", c ); break; } // integer else if( *format == 'd' ) { int d = va_arg( argptr, int ); printf( "Printing an integer: %d\n", d ); } *format++; } va_end( argptr ); } int main( void ) { my_printf( "sdc", "This is a string", 29, 'X' ); return( 0 ); } </pre> <p>This code displays the following output when run:</p> <pre class="example-code"> Printing a string: This is a string Printing an integer: 29 Printing a character: X </pre> </div> </td> </tr> </table></body></html><hr></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -