You are on page 1of 22

Lect 4 - 5

Basics of C Language: Phases in Development of program, Introduction to C , Character Set, Constants, Variables & Keywords

Phases in Program

Development

of

Developing the program Compiling the program Linking the program Testing the program Documenting the program

Developing the program


Study the problem After detailed study, Identify the output required from program Identify Input data Prepare a detailed list of steps called Algorithm Translate the steps in a computer program in C Store the program (source code) in a disk file with extension .c

Compiling the program


To translate the program into machine language using a C compiler C compiler has in-built pre-processor Pre-processor processes source code before passing to the compiler Directives (Pre-processor commands) tell the preprocessor to process the code. Expanded version of source code is produced C compiler takes the expanded version of source code and produces a machine code (object code) and saved in a disk file with same name with extension .obj if there are no syntax errors.

Linking the program

Machine code version is not in the form that you can run To produce an executable code, known as run file , object codes are linked together with library functions After linking process is over , a disk file with same name but with extension .exe is produced

Testing the program


To assure that program is correct and gives the desired output Program is executed with all possible values of input data If output matches the desired results then it is correct In case it does not match, source code scanned to find out logical errors (errors in designing the algorithm or while coding)

Documenting the program

Recording the important information regarding the program Enables everyone to understand the logic and purpose of the program Facilitate in the maintenance and upgradation

Structure of C Program
Include header file section Global Declaration Section /* comments */ main () Function name { /* comments */ Declaration part Executable part } User-defined functions { }

Compiler

A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language.

Source Program
( Normally a program written in a high-level programming language)

Compiler

Target Program
( Normally the equivalent program in machine code relocatable object file)

error message

Compiling

To write a program takes these steps: 1. Edit the Program 2. Compile the program into Machine code files. 3.Link the Machine code files into a run able program (also known as an exe). 4.Debug or Run the Program. With some languages like Turbo Pascal and Delphi steps 2 and 3 are combined.

Interpreter

The steps to run a program via an interpreter are : 1. Edit the Program 2. Debug or Run the Program 3. This is a far faster process and it helps novice programmers edit and test their code quicker than using a compiler. The disadvantage is that interpreted programs run much slower than compiled programs. As much as 5-10 times slower as every line of code has to be re-read, then re-processed.

Character Set

The characters & symbols that a C program can understand & accept. It is the combination of alphabet or character, digit, special characters & white spaces (empty space).

Letter: A Z (Uppercase) & a z (Lowercase) Digit: 0 9 Special Characters: total 30 special characters Empty Space/White Space: horizontal, newline tab, carriage return, form feed etc.

Contd

Special Characters: comma , Period . Colon : Semicolon ; Question Mark ? Single Quote Double Quote Left Parenthesis ( Right Parenthesis ) Left Bracket [ Right Bracket ] Left Brace { Right Brace } Left angle bracket < Right angle bracket >

Special Characters: Equal to sign = Exclamation Mark ! Vertical Bar | Slash/Forward Slash / Backslash \ Tilde Symbol ~ Plus Sign + Minus/Hyphen Asterisk Sign * Hash Sign # %age Sign % Caret Sign ^ Ampersand Sign & At the rate sign @ Underscore Sign _

Delimiters

In C PL, there are some special character used for special syntactic meaning & these are called C delimiters.

There are five delimiters:

: () [] {} ;

Used for label entry Used for expression & enclosing the arguments in the function declaration Used for describing the size for array Used for beginning & ending of function, blocks & main program Used at the end of every C statement except control structure statements.

Keywords

C language has some reserve words which can not be used as variables or identifiers. These reserved words are keywords. There are 40 keyword among which 32 are used for High Level Programming (standard keywords). & remaining 8 are used for Low Level Programming (optional keywords).

Contd Standard Keywords


auto continue enum if short switch volatile break default extern int signed typedef while case do float long sizeof union char double for register static unsigned const else goto return struct void

Contd Optional Keywords


ada asm entry far fortran huge near pascal

Identifiers

There are some words or names which identify whether it is a constant or variable. These are the data names used by the programmer. Identifier gives us the unique identification having unique sequence of character used for special purpose.

Rules for Identifiers


It must be from the character set (unique). Blank Spaces are not allowed. It should not be a reserve word or keyword. They are always case sensitive. The character _ underscore from the character set should not be used as an identifier. It should be within the alphabets. eg. pay_rate is valid but pay_rate_ is invalid. The length of an identifier should not be long (max. 32 char.). Hyphen (-) should not be used in an identifier. Always avoid a single char. Like a, m etc. eg: - simple interest it should be si.

Constants

When you either enter the data for input or to assign the data to some identifier, then there be need of some storage space, so that entered or assigned data can be processed in a meaningful way. So the processed data be stored in two forms. These two forms are called constant or variable. Those quantities whose value does not vary during the execution of the program i.e. value is fixed.

Types of Constant

Constant

1) Numeric : - a) Integer Integer

b) Real/Float Real/Float without exponent

Decimal Octal Hexadecimal

with exponent 2) Character (Non numeric) : -

single string Backslash character

Backslash Character

Also called Escape Sequence.


\a or \a :- To ring a beep. \b or \b :- It moves one space back. \n or \n :- Move control to next line. \f or \f :-Form feed (move one page next). Used in the printing the hard copy only. \t or \t :- Horizontal Tab (it moves eight spaces of tab). \v or \v :- Vertical Tab (it moves eight lines down). \r or \r :- Carriage Return (it replaces a word or string from the beginning of the other string) \ or \ :- It displays a single quote in output statement. \ or \ :- It displays a double quote in output statement. \? or \? :- It displays question mark. \0 or \0 :- Null Character (it tells us the end of the string).

You might also like