6月1日学习
重拾起C语言的学习,再踏上编程之路!
一、函数
1.function
strcpy
char * strcpy ( char * destination, const char * source );
Copy string
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
Parameters
destination
Pointer to the destination array where the content is to be copied.
source
C string to be copied.
2.memset
void * memset ( void * ptr, int value, size_t num );
Fill block of memory
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
int main(){
char arr[] = "keep on going";
memset(arr, '*', 5);
printf("%s\n", arr);
//运行结果为 *****on going
return 0;
}
3.自定义函数
//定义函数
get_max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main() {
int a = 10;
int b = 20;
int max = get_max(a,b);
printf("max = %d\n", max);
return 0;
}
void Swap(int* pa,int* pb) {
int tmp = 0;
tmp = *pa;
*pa = *pb;
*pb = tmp;
}
int main() {
int a = 10;
int b = 20;
printf("a = %d b = %d\n", a,b);
Swap(&a,&b);
printf("a = %d b = %d\n", a, b);
return 0;
}