You are on page 1of 7

Programming using C UNIT – I I Semester B.C.

FUNDAMENTALS

Program

A Computer program is a collection of instructions necessary to solve a


specific problem. To solve a problem using a computer, we must express the
solution to the problem in terms of the instructions of the computer. The
approach or method that is used to solve the problem is known as an algorithm.

To develop a program for solving a particular problem, we first express the


solution to the problem in terms of an algorithm and then develop a program
that implements that algorithm.

Ex : Algorithm to find no. is even/odd.

1. Divide the number by two.

2. If the remainder of the division is zero, the number is even; otherwise, the
number is odd.

Computer Language

The instructions are written in the statements of a particular computer


language.

Computer Language may be

1. Machine language
2. Assembly language
3. Higher-Level language

INTRODUCTION TO ‘C’

‘C’ is a powerful general purpose and most popular computer programming


language. It was developed in the 1970’s by Dennis M. Ritchie at Bell Telephone
Laboratories along with the UNIX operating system. ‘C’ is an outgrowth of BCPL
(Basic Combined programming language) and ‘B’ language.

‘C’ is often called a middle-level computer language, because it has the


features of both high-level languages, such as BASIC, PASCAL etc., and low-level
languages such as assembly language.

Department of Computer Science 1 A.S.N. Degree College, Tenali


Programming using C UNIT – I I Semester B.C.A

‘C’ has very small instruction set. But the actual implementation varies from
compiler to compiler and includes extensive library functions, which enhance the
basic instructions. For example, all the input and output commands are
implemented as functions but not kept as part of the language constructs.

‘C’ is highly portable i.e., software written for one computer can be run an
another computer. ‘C’ language is well suited for structured programming.
A ‘C’ program is basically a collection of functions. It encourages us to write our
own functions and add to the ‘C’ library. Also ‘C’ is modular i.e. a unit of task can be
performed by a single function. The large number of functions makes programming
task simple.

The ‘C’ character set

A character denotes any alphabet, digit or special symbol used to represent


information. Following are the valid alphabets, numbers and special symbols
allowed in C.
Alphabets : A, B, C-----------X, Y, Z.
a, b, c-----------x, y, z.
Digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Special Symbols :

~ Tilde
‘ Apostrophe/Single quote
! Exclamatory
@ At the rate
# Hash
% Percentage
^ Caret
& Ampersand
* Asterisk
( Open parenthesis
) Closing parenthesis
_ Underscore
- Hyphen
+ Plus sign
= Equal sign
\ Back slash
/ Forward slash
{ Open brace
} Closing brace
[ Open bracket

Department of Computer Science 2 A.S.N. Degree College, Tenali


Programming using C UNIT – I I Semester B.C.A

] Closing bracket
: Colon
; Semi colon
“ Double quotes
? Question mark
< Less than symbol
> Greater than symbol
| Pipe symbol
$ Dollar sign

‘C’ Tokens : A ‘C’ program must be a syntactically valid sequence of characters of


the language. In every ‘C’ program, the most basic element recognized by the
compiler is a single character or group of characters called ‘C’ token.

A token is an atomic unit of source program i.e. the compiler will not break
down the token further. For example main, { }, () are all tokens of ‘C’ program. The
tokens comprise the basic elements of the language.

Key words : Key words are predefined tokens in C. These are also called as
reserved words. Keywords have special meaning to the ‘C’ compiler. These
keywords can be used only for their intended action; they cannot be used for any
other purpose.

The standard keywords are:

auto break case char const continue

default do double else enum extern

float for goto if int long

register return short signed sizeof static

struct switch typedef union unsigned void

volatile while

Identifiers : Identifiers are names we give to entities such as variables,


functions, structures etc. They are created to unique name to a C entity to identify
it during the execution of a C program.

Department of Computer Science 3 A.S.N. Degree College, Tenali


Programming using C UNIT – I I Semester B.C.A

Structure of ‘C’ program

‘C’ program consist of one or more functions. Each function performs a


specific task. A function is group or sequence of ‘C’ statements that are executed
together. And it is similar to sub programs in other high-level languages.

