#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
Debug(自己写代码用)版本是调试版本,Release(测试用)是不能调试的
函数:
一:函数的组成:
ret_type fun_name(para1,*)
{
statement; //语句项目, {}内的是函数体
}
ret_type 返回类型
fun_name 函数名
para1 函数参数
二:C语言中函数的分类:
1.库函数:
①解释
频繁大量使用的函数,提前定义好,要用直接掉用库里面的函数,可以减少时间和统一标准
可以看 www.cplusplus.com / http://en.cppreference.com (有中文版本,把en改zh就好) / MSDN
②分类
IO函数 例子: printf scanf getchar putchar
字符串操作函数 例子: strcmp strlen
字符操作函数 例子: toupper(小写转大写)
内存操作函数 例子: memcpy memcmp memset
时间/日期函数 例子: time
数学函数 例子: sqrt pow
其他库函数
2.自定义函数
①解释
自定义函数和库函数一样,有函数名,返回类型和函数参数。但是不一样的是这些都是我们自己来设计。
strcpy: 类型:function 引用头文件#include <string.h>
作用:Copy string:Copies the C string pointed by source into the array pointed by destination
格式:char * strcpy( char * destination, const char * source);
int main()
{
char arr1[20] = { 0 };
char arr2[] = "hello bit"; //把arr2的内容放arr1中
strcpy(arr1, arr2);
printf("%s ",arr1);
return 0;
}
memory - 英语中是记忆 - 计算机内存
memset - 内存设置
memset: 类型:function
作用:Fill block of memory:Sets the first num bytes of the block-of memory pointed bu ptr to the specified valua
格式:boid * memset( void * ptr, int valua, (unsigned int)size_t num)
意思:ptr 所指向那块内存的前 num 个字节的内容全部设置成所想要的value值
prt:Pointer to the block of memory to fill. value:Value to be set num:Number of bytes to be set to the value
int main()
{
char arr[] = "hello bit";
memset(arr, 'x', 5);
printf("%s\n", arr);
return 0;
}
写一个函数可以找出两个整数中的最大值
int get_max(int x,int y)
{
int z = 0;
if (x < y)
z = x;
else
z = y;
return z; //返回z-返回较大值
}
int main()
{
int a = 10;
int b = 20;
//函数的调用
int max = get_max(a,b);
printf("max = %d\n", max);
return 0;
}
//函数返回类型的地方写成: void ,表示这个函数不返回任何值,也不需要返回
void Swap(int x, int y) //到这个函数里又为 x , y 创建了一个空间
{ // x 的地址是0x0020f894 , y 的地址是 0x0020f898
int z = 0; //交换的是 x,y ,与 a,b 没有关系,空间不是一个空间
x = z; //所以代码出问题了
y = x;
z = y;
}
int main()
{
int a = 10; //开创了两个空间,一块取名位a,一块为b
int b = 20; // a 空间的编号为:0x0022f894 , b 空间的编号为:0x0022f888
//写一个函数 — 交换两个整形变量的值
printf("交换前:a=%d b=%d\n", a, b);
Swap(a, b);
printf("交换后:a=%d b=%d\n", a, b);
return 0;
}
int main()
{
int a = 10; //发4字节的空间
int* pa = &a; //pa就是一个指针变量,把 a 的地址交给 pa , * pa 就是 a
*pa = 20;
printf("%d\n", a);
return 0;
}
void Swap(int* pa, int* pb) //*pa 访问到的就是 a , *pb 访问到的就是 b
{
int z = 0;
z = *pa;
*pa = *pb;
*pb = z;
}
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;
}