http://www.ddj.com/cpp/221600722
Q: HOW DO I... put timers with default actions in my C code?
A: Many times, we need to write programs that will only wait a certain specified amount of time for a user to do something. After that time, we need to assume that the user isn't going to do anything and move on to some specified default action. This Snippet works exactly like the get_ch() function in most PC C compiler libraries, except that it will only wait as long as you tell it for the user to hit a key. If no key is pressed within the time limit, the function returns with an EOF. As usual, a test harness has been added to allow for stand-alone testing. To compile the stand-alone test, simply define the TEST macro.
/*
** TIMEGETC.C - waits for a given number of seconds for the user to press
** a key. Returns the key pressed, or EOF if time expires
**
** Public domain by Bob Jarvis
** Test harness added by Bob Stout
**
** Note: This relies on the non-standard kbhit() function, which is available
** in all Windows PC compilers - including MinGW32 (gcc).
*/
#include <stdio.h>
#include <time.h>
#include <conio.h>
int timed_getch(int n_seconds)
{
time_t start, now;
start = time(NULL);
now = start;
while(difftime(now, start) < (double)n_seconds && !kbhit())
{
now = time(NULL);
}
if(kbhit())
return getch();
else return EOF;
}
#ifdef TEST
main()
{
int c;
const char *ctrlchars[] =
{
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS_", "HT_", "LF_", "VT_", "FF_", "CR_", "SO_", "SI_",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM_", "SUB", "ESC", "FS_", "GS_", "RS_", "US_"
};
printf("Starting a 5 second delay...\n");
c = timed_getch(5);
if(c == EOF)
printf("Timer expired\n
搞了几天的问题。编译一个文件时,老是出下面这个错.
checking for C compiler default output file name… configure: error: C compiler cannot create executables
没法子,找高人帮我处理,哈哈….记录下来.因为64位的机器,默认对CFLAGS的这是进行了设置,所以使用下面的命令清空他就行了,这样软件就不会报 ......
一 产生 运行时库是程序在运行时所需要的库文件,通常以LIB或DLL形式提供。 C运行时库就是C run-time library,诞生于20世纪70年代,是C而非C++语言世界的概念,C程序运行时需要这些库中的函数。 C 语言是所谓的“小内核”语言,就其语言本身来说很小(不多的关键字,程序流程控制,数据类型等)。所以,C语言内核开发出 ......
当要交换两个数的值时,通常的做法是定义一个临时变量,然后再进行交换。那么能不能不用临时变量而交换两个数的值呢?可以的!C语言提供的异或运算就可以实现这样的操作。
异或运算符^也称XOR运算符,它的规则是若参加运算的两个二进位同号,则结果为0(假);异号为1(真)。即0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, ......