Compiling and Running your program

So far we have discussed about C, features of C, structure of a C program, library function printf( ) and how to program in C language etc. So now we are moving into more detailed explanation. You can use Turbo C or Borland C compilers for your program. Both are providing you a way to code your program, compile it and execute the program. If you are using 'Borland C' then linking header files is necessary. Turbo C won't warn if you are not linked header files into your program. So now we can see a sample program as a step to explore the power of C language. So open your C editor (Turbo or Borland) and start coding.

Learning by Example I

Qn: A Program to print your Name on the Screen
/*Program to print name on the screen */
#include<stdio.h>
main( )
{
printf("Your Name");
}

So you've done! Now you can compile your program to convert it into object code. For that select Compile - > Compile; then a compiling box will be displayed on the screen showing file name, lines compiled, number of warnings & errors if any in the program. If your program has successfully compiled then you can turn into execute or run the program. If there are any errors in the program, number of errors will be displayed on the compiling window and when you hit 'Enter' key, details of the errors listed in the message window; select the error message using up/down arrow keys and hit 'Enter' key to locate the error instruction, correct the errors and compile it.

For executing the program select Run->Run from the menu. You r editor will suddenly blink and return back to the editor window where you've coded your program. So your program has been executed, you can view the output of the program by pressing Alt+F5 keys. You can now see 'Your name' (whatever given in printf) printed on the screen. Ok, now lets have a look at our code:

We have written a comment line on the first line. In link section we linked header file as we've used printf( ) function in our program. As we discussed already, printf( ) is a function which means which is already coded and stored in the C library. Instructions for printf( ) function is written in a header files ( having extension .h) 'stdio.h'. stdio means Standard Input and Output. So if you are using any of the library function in your program, you should link appropriate header file into your program.

If you again run your program, output will be displayed on the screen with the old program output. So you may be want to print your outputs on a clean window, you can clear your screen before you are printing any output. For this you can use a library function 'clrscr( )'. Just put this function in your program where you want to clear your screen. clrscr( ) function's header file is , so do link this header file into your program. In our program you can insert clrscr( ) just before that printf( ) function; Run your program again and you will see your name printed in a cleared screen.

It may be disturbing everytime pressing Alt+F5 keys to view the output. Why we are redirected to the editor window after program run? Because process control executes each line of instructions and when comes out of the closing brace, program terminates and get back to the coding window. You can pause the program termination by just putting getch( ) function just before the closing brace. getch( ) is actually a function which accepts a value from the keyboard. So if you insert a getch( ) function as your last instruction, the program control will wait for a value to be inputted to the getch( ); so till entering a value, you can view your program outputs on the screen. By this you can avoid the usage of hitting Alt+F5.

So from the above modification our program will be look as follows:

/*Program to print name on the screen */
#include<stdio.h>
#include<conio.h>
main( )
{
clrscr( );
printf("Your Name");
getch( );
}

Previous
Next

C PROGRAM BASICS

main( )
{ /* This is the beginning */
printf("Welcome to the world of C");
}

The first line informs the system that the name of the program is main( ) and the execution begins at this line. The main( ) is a special function used by the C system to tell the computer where the program starts. Every C program must have exactly one main( ) function. If we use more than one main( ), the compiler cannot tell wich one marks the begining of program. The empty parenthesis immediately following the main indicates that the function main has no arguments. The opening brace { in the second line marks the begining of the function main( ) and the closing brace } in the last line indicates the end of the function. All the statements between these two statements form the function body. The function body contains a set of instructions to perform a given task.

In this case the function body contains two statements out of which the printf line is an executable statement. The lines begin with /* and ends with */ is known as 'comment line'. Comment lines are non-executable statements and therefore anything between /* and */ is ignored by the compiler.

printf is a predefined standard function for printing output. Predefined means it is a function that has already been written and linked together with our program at the time of linking. The printf function causes every thing between the starting and ending quotation marks to be printed out.

printf("Hello Guest\nWelcome to Program Logic");

