Listing 1.1 first.c program
#include <stdio.h>
int main (viod) / * * a simple procedure c /
{
int num; / * define a variable named num * /
num = 1; / * assign a value to num * /
printf ( "I am a simple"); / * use printf () function * /
printf(“computer.\n”);
printf(“My favourite number is %d because it is first.\n”,num);
return 0;
}
Not surprisingly, the program will print out the following on the screen:
I am a simple computer.
My favourite number is 1 because it is first.
[Tips: If the output of the program on the screen flashed, you can add additional code in the program, so the window waiting for the user to press a key after the close. One way is to join the program before the return statement:
getchar();
This line of code makes the program waits for a keystroke, a window will be closed after pressing a button the user. ]
Next is some explanation of the program code:
#include <studio.h> ------- comprising another header files
// diverted to tell the compiler to studio.h included in the current program. studio.h is part of the standard C compiler package, which provides support for keyboard input and screen output.
int main (viod) ------ function name
// C comprising one or more program functions, which are substantially C program module.
//清单中其中包含了一个main()函数,它所包含的括号表明它是一个函数名。int则表示该函数返回一个整数,viod则表明main()不带任何参数。
/*一个简单的c程序*/ -------- 注释
注释在/*和*/两个符号之间。注释能提高程序的可读性。编译时,编译器会忽略所注释内容。
{-------函数体开始
{-------函数体结束
左花括号({)表示函数定义开始
右花括号(})表示函数定义结束
int num; -----声明
该声明表明,将使用一个名为num的变量,而且num是int (integer整数)类型
num=1;------赋值表达语句
//把值1赋给名为num的变量
printf(“I am a simple”); ------调用一个函数
该语句使用printf()函数,在屏幕上显示I am a simple,光标停在同一行。
//printf()是标准的C库函数,在程序中使用函数叫作调用函数
printf(“computer.\n”);
这行代码也调用了printf()函数,不同的是它加了一个\n,它的作用是告诉计算机另起一行
printf(“My favourite number is %d because it is first.\n”,num);
最后调用一个printf()把num的值(1)内嵌在用双引号括起来的内容中一并打印。%d告诉计算机以何种形式输出num的值,打印在何处。
return 0;-----return语句
C函数可以给调用方提供(或返回)一个数。
必须以右花括号表示程序结束