我的C/C++备忘录

/

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 : 0x00005f15
64位 : -1, -1, 18446744073709551615, ffffffffffffffff, 0xffffffffffffffff

2.数值范围
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 / nologo
	b.cpp

> b
nstr = 1 //参数个数+1
str[0]b //参数0为程序名
> b.exe
nstr = 1
str[0]b.exe

> b you must be kidding me
nstr = 6 //命令行字符串为5个
str[0]b //运行程序名
str[1]you
str[2]must
str[3]be
str[4]kidding
str[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 me
		Sleep 1213 ms, total:1.213 sec
		number of nstr = 8
		str[0]b
		str[1]1213
		str[2]hey
		str[3]you
		str[4]must
		str[5]be
		str[6]kidding
		str[7]me
	cmdline : 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值