The argument in the printf contains a combination of two characters '\n'. This combination is collectively called the 'new line character'. It causes the computer to go to the next line. Hence the above printf statement will output :

Hello Guest
Welcome to Program Logic

Basic Structure of a C Program

Documentation section
Link section (Header File inclusion section)
Definition section
Global declaration section
main ( ) function section
{
Declaration part
Execution part
}
Subroutines

The documentation section consist of a set of comment lines giving the name of the program and other details.

The link section provides instruction to link functions from system library.

The definition section defines all symbolic constants.

There are some variables that are used in more than one function, such variables are called global variables and are declared in the global declaration section outside of all the functions.

Every C program must have one main( ) function. This section contains two parts, declaration part and execution part. Declaration part declares all the variables used in the executable part. These two parts must appear between the opening and the closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main( ) is the logical end of the program.

The subroutine section contains all the user defined function that are called in the main( ) function.

Previous
Next

OVERVIEW OF C

The programming language C was an offspring of the Basic Combined Programming Language (BCPL) called 'B'. B language was modified by Dennies Ritchie and was implemented at AT & T Bell Laboratories in 1972. The new language was named C. Since it was developed along with the Unix Operating System it is strongly associated with Unix. This OS was coded almost entirely in C.

The increasing popularity of C is probably due to its many desirable qualities. It is a language whose rich set of built in functions and operators can be used to write any complex program. The C compilers combines the capabilities of an assembly language with the features of a high level language and therefore it is well suited for writing both system software and business packages.

Programs written in C are efficient and fast this is due to its variety of data types and powerful operators. There are only 32 keywords and a strength lies in its built in function. C is highly portable. This means that C programs written for one computer can be run on another with little or no modification.

Another important feature of C is its ability to extend itself. A 'C ' program is basically a collection of function that are supported by the C library. We continuously add our own functions to the C library.

Previous
Next

COMPUTER PROGRAM

A computer program is a set of instructions to be followed by the computer. These instructions are the steps to be followed sequentially in order to achieve the result.

CHARACTERISTICS OF A GOOD PROGRAM

1. Accuracy : The program must do what it is supposed to do and meet the critical laid down in its specification.

2. Reliability : The program must always do what it is supposed to do and never crash.

3. Efficiency : The program must use the available store space and resources in such a way that the system speed is not waste.

4. Robustness : The program should cope with invalid data without stopping with no indication to the cause and without creating errors.

5. Usability : The program must be easy to use and well documented.

6. Maintainability : The program must be easy to amend having good structuring and documentation.

7. Readability : The code in the program must be well laid out and explained with comments.

8. Portability : It should be seen that the program is easy to transfer to other machines as well.

TYPES OF DATA

Constants : This is the data which is predetermined before a program is used. Values remain unchanged during the execution of a program.

Variables : Here the data may change or be assigned different values as the program runs.

OP CODE & OPERAND

A Machine language instruction normally has a two part format. The first part is an instruction , is the operation code, that tells the computer what function to perform, and the second one is operand, that tells the computer where to find or store the data or other instructions that are to be manipulated.

All programming languages has its own syntax, rules or grammar. So we should follow the syntax of the programming language in which we are programming. Breaking any of these will cause bugs or Errors in our program.

Logic, Run time & Syntax Errors

Syntax Errors arises due to the lack of knowledge in the programming language syntax. It arises because of the error in the syntax of programming instructions. Programs having syntax errors cannot be compiled or executed. Compilers will show syntax errors in the program specifying the line number and information about the error.

Logic Errors are another kind of error and causes because of the faulty or bad logic of the programmer. It may result in the programs functionality and often produce undesired output. For example instead of adding two numbers like a+b, programmer coded like a-b; Programmer used a-b instead of instruction a+b. This type of errors cannot be detected by compilers because instructions may be valid (both a+b and a-b are valid instructions) but it has some logic errors and the program will results undesired or unpredictable result.

Run time Errors are errors occur when a program executes. Compilers cannot predict the run time errors in the program. Its arises only when a program runs. eg. Division by Zero Error.

