1:C语言程序入口
C语言Helloword的实现
// C语言.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <stdio.h> //标准的输入输出库,箭头表示库文件
int _tmain(int argc, _TCHAR* argv[]) //char *=_TCHAR * 为入口函数
{
printf("helloword
");
printf("helloword");
fflush(stdin); //函数体
getchar();
return 0;
}
2:C语言变量和表达式
//求矩形的面积
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
//求矩形的面积
int a;
int b;
int c;
a=3;
b=4;
c=a*b; //c语言中的表达式:算数表达式,关系,逻辑,位运算(<<, >>,&与域 |位域 ~异域),赋值
printf("长:%d,宽:%d,面积:%d",a,b,c); 数据类型占用空间,要掌握???
fflush(stdin);
getchar();
return 0;
}
//变量的数据类型:
//short:最大内存65535:保存存小的数值 long:64位 占8个字节长度 保存大的数值,金钱
3:C语言字符类型
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
char c1;
char c2;
char c3;
c1='W'; //字符串赋值要用单引号引用
c2='1';
c3=49; 输出 C3=1 c3=50则输出c3=2
printf("c1=%c,c2=%c,c3=&c",c1,c2,c3); 再加入:c1=%d c2=%d,则c1=87 c2=49.
if(c2==49)
{
printf("OK"); //在C语言中字符类型和整数类型是可以通用的。
}
fflush(stdin);
getchar();
return 0;
}
4:C语言条件语句
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
//输入成绩,是否合格
int score;
printf("Please input:
");
scanf("%d",&score);
if(score>=60)
{
printf("成绩合格
");
}
else
{
printf("成绩不合格");
} //思考方法二if esle嵌套
fflush(stdin);
getchar();
return 0;
}
方法三:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
//输入成绩,是否合格
int score;
printf("Please input:
");
scanf("%d",&score);
switch(score/10)
{ case 10:
case 9:
printf("成绩优秀
");
break;
case 8:
printf("成绩良好
");
break;
default:
sprintf("不及格
");
break;
}
fflush(stdin);
getchar();
return 0;
}
5:C语言循环
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
//求1+2+3...+100
int n=1;
int sum=0;
while(n<=100)
{
sun=sum+n;
n=n+1;
}
rintf("1+2+3...+100=%d",sum);
fflush(stdin);
getchar();
return 0;
} //思考其他方法:
do..while方法
n=1;
sum=0;
do{
sun+=n;
n++;
}while(n<=100);
printf("1+2+3...+100=%d",sum);
for方法
n=1;
sum=0;
for(n=1;n<=100;n++)
{
sum+=n;
}
printf("1+2+3...+100=%d",sum);
拓展:求1到100内,偶数的和
for(n=1,n<=100,n++)
{
if(n/2==0) //关键
sum+=n;
}
作业:1+2+...+1000;
6:C语言学会数组处理
数组,字符串数组,字符数组
字符串就是一个字符数组
数组:定义相同类型的变量
#include <stdio.h>
#include <stdlib.h>
int main()
{
//求一组玩家金币的最高值
int player[10]={11,33,44,667,88,334,99,12,222,444}; //每个储存空间都占4个字节。
int max;
printf("第四个玩家,金币数:%d
",player[3]); //0--9 下标是当前数减一,下标可以是常量,也可以是变量,也可以是表
达式。 //下表不能超过长度减一,如果大于10就会越界,也不能小于0。
max=player[0]; //对max初始化。
for(int i=1;i<=9;i++)
{ if(player[i]>max)
{
max=player[i];
}
printf("
player[%d]= %d",i,player[i]);
}
printf("
最高金币数:%d
",max);