java语言c语言基础_c语言基础

#include

int main(){

//一般都会在后面加 \n

printf("hello world! \n");

return 0;

}

#include

int main(){

int a,b,sum;

a = 99;

b = 99;

sum = a + b;

printf("sum = %d",sum);//%d 用来输出整数

}

#include

int main(){

char a = 'A';

a = a + 32; //一个大写的char字母加上32就会等于小写字母

printf("%c",a);//%c 用来输出char类型的字符

}

#include

int main(){

double a,b,sum;

a = 3.14;

b = 0.2;

sum = a + b;

printf("sum = %f \n",sum);//%f用来输出小数

printf("sum = %d \n", sum);//如果用%d来输出小数的话,则输出0

}

#include

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

int main() {

char a,b;

a = getchar();

b = getchar();//从键盘键入一个字符

a += 3;

b += 3;

putchar(a);

putchar(b);//将字符输出到控制台

putchar('\n');

return 0;

}

7cec5941dcaa08823c7848938a4d7939.png

使用以上表中的数学函数时要在程序头加入 include

#include

/*

输入一个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

int main() {

//输出大的那个数,用三目运算符

int a = 10;

int b = 90;

(a>b)?printf("%d",a):printf("%d",b);

}

#include

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);

}

078f5913306e6f975eac0efbc431447d.png

46be35c1e0ab40cfeb851aa38b9be181.png

#include

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

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

p1 = &b;

p2 = &a;

}

printf("a= %d, b= %d \n",a,b); //输出a和b的值不变

printf("max= %d, min= %d",*p1,*p2); //p1和p2和值对换了

}

0a5ed1e6adc9d1b274a7bc6c590a04ba.png

自定义类型:

#include

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

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 #include

//自定义一个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

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

//定义一个共用体类型

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:

ca4ae24dcb0dbfcd9cb44bbc9fdd34c4.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值