Introduction to C programming

Introduction to C programming:




Outline:

• Features of C language 

• Structure of C Program 

• Flow Charts 

• Algorithms 

• Types of errors 

• Debugging 

• Tracing/stepwise execution of program


Features of C language:

  • C has set of built in functions and operators. So we can write any complex program.
  • C has set of data types and operators so C is efficient and fast.
  • C  is  highly  portable  means  the  program  written  for  one computer can be run on another computer.  
  • C has only 32 keywords which are easy to remember.   
  • C has set of built in functions and standard functions.   
  • C has system library.   And we can add our own functions o C library.   
  • C can be extending itself.

Structure of C Program:


1. Documentation Section: 

•      Documentation Section consists set of comment lines. 
•     Comment does not affect the program. 
•      It is used only for documentation purpose. 
•      /* ……… */ is used as comment line. 
•     Ex : /* write a program to find area of Circle.(area=PI*r*r, here PI=3.14) */

2. Link Section: 

•     Link section provides link between compiler and system library. 
•     Ex: #include<stdio.h>         #include<conio.h>

3. Definition Section: 

•     Definition Section defines all symbolic constants. 
•     Ex: #define PI 3.14

4. Global Declaration Section:
 
•      There are several variables that are used in more than one functions. Such variables are known as global variables. 
•     Global variables are declared in this section. 
•     Ex: int count;

5. main( ) function section: 

•     Every program must have one main function. 
•     It has two parts :

1. Declaration Part : 
In this part the variables are declared. Ex : int a,b;

2. Executable Part : 
In this part one or more statement to be executed. Ex : printf(), scanf();

6. Subprogram Section : 

•     This section contains user define functions. 

•     This function is called from main( ) function.







                C Development Life Cycle / process of compiling and running a C program.


1. Creating the program :

•        In UNIX operating system, the program is created using any text editor like vi,                     ex,ed. 

•        This program is called source code and saved with .c extension.

2. Compiling the program : 

•       The program is compiled using cc filename.c 

•       The compiler also checks for syntax errors. 

•       The command cc translates a C source program into an executable program.

•       Here the compiler converts the high level C statements into machine language.

3. Linking the program : 

•       During compiling, the compiler does not only compiling but also linking. 

•       In linking, the link is make between program and system library which contains built-          in functions like printf, scanf.

•       And after linking, the final product is known as executable object code with .exe                    extension.

4. Running the program : 

•      Type a.out to execute the program. 

•      Then loader loads the program into memory to initializing execution. 

      If the program does not give proper output, then correct the program by editing the                source program.

•      Then again execute that program, this process is called debugging.

Flowchart:

•      Flowchart is a graphical representation of an algorithm. Programmers often use it as a           program-planning tool to solve a problem. It makes use of symbols which are                         connected among them to indicate the flow of information and processing. 

•     The  process  of drawing  a flowchart  for  an  algorithm  is  known  as “flowcharting”.

Basic Symbols used in Flowchart Designs: 

• Terminal: The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is generally used in a program logic under some error conditions. Terminal is the first and last symbols in the flowchart. 




• Input/Output: A parallelogram denotes any function of input/output type. Program instructions that take input from input devices and display output on output devices are indicated with parallelogram in a flowchart.







• Processing: A box represents arithmetic instructions. All arithmetic processes such as adding, subtracting, multiplication and division are indicated by action or process symbol.







• Decision: Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are indicated by diamond in flowchart.










• Connectors: Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors to avoid any confusions. It is represented by a circle. 
• Flow lines: Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the direction of flow of control and relationship among different symbols of flowchart.




Example : Draw a flowchart to input two numbers from user and display the largest of two numbers:





Algorithm :

•    In programming, algorithm is a set of well defined instructions in sequence to solve             the problem. 
•    Qualities of a good algorithm 
•    Input and output should be defined precisely. 
•    Each steps in algorithm should be clear and unambiguous. 
•    Algorithm should be most effective among many different ways to solve a problem. 
•    An algorithm shouldn't have computer code. Instead, the algorithm should be                         written in such a way that, it can be used in similar programming languages. 








Write an algorithm to add two numbers entered by user:

•     Example 1: Determine and Output Whether Number N is Even or Odd 
•     Algorithm: 
•     Step 1: Read number N, 
•     Step 2: Set remainder as N modulo 2, 
•     Step 3: If remainder is equal to 0 then number N is even, else number N is odd, 
•     Step 4: Print output





Errors in C/C++ :

•     Error is an illegal operation performed by the user which results in abnormal working of the program. 

Types Of Errors in C/C++

  • Syntax error
  • Run-time error
  • Linker error
  • Logical error
  • Semantic error

• Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are known as syntax errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by compiler and thus are known as compile-time errors. 

Most frequent syntax errors are: 
•      Missing Parenthesis (}) 
•      Printing the value of variable without declaring it.
•      Missing semicolon

• Run-time Errors : Errors which occur during program execution(run- time) after successful compilation are called run-time errors. One of the most common run-time error is division by zero also known as Division error. These types of error are hard to find as the compiler doesn’t point to the line at which the error occurs. 

• Linker Errors: These error occurs when after compilation we link the different object files with main’s object using Ctrl+F9 key(RUN). These are errors generated when the executable of the program cannot be generated. This may be due to wrong function prototyping, incorrect header files. One of the most common linker error is writing Main() instead of main(). 

• Logical Errors : On compilation and execution of a program, desired output is not obtained when certain input values are given. These types of errors which provide incorrect output but appears to be error free are called logical errors. These are one of the most common errors done by beginners of programming. 
• These errors solely depend on the logical thinking of the programmer and are easy to detect if we follow the line of execution and determine why the program takes that path of execution. 

• Semantic errors : This error occurs when the statements written in the program are not meaningful to the compiler. 


Debugging :

Debugging is the routine process of locating and removing computer program bugs, errors or abnormalities, which is methodically handled by software programmers via debugging tools. Debugging checks, detects and corrects errors or bugs to allow proper program operation according to set specifications. 
• Debugging is also known as debug


Stepwise execution in C :



Post a Comment

0 Comments