A C program consists of one or more functions, which are similar to
the functions and subroutines of a Fortran program or the procedures
of PL/I, and perhaps some external data definitions.
main
is such a function, and in fact all C programs
must have a main
. Execution of the program
begins at the first statement of main
.
main
will usually invoke other functions to perform its job,
some coming from the same program, and others from libraries.
One method of communicating data between functions is by
arguments. The parentheses following the function name
surround the argument list; here main
is a function of
no arguments, indicated by ( ). The {} enclose the
statements of the function. Individual statements end with
a semicolon but are otherwise free-format.
printf
is a library function which will format and
print output on the terminal (unless some other destination is
specified). In this case it prints
hello, world
A function is invoked by naming it, followed by a list of arguments
in parentheses.
The basic conditional-testing statement in C is the if statement:
c = getchar( );
if( c == '?' )
printf("why did you type a question mark?\n");
The simplest form of if is
if (expression) statement
The condition to be tested is any expression enclosed in
parentheses. It is followed by a statement. The expression is
evaluated, and if its value is non-zero, the statement is executed.
There's an optional else clause, to be described soon.
The character sequence `==' is one of the relational operators in C;
here is the complete set:
== equal to (.EQ. to Fortraners)
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
The value of ``expression relation expression'' is 1 if the relation
is true, and 0 if false. Don't forget that the equality test is
`=='; a single `=' causes an assignment, not a test, and invariably
leads to disaster.
Tests can be combined with the operators `&&' (AND), `||' (OR), and
`!' (NOT). For example, we can test whether a character is blank or
tab or newline with
if( c==' ' || c=='\t' || c=='\n' ) ...
C guarantees that `&&' and `||' are evaluated left to right -- we
shall soon see cases where this matters.
he basic looping mechanism in C is the while statement. Here's a
program that copies its input to its output a character at a time.
Remember that `\0' marks the end of file.
main( ) {
char c;
while( (c=getchar( )) != '\0' )
putchar(c);
}
The while statement is a loop, whose general form is
while (expression) statement
Its meaning is
(a) evaluate the expression
(b) if its value is true (i.e., not zero) do the statement, and go
back to (a)
Because the expression is tested before the statement is executed,
the statement part can be executed zero times, which is often
desirable. As in the if statement, the expression and the statement
can both be arbitrarily complicated, although we haven't seen that
yet. Our example gets the character, assigns it to c, and then tests
if it's a `\0''. If it is not a `\0', the statement part of the
while is executed, printing the character. The while then repeats.
When the input character is finally a `\0', the while terminates,
and so does main.
Text is usually kept as an array of characters, as we did with
line[ ] in the example above. By convention in C, the last character
in a character array should be a `\0' because most programs that
manipulate character arrays expect it. For example, printf uses the
`\0' to detect the end of a character array when printing it out
with a `%s'.
We can copy a character array s into another t like this:
i = 0;
while( (t[i]=s[i]) != '\0' )
i++;
Most of the time we have to put in our own `\0' at the end of a
string; if we want to print the line with printf, it's necessary.
This code prints the character count before the line:
main( ) {
int n;
char line[100];
n = 0;
while( (line[n++]=getchar( )) != '\n' );
line[n] = '\0';
printf("%d:\t%s", n, line);
}
Here we increment n in the subscript itself, but only after the
previous value has been used. The character is read, placed in
line[n], and only then n is incremented.
There is one place and one place only where C puts in the `\0' at
the end of a character array for you, and that is in the
construction
"stuff between double quotes"
The compiler puts a `\0' at the end automatically. Text enclosed in
double quotes is called a string; its properties are precisely those
of an (initialized) array of characters.
In C, as in Fortran or PL/I, it is possible to make arrays whose
elements are basic types. Thus we can make an array of 10
integers with the declaration
int x[10];
The square brackets mean subscripting; parentheses are used only for
function references. Array indexes begin at zero, so the
elements of x
are
x[0], x[1], x[2], ..., x[9]
If an array has n elements, the largest subscript is n-1.
Multiple-dimension arrays are provided, though not much used
above two dimensions. The declaration and use look like
int name[10] [20];
n = name[i+j] [1] + name[k] [2];
Subscripts can be arbitrary integer expressions.
Multi-dimension arrays are stored by row (opposite to Fortran), so
the rightmost subscript varies fastest; name has 10 rows and 20
columns.
Here is a program which reads a line, stores it in a buffer, and
prints its length (excluding the newline at the end).
main( ) {
int n, c;
char line[100];
n = 0;
while( (c=getchar( )) != '\n' ) {
if( n < 100 )
line[n] = c;
n++;
}
printf("length = %d\n", n);
}
As a more complicated problem, suppose we want to print the count
for each line in the input, still storing the first 100 characters
of each line. Try it as an exercise before looking at the
solution:
main( ) {
int n, c; char line[100];
n = 0;
while( (c=getchar( )) != '\0' )
if( c == '\n' ) {
printf("%d0, n);
n = 0;
}
else {
if( n < 100 ) line[n] = c;
n++;
}
}
Dear friends for more details of C-Language you Can AlsoVisit Here