In C, the program to print ``hello, world'' is
#includemain() { printf("hello, world\n"); }
On the UNIX operating system you must create the program in a file whose name ends in `` .c '', such as hello.c , then compile it with the command cc hello.c
The compilation will proceed silently, and make an executable file called a.out . If you run a.out by typing the command a.out
Structure of a C Program
A C program, whatever its size, consists of functions and variables .
A function contains statements that specify the computing operations to be done, and variables store values used during the computation.
In our example is a function named main . Normally you are at liberty to give functions whatever names you like, but `` main '' is special - your program begins executing at the beginning of main. This means that every program must have a main somewhere.
A method of communicating data between functions is for the calling function to provide a list of values, called arguments , to the function it calls. The parentheses after the function name surround the argument list.
The statements of a function are enclosed in braces { } .
Variables and Arithmetic Expressions
#include/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } }
The program consists of the definition of a single function named main . It is longer than the one that printed `` hello, world" and introduces several new ideas, including comments, declarations, variables, arithmetic expressions, loops , and formatted output.
Any characters between /* and */ are ignored by the compiler; they may be used freely to make a program easier to understand. Comments may appear anywhere where a blank, tab or newline can
n C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements.
A declaration announces the properties of variables; it consists of a name and a list of variables, such as
int fahr, celsius;
int lower, upper, step;
The type int means that the variables listed are integers; by contrast with float , which means floating point, i.e., numbers that may have a fractional part.
The range of both int and float depends on the machine you are using; 16-bits ints, which lie between -32768 and +32767, are common, as are 32-bit ints.
A float number is typically a 32-bit quantity, with at least six significant digits and magnitude generally between about 10-38 and 1038.
C provides several other data types (The size of these objects is also machine-dependent.) :
char - "character" a single byte
short - short integer
long - long integer
double - "double-precision" floating point
Information about "printf"
printf is a general-purpose output formatting function
Its first argument is a string of characters to be printed, with each % indicating where one of the other (second, third, ...) arguments is to be substituted, and in what form it is to be printed.
For instance, %d specifies an integer argument, so the statement printf("%d\t%d\n", fahr, celsius); causes the values of the two integers fahr and celsius to be printed, with a tab ( \t ) between them.
Each % construction in the first argument of printf is paired with the corresponding second argument, third argument, etc.
If we augment each %d in the printf statement with a width, the numbers printed will be right-justified in their fields. We use printf("%3d %6d\n", fahr, celsius); to print the first number of each line in a field three digits wide, and the second in a field six digits wide
Examples of using printf :
%d - print as decimal integer
%6d - print as decimal integer, at least 6 characters wide
%f - print as floating point
%6f - print as floating point, at least 6 characters wide
%.2f - print as floating point, 2 characters after decimal point
%6.2f - print as floating point, at least 6 wide and 2 after decimal point
%o for octal, %x for hexadecimal, %c for character, %s for character string and %% for itself.
The for statement
#include/* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = 0; fahr <= 300; fahr = fahr + 20) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); }
The for statement is a loop, a generalization of the while. Within the parentheses, there are three parts, separated by semicolons.
The first part, the initialization is done once, before the loop proper is entered :
fahr = 0
The second part is the test or condition that controls the loop. This condition is evaluated; if it is true, the body of the loop is executed :
fahr <= 300
Then the increment step is executed, and the condition re-evaluated. The loop terminates if the condition has become false :
fahr = fahr + 20
As with the while, the body of the loop can be a single statement or a group of statements enclosed in braces. The initialization, condition and increment can be any expressions.
The quantities LOWER, UPPER and STEP are symbolic constants, not variables, so they do not appear in declarations.
#include#define LOWER 0 /* lower limit of table */ #define UPPER 300 /* upper limit */ #define STEP 20 /* step size */ /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); }
Symbolic constant names are conventionally written in upper case so they can ber readily distinguished from lower case variable names.
There is no semicolon at the end of a #define line.
Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters.
The standard library provides several functions for reading or writing one character at a time such as getchar and putchar.
getchar() :getchar reads the next input character from a text stream and returns that as its value i.e. c = getchar();
putchar() : prints the contents of the integer variable "c" as a character.
#include/* copy input to output; 1st version */ main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } }
getchar returns a distinctive value when there is no more input. This value is called EOF, for ``end of file". To store this we can't use char since "c" must be big enough to hold EOF in addition to any possible char. Therefore we use int.