Sunday, December 19, 2010

4. Key words


There are certain reserved words, called keywords, which have standard, predefined meanings in C.

These keywords can be used only for their intended purpose; they cannot be used as programmer-defined identifiers.

Some standard keywords are:


auto extern sizeof break floatn static case for char
switch register return long int else while short void

Some C compilers may recognize other keywords.

Note that the keywords are all lowercase. Since uppercase and lowercase characters are not equivalent, it is possible to utilize an uppercase keyword as an identifier.

Normally, however, this is not done, as it is considered a poor programming practice.


3. Identifiers

Identifiers are names that are given to various program elements, such as variable, functions and arrays.

Identifiers consist of letters and digits, in any order, except that the first character must be a letter.;

Both upper- and lowercase letters are permitted, though common usage favors the use of lowercase letters for most types of identifiers. Upper- and lowercase letters are not interchangeable.

The underscore (_) character can also be included, and is considered to be a letter. An underscore is often used in the middle of an identifier.

An identifier may also begin with an underscore, though this is rarely done in practice.

The following names are valid identifiers:


x y12 sum_1 tax_rate area _temperature

2. The C character set


C uses the uppercase letter A to Z, the lowercase letters a to z, the digits 0
to 9,
and certain special characters as building blocks to form basic program elements ( e.g., constants, variables, operators, expressions, etc.).

Some of the special characters are listed below:
+ - * ~ % / & ( ) { } [ ] ? " < > ! ; :
Most versions of the language also allow certain other characters, such as @ and $, to be included within strings and comments.
C uses certain combinations of these characters, such as \b,\n and \t, to represent special conditions such as backspace, new line, and horizontal tab, respectively

These character combinations are known as escape sequences.

1. Introduction

C language consists of some basic elements which are used to construct simple C statements.

These elements include the C character set, identifiers, and keywords, data types, constants, variables and arrays, declaration, expressions and statements.

We will see how these basic elements can be combined to form more comprehensive program components.

7. A sample of C language program


Example of C program to calculate area of circle:






Explanation of above program :

# is the preprocessor directive which commands that the content of file should be included at the time of compilation.

< stdio.h> is the header file which contains all input and output functions like scanf(), printf() which are frequently used in c programs.

main() is the first function which is executed by a C program.

1. int a,r=5; -> This statement declares 2 variables a and r of type integer and r has been assigned a value of 5.

2. const pi=3.14; -> This statement declares pi as constant and value 3.14 has been assigned to it.

3. a = pi * r *r; -> This statement computes the area of circle and assign it to variable a

4. printf("%f",a); -> This statement prints the area of circle using printf function.

5. getch(); -> It is used to get character from user.

Note: /* Any thing written within it is consider as comment statements. */

6. Structure of C Program

Every C program is made of function and every function consist of instructions called statement.

Structure of C Program.

#include //stdio.h is the header file
main() // main function is the first function which is executed by a C program.

All C statements are written within main function.
{

// All C statements.

}

Functions

Every C program consists of one or more modules called functions. One of the functions must be called main( ).
The program will always begin by executing the main function, which may access other functions.

Any other function definitions must be defined separately, either ahead of or after main.

A function name is always followed by a pair of parenthesis, namely, ( ). And all function statements are enclosed within a pair of braces { }.

The program execution always starts with main function. When the closing brace of the main function is reached, program execution stops, and the control is handed back to the OS (Operating System).

Statements

Single C language instruction is called a statement. Statements are written according to the grammar of C language. Every C language statement must ends with semicolon(;).

In order to write a C program we should follow some basic rules which are described below:

a) Usually all statements in C are entered in small alphabets.

b) Blank spaces may be inserted between two words to improve the program readability. However no blank spaces are allowed within a variables, constants or key words.

c) It has no specific rules for the position at which statements is to be retained that's why it’s often called a free form language.

d) All C statements must end with a semicolon (; )

5. The components of C language

There are five main component of C language are:

1. The character set: Any alphabet ,digit or special symbol ,used to represent information is denoted by character. The character in C are grouped into four categories.

