volatile,可变参数,memset,内联函数,宽字符窄字符,国际化,条件编译,预处理命令,define中##和#的区别,文件缓冲,位域
1.volatile:要求參數(shù)修改每次都從內(nèi)存中的讀取。這種情況要比普通運行的變量需要的時間長。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
?
void main()
{
??? time_t start, end;
??? double res = 0;
??? time(&start);? //獲取時間,傳遞給start
??? //volatile強制每次從內(nèi)存讀取
??? volatile int i;
??? for (i = 0; i < 3000000000; i++)
??? {
??????? res += i;
??? }
??? printf("\n%f", res);
??? time(&end);? //獲取結(jié)束時間
??? printf("\n volatile消耗時間%fs", difftime(end, start));
??? system("pause");
}
?
void main1()
{
??? time_t start, end;
??? double res = 0;
??? //獲取時間,傳遞給tart
??? time(&start);
??? int i;
??? for (i = 0; i < 3000000000; i++)
??? {
??????? res += i;
??? }
?
??? printf("%f", res);
??? //獲取結(jié)束時間
??? time(&end);
??? printf("\n消耗時間%fs", difftime(end, start));
??? system("pause");
}
當設(shè)置了成按照C99標準運行之后,使用volatile變量之后的程序運行的時間將比register的長。
因為volatile是強制程序中內(nèi)存中讀取數(shù)據(jù),所以可以通過修改內(nèi)存中的這個參數(shù)來不斷改變傳入到cpu里的這個值。
2.可變參數(shù)
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>?? //作用是預(yù)定義一些宏,讓后把我們需要的參數(shù)拷貝過來
#include <memory.h>
?
int add(int n, ...) //...代表多少個參數(shù)都可以,其中n表示可變參數(shù)的個數(shù)
{
??? va_list v;???? //保存可以變長的參數(shù)列表
??? va_start(v,n); //保存n之后的所有參數(shù)
??? for (int i = 0; i < n; i++)
??? {
??????? //按照int取出一個參數(shù)
??????? int data = va_arg(v,int);
??????? printf("\n%d",data);
??? }
??? //釋放列表
??? va_end(v);
?
??? return 0;
}
?
void main()
{
??? //第一個表示的是可變參數(shù)的數(shù)量,一定要明確寫出多少個
??? add(5,1,2,4,5,6);
??? system("pause");
}
再如,字符串類型的可變參數(shù)
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>?? //作用是預(yù)定義一些宏,讓后把我們需要的參數(shù)拷貝過來
#include <memory.h>
?
/*當沒有指定可變參數(shù)的數(shù)量的時候可以會出現(xiàn)執(zhí)行不完的情況*/
//這里的第一個參數(shù)不是可變參數(shù)的個數(shù),后面的n才是
void run(int num, int n, ...)
{
??? va_list v;????? //保存可以變長的參數(shù)列表
??? va_start(v,n);? //從n的之后所有參數(shù)保存之
??? int i;
??? for (i = 0; i < n;i++)
??? {
??????? char *p = va_arg(v,char *);
??????? system(p);
??? }
??? va_end(v);//釋放列表
}
?
void main()
{
??? run(1, 3, "calc", "notepad", "tasklist");
??? system("pause");
}
當程序運行之后,會出現(xiàn)計算器,筆記本,打印出已經(jīng)開啟的線程。
3.void *memset(void *s,int ch,size_t n);
A:作用是在一段內(nèi)存塊中填充某個給定的值,它是對較大的結(jié)構(gòu)體或數(shù)組進行清零操作的一種最快的方法。
???????? B:需要的頭文件
<memory.h>or <string.h> memset
<wchar.h>wmemset
關(guān)于strcpy()函數(shù)
memset
4.內(nèi)聯(lián)函數(shù)
5.寬字符,窄字符,國際化
#include <stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<locale.h>?? //本地化
?
void main()
{
??? //指定窄字符
??? MessageBoxA(0,"A我的","A我的",0);
??? //指定寬字符(通過加L的方式)
??? MessageBoxW(0,L"W我的",L"我的",0);
??? MessageBox(0,L"OK我",L"喔喔速度",0);
??? //下面的方式兼容寬字符和窄字符
??? MessageBox(0,TEXT("OK我1"),TEXT("我阿達"),0);
??? system("pause");
}
國際化
?
6.include
#undef限定作用域
去掉#undef后:
7.條件編譯(#if #else #endif),這種只是處理兩種
8.#if?#elif #endif的使用
9.通過#define MM作為一個開關(guān),控制開啟或者關(guān)閉某段程序
?
10. #ifndef,#endif
11.?define中#號的作用
12.define中##號的作用
13.define中多條語句拼接
#include <Windows.h>
#include <stdio.h>
?
//通過'\' 來拼接兩條語句
#define Run(M) system(M); \
??? printf("%s", M);?? //這句可以打印名稱。
void main()
{
??? Run("calc");
??? system("pause");
}
?
14.優(yōu)先級
#include <stdio.h>
?
void main()
{
??? int a[5] = { 1, 2, 3, 4, 5 };
??? int *p = a;
??? //printf("%d\n",*p++);? //++的優(yōu)先級大于 *?單獨執(zhí)行的結(jié)果是1
??? //printf("%d\n",*(p++));//單獨執(zhí)行的時候結(jié)果為1
??? //printf("%d\n",*++p);? //單獨執(zhí)行的時候結(jié)果為2
??? printf("%d",++*p);????? //優(yōu)先級必須要有接觸,單獨執(zhí)行的時候結(jié)果為2
?
??? getchar();
}
15.打印long long處理非常大的數(shù)據(jù)使用(lld):
long long data = 11234567890;//sizeof(longlong)
printf("%lld", data);//打印longlong 處理非常大的數(shù)據(jù)
?
下面輸出的時候的執(zhí)行順序是從右向左執(zhí)行。
?
16.關(guān)于文件緩沖
//#define? _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<locale.h>//本地化
?
void main()
{
??? printf("\n%c", *(stdin->_ptr));//取出緩沖區(qū)內(nèi)容
??? printf("\n%d", stdin->_cnt);//緩沖區(qū)還有多少個字符
??? printf("\n");
??? char? ch = getchar();
??? printf("\n");
??? printf("\n%c", *(stdin->_ptr));//取出緩沖區(qū)內(nèi)容
??? printf("\n%d", stdin->_cnt);//緩沖區(qū)還有多少個字符
??? ch = getchar();
??? printf("\n");
??? printf("\n%c", *(stdin->_ptr));//取出緩沖區(qū)內(nèi)容
??? printf("\n%d", stdin->_cnt);//緩沖區(qū)還有多少個字符
??? printf("\n");
??? putchar(ch);
??? //stdout stdin不需要刷新,會及時更新,非緩沖
??? //file? fflush,刷新緩沖區(qū)
??? system("pause");
?
??? //char str[100] = "12345";sizeof(str)=100.strlen(str)=5
?
??? getchar();
}
?
17.位域(通過這種方式有時候可以用于節(jié)約內(nèi)存)
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的volatile,可变参数,memset,内联函数,宽字符窄字符,国际化,条件编译,预处理命令,define中##和#的区别,文件缓冲,位域的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有序链式队列
- 下一篇: 交行pay类刷卡金怎么用