[英]how to convert a double into int to use it as argument in usleep?
我有一個像56789098.9899這樣的數字 ,我想在usleep()中使用它,它只接受一個無符號整數作為參數。如何解決這個問題?
sleeptime = time_alloted-time_taken。
睡眠時間是一個無符號的int變量。
首先, usleep
接受usenconds_t
爭論,而useconds_t
不必是unsigned int
。 在這里查看usleep
規范。 http://pubs.opengroup.org/onlinepubs/7908799/xsh/usleep.html
其次,請注意參數不能大於1,000,000。 還要記住檢查usleep
的返回值,因為它可能會失敗。
然后只使用類型轉換就足夠了。
它應該“按原樣”工作,所以以下代碼對我來說很好:
#include "stdio.h"
void func(unsigned int i)
{
printf("Going to sleep for %d\n", i);
usleep(i);
}
int main(void)
{
printf("hello world\n");
unsigned int sleep;
sleep = 56789098.9888 - 124234.45345;
func(sleep);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.