1.常用的4个返回关键词
关键词 | 作用 |
---|---|
continue | 结束一次循环 |
break | 结束循环 |
return | 结束一个函数(包括自定义函数) |
exit | 结束整个程序 |
2.const修饰的指针
- const离谁最近,const就修饰谁
- const修饰谁,谁就不能变
const char *p; //*p不能变
char const *p; //*p不能变
char * const p; //p不能变
const char * const p; //p和*p都不能变
3.string.h中的内存操作函数
函数原型 | 作用 |
---|---|
void *memset(void *str,char ch,unsigned n) | 将str 中n 个字节设置为ch |
void *memchr(void *str,char ch,unsigned n) | 在str 前n 个字节中搜索ch 第一次出现的地址 |
int memicmp(void *str1,void *str2,unsigned n) | 比较两个字符串str1 和str2 前n 个字节,忽略大小写 |
void *memcpy(void *des,void *sou,unsigned n) | 从sou 中拷贝n 个字节到des 中 |
void memmove(void *des,void *sou,unsigned n) | 将sou 中前n 个字节拷贝到des 中,替换des 前n 个字节 |
注:如果源和目标参数有重叠,使用memmove
比memcpy
有保障
4.time.h中常用时间函数
函数原型 | 函数功能 | 函数用法 |
---|---|---|
time_t time(time_t *time); | 返回系统当前日历时间. 如果系统丢失时间设置,则函数返回-1 | 对time() 的调用,既可以使用空指针,也可以使用指向time_t类型变量的指针 |
struct tm *localtime(const time_t *time); | 返回指向以tm结构形式的分解形式time的一个指针,该事件表示本地时间(即所用计算机上的时间). | 变元time指针一般通过调用函数time() 获得 |
char *asctime(const struct tm *ptr); | 返回指向一个串的指针. | ①其中保存ptr所指结构中存储信息的变换类型,具体格式如下day month date hours:minutes:seconds year \n \0 例如: Fri Apr 15 9:15:27 2016 ②由ptr指向的结构一般是通过调用 localtime() 或gmtime() 得到的③保存 asctime() 返回的格式化时间串空间是静态空间变量,因此每次调用asctime()时都用新串冲掉该静态字符数组中的原值 |
struct tm *gmtime(const time_t *time); | 返回一个指针,指针指向以tm结构形式的分解格式time.时间用UIC(Coordinated Universal Time) 即格林尼治时间表示.如果系统不支持 UTC ,即返回空指针 | time指针一般是通过调用time() 取得,gmtime() 用来存放分解时间的结构变量,是静态分配的,每次调用gmtime() 都需要重写 |
//函数举例
#include <stdio.h>
#include <time.h>
int main(int argh,const char *argv[])
{
struct tm *local;
time_t tm;
tm = time(NULL);
local = localtime(&tm);
printf("Local time and date:%s\n",asctime(local));
local = gmtime(&tm);
printf("UTC time and date:%s\n",asctime(local));
return 0;
}
/**程序运行结果
* Local time and date:Sat May 10 16:05:18 2013
* UTC time and date:Sat May 10 08:05:18 2013
*/
5.排序函数qsort()
(1)函数原型 void qsort(void *buf,size_t num,size_t size,int (*compare)(const void *,const void *));
(2)头文件stdlib.h
(3)函数qsort()
使用了QuickSort算法对buf指向的数组进行排序.QuickSort算法一般被认为是最佳的通用排序算法
(4)变元num指出数组中元素的数目
(5)size说明每个元素的大小(按字节)
(6)compare指向的函数用于比较数组的两个元素
compare函数必须是如下的形式: int func_name(const void *arg1,const void *arg2)
其返回值必须为:
比较 | 返回值 |
---|---|
arg1 < arg2 | < 0 |
arg1 = arg2 | = 0 |
arg1 > arg2 | > 0 |
数组必须按照升序排序,最低地址处存放最低元素,如果数组中,不含关键字(key),函数返回空指针
(7)程序举例
#include <stdio.h>
#include <stdlib.h>
int main(int argh,const char *argv[])
{
//变量定义
int comp(const void *,const void *);
void printEveryElemntOfArray(int count,int *array);
int i;
int num[12] = {14, 5, 9, 7, 6, 0, 91, 4, 1, 3, 2, 8};
//排序前
printf("Original array:");
printEveryElemntOfArray(12,num);
//排序操作
qsort(num,12,sizeof(int),comp);
//排序后
printf("Sorted array:");
printEveryElemntOfArray(12,num);
return 0;
}
int comp(const void *num1,const void *num2)
{
return *(int *)num1 - *(int *)num2;
}
void printEveryElemntOfArray(int count,int *array)
{
for (i = 0; i < count; i++)
{
printf("%d",array[i]);
}
printf("\n");
}
6.四舍五入函数(math.h)
函数原型 | 作用 |
---|---|
double floor(double x); | 四舍 |
double ceil(double x); | 五入 |
//程序举例
#include <stdio.h>
#include <math.h>
int main(int argh,const char *argv[])
{
double number = 123.54;
double down = 0;
double up = 0;
down = floor(number);
up = ceil(number);
printf("number = %5.2lf\n",number);
printf("down = %5.2lf\n",down);
printf("up = %5.2lf\n",up);
return 0;
}
/**运行结果 *number = 123.54 *down = 123.00 *up = 124.00 */
7.计算x的y次幂
double pow(double x,double y)
8.延时函数
unsigned sleep(signed seconds) //头文件unistd.h
9.由一个日期,怎么知道是星期几?
①mktime()
②localtime()
③或用如下代码
// 0 = sunday
int dayOfWeek(int year,int month,int d)
{
static int t[] = {0,3,2,5,0,3,5,1,4,6,2,4};
y -= m < 3;
return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
}
10.怎样显示一个百分比或转动的短棒的进展表示器
①输出字符\r
通常可以复写当前行
②字符\b
代表退格,通常会使光标左移一格.记住要用fflush()