源码地址 https://github.com/Wy2160640/C
001.第一个C程序
文件1.c
/* The first C programme */ #include <stdio.h> /* 包含标准输入输出头文件 */ main() /* 主函数 */ { printf("Hello World! "); /* 打印输出信息 */ }
编译运行
gcc 1.c ./a.out Hello World!
002.运行多个源文件
文件print.c
#include "print.h" void printHello() { printf("hello word! "); }
文件helloword.c
#include "print.h" int main(void){ printHello(); return 0; }
文件print.h
#include "stdio.h" void printHello(void);
编译运行
gcc print.c helloword.c ./a.out
003.求整数之积
文件3.c
/* Input two numbers, output the product */ #include <stdio.h> main() { int x,y,m; /* 定义整型变量x,y,m */ printf("Please input x and y "); /* 输出提示信息 */ scanf("%d%d",&x,&y); /* 读入两个乘数,赋给x,y变量 */ m=x*y; /* 计算两个乘数的积,赋给变量m */ printf("%d * %d = %d ",x,y,m); /* 输出结果 */ }
编译运行
gcc 3.c ./a.out
004.比较实数大小
/* 输入两个浮点数,输出它们中的大数 */ #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 */ }
编译运行
gcc 4.c ./a.out
005.字符的输出
/* */ #include <stdio.h> main() { char ch,nch; /* */ int count; /* */ int k; /* */ printf("Please input a string with a # in the end. "); scanf("%c",&ch); /* */ while(ch != '#') /* */ { if(ch >= '0' && ch <= '9') { /* */ count = ch-'0'+1; /* */ scanf("%c",&nch); /* */ for(k=0;k<count;k++) /* */ printf("%c",nch); } else printf("%c",ch); /* */ printf(" "); /* */ scanf("%c",&ch); /* */ } printf("# "); /* */ }
编译运行
gcc 5.c ./a.out
006.显示变量所占字节数
/* 输出不同类型所占的字节数*/ #include <stdio.h> void main() { /* sizeof()是保留字,它的作用是求某类型或某变量类型的字节数, */ /* 括号中可以是类型保留字或变量。*/ /*int型在不同的机器,不同的编译器中的字节数不一样,*/ /*一般来说在TC2.0编译器中字节数为2,在VC编译器中字节数为4 */ 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(); }
编译运行
gcc 6.c ./a.out
007.自增自减运算
/* */ #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(); }
编译运行
gcc 7.c ./a.out
008.数列求和
#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(); }
编译运行
gcc 8.c ./a.out
009.乘法口诀表
#include <stdio.h> #include <conio.h> void main(void) { int i,j,x,y; clrscr(); printf(" * * * ³Ë·¨¿Ú¾÷±í * * * "); x=9; y=5; for(i=1;i<=9;i++) { gotoxy(x,y); printf("%2d ",i); x+=3; } x=7; y=6; for(i=1;i<=9;i++) { gotoxy(x,y); printf("%2d ",i); y++; } x=9; y= 6; for(i=1;i<=9;i++) { for(j=1;j<=9;j++) { gotoxy(x,y); printf("%2d ",i*j); y++; } y-=9; x+=3; } printf(" "); }
编译运行
gcc 9.c ./a.out
010.猜数字游戏
#include <stdio.h> #include <conio.h> void main() { int Password=0,Number=0,price=58,i=0; clrscr(); printf(" ====This is a Number Guess Game!==== "); while( Password != 1234 ) { if( i >= 3 ) return; i++; puts("Please input Password: "); scanf("%d",&Password); } i=0; while( Number!=price ) { do{ puts("Please input a number between 1 and 100: "); scanf("%d",&Number); printf("Your input number is %d ",Number); }while( !(Number>=1 && Number<=100) ); if( Number >= 90 ) { printf("Too Bigger! Press any key to try again! "); } else if( Number >= 70 && Number < 90 ) { printf("Bigger! "); } else if( Number >= 1 && Number <= 30 ) { printf("Too Small! Press any key to try again! "); } else if( Number > 30 && Number <= 50 ) { printf("Small! Press any key to try again! "); } else { if( Number == price ) { printf("OK! You are right! Bye Bye! "); } else if( Number < price ) { printf("Sorry,Only a little smaller! Press any key to try again! "); } else if( Number > price ) printf(" Sorry, Only a little bigger! Press any key to try again! "); } getch(); } }
编译运行
gcc 10.c ./a.out