Previous
Next

ALGORITHMS

An Algorithm is a sequence of finite instructions or steps used to complete a task or a problem. There is no generally accepted rule for algorithms, its a sequence of steps carried out to complete a task. But if we are considering computer programs, we need somewhat a standardized format for Algorithms, that means Algorithms are an aid for programs, so there should be a definite start and an end for the algorithm.

Algorithms can be expressed in different notations, including natural languages, Pseudo codes, flow charts etc. Flow Charts and Pseudo Codes are widely used for Technical and Complex algorithms. and are used in the structured program designs. Natural language expressions cannot be used as its may make ambiguity.

FLOWCHARTS

 A Flowchart is a pictorial representation of an Algorithm or a Process, showing each steps in various kinds of boxes in order, and connecting these boxes with arrows.

Flowchart is the representation of the programming steps. It shows the various steps in sequence which are needed to run the program. Flowchart uses boxes of different shapes to denote different types of instructions. The instructions are written within these boxes using clear and concise statements. These boxes are connected by solid lines having arrow marks to indicate the flow of operations that is the exact sequence in which the instructions are to be executed. Flowchart helps in detecting errors in the program logic also. Once the flowchart is ready, the programmer can concentrate only on coding the operations in each box of the flowchart in terms of the statements of the programming language. This usually ensure an error-free program.

Flow Chart Symbols


Symbols

A typical flowchart from older Computer Science textbooks may have the following kinds of symbols:
Start and end symbols
Represented as lozenges, ovals or rounded rectangles, usually containing the word "Start" or "End", or another phrase signaling the start or end of a process, such as "submit enquiry" or "receive product".
Arrows
Showing what's called "flow of control" in computer science. An arrow coming from one symbol and ending at another symbol represents that control passes to the symbol the arrow points to.
Processing steps
Represented as rectangles. Examples: "Add 1 to X"; "replace identified part"; "save changes" or similar.
Input/Output
Represented as a parallelogram. Examples: Get X from the user; display X.
Conditional or decision
Represented as a diamond (rhombus). These typically contain a Yes/No question or True/False test. This symbol is unique in that it has two arrows coming out of it, usually from the bottom point and right point, one corresponding to Yes or True, and one corresponding to No or False. The arrows should always be labeled. More than two arrows can be used, but this is normally a clear indicator that a complex decision is being taken, in which case it may need to be broken-down further, or replaced with the "pre-defined process" symbol.

Connectors
There are instances when a flowchart becomes too large to fit in a single page and the use of flow lines becomes impossible. In such situations awe use the connector symbols as as substitute for flow lines.
Sample Flowchart to find the sum of first 50 natural numbers


Finding Largest among three input numbers

ADVANTAGES OF FLOWCHART

1. Conveys better meaning : Since a flowchart is a pictorial representation of a program, it is easier for a programmer to understand and explain the logic of the program to some other programmer.

2. Effective joining of a part of a system : A group of programmers are normally associated with the design of large software system. Each programmer is responsible for designing only a part of the entire system. If each programmers draws a flowchart for his part of design, the flowcharts of all the programmers can be placed together to visualize the overall system design.

3. Efficient Coding :
Once a flowchart is ready, programmers find it very easy to write the concerned program because the flowchart acts as a road map for them.

4. Systematic Debugging : A flowchart is very helpful in detecting, locating and removing mistakes in a program in a systematic manner.

5. Systematic Testing : Testing is the process of confirming whether a program will successfully do all the jobs for which it has been designed under the specified constraints. For testing a program different sets of data are fed as input to that program to test the different parts in the program logic.

DRAWBACKS OF FLOWCHARTS

  1. Flowcharts are something new and strange to work with for beginners.
  2. When modifications are made in the original program corresponding changes must be made in the flowcharts of the program in the documentation.
  3. Flowcharts may not reveal significant steps to be followed in actual coding.
  4. Flowcharts are often cumbersome to use and costly to produce.
  5. Flowchart don't constitutes a programming language. They are person to person means of communication, not person to computer. 

