使用以上表中的数学函数时要在程序头加入 include <math.h>
#include<stdio.h> /* 输入一个0到999的数字,输出各个位数 */ int main() { int a = 678; int b = 678/100; int c = (a-b*100)/10; int d = (a-b*100-c*10); printf("个位数为:%d \n", d); printf("十位数为:%d \n", c); printf("百位数为:%d \n", b); return 0; }三目运算符:
#include<stdio.h> int main() { //输出大的那个数,用三目运算符 int a = 10; int b = 90; (a>b)?printf("%d",a):printf("%d",b); } #include <stdio.h> int main(){ // char str1[10]; // scanf("%s",str1); // printf("%s",str1); char str2[10],str3[10]; //使用上面的代码输入hello world时,屏幕只会输出hello, scanf("%s",str2,str3); /*应该使用两个数组来接收,因为空格会被解析为\0 */ printf("%s %s",str2,str2); } #include <stdio.h> int main(){ char str[20]; gets(str); //从键盘输入字符 puts(str); //如果输入的字符包含空格,也可以正常输出 } int main(){ int a = 3; int *i_pointer; //定义一个指针类型变量 i_pointer = &a; //指向整数a printf("a: %d, i pointer: %d\n",a,i_pointer); //打印地址 printf("a: %d, i pointer: %d\n",a,*i_pointer); //打指针所以指的对象 *i_pointer = 4; //相当于 a = 4 printf("a: %d, i pointer: %d\n",a,i_pointer); printf("a: %d, i pointer: %d\n",a,*i_pointer); }#include <stdio.h> int main(){ int *p1,*p2,a,b; //定义两个指针变量,两个int变量 printf("please enter two integer number: \n"); scanf("%d%d",&a,&b); //从键盘输入 a 和 b p1 = &a; //p1指向a p2 = &b; //p2指向b if(a<b){ //如果a<b交换指针地址 p1 = &b; p2 = &a; } printf("a= %d, b= %d \n",a,b); //输出a和b的值不变 printf("max= %d, min= %d",*p1,*p2); //p1和p2和值对换了 }
自定义类型:
#include <stdio.h> int main(){ struct Student{ int No; char name[10]; double source; } cai = {1,"chaijunwei",99.9}; printf("No: %d \nname:%s \nsource:%f\n",cai.No,cai.name,cai.source); Student cai2 = {1,"cai2",99.9}; printf("No: %d \nname:%s \nsource:%f",cai2.No,cai2.name,cai2.source); }自定义类型的数组:
#include <stdio.h> int main(){ struct Student{ int No; char name[10]; double source; }; Student students[2] = { {1,"chai1",80}, {2,"chai2",99}, }; int i = 0; for(;i < 2; i ++){ printf("%s",students[i].name); } return 1; }自定义的类型,用指针传递,用->符号:
#include <stdio.h>#include <stdio.h> //自定义一个Book类型 struct Book{ int id; char name[10]; }; //声明函数 void printBook(struct Book *book); int main(){ struct Book book = {1,"java"}; printBook(&book);//将book的内存地址传入方法 } void printBook(struct Book *book){ printf("bookName: %s",book->name);//使用->来获得属性值 }链表:
#include <stdio.h> struct LinkeList{ int element; struct LinkeList *next; }; int main(){ struct LinkeList *first,node1,node2,node3; node1.element = 1; node2.element = 2; node3.element = 3; first = &node1; node1.next = &node2; node2.next = &node3; node3.next = NULL; do{ printf("%d \n",first->element); first = first->next; } while(first != NULL); return 0; }共用体类型:
#include <stdio.h> //定义一个共用体类型 union Cate{ int clas; char name[10]; }; //可能是学生也可能是老师,如果是学生就输入clas,如果是老师就输入name struct Person{ char job[10]; union Cate cate; }; int main(){ struct Person person1; scanf("%s",person1.job); if(person1.job[0] == 's'){ scanf("%d",&person1.cate.clas); printf("job: %s\nclass: %d",person1.job,person1.cate.clas); }else if(person1.job[0] == 't'){ scanf("%s",person1.cate.name); printf("job: %s\nclass: %s",person1.job,person1.cate.name); } }typedef:
转载于:https://www.cnblogs.com/lastingjava/p/10420732.html