定义常量的两种方式
#define AGE 30
和const int AGE = 30;
指针变量(图解)
变量类型
#include<stdio.h> int main(){ int a = 5; //整型变量 float b = 1.1234567890; //浮点数变量 char c = 'n'; //字符变量, '' char *d = "abc"; //字符串变量, "" printf("%d\n", a); printf("%f\n", b); //精确到小数点后6位 printf("%c\n", c); printf("%s\n", d); }
字符串简单操作
#include<stdio.h> int main(){ char a[] = {'h', 'e', 'l', 'l', 'o', '\0'}; char a1[6] = {'h', 'e', 'l', 'l', 'o'}; char b[30] = "hello"; char c[15] = "programgirl"; char d[15]; strcpy(d,b); //将第二个变量赋值给第一个变量 printf("a=%s\na1=%s\nb=%s\nd=%s\n", a, a1, b, d); strcat(b,c); //将第二个变量拼接在第一个变量的后面 printf("a=%s\na1=%s\nb=%s\nd=%s\n", a, a1, b, d); printf("a-length:%d\n",strlen(a)); printf("a1-length:%d\n",strlen(a1)); printf("b-length:%d\n",strlen(b)); /*结果 a=hello a1=hello b=hello d=hello a=hello a1=hello b=helloprogramgirl d=hello a-length:5 a1-length:5 b-length:16 */ return 0; }
结构体
struct
和typedef
#include<stdio.h> //定义结构体 Person struct Person { char name[50]; //名字 int age; //年龄 }; //定义结构体 Teams, typedef 为 Team类型取一个新名字 Team typedef struct Teams { char name[50]; //名字 int count; //人数 }Team; typedef int NUM; int main(){ struct Person person; //声明 person,类型为 Person strcpy(person.name, "xiaoming"); person.age = 20; // 用 typedef 取得新名字 Team 来定义 Teams 类型的结构体 team Team team; strcpy(team.name, "teamname"); team.count = 10; NUM a = 18; // 输出 person信息 printf("person name:%s\n", person.name); printf("person age:%d\n", person.age); // 输出 team 信息 printf("team name:%s\n", team.name); printf("team count:%d\n", team.count); // 输出a 的值 printf("a:%d\n",a); /*结果 person name:xiaoming person age:20 team name:teamname team count:10 a:18 */ return 0; }