PSEUDO CODE [STRUCTURED ENGLISH]

PSEUDO CODE is another program analysis tool that is used for planning program logic 'pseudo' means imitation and code refers to the instructions written in a programming language. Pseudo code is therefore an imitation of actual computer instructions. These pseudo instructions are phrases written in ordinary natural language.

Instead of using symbols to describe the logic steps of a program as in a flow charting, pseudo code uses a structure that resembles computer instructions. Because it emphasizes the design of the program. Pseudo code is also called Program Design Language [ PDL]. Pseudo code is made up of the following basic logic structures.
  1. Sequence
  2. Selection [if....then....else, if...then]
  3. Iteration [ do...while,Repeat...until]
A sequence structure is a single step or action included in a process. It does not depend n the existence of any condition and when encountered it is always executed. Eg: To purchase a book in a book store you would probably follow a procedure similar to the one that follows

a. Pick out a desirable book
b. Take the book to the checkout counter
c. Pay for the book
d. Obtain receipt
e. Leave the store

Selection logic also known as decision logic, is used for making decisions. It is used for selecting the proper path out of the two or more alternative paths in the program logic. Selection logic is depicted as either an if.. then... else or if... then structure.

Iteration logic is used when one or more instructions may be executed several times depending on some conditions. It used two structures called the Do while and repeat until. Both these structures are used for looping.

Previous
Next

INTRODUCTION TO COMPUTER PROGRAMMING

A computer program is a collection of instructions that describe a task, or set of tasks, to be carried out by a computer. We can instruct a human being to do a particular task by speaking up or through any other media. As computers are bare machines we have to instruct them using some languages that are understood by the computer. For this purpose, communicating with the computer for instructing them to do a particular task we use special languages called 'Programming Languages'. The task of instructing the machines using programming languages is called 'Programming". Those persons who deals with the programming is known as 'Computer Programmers'.

We can classify the programming languages into two broad categories.
  1. Low Level Languages
  2. High Level Languages
1. Low Level Languages : Low Level languages are of two types :-
  • Machine Languages and  
  • Assembly Languages.
Machine Language: As computers are electronic devices, they internally represents all data as binary 1 and 0. a binary 0 may be used to represent power off or low voltage whereas 1 may represent high voltage or power on state. Using machine language, programmer programs in computer's own language, that is binary language. A programmer programming in machine language deals with binary 0's and 1's. In machine all instructions are represented as combination's of binary 0's and 1's. The memory addresses for the data also be specified in the program. Hence an instruction such as a+b, should be in binary language, there may be corresponding binary combination's for each operator and operands, addresses for operands should be specified in the program. Hence programming in machine language is not an easy task. The limitation of machine language programming are : -
  • Difficult to program
  • Difficult to modify
  • Error prone
  • Machine Dependent (No Portability) : Programs written in machine language are not portable, which means we cannot copy it into another machine and use.
The main advantage of machine language is that, programs written in machine language can be fastly executed, because there's no need to convert the program into machine's code, its already in binary language.

Assembly Language: Assembly Language is also known as Symbolic language. In assembly language, we can program using symbols and alphabets. The following are the limitations of Assembly Language : -
  • Machine Dependent.
  • Deep knowledge in hardware required by the programmer.
Assembly language programs can be written using symbols and digits, as computer internally represent all data in binary form, an assembly program must be translated into equivalent machine code before executing the program. This translation is done by a translation program known as Assembler.

2. High Level Languages are user friendly languages; using which anybody having a little knowledge of programming language can program without bothering about internal structure or memory addresses. Examples of high level languages are: COBOL, BASIC, C, C++, JAVA, PYTHON etc. Programs written in high level languages can be easily ported to any other system with a little or no modification.

Programs written in high level languages such as C, C++ etc. are cannot directly understand by the computer, so they have to translated into machine code, and this translation is done by Compilers or Interpreters.

Next

Design by The Blogger Templates The Blog Full of Games

© CourseZone
2008 - 2010 All Rights Reserved.