Professional Documents
Culture Documents
Elements of a C Program
Program D. 1 is an example of a simple program in C. Let us walk through this program and start to
see what the different lines are doing.
The characters /* and */ mark the start and end of the program comment. As with any
programming language, program comments are used to put clarificatory notes within the program
code, and are ignored by the compiler. Program comments may be used anywhere in the program,
and may be of any length. However, the programmer should check that no part of the actual code
is in between the /* and */ characters so that it will not be ignored by the compiler. In compilers
that support both C and C++, the double forward slash ( // ) is also used for comments. The
comments. however, extend only from the double slash at the end of the line.
This C program starts with #include <stdio.h>. This line is called a preprocessor directive and it
will include the "standard I/O library into your program. The standard I/O library lets you read
input from the keyboard (called "standard in"), write output to the screen (called "standard out"),
process text files stored on the disk, and so on. It is an extremely useful library. C has a large
number of standard libraries like stdio, including string, time and math libraries. A library is
simply a package of code that someone else has written to make your programming easier.
The line main() declares the main function. Every C program must have a function named main
somewhere in the code. At run time, program execution starts at the first line of the main function.
In C, the { and } symbols mark the beginning and end of a block of code. In this case, the block
of code making up the main function contains three lines.
The printf statement in C allows you to send output to standard out (for us, the screen). The text to
be printed is in double quotes ("1his is the output of my first C program.\n"). The \n at the end of
the text tells the program to print a newline as part of the output.
Return 0, return a value from the function or stop the execution of the function. Another option is
using the getch( ) function; line that reads a character from the keyboard without echo on the on
screen and does not wait for carriage return. More uses of getch( ) and other user inputs will be
discussed later.
/* A simple C Program */
#include <stdio.h>
#include <stdlib.h>
main()
{ printf("This is the output of my first C program.\n");
getch();
}// end of Program D. 1
Sample Output
This is the output of my first C program.
Bit The smallest unit of information in a computer system, short for binary digit.
Byte A group of eight bits, holds the equivalent of one character (single letter,
number or special symbol). One kilobyte is equal to 1024 bytes.
There are five atomic data types in C, namely char (character), int (integer), float (floating
point), double (double floating point) and void (valueless). The sizes of these types are shown in
Fqble E. l.
Values of type char are used to hold ASCII characters or any 8-bit quantity. Variables of type
int are used to hold integer quantities. Variables of type float and double are used to hold real
numbers (real numbers have both an integer and a fractional component).
The void type has three uses. The first is to declare explicitly a function as returning no value;
the second is to declare explicitly a function as having no parameters; the third is to create generic
pointers.
E. 2 Type Modifier
Excepting type void, the basic data types may have various modifiers preceding them. A
modifier is used 10 ulter the meaning of the base type to fit the need% of various situations more
precisely. The four basic C modifier are signed, unsigned, long and short.
The modifiers signed, unsigned, long and short may be applied to character and integer base
types. However, long may also be applied to double. Table E.2 shows all allowed combinations that
adhere to the ANSI C standard, along with their bit widths and range assuming a 16-bit word.
The use of signed on integers is redundant (but allowed) because the default integer
declaration assumes a signed number.
Table E.2 All Possible Combinations of C's Basic Types and Modifiers
The following are the rules for naming variables in C, while table E.3 shows examples of
valid and invalid variable names. Note: The same rules may also be applied in naming C functions.
o
Variable names may contain letters, digits or an underscore.
o
Variable names cannot begin with a digit.
o
Variable names cannot contain punctuation marks, math symbols or other special symbols.
o
ANSI guarantees only the first 32 characters to be significant.
o
Names reserved for function names and the standard library cannot be used as variable
names.
C is a case sensitive language, so care must be observed when choosing and using variable names.
Example, variable low is different from variable Low.
A variable declaration begins with the type, followed by the name of one or more variables,
separated by a comma and terminated by a semicolon. For example,
Declarations can be spread out, allowing space for an explanatory comment. Variables can also be
initialized when they are declared; this is done by adding an equal sign and the required value after the
declaration.
a = b
An expression is the smallest syntactical unit in C, and it is composed of operands and operators.
Operands are variable names upon which an operator acts. In the above example a and b are the
operands, while the equal sign is the operator.
#include <stdio.h>
#include <conio.h>
int main ( )
{
int a, b, c; / *variable declaration* /
clrscr ( ) ;
printf("%d + %d= %getch() ;
}//end of Program E.1
Sample Output:
5 + 7 = 12
The computer adds the value in a (5) to the value in b (7) to form the result 12, and then places
that new value (12) into the variable c. The variable c is assigned the value 12. For this reason,
the = in this line is called "the assignment operator."
The printf statement then prints the line "5 + 7 12." Three %d characters appeared in the
double-quoted character sequence. These are called conversion character sequences, which are
used to convert the data associated with the conversion character, and at the end of the printf
line there are three variable names: a, b and c. C matches up the first %d with a and substitutes
5 there. It matches the second %d with b and substitutes 7. It matches the third %d with c
and substitutes 12. Then it prints the completed line to the screen: 5 + 7 = 12. The +, the = and
the spacing are a part of the format line and get embedded automatically between the %d
operators as specified by the programmer. Table E.4 lists the various conversion characters
that may be used with the printf() function.
x,X Converts int to unsigned hexadecimal number. If uppercase X is used, the hex letters are
uppercase.
F Converts double to decimal notation.
% Displays a % sign.
Also, note that the first statement character within the quotation marks of the printf() statement
is the backslash ( \ ). This character is used to tell printf() an escape sequence is to be executed,
depending on what comes after the backslash. The next character after the backslash is ‘n’, and this
causes whatever follows to be printed on the next or new line. Taken together, the \n is called the
newline escape sequence used in C.