void main() {
printf("hello world\n");
}
c语言实现hellworld
#define 标识符 常量 定义为常量 #为预处理命令
static修饰的数,为全局变量,后面操做给他变化,就实际发生变化了。
extern声明外部变量
数值是以补码方式存储的,正数的补码与原码相同。负数的补码,将改数的值取绝对值二进制数按位取反再+1
2e5指的是2乘10的五次方
字符常量a就是a,字符串常量a就是a\0
自己写的文件导入用#include “xxx”
#include <stdio.h>
main(){
char c;
c=getchar();
putchar(c);
}
getchar与putchar的用法
a=567,567是值。a是变量,&a是地址值。
scanf(%d,%*d,%d,&a,&b,&c) 输入123时带*号的会自动舍去
ccanf(%5d,&a)只能截取5个数字
C语言中if(){
}
#include <stdio.h>
int main(void){
int i,sum=0;
i=1;
while(i<=100){
sum=sum+i;
i++;
}
printf("%d\n",sum);
return 0;
}
while循环语句用法
#include <stdio.h>
int main(void){
int a;
printf("input integer number: ");
scanf("%d",&a);
switch (a){
case 1:printf("Monday\n");
case 2:printf("Tuesday\n");
case 3:printf("Wednesday\n");
case 4:printf("Thursday\n");
case 5:printf("Friday\n");
case 6:printf("Saturday\n");
case 7:printf("Sunday\n");
default:printf("error\n");
}
return 0;
}
switch语句用法
#include <stdio.h>
main(){
void a();
void b();
a();
printf("-----");
b();
}
void a(){
printf("xxxxxxx");
}
void b(){
printf("aaaaaaa");
}
函数基本调用写法
#include <stdio.h>
main(){
int max(int x ,int y);//声明
int a,b,c;
scanf("%d",&c);
scanf("%d",&b);
a=max(b,c);
printf("最大的是%d",a);
}
int max (int x,int y){
return x>y?x:y;
}
带形参的函数传入方法
#include <stdio.h>
void a();
void b();
main(){
a();
}
void a(){
void b();
printf("aaaaaa");
b();
}
void b(){
printf("bbbbbb");
}
函数的嵌套调用书写方法
#include <stdio.h>
main(){
int a,b;
int *pointa,*pointb;
a=10;
b=20;
pointa=&a;
pointb=&b;
printf("%d,%d",a,b);
printf("%d,%d",*pointa,*pointb);
}
指针的运用,*代表的是取地址
a表示a对应的存储单元中的数据。
&a表示a对应的存储单元的地址。
*a表示:首先,要求a对应的存储单元中的数据一定是另一个存储单元的地址。
于是,*a表示另一个存储单元中的数据。
当a声明的类型是int时,a中存储的是一个整数数值,通过a可以访问(读取或修改)这个数值。
当a声明的类型是int*时,a中存储的是一个存储单元的地址,而该存储单元中存储的数据是一个整数数值;通过*a可以访问(读取或修改)这个数值。a == &*a 都是该存储单元的地址。
当a声明的类型是int**时,a中存储的是一个存储单元的地址,而该存储单元中存储的数据是另外一个存储单元的地址,另外这个存储单元中存储的是一个整数数值;通过**a可以访问(读取或修改)这个数值。 getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).当用户键入回车之后,getchar才开始从stdio流中每次读入一个字符.getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键.
#include <stdio.h>
#define p char*
typedef char * a;
main(){
p x,y;//相当于char *a,b
a a,b;//相当于char *a.*b
printf("%d %d\n",sizeof(x),sizeof(y));8 1
printf("%d %d",sizeof(a),sizeof(b));8 8
}
typedef与define的定义
#define N 1+2
float a = N/2.0;
/*
按照常规做法,可能会认为结果是3/2 = 1.5
但是实际上,结果应该为1+2/2.0 = 2.0
若想要实现3/2,则#define N (1+2)
即为避免边缘效应,一定要加!括!号!
*/
#define还可以控制流程开关
#define xx 0 or 1
#if 1 {
}
#else 0{
}
#include <stdio.h>
struct student
{
char sex;
int age;
} boy={'m',11};
main(){
struct student *p;
p = &boy;
printf("%c %d",boy.sex,boy.age);
printf("%c %d",(*p).sex,(*p).age);
}
结构体的使用与结构体结合指针的使用。
#include <stdio.h>
struct student
{
char sex;
int age;
struct student *next;
};
main(){
struct student *head,a,b;//之前是*a下面就是-> 若声明的是结构体就用.若是指针就是->
a.age=12;
a.sex='m';
b.age=13;
b.sex='w';
head=&a;
a.next=&b;
do {
printf("%d %c\n",head->age,head->sex);
head=head->next;
} while(head);
}
链表的使用与结构体指针的定义。
结构体所占内存长度是所有成员所占内存长度。共用体最长长度等最大成员的长度
#include <stdio.h>
#include <stdlib.h>
#define TOTAL 4 //人员总数
struct{
char name[20];
int num;
char sex;
char profession;
union{
float score;
char course[20];
} sc;
} bodys[TOTAL];
int main(){
int i;
//输入人员信息
for(i=0; i<TOTAL; i++){
printf("Input info: ");
scanf("%s %d %c %c", bodys[i].name, &(bodys[i].num), &(bodys[i].sex), &(bodys[i].profession));
if(bodys[i].profession == 's'){ //如果是学生
scanf("%f", &bodys[i].sc.score);
}else{ //如果是老师
scanf("%s", bodys[i].sc.course);
}
fflush(stdin);
}
//输出人员信息
printf("\nName\t\tNum\tSex\tProfession\tScore / Course\n");
for(i=0; i<TOTAL; i++){
if(bodys[i].profession == 's'){ //如果是学生
printf("%s\t%d\t%c\t%c\t\t%f\n", bodys[i].name, bodys[i].num, bodys[i].sex, bodys[i].profession, bodys[i].sc.score);
}else{ //如果是老师
printf("%s\t%d\t%c\t%c\t\t%s\n", bodys[i].name, bodys[i].num, bodys[i].sex, bodys[i].profession, bodys[i].sc.course);
}
}
return 0;
}
union的用法如上
C语言允许用户使用 typedef 关键字来定义自己习惯的数据类型名称,来替代系统默认的基本类型名称、数组类型名称、指针类型名称与用户自定义的结构型名称、共用型名称、枚举型名称等。一旦用户在程序中定义了自己的数据类型名称,就可以在该程序中用自己的数据类型名称来定义变量的类型、数组的类型、指针变量的类型与函数的类型等。
关于extern关键字,http://blog.csdn.net/xingjiarong/article/details/47656339可以引用变量与方法、