A sample C program
Write an example to help us get started with C.
/* first.c -- The first program */
#include <stdio.h> // preprocessor directive
int main(void) // function, first be called
{ // function define start
int num; // declaration, int -- keyword, num -- ifentifier
num = 1; // Assignment expression statement
printf("I am a simple "); // function call, "I am a simple " -- actual argument
printf("computer.\n"); // function call, main() is calling function for printf()
printf("My favorite number is %d becase it is first.\n", num); // function reference, \n is newline character, newline character is a escape sequence
return 0; // jump statement
} // function define end
sample analysis
second sample
/* m2cm.c -- from m to cm */
#include <stdio.h>
int main(void)
{
int metre, centimetre;
metre = 2;
centimetre = metre * 100;
printf("There are %d centimetres in %d metres!\n", centimetre, metre);
printf("yes, I said %d centimetres!\n", 100 * metre);
return 0;
}
third sample
/* two_func.c -- One source code file include two functions. */
#include <stdio.h>
void butler(void); // prototype
int main(void)
{
printf("I will summon the butler function.\n");
butler(); // function call
printf("Yes, Bring me some tea and writeable DVDs.\n");
return 0;
}
void butler(void) // function definition
{
printf("You rang, sir?\n");
}
???
What is the basic unit of C language program?
:function