You are on page 1of 6

D.

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.

Program D.1 A Simple C Program

/* 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.

E. Data Types and Variables

E.1 Data Types


C deals directly with the computer to manipulate data, defining a couple of terms will be a lot of help
in understanding how C works with data.

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.

Table E.1 Size and Range of C’s Basic Data Types

Type Bit Width Range


char 8 0 to 255
int 16 -32768 to 32767
float 32 3.4E-38 to 3.4E+38
double 64 1.7E-38 to 1.7E+38
void 0 valueless

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

Type Bit Width Range


char 8 -128 to 127
unsi ned char 8 O to 255
si ned char 8 -128 to 127
Int 16 -32768 to 32767
unsi ned int 16 O to 65535
si ncd int 16 -32768 to 32767
short int 16 -32768 to 32767
unsi ncd short int 16 O to 65535
si ned short int 16 -32768 to 32767
Lon int 32 -2147483648 to 2147483647
unsi ed Ion int 32 O to 4294967295
si ned Ion int 32 -2147483648 to 2147483647
Float 32 3.4E-38 to 3.4E+38
Double 64 I .7E-308 to 1.7E+308
Lon double 64 I .7E-308 to J.7E+308

E.3 Program Variables


In C, a variable must be declared before it can be used. Variables can be declared at the start
of any block or code, but most are found at the start of each function. Most local variables are created
when the function is called, and are destroyed on return from that function.

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.

Table E.3 Examples of valid and invalid variable names.

Valid Variable Names Invalid Variable Names


IncomeTaxRate 1stsemester
average_score %interest
_total_amount salary+allowance
port368 result!

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,

int high, low;


double balance, profit, loss;

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.

int high = 250; /* Maximum Temperature * /


int low = -40; / * Minimum Temperature * /
Just like in algebra, program variables are given their values through an expression, such as:

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.

However, C cannot act on an expression alone. For an expression to become a valid


instruction, it must become a statement being terminated an expression with a semicolon. A statement
is an expression, or a group of expressions, terminated with a semicolon. For example:
a = b;
is a statement that assigns the value of the variable b into the variable a. It is thus considered an
assignment statement, and the equal sign, the assignment operator. When the compiler sees the
semicolon at the end of the statement, it knows that the statement is complete, and the compiler can
go ahead with generating the appropriate object code for that statement.
Program E. 1 illustrates how a variable is defined, how a value is assigned to it, and how
the contents are displayed.

Program E. 1 Variables and Assignment Statements

#include <stdio.h>
#include <conio.h>
int main ( )
{
int a, b, c; / *variable declaration* /

a=5; /*assignment statement* /


b=7; /*assignment statement* /
c=a + b; /*assignment statement* /

clrscr ( ) ;
printf("%d + %d= %getch() ;
}//end of Program E.1

Sample Output:
5 + 7 = 12

Here is an explanation of the different lines in Program E. 1 :


 The int a,b,c; declares three integers variables named a, b, c. Integer variables hold whole
numbers.
 The next line initializes the variable named a to the value 5.
 The next line sets b to 7.
 The next line adds a and b and "assigns" the result to c.

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.

Table E.4 Printf( ) Conversion Characters


Character Description
c Converts int to an unsigned char and displays it.

d,l Converts int to a signed decimal number.

o Converts int to unsigned octal number.

u Converts int to unsigned decimal number.

x,X Converts int to unsigned hexadecimal number. If uppercase X is used, the hex letters are
uppercase.
F Converts double to decimal notation.

e, E Converts double to scientific notation.

g, G Converts double to the f or e style, whichever is shorter.

ld Converts long to a signed decimal number.

lu Converts long to an unsigned decimal number.

s Displays an array of characters as a string.

p Displays an 1value as a memory address.

% 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.

Table E.5 Escape Sequence


\a Alert, causes an audible or virtual alert
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\\ Displays a backslash
\’ Displays a single qoute
\” Displays a double qoute

You might also like