Every ‘C’ program starts with a function called main(). This is the place where
program execution begins. The functions are building blocks of ‘C’ program. Each
function has a name and a list of parameters or arguments that the function will
receive. Arguments are variables or values sent to the function, which are used
inside the function.

The following is a simple ‘C’ program structure.

# include <header files>


#define macro definitions
function proto type declarations;
global variables declarations ;
/* C program structure */
main( )
{
variable declarations
statements
and
function calls
}
The first line include “header files” tells the compiler to read the mentioned
header file. #define is used to define macro. Function prototyping declarations and
global variable declarations will be given at global area. The next line is a comment
line. Comments in the ‘C’ program are optional and may appear anywhere in a ‘C’
program. Comments are enclosed between /* and */.

main( ) is the start of the main program. The word main is followed by a pair
of ordinary parenthesis( ), which indicates that main is also a function. The left brace
‘{ ‘ represents the beginning of the function where as the right brace ‘}’ represents the
end of the function. The main contains variable declarations, program statements
and function calls.

Basic rules to write ‘C’ program:

The following are some of the rules to write ‘C’ programs.

1. All ‘C’ statements end with semicolon.

Department of Computer Science 4 A.S.N. Degree College, Tenali


Programming using C UNIT – I I Semester B.C.A

2. ‘C’ is case-sensitive. That is, upper case and lower case characters are different.
generally the statements are typed in lower case.
3. A ‘C’ statement can be written in one line or it can split into multiple lines.
4. Braces must always match upon pairs i.e., every opening brace ‘{‘ must have a
matching closing brace ‘}’.
5. No restriction in using blank spaces or blank lines, that separate different words
or different parts of a program. But blank spaces are not allowed within a
variable, constant or keyword.
6. Comments cannot be nested.
7. A comment can be split into more than one line.

Comments :

A comment is used in a program to document a program and to enhance its


readability. A comment can be initiated by two character / and *, terminated by
the characters * and /. All characters included between /* and */ are treated as
part of comment statement and are ignored by the ‘C’ compiler.

Compiling and Executing ‘C’ Programs

A Compiler is a software that translates program written in higher-level


language i.e. source program to object code.

Fig. Below shows the steps that are involved in entering, compiling and
executing a ‘C’ program.

Department of Computer Science 5 A.S.N. Degree College, Tenali


Programming using C UNIT – I I Semester B.C.A

Start

Edit
Source
Program
Prog1.c
Compile
(and assemble)

Yes
Object
Errors?
Program
No Prog1.obj

Link
Libraries Executable
Program
and other
Prog1.exe
object
programs Execute

No

Results
OK ?
Yes

Done

The program is first typed into a File like prog1.c on the computer system. ‘C’
program has the .c as extension. A text editor is used to enter the ‘C’ program. The
program that is entered into the file is known as source program.
The compilation process is initiated by giving a special command on the
system.

Department of Computer Science 6 A.S.N. Degree College, Tenali


Programming using C UNIT – I I Semester B.C.A

In the first step of the compilation process, the compiler examines each
program statement contained in the source program and checks to ensure that it
conforms to the syntax and semantics of the language. If any mistakes are discovered
by the compiler, they are reported to the user. The errors have to be corrected in the
source program, and the compilation process must be restarted. The successful
compilation process creates object code with .obj extension i.e. prog1.obj.

After the program has been translated into object code, it is ready to be linked.
This process is performed automatically, when we give execute command.
The final linked file, which is in an executable object code format, is stored in
another file with .exe extension i.e. prog1.exe and program will be executed
automatically.

When the program is executed, each of the statements of the program is


sequentially executed. If the program requests any data from the user, known as
input, the program temporarily suspends its execution so that the input can be
entered. After that results that are displayed by the program, known as output,
appear in a console.

If all goes well, the program performs its intended functions. If the program
does not produce the desired results, it is necessary to go back and reanalyse the
programs logic. The process of finding out logical errors in a program is called
debugging.

Integrated Development Environment

The process of editing, compiling, running and debugging is managed by a


single integrated application known as an Integrated Development Environment or
IDE.

Language Interpreters

Interpreter is a software used to convert higher-level language to object code


line by line. An interpreter analyzes and executes the statements of a program at the
same time.

Department of Computer Science 7 A.S.N. Degree College, Tenali

You might also like