一、C语言简单程序简介
#include<stdio.h> //指示和头文件,#include是编译预处理指令。
stdio.h包含了输入输出的函数信息以供编译器使用
int main() //C程序是从main函数开始的,main函数是C程序的基本模块
{ //花括号是表示函数体的开始与结束
int num; //声明,在C中所有变量必须先声明后使用
num=1; //赋值
printf("I am a simple "); //C语言的标准函数printf()。作用是打印,即输出
printf("computer.\n");
printf("My favourite member is %d because it is first.\n",num);
return 0; // return语句,因为main函数为int型,所以要有返回值。
}
二、C语言的结构
一个简单的标准C程序的格式
#include<stdio.h>
int main()
{
statements
return 0;
}
三、函数的调用(多个函数)
#include<stdio.h>
void butler(); //函数原型即函数的声明
int main()
{
printf("I will summon the butler function.\n");
butler(); //函数的调用
printf("Yes.Bring me some tea and writeable CD-ROMS.\n");
return 0;
}
void butler() //函数的定义
{
printf("You rang,sir?\n");
}
练习:
1.3
#include<stdio.h>
int main()
{
int n;
printf("Enter your age:");
scanf("%d",&n);
n = n*365;
printf("Days: %d\n",n);
return 0;
}
1.5
#include<stdio.h>
int main()
{
int toes,m,n;
toes = 10;
m = toes+toes;
n = toes*toes;
printf("toes = %d, toes+toes = %d, toes*toes = %d\n",toes,m,n);
return 0;
}
1.7
#include<stdio.h>
void one_three();
void two();
int main()
{
printf("starting now: \n") ;
one_three();
two();
printf("three!\n");
printf("done!\n");
return 0;
}
void one_three()
{
printf("one\n");
}
void two()
{
printf("two\n");
}
有一个疑问:
#include<stdio.h>
int main()
{
int dogs; // 把int 改为char ,%d改为%c ,
// 用Linux的Vim编译运行时,输入字符a,输出的结果不是a,
printf("How many dogs do you have?\n");
// 而 用VC6.0编译运行时输出就是a
scanf("%d",&dogs);
printf("So you have %d dog(s)!\n",dogs);
return 0;
}
欢迎有其他朋友留言交流!