Thursday 16 August 2012

Errors in C

Errors (also known as bugs) are common while writing a program. The errors should be detected and removed, or else, the whole set of statements of a program will simply produce wrong result. Errors can be detected only while running the program.

In C errors are of 3 types - Syntax Error , Logical Error and Run-time Error .

  • Syntax Error : These are generally typing error of the statements that are not according to the grammatical rules or syntax of the language. 
  • Syntax error will definitely occur and the program will not compile if semicolon (separator) is not placed at the end of the statement.
  • Logical Error : It occurs due to entering logically incorrect statement in the program.
  • If an addition program is instructed to do subtraction, it will compile, but produce a wrong output. This is logical Error.
  • Run - time Error : A program free of syntax and logical errors, may not produce result due to run - time errors, which usually occur due to the following reasons : 
  • Statements instructing to divide a number by zero.
  • Statements instructing to find logarithm of a negative number. 
  • Statements instructing to find the square root of a negative number. 


Logic Building - Examples

Let's Discuss some Examples.

1. Display the value of a variable :
Pseudocode in English :
  • Declare a variable.
  • Take the input.
  • Display the variable.
Flowchart : 
  • Declare three numeric variable.
  • Take the inputs from user.
  • Perform the sum.
  • Display the result.
Flowchat :


See here check is intentionally skipped.
  • Here int is data type in C , it stands for Integers. ( It will be discussed later in greater detail ) 


Wednesday 15 August 2012

Logic Building

Programming Logic
To run an error - free program, the logic of the program should be very clear. A programmer is said to be good, if his/her programming logic is lucid. To make the conception clear, the following methods are highly necessary before starting a program.

Algorithm
Before solving a problem with the help of a computer, it is essential to plan the solution is a step - by - step manner. A set of instructions to solve a problem is termed as an algorithm. Algorithms can be expressed in different ways, like, by drawing flowcharts using specific symbols or writing the solution as a set of instructions in our native language/English. It is easy to understand and can be implemented in any programming language.

Two distinct kind of algorithms :
1. Pseudocode
2. Flowchart

Pseudocode
Pseudocode allows a programmer to represent logic in his/her native language. It is easier to convert the program logic developed using pseudocode to any high-level language code. Following points have to be taken care into consideration while developing the logic of a program using pseudocode.
  • Pseudocode must be language - independent.
  • Indent lines make pseudocode easier to read and understand.
  • Pseudocode are meant to concentrate on logic of the program rather than language syntax. 
Flowchart
A flowchart is symbolic representation of the step - by - step solution of a given problem. Flow-charting is a method of charting the flow of the steps that the computer follows. A flowchart is a type of map showing which steps in a process precede or follow others. It indicates the flow of the entire process, the sequence of the data input, operations, decisions, results and other relevant information, pertaining to a particular problem.

Symbols used in Flow chart



Lets Program

Let's Discuss a Simple program in C that display "a welcome message".

/*C program to display a welcome message*/

#include<stdio.h>
main()
        {
         printf("Hello sir! welcome to the programming world \n");
         exit(0);
        }
The output of the above program will be:  Hello sir! welcome to the programming world
How to compile and execute the program?
  • First of all you need a C compiler to compile the above bunch of codes.  There's a huge number of free c compilers for windows OS available on net e.g Turbo C/C++ , Dev C++ , Eclips etc. Google any of it and install on your system. I recommend Dev c++ IDE for beginners. In Linux, terminal is enough to start with.
  • Compile the code above with any of the standard compiler and run. Voila you must get the same output as stated above.
Let's discuss the components of the above program
  • main() is a special function in C, which must exist in a C program as the execution of the program from this point.
  • printf is a standard C function called from main, used for displaying ouput.
  • "\n" indicates that the output will be displayed in a newline.
  • 'stdio.h' is a header file responsible for input-output function.
  • exit() is also a standard function that terminates the program after execution. It takes a value as parameter. In the program above it takes 0 as parameter. But it is actually not needed here as it has been used after the last line. Hence without using this function, the program will terminate automatically after displaying the output.

Introduction to C

AAT & T's Bell Labs in the year 1972 Dennis Ritchie, head of the system software research Department developed one of the most efficient languages of all the times Called C. He incorporated some new features of his own along with BCPL ( Basic Combined Programming Language ) and Language B. Day by day C evolved as a compact, coherent and powerful 'one man language'.

Basic Structure of a C Program
  • C is a case - sensitive language. (Statements are generally written in lower case letters).
  • Any C statement ends with a semicolon (;).
  • Blank spaces can be inserted between two distinct words to enhance the readability of the statement. ( Spaces are forbidden while declaring a variable )
  • A program may contain one or more #include statements, which is known as 'Preprocessor Directives'. ( A Preprocessor process the program before compiler do ). 
  • main() function is an essential part of C language, where execution of the program starts. ( You must define the main () function in the program. ) 
  • main() function can be placed anywhere in the program but the execution starts from there only. The group of sentences within the main() function are executed sequentially. 
  • Comments can be inserted in a program to increase the clarity of the motive of the program. ( Comment statements must be enclosed with /*...*/ in a C program.
  • Certain headers files are necessary for compiling a C program. 'stdio.h' is an example of a header file, which controls the functions performing the tasks of input and output. 
  • A program can consists of one or more functions , which is actually a group of C statements that are executed together.
In spite of the above rules for a C program , there is no specification for the position of inserting different parts of a statement. That's why C is also referred as a 'Free form language'.