c语言基础

#include<stdio.h>

int main(){
    //一般都会在后面加 

    printf("hello world! 
");
    return 0;
}
#include <stdio>
int main(){
    int a,b,sum;
    a = 99;
    b = 99;
    sum = a + b;
    printf("sum = %d",sum);//%d 用来输出整数
}
#include <stdio>
int main(){
    char a = 'A';
    a = a + 32; //一个大写的char字母加上32就会等于小写字母
    printf("%c",a);//%c 用来输出char类型的字符
}
#include <stdio>
int main(){
    double a,b,sum;
    a = 3.14;
    b = 0.2;
    sum = a + b;
    printf("sum = %f 
",sum);//%f用来输出小数
    printf("sum = %d 
", sum);//如果用%d来输出小数的话,则输出0
}
#include<stdio.h>

int main() {
    int a,b,avg;
    scanf("%d%d",&a,&b);//scanf 相当于java的java.util.Scanner float用%f double用%lf
    avg = (a + b)/2;
    printf("avg = %d", avg);
}
#include<stdio.h>

int main() {
    char a,b;
    a = getchar();
    b = getchar();//从键盘键入一个字符
    a += 3;
    b += 3;
    putchar(a);
    putchar(b);//将字符输出到控制台
    putchar('
');
    return 0;
}

使用以上表中的数学函数时要在程序头加入 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 
", d);
    printf("十位数为:%d 
", c);
    printf("百位数为:%d 
", 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); /*应该使用两个数组来接收,因为空格会被解析为 */
	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
",a,i_pointer); //打印地址
	printf("a: %d, i pointer: %d
",a,*i_pointer); //打指针所以指的对象
	*i_pointer = 4; //相当于 a = 4
	printf("a: %d, i pointer: %d
",a,i_pointer);
	printf("a: %d, i pointer: %d
",a,*i_pointer);
}
#include <stdio.h>

int main(){
	int *p1,*p2,a,b; //定义两个指针变量,两个int变量
	printf("please enter two integer number: 
");
	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 
",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 
name:%s 
source:%f
",cai.No,cai.name,cai.source);
	
	Student cai2 = {1,"cai2",99.9};
	printf("No: %d 
name:%s 
source:%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 
",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
class: %d",person1.job,person1.cate.clas);
	 }else if(person1.job[0] == 't'){
		 scanf("%s",person1.cate.name);
		 printf("job: %s
class: %s",person1.job,person1.cate.name);
	 }
 }

typedef:

原文地址:https://www.cnblogs.com/lastingjava/p/10420732.html