文章分类 -  C 语法

c 常用语法
摘要:一、基本数据类型 1.int 1> long int、long:8个字节 %ld 2> short int、short:2个字节 %d %i 3> unsigned int、unsigned:4个字节 %zd 4> signed int、signed、int:4个字节 %d %i 2.float\d 阅读全文
posted @ 2016-03-28 10:15 lance.xiang 阅读(95) 评论(0) 推荐(0)
摘要:/* 1.作用:给已经存在的类型起一个新的名称 2.使用场合: 1> 基本数据类型 2> 指针 3> 结构体 4> 枚举 5> 指向函数的指针 */ #include <stdio.h> typedef int MyInt; typedef MyInt MyInt2; // 给指针类型char *起 阅读全文
posted @ 2016-03-28 10:14 lance.xiang 阅读(107) 评论(0) 推荐(0)
摘要:/* 1.所有的预处理指令都是以#开头 2.预处理指令分3种 1> 宏定义 2> 条件编译 3> 文件包含 3.预处理指令在代码翻译成0和1之前执行 4.预处理的位置是随便写的 5.预处理指令的作用域:从编写指令的那一行开始,一直到文件结尾,可以用#undef取消宏定义的作用 6.宏名一般用大写或者 阅读全文
posted @ 2016-03-28 10:12 lance.xiang 阅读(207) 评论(0) 推荐(0)
摘要:#include <stdio.h> // main 主函数入口 int main() { enum Sex { Man, Woman, Unkown}; // 0男 1女 -1不详 //int sex = 3; //enum Sex s = Unkown; // 1.定义枚举类型 enum Sea 阅读全文
posted @ 2016-03-28 10:07 lance.xiang 阅读(92) 评论(0) 推荐(0)
摘要:#include <stdio.h> struct Student { int age; int no; }; // 如果结构体作为函数参数,只是将实参结构体所有成员的值对应地赋值给了形参结构体的所有成员 // 修改函数内部结构体的成员不会影响外面的实参结构体 void test(struct St 阅读全文
posted @ 2016-03-28 10:06 lance.xiang 阅读(615) 评论(0) 推荐(0)
摘要:/* 数组:只能由多个相同类型的数据构成 结构体:可以由多个不同类型的数据构成 */ #include <stdio.h> int main() { //int ages[3] = {[2] = 10, 11, 27}; //int ages[3] = {10, 11, 29}; // 1.定义结构 阅读全文
posted @ 2016-03-28 10:02 lance.xiang 阅读(120) 评论(0) 推荐(0)
摘要:#include <stdio.h> char *test(); int main() { char *name = test(); printf("name=%s\n", name); return 0; } char *test() { return "rose"; } #include <st 阅读全文
posted @ 2016-03-28 09:55 lance.xiang 阅读(82) 评论(0) 推荐(0)
摘要:一、指针变量的定义 1. 格式:变量类型 *指针变量名; 2. 举例:int *p; char *p2; 3. 注意:定义变量时的*仅仅是指针变量的象征 二、利用指针变量简单修改其他变量的值 1.指向某个变量 int a; int *p; p = &a; 或者 int *p = &a; 2.修改所指 阅读全文
posted @ 2016-03-28 09:51 lance.xiang 阅读(100) 评论(0) 推荐(0)
摘要:#include <stdio.h> /* (不包括\0) 编写一个int string_len(char *s),返回字符串s的字符长度 */ int string_len(char *s); int main() { //char *name = "itlance"; // 男 \u434\u4 阅读全文
posted @ 2016-03-28 09:50 lance.xiang 阅读(78) 评论(0) 推荐(0)
摘要:#include <stdio.h> /* 1.数组元素的访问方式 int ages[5]; int *p; p = ages; 1> 数组名[下标] ages[i] 2> 指针变量名[下标] p[i] 3> *(p + i) 2.指针变量+1,地址值究竟加多少,取决于指针的类型 int * 4 c 阅读全文
posted @ 2016-03-28 09:49 lance.xiang 阅读(131) 评论(0) 推荐(0)
摘要:#include <stdio.h> int main() { int a = 10; int *p = &a; int **pp = &p; // a = 20; // *p = 20; /* (*pp) == p *(*pp) == *p = a **pp == *p = a */ **pp = 阅读全文
posted @ 2016-03-28 09:41 lance.xiang 阅读(114) 评论(0) 推荐(0)
摘要:#include <stdio.h> void change(int); int main() { /* int a = 90; change(&a); printf("a=%d\n", a); */ // 变量类型 变量名; // 格式:变量类型 *变量名; // 定义了一个指针变量p // 指针 阅读全文
posted @ 2016-03-28 09:35 lance.xiang 阅读(110) 评论(0) 推荐(0)
摘要:/* 编写一个函数char_contains(char str[],char c), 如果字符串str中包含字符c则返回数值1,否则返回数值0 */ #include <string.h> #include <stdio.h> // 可读性 -> 性能 -> 精简(重构) int char_cont 阅读全文
posted @ 2016-03-28 09:30 lance.xiang 阅读(458) 评论(0) 推荐(0)
摘要:#include <stdio.h> int main() { //char name[] = {'l', 'a', 'c', 'H', 's', 't', '\0'}; char name[] = "lacHst"; name[3] = 'H'; /* int size = sizeof(name 阅读全文
posted @ 2016-03-28 09:29 lance.xiang 阅读(103) 评论(0) 推荐(0)
摘要:#include <stdio.h> // 数组作为函数参数,可以省略元素个数 // 数组作为函数参数,传递是整个数组的地址,修改函数形参数组元素的值,会影响到外面的实参数组 void change(int array[]) { //printf("array==%p\n", array); arr 阅读全文
posted @ 2016-03-28 09:24 lance.xiang 阅读(87) 评论(0) 推荐(0)
摘要:#include <stdio.h> /* 提示用户输入5个学生的成绩,算出平均分并且输出 */ int main() { // 1.定义一个数组来存储成绩 int scores[5]; // 2.提示输入成绩 // 用来存储总分 int sum = 0; for (int i = 0; i<5; 阅读全文
posted @ 2016-03-28 09:22 lance.xiang 阅读(274) 评论(0) 推荐(0)
摘要:#include <stdio.h> int main() { // 使用注意 // 都是正确写法 //int ages[5] = {10 , 11, 12, 67, 56}; //int ages[5] = {10, 11}; //int ages[5] = {[3] = 10, [4] = 11 阅读全文
posted @ 2016-03-28 09:19 lance.xiang 阅读(76) 评论(0) 推荐(0)
摘要:#include <stdio.h> int main() { //0000 0000 0000 0000 0000 0000 0000 1001 int a = 9; // 0100 0001 char c = 'A'; //printf("%c\n", 67); //printf("%d\n", 阅读全文
posted @ 2016-03-28 09:18 lance.xiang 阅读(122) 评论(0) 推荐(0)
摘要:/* 写一个函数,用来输出整数在内存中的二进制形式 */ #include <stdio.h> void printBinary(int number); int main() { /* 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0 阅读全文
posted @ 2016-03-28 09:16 lance.xiang 阅读(233) 评论(0) 推荐(0)
摘要:#include <stdio.h> int main() { /* 按位与 & 10101010000 00000100000 00000000000 10111011 10101101 10101001 1001 0101 0001 */ /* 按位或 | 1001 0101 1101 */ / 阅读全文
posted @ 2016-03-28 09:15 lance.xiang 阅读(135) 评论(0) 推荐(0)