1 /*the first c programme*/ #include <stdio.h> main(){ int num = 1; scanf("%d",&num); printf("%d",num); getch(); }
主要考察输入输出.然后输出结果是:
你输入什么就在屏幕输出什么.
2
#include "print.h" int main(void){ printHello(); getch(); return 0; } void printHello(){ printf("Hello world! "); }
主要考察函数调用
3
/*Input 2 numbers output the product*/ #include <stdio.h> main(){ int x,y,m; printf("Please input x and y "); scanf("%d%d",&x,&y); m=x*y; printf("%d * %d = %d ",x,y,m); getch(); }
主要考察函数输入输出
//获取从键盘输入字符
scanf("%d%d",&x,&y);
//打印到屏幕字符
printf("%d * %d = %d ",x,y,m);
4
/*输入两个浮点数 输出他们中的大数*/ #include <stdio.h> main(){ float x,y,c; /* 变量定义 */ printf("Please input x and y: "); /* 提示用户输入数据 */ scanf("%f%f",&x,&y); c=x>y?x:y; /* 计算c=max(x,y) */ printf("MAX of (%f,%f) is %f",x,y,c); /* 输出c */ }
5
6 输出不同类形状所占字节数
#include <stdio.h> void main(){ printf("The bytes of the variables are: "); printf("int:%d bytes ",sizeof(int)); /* char型的字节数为1 */ printf("char:%d byte ",sizeof(char)); /* short型的字节数为2 */ printf("short:%d bytes ",sizeof(short)); /* long型的字节数为4 */ printf("long:%d bytes ",sizeof(long)); /* float型的字节数为4 */ printf("float:%d bytes ",sizeof(float)); /* double型的字节数为8 */ printf("double:%d bytes ",sizeof(double)); /* long double型的字节数为8或10或12 */ printf("long double:%d bytes ",sizeof(long double)); getchar(); }
sizeof(number type);
7
/* */ #include <stdio.h> main() { int a=5,b,c,i=10; b=a++; c=++b; printf("a = %d, b = %d, c = %d ",a,b,c); printf("i,i++,i++ = %d,%d,%d ",i,i++,i++); printf("%d ",++i); printf("%d ",--i); printf("%d ",i++); printf("%d ",i--); printf("%d ",-i++); printf("%d ",-i--); getchar(); }
8
#include <stdio.h> main (){ int i,j,n; long sum=0,temp=0; printf("Please input a number to n: "); scanf("%d",&n); if(n<1){ printf("the n must no less than 1! "); return; } for(i=1;i<=n;i++){ temp = 0; for(j=1;j<=i;j++){ temp += j; } sum+=temp; } printf("The sum of the sequence(%d) is %d ",n,sum); getchar(); getchar(); }
10 010 猜数字游戏