我的C/C++备忘录

来源:互联网 发布:sqlserver rowguid 编辑:程序博客网 时间:2024/06/05 10:43

/

1.printf格式化输出参考 http ://www.cplusplus.com/reference/cstdio/printf/?kw=printf#include <stdio.h>#include <stdlib.h>#include <locale.h>#include <time.h>int main(){unsigned long long ago = -1ULL;setlocale(LC_ALL, "CHS");//让命令行支持中文int r[] = { 24, 52, 6, 266, 2232, 342 };for (int i = 0; i < sizeof(r) / sizeof(*r); i++)//for(int i=0;i<sizeof(r)/sizeof(r[0]);i++) {printf("%S:0x%x\n", L"测试中文", r[i]);//UNICODE使用%S输出,%x输出小写字母的十六进数,%X输出大写的十六进数}srand(time(0));for (unsigned long mask = 1; mask>0; mask <<= 1){printf("%s:%#010x,rand:%#010x\n", "测试移位", mask, rand());//加入%#x输出带0x前缀的十六进数,010指填充0个数+0x前缀共10个字符}wprintf(L"64位输出:%lld,%I64d,%I64u,%llx,%#llx", ago, ago, ago, ago, ago);//加入%#x输出带0x前缀的十六进数,010指填充0个数+0x前缀共10个字符}result测试中文 : 0x18测试中文 : 0x34测试中文 : 0x6测试中文 : 0x10a测试中文 : 0x8b8测试中文 : 0x156测试移位 : 0x00000001, rand : 0x00001db7测试移位 : 0x00000002, rand : 0x00004d23测试移位 : 0x00000004, rand : 0x000032b9测试移位 : 0x00000008, rand : 0x000018da测试移位 : 0x00000010, rand : 0x00000a7e测试移位 : 0x00000020, rand : 0x00004f68测试移位 : 0x00000040, rand : 0x00006159测试移位 : 0x00000080, rand : 0x00003ee9测试移位 : 0x00000100, rand : 0x00000bbc测试移位 : 0x00000200, rand : 0x000055a9测试移位 : 0x00000400, rand : 0x000003e2测试移位 : 0x00000800, rand : 0x00003daa测试移位 : 0x00001000, rand : 0x000049ad测试移位 : 0x00002000, rand : 0x00002585测试移位 : 0x00004000, rand : 0x00004e89测试移位 : 0x00008000, rand : 0x00002949测试移位 : 0x00010000, rand : 0x00003555测试移位 : 0x00020000, rand : 0x0000233a测试移位 : 0x00040000, rand : 0x00002e9f测试移位 : 0x00080000, rand : 0x00002ba1测试移位 : 0x00100000, rand : 0x00003bb9测试移位 : 0x00200000, rand : 0x0000080d测试移位 : 0x00400000, rand : 0x00003769测试移位 : 0x00800000, rand : 0x00000db9测试移位 : 0x01000000, rand : 0x0000394e测试移位 : 0x02000000, rand : 0x000057ed测试移位 : 0x04000000, rand : 0x0000512c测试移位 : 0x08000000, rand : 0x00007a8f测试移位 : 0x10000000, rand : 0x000041b2测试移位 : 0x20000000, rand : 0x00005a53测试移位 : 0x40000000, rand : 0x00005e87测试移位 : 0x80000000, rand : 0x00005f1564位 : -1, -1, 18446744073709551615, ffffffffffffffff, 0xffffffffffffffff2.数值范围for (unsigned char i = 0; i < 256; i++){}这是一个死循环, 看出来了吗 ?请问下面会输出什么 ?for (char i = 0; i < 128; i++){printf("i=%d\t", i);if (((i + 1) % 8) == 0) printf("\n");}3.命令行参数, 从程序执行时输入的程序名 与附带的参数表(一般以空格分隔)#include <iostream>using namespace std;int main(int nstr, char *str[]) //其他等价形式 int main(int argv,char **argc){cout << "nstr=" << nstr << endl;for (int i = 0; i < nstr; i++){cout << "str[" << i << "]" << str[i] << endl;}}-prompt $g> cl b.cpp / EHsc / nologob.cpp> bnstr = 1 //参数个数+1str[0]b //参数0为程序名> b.exenstr = 1str[0]b.exe> b you must be kidding menstr = 6 //命令行字符串为5个str[0]b //运行程序名str[1]youstr[2]muststr[3]bestr[4]kiddingstr[5]me实践 : 使用命令行如何实现一个延时程序 ?#include <iostream> using namespace std;int wmain(int nstr, wchar_t **str){long Tms = 500;if (nstr > 1) Tms = _wtol(str[1]);cout << "Sleep " << Tms << " ms" << ",total:" << Tms / 1000.0 << " sec" << endl;Sleep(Tms);}#include <windows.h>#include <iostream>using namespace std;int main(int nstr, char *str[]){long Tms = 500;if (nstr > 1) Tms = atol(str[1]);cout << "Sleep " << Tms << " ms" << ",total:" << Tms / 1000.0 << " sec" << endl;Sleep(Tms);}#include <iostream>#include <shellapi.h>#pragma comment(lib,"shell32.lib")using namespace std;int main(int nstr, char **str){cout << "number of nstr=" << nstr << endl;for (int i = 0; i < nstr; i++){cout << "str[" << i << "]" << str[i] << endl;}wchar_t *cmdline = GetCommandLineW();int nwstr;wchar_t **wstr = CommandLineToArgvW(cmdline, &nwstr);wcout << "cmdline:" << cmdline << endl;cout << "number of wstr=" << nwstr << endl;for (int i = 0; i < nwstr; i++){wcout << "str[" << i << "]" << wstr[i] << endl;}}UNICODE 加强版本#include <windows.h>#include <shellapi.h>#pragma comment(lib,"shell32.lib")int wmain(int nstr, wchar_t **str){cout << "number of nstr=" << nstr << endl;for (int i = 0; i < nstr; i++){wcout << "str[" << i << "]" << str[i] << endl;}wchar_t *cmdline = GetCommandLineW();int nwstr;wchar_t **wstr = CommandLineToArgvW(cmdline, &nwstr);wcout << "cmdline:" << cmdline << endl;cout << "number of wstr=" << nwstr << endl;for (int i = 0; i < nwstr; i++){wcout << "wstr[" << i << "]" << wstr[i] << endl;}}>b 1213 hey you must be kidding meSleep 1213 ms, total:1.213 secnumber of nstr = 8str[0]bstr[1]1213str[2]heystr[3]youstr[4]muststr[5]bestr[6]kiddingstr[7]mecmdline : b 1213 hey you must be kidding me  number of wstr = 8  wstr[0]b  wstr[1]1213  wstr[2]hey  wstr[3]you  wstr[4]must  wstr[5]be  wstr[6]kidding  wstr[7]me


 

0 0
原创粉丝点击