数据类型 基本类型 整型 字符型 实型(浮点型) 单精度型 双精度型 枚举类型 构造类型 数组类型 结构体类型 共用体类型 指针类型 空类型
# include <stdio.h>
# include <string.h>
# include <stdbool.h>
# define PRICE 30
void greet ( )
{ printf ( "你好!\n" ) ;
}
enum Weekday
{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
} ;
struct Student
{ char name[ 50 ] ; int age; float score;
} ;
union Data
{ int intValue; float floatValue;
} ; int main ( )
{ int age = 30 ; printf ( "年龄: %d\n" , age) ; char initial = 'A' ; printf ( "首字母: %c\n" , initial) ; float height = 170.5f ; double pi = 3.14159 ; printf ( "身高: %.1f 米\n" , height) ; printf ( "π值: %lf\n" , pi) ; bool isMale = true; printf ( "性别: %s\n" , isMale ? "男" : "女" ) ; greet ( ) ; int numbers[ 5 ] = { 1 , 2 , 3 , 4 , 5 } ; for ( int i = 0 ; i < 5 ; i++ ) { printf ( "numbers[%d]: %d\n" , i, numbers[ i] ) ; } int num = 100 ; int * ptr = & num; printf ( "数值: %d\n" , num) ; printf ( "通过指针访问数值: %d\n" , * ptr) ; enum Weekday today = Wednesday; switch ( today) { case Monday: printf ( "今天是星期一。\n" ) ; break ; case Tuesday: printf ( "今天是星期二。\n" ) ; break ; case Wednesday: printf ( "今天是星期三。\n" ) ; break ; case Thursday: printf ( "今天是星期四。\n" ) ; break ; case Friday: printf ( "今天是星期五。\n" ) ; break ; case Saturday: printf ( "今天是星期六。\n" ) ; break ; case Sunday: printf ( "今天是星期日。\n" ) ; break ; } enum { Red = 100 , Green, Blue} ; enum { Jan = 1 , Feb, Mar} ; printf ( "Green 的值是:%d\n" , Green) ; struct Student student1; strcpy ( student1. name, "张三" ) ; student1. age = 20 ; student1. score = 88.5 ; printf ( "姓名: %s, 年龄: %d, 成绩: %.1f\n" , student1. name, student1. age, student1. score) ; union Data data; data. intValue = 100 ; printf ( "作为整数时的值: %d\n" , data. intValue) ; data. floatValue = 3.14 ; printf ( "转换为浮点数后的值(可能不准确): %.2f\n" , data. floatValue) ; int num1, total; num1 = 10 ; total = num1 * PRICE; printf ( "total = % d" , total) ; return 0 ;
}