高质量C编程10-常用知识点收集
2016-05-13 16:22
309 查看
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 |
(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()
相关文章推荐
- install jdk 1316 error
- mysql-Python数据库的连接
- QTableView中文本根据内容自动换行的简单方法
- _USER_ERROR__missing_dlib_all_source_cpp_file__OR......
- 自定义顺序存储结构线性表--java语言描述
- jvm优化堆大小设置
- [Java视频笔记]day24
- python中用的到奇淫技巧
- javac命令无法使用解决办法
- C#中is,as,using关键字的使用
- JAVA经典题目及答案(前30)
- VBA中的几个小Tips(一)
- 平衡二叉树创建,左旋,右旋,左平衡和右平衡
- Java学习篇之---环境变量和系统属性
- Struts2中使用Session的两种方法
- java的spilt(“,”)方法bug处理
- boost::string
- E - Gerald's Hexagon
- PHP PDO
- 接(第二篇) Java 多线程 编程 基础 教程