不在百度做推廣他會把你的網(wǎng)站排名弄掉怎么可以讓百度快速收錄視頻
????????函數(shù):子程序,是一個大型程序中的某部分代碼,由一個或多個語句塊組成,它負責完成某項特定任務,而且相較于其他代碼,具有相對獨立性。一般會有輸入?yún)?shù)并有返回值,提供對過程的封裝和細節(jié)的隱藏,這些代碼通常被集成為軟件庫。
????????庫函數(shù)
????????可以在Reference - C++ Reference (cplusplus.com)上查看庫函數(shù)的詳細介紹。
char * strcpy ( char * destination, const char * source );
#include <stdio.h>
#include <string.h>//char * strcpy ( char * destination, const char * source );
int main()
{char arr1[20] = {0};char arr2[20] = "Hello World!";strcpy(arr1,arr2);printf("%s\n",arr1);return 0;
}
void * memset ( void * ptr, int value, size_t num );
#include <stdio.h>
#include <string.h>//void * memset ( void * ptr, int value, size_t num );
int main()
{char arr1[20] = "Hello World!";memset(arr1,'$',5);printf("%s\n",arr1);return 0;
}
????????數(shù)組名就是首元素地址,可以把首元素地址加上數(shù)字,再配合后面的num,可以達到對字符數(shù)組即字符串內(nèi)任意位置的字符進行修改。
#include <stdio.h>
#include <string.h>//void * memset ( void * ptr, int value, size_t num );
int main()
{char arr1[20] = "Hello World!";memset(arr1+6,'$',5);printf("%s\n",arr1);return 0;
}
????????使用庫函數(shù)時,必須包含對應的頭文件。
????????自定義函數(shù)
????????求兩個數(shù)中的最大值
#include <stdio.h>
#include <string.h>int get_max(int a,int b){if(a >= b)return a;elsereturn b;//return (a > b ? a : b);
}
int main()
{//求兩個數(shù)中的最大值int a = 0;int b = 0;int c = 0;scanf("%d %d",&a,&b);//c = (a > b ? a : b);c = get_max(a,b);printf("%d\n",c);return 0;
}
????????交換兩個數(shù),注意形參和實參的區(qū)別,傳值和傳址的區(qū)別。
????????如果不用指針傳地址的話,交換的只是形參,而不會改變實參,從而達不到通過函數(shù)來交換數(shù)據(jù)的效果。
#include <stdio.h>
#include <string.h>void swap(int x,int y){int z = x;x = y;y = x;
}
int main()
{//交換兩個數(shù)int a = 0;int b = 0;int c = 0;scanf("%d %d",&a,&b);swap(a,b);printf("%d %d\n",a,b);return 0;
}
????????結果如圖,并沒有達到交換的效果:
????????通過調(diào)試可以看出,a,b和x,y地址不同,是完全不同的兩個變量,交換形參x和y的值并不能交換a和b的值。
? ? ? ? 當實參傳遞給形參的時候,形參是實參的一份臨時拷貝,對形參的修改不會影響實參。
????????應該傳遞變量的地址,通過對地址解引用來改變變量的值。
#include <stdio.h>
#include <string.h>void swap(int* a,int* b){int c = *a;*a = *b;*b = c;
}
int main()
{//交換兩個數(shù)int a = 0;int b = 0;int c = 0;scanf("%d %d",&a,&b);swap(&a,&b);printf("%d %d\n",a,b);return 0;
}
? ? ??
????????在需要修改實參的情況下,通過傳址傳參才能達到修改實參的效果。
? ? ? ? 一個工程中,可以有多個.c文件,但是只能有一個main函數(shù)。
? ? ? ? 函數(shù)可以嵌套調(diào)用,但是不能嵌套定義。
? ? ? ? 函數(shù)可以鏈式調(diào)用
#include <stdio.h>int main()
{printf("%d\n",printf("%d",printf("%d",43)));return 0;
}
????????先是 main 函數(shù)進去,然后 printf 一層層調(diào)用,先是最里層 printf 打印43,43是兩個字符,所以 printf("%d",43) 返回值是 2 ,第二層 printf 相當于 printf("%d",2) ,所以第二層 printf 打印 2 ,2是 1 個字符,所以 printf("%d",2) 返回值是 1 ,第三層 printf 相當于 printf("%d",1) ,所以第三層 printf 打印 1 ,最后打印的結果就是4321,如圖:
? ? ? ? 函數(shù)不寫返回值的時候,默認返回類型是 int ,但還是建議寫好返回值,這樣更直觀。在寫好返回值但是不返回的時候,在一些編譯器上返回的是函數(shù)執(zhí)行過程中最后一條指令執(zhí)行的結果。
int main(void)
{return 0;
}
????????這種寫法是明確的說明,main函數(shù)不需要參數(shù)。