1 Letters A...Z and a...z
2 Digits 0,1,2,.....9
3 Special Symbol ~,`,!,@,#,$,%,^,&,*,(),.,<,>,?,/,",:,;,{},[]
4 White Space blank space, Carriage return, form feed, newline, horizontal tab

2. The data types: The power of the programming language depends, among other thing, on the range of different types of data it can handle.

3. Constants: A constant is a fix value that doesn't change while program execution.

4. Variable: A variable is an entity whose value can change during program execution.

5. Keywords: Keywords are those word which have been assigned specific meaning in C language. Keywords should not be used as variable names to avoid problem.

4. Saving and Running a program

Press Alt F, move the cursor to save , press Enter and then type file name with extension .C in given Text Box and press Enter to save the program.

Running a program

Press Alt R to pull down the Run menu and Enter to begin compilation, A compile window opens and shows progress and output can be viewed in output window and press Alt W to pull down window menu, move the cursor down to output and press Enter. Press Alt W and move the cursor to close to close output window immediately.

Exiting Borland C

Press Alt X to Exit Borland C and return to Dos Prompt.

3. Editing File

To open a new file, first press Alt F then Enter key, File pull down will activated, select new and then press Enter key, the display will be change into a blank screen of text editor having file name NONAME00.C, on which you can start entering your code through keyboard.

To open existing file press Alt F and then Enter, from the activated file pull down menu select Open, A Open Dialoge Box will appear, select desired file name path and press Enter.

3. Program design & implementation

There are two types of designing approach:

1. Top down design.
2. Bottom up design.

Both have been describe together with examples.

Any problem can be dealt with two ways, viz. top down or bottom up. A simple example is given here to illustrate the concept.
Sorting an array of numbers involves the following two steps:

1. Comparison
2. Exchange

1. Top down design:

At the top level, an algorithm has to be formulated to carry out sorting. Once the algorithm is confirmed, then the algorithms for comparison and exchange are formulated, before implementation of the entire algorithm.

Therefore, in this approach, one begins from the top level without bothering about the minute details for implementation, to start with.

2. Bottom up design:

The bottom up approach is just the reverse. The lower level tasks are first carried out and are then integrated to provide the solution.

In this method, lower level structures of the program are developed first and then progressively higher level structures are created.

Here the algorithms for exchange and comparison will be formulated before formulating the algorithm for the whole problem.

In any case, dividing the problem into small tasks and then solving each task provides the solution.

If the program development is assumed to be a single task and the program statements are developed in sequence, then it is quite likely that the program may not work.

Therefore, either the top down or bottom up methodology has to be adopted for dividing the problem into smaller modules and then solving it.

Difference between Top design and Bottom up approach:


In the top down methodology, the overall structure is defined before getting into details, but in the bottom up approach, the details are worked out first before defining the overall structure.

Implementation

A good program is one which contains a main function and the main function calls procedures or sub-programs or functions to carry out specific tasks.

The subprograms are dependent on the main program. They do what the main program asks them to do.

Each language gives different names to sub-programs. Sub programs are known as functions in C.

The quality of program will also get enhanced when the program is made modular as mentioned above with functions.

Each function will receive input and may return some output to the called program.

By dividing the problem into a number of functions, the problem can be divided and conquered.

This facilities focus on small programs and dealing with one problem at a time

2. Problem solving strategies

The most popular method of problem solving is to divide and conquer. This means that the problem has to be divided into smaller problems, each of which must be solved to get the complete solution.

For instance, if it is required to find the second smallest element in an array, the problem could be divided into two parts viz.

Arranging the elements in the array in ascending or descending order and then getting the second smallest element form the sorted array.

This strategy is called divide and conquer.

If you want to find the sum of the digits in a number, it can be divided into 3 parts:

1. Finding modulus of 10 i.e. finding the remainder when the number is divided by 10.

2. Adding the remainder to a sum whose initial value is zero.

3. Dividing the number by 10 and then repeating the process from (1) again till the quotient is zero.

1. Introduction


Firstly we will discuss here Fundamental concept of Problem solving. Then This chapter gives the viewer/reader an overview of C language and tells about the structure of C program and various component of a C program. C language allows us to create custom data types.
Problems are the undesirable situations that prevent any software from fully achieving its objectives.
When we wish to transform the existing situation into a more desired one, problem occurs, and a need for solving the problem arises.

Defining a problem clearly in terms of goals and objectives helps largely in problem solving.
There are three related terms that need to be well understood to successfully solve the problem:

1. Mission: It is the broad statement of the purpose of the organization.
2. Goals: It is the general statement of what is to be accomplished.
3. Objectives: It is the statement of measurable results to be achieved in a specified time frame.

2. C program window

How to open C program code window ?

Before opening C on your computer. Please ensure that C compiler has been loaded or not. If not then please load it first and then try any of the following ways to open C which suit your computer setting.

1.Start>>Program >> Turbo c.

2. If turbo C icon is present on your computer desktop then simply click on it.

3. My computer >>c:\>> tc (folder)>> bin (folder) >> tc.exe (icon).


4. Or make sure the path where you have loaded the C on your computer and try to run it from there.


After following the above instructions the screen which is given below will appear on your desktop:





Description of C code window

1. =: Interface to external programs.

2. File: File related option such as opening and saving file.

3. Edit: Cut,Copy,Paste operation.

4. Search: Find,Find & Replace operation.

5. Run: Compile and run the file currently loaded in the text editor. And
debugging such as setting/clearing trace points can be performed from this menu.

6. Compile: The menu item compiles a source file to an object file or an .exe file.

7. Debug: Provides interactive debugging. Variables can be examined/set/cleared, and we can watch variables change during execution.

8. Project: This menu item controls Borland C++'s handling of large programs that are in multiple source file.

9. Option: Default option are set during installation . The user can change any option at any time through this menu.

10. Windows: Windows operation include zoom, arranging windows on the screen , and closing windows.

11. Help: Borland C++ includes a context sensitive help capability. Select Help or press f1 for general Help, Shift f1 for indexed help or Ctrl f1 for context sensitive help




1. Introduction of C language

Introduction:


C is general-purpose, structured programming language, developed by Denis Ritchie in 1972 at Bell lab USA.

Its instructions consist of terms that resemble algebraic expressions, augmented by certain English keywords such as if, else, for, do and while

In this respect C resembles other high-level structured programming languages such as Pascal and FORTRAN.

C also contains certain additional features, however, that allow it to be used at a lower level, thus bridging the gap between machine language and the more conventional high-level languages.

This flexibility allows C to be used for system programming as well as for applications programming.

C is characterized by the ability to write very concise source programs, due in part to the large number of operators included within the language.

It has a relatively small instruction set, though actual implementations include extensive library functions which enhance the basic instructions.

C compilers are easily available for computers of all sizes, and C interpreters are becoming increasingly common.

The compilers are usually compact, and they generate object programs that are small and highly efficient when compared with programs compiled from other high-level languages.

The interpreters are less efficient, though they are easier to use when developing a new program.

Many programmers begin with an interpreter, and then switch to a compiler once the program has been debugged (i.e., once all of the programming errors have been removed).