它的使用方式如下:
條件式 ? 成立傳回值 : 失敗傳回值
範例程式:
#include
int main(int argc,char *argv[])
{
// a = 1 表示為 true 反之為 false
int a = 0;
int b = 20;
int x = 11;
// 若 a 為 true 則 x = b
// false 則 x = c
x = ( a ? b : c);
printf("x = %d\n",x);
return 0;
}
目前分類:程試設計 (6)
- Jan 27 Sun 2008 01:25
「條件運算子」(Conditional operator)
- Dec 05 Wed 2007 23:19
資料型態(datatype)
這個...雖然很基本,但確很重要

關鍵字 | 位長 | 範圍 | printf chars |
char |
1 | -128..127 (或0..255,與體系結構相關) | %c |
unsigned char |
1 | 0..255 | |
signed char |
1 | -128..127 | |
int |
2 (Dos或win16) or 4 (win32或unix) |
-32768..32767 or -2147483648..2147483647 |
%i, %d |
unsigned int |
2 (Dos或win16) or 4 (win32或unix) |
0..65535 or 0..4294967295 |
%u |
signed int |
2 (Dos或win16) or 4 (win32或unix) |
-32768..32767 or -2147483648..2147483647 |
%i, %d |
short int |
2 | -32768..32767 | %hi |
unsigned short |
2 | 0..65535 | %hu |
signed short |
2 | -32768..32767 | |
long int |
4 | -2147483648..2147483647 | %li, %ld |
unsigned long |
4 | 0..4294967295 | %lu |
signed long |
4 | -2147483648..2147483647 | |
long long |
8 | -9223372036854775808..9223372036854775807 | %lli |
unsigned long long |
8 | 0..18446744073709551615 | %llu |
float |
4 | 3.4x10-38..3.4x10+38 (7 sf) | %f, %e, %g |
double |
8 | 1.7x10-308..1.7x10+308 (15 sf) | %f, %e, %g |
long double |
8 或以上 | 編譯器相關 | %Lf, %Le, %Lg |
- Nov 10 Sat 2007 02:10
Linking to C function in C++
假如在C++ 中要呼叫C 的function,
則需加入如下宣告:
#ifdef __cplusplus
則需加入如下宣告:
#ifdef __cplusplus
extern "C" {
#endif
/* put your c function definiction here */
#ifdef __cplusplus
}
#endif
- Nov 07 Wed 2007 05:25
計算機概論(bit byte word ....的轉換)
最近在寫程式的時候候,由其是assembly 常常會把程式給寫到死掉...不然就是結果和預期的結果不太一樣
原因主要是因為資料的大小給錯或是亂給
...
-----------------------------------------------------------------
------------------------------------------------------------------
結論:
所以它們之間的關系為:
bit 可以代表0,1
1byte = 8 bits
1word = 2 bytes = 16bits
1doubleword = 2 words = 4bytes = 32bit
原因主要是因為資料的大小給錯或是亂給

-----------------------------------------------------------------
- 位元(bit, b)
- 位元組(byte, B)
- 字組(word)
------------------------------------------------------------------
結論:
所以它們之間的關系為:
bit 可以代表0,1
1byte = 8 bits
1word = 2 bytes = 16bits
1doubleword = 2 words = 4bytes = 32bit
- Oct 25 Thu 2007 00:25
callback的補充
簡單的說,如果你使用了某個function,那麼你就是『call』了一個function。
如果系統或是函式是要求你給一個function pointer,
這個function pointer指到一個實際的函式(多半這個函式是你自己寫的)。
然後它會在適當的時間呼叫此function,則此function就是所謂的 callback function。
因為這個function是被『callback』了。
如果系統或是函式是要求你給一個function pointer,
這個function pointer指到一個實際的函式(多半這個函式是你自己寫的)。
然後它會在適當的時間呼叫此function,則此function就是所謂的 callback function。
因為這個function是被『callback』了。
- Oct 25 Thu 2007 00:05
何謂callback function
何謂 callback function 呢?
用Google找了一下.....嗯....
...
簡單的說就是回傳某個函數的指標, 呼叫者便可透過這個函數指標直接執行函數...
以下是我找到的範例
//---------------------------------------------------
//說明:
// 這個是一個callback function...
// 由Google上找到的例子加以修改而成的...
//---------------------------------------------------
//若要測試,記得只能存成.c檔...
//(有warning,可以不用理)
//不可以存成.cpp檔...因為complier不會過...
//---------------------------------------------------
#include <stdio.h>
//就是structure
struct table
{
char id;
void *func;
};
void *funcA() { printf("This is funcA()\n"); }
void *funcB() { printf("This is funcB()\n"); }
void *funcC() { printf("This is funcC()\n"); }
void *funcD() { printf("This is funcD()\n"); }
const struct table tb[] = { {1, funcA},
{2, funcB},
{3, funcC},
{4, funcD} };
//宣告callback function的prototype(原型)
void (*func)(void);
int main(int argc,char *argv[])
{
int index, i;
index = 1;
for (i = 0; i < 4; i++)
{
if (index == tb[i].id)
{
func = tb[i].func;
func();//呼叫callback function
}
index++;
}
printf("如果在這裡呼叫callback function呢?\n");
func();
return 0;
}
輸出的結果:
This is funcA()
This is funcB()
This is funcC()
This is funcD()
如果在這裡呼叫callback function呢?
This is funcD()
Press any key to continue
心得:要用心體會呀!!!!
用Google找了一下.....嗯....

簡單的說就是回傳某個函數的指標, 呼叫者便可透過這個函數指標直接執行函數...
以下是我找到的範例
//---------------------------------------------------
//說明:
// 這個是一個callback function...
// 由Google上找到的例子加以修改而成的...
//---------------------------------------------------
//若要測試,記得只能存成.c檔...
//(有warning,可以不用理)
//不可以存成.cpp檔...因為complier不會過...
//---------------------------------------------------
#include <stdio.h>
//就是structure
struct table
{
char id;
void *func;
};
void *funcA() { printf("This is funcA()\n"); }
void *funcB() { printf("This is funcB()\n"); }
void *funcC() { printf("This is funcC()\n"); }
void *funcD() { printf("This is funcD()\n"); }
const struct table tb[] = { {1, funcA},
{2, funcB},
{3, funcC},
{4, funcD} };
//宣告callback function的prototype(原型)
void (*func)(void);
int main(int argc,char *argv[])
{
int index, i;
index = 1;
for (i = 0; i < 4; i++)
{
if (index == tb[i].id)
{
func = tb[i].func;
func();//呼叫callback function
}
index++;
}
printf("如果在這裡呼叫callback function呢?\n");
func();
return 0;
}
輸出的結果:
This is funcA()
This is funcB()
This is funcC()
This is funcD()
如果在這裡呼叫callback function呢?
This is funcD()
Press any key to continue
心得:要用心體會呀!!!!