Monday, October 31, 2011

UNIT 2 BASICS OF C (MCS 011, AN INTRODUCTION SO C, - IGNOU MCA FIRST SEMESTER)

MCS-011, PROBLEM SOLVING AND PROGRAMMING, AN INTRODUCTION TO C, C++, MCA - MASTER OF COMPUTER APPLICATIONS; IGNOU; Indira Gandhi National Open University, School of Computer and Information Science;
-------------------------------------------------------
UNIT  2  BASICS OF  C
-------------------------------------------------------
Structure :
2.0  Introduction
2.1  Objectives
2.2  What is a Program and What is a Programming Language ?
2.3  C Language
     2.3.1  History of C
     2.3.2  Salient Features of C
2.4  Structure of a C Program
     A Simple C Program
2.5  Write a C Program
2.6 Compiling a C Program
     2.6.1 The C Compiler
     2.6.2 Syntex and Semantic Errors
2.7  Link and Run the C Program
     2.7.1  Run the C Program through the Menu
     2.7.2  Run from an Executable File
     2.7.3  Linker Errors
     2.7.4  Logical and Runtime Errors 
2.8  Diagrammatic Representation of Program Execution Process
2.9  Summary
2.10 Solutions / Answers
2.11 Future Readings

-------------------------------------------------------
2.0  INTRODUCTION
-------------------------------------------------------
In this Unit Presenting C Language - a standardized, industrial - strength programming language known for its power annd portability as an implementation vehicle for these problem solving techniques using computer.

A language is a mode of communication between two people.  It is necessary for those two people to understand the language in order to communicate.  But even if the two people do not understand the same language, a translator can help to convert one language to the other, understood by the second person.  Similar to a translator is the mode of communication between a user and a computer is a computer language.  One form of the computer language is understood by the user, while in the other form it is understood by the computer.  A translator (or compiler) is needed to convert from user's from the computer's form.   Like other languages, a computer language also follows a particular grammar known as the syntax.

-------------------------------------------------------
 2.1   OBJECTIVES
-------------------------------------------------------
 After going through this unit we will be able to :
  1. Define what is a Program ?
  2. Understand what is a C Programming Language ?
  3. Compile(संकलन करना, तैयार करना, एकत्र करना) a C Program;
  4. Identify the syntax errors;
  5. Run a C program; and
  6. Understand what are Run Time Errors  &  What are Logical Errors.
--------------------------------------------------------- 
2.2   WHAT IS A PROGRAM AND  WHAT  IS  A  PROGRAMMING  LANGUAGE ?
---------------------------------------------------------
We have seen in the previous unit that a computer has to be fed with a detailed set of instructions and data for solving a problem.  Such a procedure which we call an ALGORITHM is a series of steps arranged in a logical sequence. (FOR MORE DEFINITION SEE THE BELOW DEFINITION FROM WIKIPEDIA) :



In practice it is necessary to express an algorithm using a programming language. A procedure expressed in a programming language is known as a COMPUTER PROGRAM.

Computer Languages are designed to be machine-independent. Most of the programming language ideally designed, to execute a program on any computer regardless of who manufactured it or what model it is.

Programming Language can be divided into two categories :
  1. Low Level Language (LLL) or Machine Level Language (MLL): The Computer Language whose design is governed by the circuitry(विद्युत्-परिपथ तंत्र) and the structure of the machine is known as the Machine Language. This language is difficult to learn and use.  It is specific to a given computer and is different from different computers i.e. these languages are Machine-Dependent.  These languages have been disigned to give a better machine efficiency, i.e. faster program execution. Such languages are also known as Low Level Language (LLL).
    .
  2. Assembly Language : Another type of Low -Level Language is the Assembly Language.  We will code the assembly language program in the form of MNEMONICS(स्मरणोकारी, स्मरण-ओप्कारी ) .
    .
  3. Middle Level Language :



  4. High Level Language : (HLL) : or Problem Oriented Languages : These Languages are particularly oriented towards describing the procedures for solving the problem in a concise, precise and unambiguous manner.  Every High Level Language follows a precise set of rules.  They are developed to allow application programs to be run on a variety of computers.  These languages are machine independent - Like FORTRAN, BASIC, PASCAL etc. They are easy to learn and programs may be written in these languages with much less effort.  However, the computer cannot understand them and they need to be translated into machine language with the help of other programs known as COMPILERS or TRANSLATORS.
    .
  5. COMPILERScompiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).
-----------------------------------------------------------------
2.3  C LANGUAGE
-----------------------------------------------------------------
Prior to writing C programs, it would be interesting to find out what really is C language, how it came into existence and where does it stand with respect to other computer languages.  We will briefly outline these issues in the following section.
HINDI ARTICLE ON C LANGUAGE :



----------------------------------------
2.3.1 History of C Language :
----------------------------------------
  • Developed at AT & T's Bell Laboratory of USA in 1972.
  • Designed and Written by Dennis Ritchie
  • C allows a precise control of input and output.
  • Late 1960s  ALGOL60 came into existence (ALGOL short for ALGOrithmic Language) - but it never become popular as it was too abstract and too general.
  • to improve this, Combined Programming Language (CPL) was developed at Cambridge University. But it was very complex and difficult to learn.
  • hence Martine Richards at Cambridge University reduced the feature of CPL and developed Basic Combined Programming Language (BCPL  -  or Before C Programming Language) which is later turned to be much less powerful and too specific. 
  • Then Ken Thompson at AT & T's Bell Labs, developed a language called B at tha same time as a further simplification of CPL.  But like BCPL this was also too specific.
  • Now Ritche inherited the features of B and BCPL and added some features on his own and developed 'C' Language - proved to be quite compact and coherent. 
  • in 1983 ANSI established and committee defined an ANSI (American National Standard Institute) standard for the C Language. 
2.3.2  SALIENT (प्रमुख, मुख्य)  FEATURES  OF  C  
  1. C is a General Purpose, Structured Programming Language
  2. Often called a Middle Level Language. i.e. it combines the elements of High Level Language (HLL) with the Functionality of Assembly Language
  3. It combines the elements of High Level Languages with the functionality of Assembly Language.
  4. Good Programming Efficiency.
  5. C Allows the manipulation (व्यवहारकौशल, जोड़ - तोड़, परिचालन) of bits, bytes and addresses. 
  6. Memory Management Functions.
  7. Portable Coding, allows the same C program to be run on machines with different hardware configurations.
  8. Flexible with system programs as well as application programs.
  9. Commonly called a STRUCTURED  LANGUAGE because of structural similarity to ALGOL and PASCAL.
  10. Easy in DEBUGGING, TESTING and MAINTENANCE. 
  11. Supports several CONTROL STRUCTURES such as - 'WHILE',  'DO-WHILE' and 'FOR'.
  12. Supports several DATA STRUCTURES - such as - 'STRUCS', 'FILES', 'ARRAYS' etc.
  13. Easy in Maintenance.
[आजकल सी दसवीं , बारहवीं से लेकर बीसीए , एमसीए आदि के पाठ्यक्रमों में निर्धारित की गई है। इसलिए कंप्यूटरशास्त्र के छात्रों के लिए सी का अध्ययन आवश्यक हो गया है। सभी प्रोग्रामरों के लिए सी सीखना इसलिए भी आवश्यक है क्योंकि अनेक अन्य भाषाएं, जैसे जावा, फ्लैश एक्शनस्क्रिप्ट, सी++, सीशार्प आदि में भी सी जैसा वाक्य-विन्यास होता है। यदि प्रोग्रामर को सी अच्छी तरह से आती हो, तो इन सब भाषाओं को सीखना उसके लिए अधिक सरल हो जाता है। सी सीखने का एक अन्य लाभ यह है कि उसमें किसी आधुनिक प्रोग्रामन भाषा के सभी आधारभूत तत्व जैसे प्रमुख डेटा टाइप (int, char, float, array, struct, आदि), लूपिंग स्ट्रक्चर (for, while, do... while, case आदि), ब्रांचिंग कथन (if... else वाले कथन) आदि मौजूद हैं। इसलिए सी सीख लेने पर प्रोग्रामन भाषाओं की मुख्य-मुख्य विशेषताएं आसानी से समझ में आ जाती हैं। चूंकि सी एक सुगठित और छोटी भाषा है उसे अन्य प्रोग्रामन भाषाओं की तुलना में जल्दी सीखा जा सकता है।]

Q.  1  A Program written in Low Level Language is faster. Why ?
Ans:  Low-level languages can be converted to machine code without using a compiler or interpreter, and the resulting code runs directly on the processor. A program written in a low-level language can be made to run very fast, and with a very small memory footprint

Q.  2  What is the Difference between a (HLL) High Level Language and  (LLL) Low Level Language ?
Ans.  Low Level Language : 
  • Low Level Language can be directly mapped to processor instructions.
  • Lower level languages are typically assembly languages that are machine specific.
High Level Language :  
  • While a single statement in a high level language may execute dozens of instructions. 
  • High level programming languages are more structured, are closer to spoken language and are more intuitive than low level languages
  • Higher level languages are EASIER TO READ and can typically sometimes work on many different computer operating systems.
Q. Why is C referred to as middle level Language ?
Ans. C is often called a middle-level computer language as it combines the elements of high-level languages with the functionalism of assembly language. 

----------------------------------------------------
2.4  STRUCTURE  of  a  'C'  PROGRAM 
----------------------------------------------------
  1. C is a Case Sensitive Language.
  2. all C programs consists of one or more FUNCTIONS.
  3. one common function in every c program is main(). This is the FIRST FUNCTION called up when the program execution begins. basically main() outlines what a program does. Although main is not given in the keyword list, it cannot be used for naming a variable.
BELOW IS AN EXAMPLE OF A SAMPLE C PROGRAM :


[Difference between main and void main] :    main() के पहले जो void शब्द है, वह किसलिए है।  प्रत्येक फंक्शन उसे बुलाने वाले फंक्शन को कुछ न कुछ लौटाता है। कई बार यह रिक्त राशि भी हो सकती है, यानी आप यों समझ लें कि वह कुछ नहीं लौटाता। लेकिन सी की दृष्टि से यह कुछ नहीं भी, कुछ होता है!  इस रिक्त राशि को NULL कहते हैं, और main() के पहले जो void शब्द है ( जिसका शाब्दिक अर्थ है, खाली ) यही सूचित करता है, कि main() फंक्शन कुछ भी नहीं लौटाता है। 
      main()   /*The Main Function*/
हर सी प्रोग्राम में भी एक खास फंक्शन होता है, जिसे main() कहा जाता है। हर प्रोग्राम में main() फंक्शन का होना अनिवार्य है।  किसी प्रोग्राम का संकलन शुरू करने से पहले यही देखता है कि main() कहां है? यदि प्रोग्राम में main() फंक्शन न हो, तो संकलक इसे एक गंभीर त्रुटि मान कर संकलन कार्य को रोक देता है।
    {    Declaration Part;
         Program Statements;
    }  /*User defined functions*/
         func1()
      { 
          xxxx yyyyy  zzzzz
      } 
         func2()
      {
          xxx yyy zzzz 
      } ..............................
        ..............................
         function()
      {
        ............................ 
       }


A SIMPLE C PROGRAM : Example 2.1:  Write a program to print a message on screen.
Ans. 
/*PROGRAM TO PRINT A 
 MESSAGE ON SCREEN*/ ---> /*Method to write Comments*/
 #include<stdio.h> -----> /*This is a Header File*/


 main() ----------------> /*The main function*/
इन छोटे प्रोग्रामों को सी में फंक्शन कहा जाता है। प्रत्येक फंक्शन की कुछ विशेषताएं होती हैं, जो ये हैं –
1. प्रत्येक फंक्शन का एक अद्वितीय नाम होता है।
2. प्रत्येक फंक्शन उसे बुलाने वाले फंक्शन को कोई न कोई डेटा प्रकार लौटाता है।
3. प्रत्येक फंक्शन कोई न कोई डेटा प्रकार स्वीकारता है।

 {      ----------------> /*Starting of Program*/
सी में धनु कोष्ठकों ({ }) का महत्व :  यदि आप प्रोग्राम 1 को देखें, तो आपको उसमें धनु कोष्ठकों ( { }) का प्रयोग हुआ मिलेगा। बायां धनु कोष्ठक चिह्न ( { ) तीसरी पंक्ति में आया है, और दायां धनु कोष्ठक चिह्न ( } ) प्रोग्राम की अंतिम पंक्ति में आया है।
   printf("C Program\n"); --> /*Output Statement*/
 }      ----------------> /*Ending of Program*/
इन दोनों धनु कोष्ठकों के भीतर कई और कोड खंड भी हो सकते हैं, और प्रत्येक कोड़ खंड को धनु कोष्ठकों से घेरना जरूरी है। इसलिए एक लंबे सी प्रोग्राम में आपको कई धनु कोष्ठक चिह्न देखने को मिलेंगे। ध्यान में रखने की बात यही है कि इन कोष्ठक चिह्नों का सही तरह से प्रयोग किया गया हो। यदि बायां धनु कोष्ठक चिह्न आया हो तो उसका जोड़ी दार दायां कोष्ठक चिह्न भी सही जगह पर डालना होगा, अन्यथा आपका प्रोग्राम ठीक तरह से काम नहीं करेगा।


बड़े सी प्रोग्रामों में जिनमें बीसियों पंक्तियां और दर्जनों धनु कोष्ठक हो सकते हैं, आप चक्कर में पड़ जाएंगे कि कौन सा कोड खंड कहां खत्म हो रहा है।    इसलिए सी प्रोग्रामन सिखाने वाले अधिकांश पुस्तकों में नौसिखियों को सबसे पहली सलाह यही दी जाती है कि कोड व्यवस्थित ढंग से लिखें। अधिकांश पुस्तकों में बाएं धनु कोष्ठक चिह्न को अलग पंक्ति में अथवा फंक्शन नाम के ठीक आगे, और दाएं धनु कोष्ठक चिह्न को कोड खंड के अंत में अलग पंक्ति में लिखने की सलाह दी जाती है।


सी में वाइट स्पेस, white space ( वाइट स्पेस उन कुंजियों को कहते हैं, जो दो वर्णों के बीच रिक्त स्थान पैदा करते हैं, जैसे स्पेस-बार कुंजी, टैब कुंजी या ऐंटर कुंजी) का कोई महत्व नहीं होता।  


HERE :
  • main() -- is starting point of program, from WHERE THE EXECUTION BEGINS. It usually CALL OTHER FUNCTIONS to HELP PERFORM ITS JOB, some that we write and others from the standard libraries provided.
    main() --declares the start of the function, while the two curly brackets{} shows the start and finish of the function. Curly Brackets in C are used to group statements together as a function, or in the body of a loop.  Such a grouping is known as a compund statement or a block.  Every Statement within a function ends with a terminator semicolon ( ; ).

    main() में, जो गोल कोष्ठक है, इसमें हम वह डेटा टाइप रखते हैं, जिस पर वह फंक्शन कोई कार्य करता है। मान लीजिए आपने ऐसा कोई फंक्शन लिखा हो, जो दो पूर्णांक संख्याओं का योग पता करता है। तो हम इस फंक्शन को ये दो संख्याएं देंगे, और वह इनका योग निकालकर हमें देगा।
    .
  • #include<stdio.h>  -- reference to a LIBRARY file called stdio.h (header file) which contains information that must be included in the program WHEN IT IS COMPILED.  Basically, all the statements starting with # in a C program are called preprocessor directives. Just remember, that this statement allows you to use some predefined functions such as, printf(), in this case.
    सी की लाईब्रेरी बहुत बड़ी है, और हमें जब जैसा काम करना होता है, हम वैसी हीं लाईब्रेरी को #include के द्वारा प्रोग्राम का हिस्सा बना लेते हैं। हमें गणित संबंधी काम करना हो, तो math.h का प्रयोग करते हैं, ग्राफिक्स का काम हो, तो graphics.h का। इस विशाल लाईब्रेरी का प्रयोग करके हम भी नई लाईब्रेरी बना सकते हैं (जब हमें सी में कुछ हद तक महारत हासिल हो जाए तब)। इस प्रकार सी का जानकार, लगातार सी भाषा को नई लाईब्रेरी के साथ और ज्यादा समृद्ध बनाता रहता है। सी का संकलक बनाने वाली कंपनियाँ भी यही करती रहती हैं। इससे मूल सी भाषा में बदलाव नहीं होता और भाषा ज्यादा उपयोगी और समृद्ध बन जाती है
    .
  • printf("This is a C program\n");  --prints the words on the screen.  The text to be printed is enclosed in double quotes. The \n at the end of the text tells the program to print a new line as part of the output.  That means now if we give a second printf statement, it will be printed in the next line.
  • /*comments*/  --   (  /* */  ) is used anywhere within a program, as long as they are placed within the delimiters /* and */.  Such comments are helpful in identifying the program's principal features or in explaining the underlying logic of various program features. Useful for TEACHERS / TEACHING -    or to ----  remember any Discussion within a Program.
EXAMPLE 2.1  Develop an Algorithm, Flowchart and Program to add Two Numbers.

ALGORITHM: (1)START  ------>   
           (2)Input Two Numbers a,b 
           (3)Calculate sum as a+b
           (4)Store the result in sum
           (5)Display the Result 
           (6)STOP
                    _____
NOW FLOW CHART  / START \ 
                  \ _____ /
                      |
                      V
                 -----------------
                / INPUT a, b    / 
               -----------------
                      |
                      V
                -------------- 
               | sum = a+b    | 
                -------------- 
                      |
                      V

                 -----------------
                /   PRINT sum   / 
               -----------------
                      |
                      V
                    _____
                  ( START )
                    ──  
NOW PROGRAM : to count sum of two integers  :
#include<stdio.h>    ---> main library (general library)
#include<conio.h>    ---> useful for clrscr(); and getch();
  main() ---------------> either main() or void main() 
(NOTE : in the main function we are not using ( ; ) colon as we dont want to terminate the main function)
  {  -------------------> Starting point of program
    clrscr();   --------> to clear / wash previous screen
 int a,b,sum; -------> a,b are Variables (Declaration)

नाम द्वारा निर्दिष्ट ये आंकड़े दो प्रकार के होते हैं, चर राशियां (वेरिएबल्स, variables) और अचर राशियां (कॉन्सटेन्ट्स, constants)।
    printf("\nENTER FIRST INTEGER: ") ---->user input

    scanf("%d",&a);  ---> intake / storing and defining &a
    printf("\nENTER SECOND INTEGER: ") --->user input
    scanf("%d",&b); ----> intake / storing and defining &b
    sum=a+b;    --------> Computing / Formula-Definition 
  getch();   -----------> holds the result on screen
}  ---------------------> end of the program

NOW OUTPUT OF THE PROGRAM


============================================= 
2.5 WRITING A C PROGRAM
-----------------------------------------------------------------------------------------
A C program can be executed on platform such as DOS (Disk Operating System), UNIX etc.
DOS stores C program with a file extension    (  .c  ) . Program text can be entered using any text editor(संपादकsuch as EDIT or any other.  To edit a file called testprog.c using 'EDIT' editor(संपादकgives:

C:> edit testprog.c      (or you can use notepad if DOS is not available)


If you are using Turbo C, then Turbo C provides its own editor which can be used for writing the program.  Just give the full pathname of the executable file of Turbo C and you will get the editor in front of you.  For example:


C:> turboc\bin\tc       (general path for tc.exe file - to run Turbo C)


Here, TC.EXE is stored in bin sub-directory of turboc directory.  After you get the menu(सूची) just type the program and store it in a file using the menu provided.  The file automatically gets the extension of  .c .


UNIX also stores C program in a file with extension .C .  This identifies it as a C program.  The easiest way to enter your text is using a text editor like vi, emacs or xedit.  To edit a file called testprog.c using vi type :


$ vi  testprog.c  


The editor is also used to make subsequent(अनुवर्ती, आगामी) changes to the program.


-------------------------------------------------------
2.6  COMPILING  A  C  PROGRAM
-------------------------------------------------------
After yo have written the program the next step is to save the program in a file with extension  .C .  This program is in High-Level Language.  But this language is not understood by the computer.   So, the next step is to convert the High-Level Language Program (source code to object code.  The compiler will compile the program successfully if the program is syntactically correct;  else the object code will not be produced.  This explained below :


WHAT IS A COMPILER :

    ___________     __________     _________
   |High Level |   |Translator|   |Machine  |
-->|Language   |-->|(Compiler)|-->|Language |
   | Program   |   |          |   |Program  |
    ===========     ==========     =========


Fig: THE  PROCESS  OF  TRANSLATION  


-----------------------------------------------
2.6.1  THE  C  COMPILER :
-----------------------------------------------
If you are working on UNIX platform, then if the name of the program file is testprog.c to compile it, the simplest method is to type :


cc testprog.c 


This will compile testprog.c, and, if successful, will produce a executable file called a.out.  If you want to give the executable file any other, you can type :


cc testprog.c -o testprog 


This will compile testprog.c, creating an executable file testprog.exe


If you are working with TurboC on DOS platform then it is done by Menu. It the program is syntactically correct then this will produce a file named as testprog.obj. If not, then the syntax error will be displayed on hte screen and the object file will not be produced.  The process of removing the errors from the program is called as the debugging (प्रोग्राम को दोष मुक्त करना)


------------------------------------------------------------------------
2.6.2  SYNTAX (वाक्यविन्यास) ERROR  AND  SEMANTIC(LOGICAL) ERRORS(तर्क त्रुटि or अर्थ त्रुटि)  :
------------------------------------------------------------------------
Every language has an associated grammar, likewise, C also follows certain syntax rules. When a C program is compiled, the compiler will check that the program is syntactically correct.  IF there are any syntax errors in the program, those will be displayed on the screen with the corresponding line numbers.
Example : 2.3:  Write a program to print a message on the screen.
   #include<stdio.h   ------NOTE---- here > is missing  
     main()
   {
     printf("Hello")    
             ---------------NOTE---- here { is missing


Now if we compile the above program, will get the following errors :
     Error test.c  1: No file name ending
     Error test.c  5: Statement missing;
     Error test.c  6: Compound statement missing  }


SEMANTIC ERRORS or LOGICAL ERRORS(तर्क त्रुटि):  

  • Shown at the time of compilation. (displayed as warning).
  • If a particular statement has no meaning.  (The program does compile with these errors, but it always advised to correct them, since they may create problems while execution).
  • e.g. - Like you have declared a variable but have not used it. 
  • These variables are unnecessarily occupying the memory.
CHECK YOUR PROGRESS 2 :
Q.1  What is the basic unit of a C program ? (Ans.: I think the basic unit is 'a function' ?)


Q.2  "The program is syntactically(वाक्य विश्लेषणात्मक) correct".  What does it mean ?
Ans. It means all commands / instructions are written correctly but there may be some command or commands / instruction or instructions which may be un-necessary.


Q.3  Indicate the syntax errors in the following program code :
Ans. eazy - useless.


-----------------------------------------------------------------------------
2.7 LINK AND RUN THE C PROGRAM 
-----------------------------------------------------------------------------
After COMPILATION --> Next Step is LINKING the PROGRAM
  • Compilation produces a file with an extension .obj
  • this .obj file cannot be executed since it contains calls to functions defined in the standard library (header files of C Language.
  • When you call a function that is not the part of the program you wrote, C remembers its name. Later the linker combines the code you wrote with the object code already found in the standard library.  This process is called linking
  • In other words LINKER IS A PGOGRAM that LINKS SEPARATELY COMPILED FUNCTION TOGETHER - INTO - ONE PROGRAM.
  • The output of the linker in an executable program i.e., a file with an extension .exe


2.7.1 RUN THE C PROGRAM THROUGH THE MENU :
When we are working with TurboC in DOS (Disk Operating System) environment, the menu in the GUI that pops up when we execute the executable file of TurboC contains several options for executing the program :
i) Link,, after Compiling.
ii) Make, compiles as well as links.
iii) Run
we also get the output on user screen.  To see the output we have to shift to user screen window.


2.7.2   RUN FROM AN EXECUTABLE FILE :
An .exe file produced by can be directly executed 


e.g. - when written directly on dos prompt like if the file name is hello.exe --- you can write directly on 


C:\>hello.exe    ---------> it will run


in UNIX a program called make which allow very complicated programs to be compiled quickly, by reference to a comfiguration file usually called makefile. e.g. :


make testprog


This will compile testprog.c as well as link your program with the standard library so that you can use the standard library functions such as printf and put the executable code in testprog


2.7.3  LINKER ERRORS :

  • Sometime program contains syntax errors --> hence the program does not compile, but sometime program compiles successfully but we are unable to get the executable file, this happens when there are certain LINKER ERRORS in the program.  
  • For example, the object code of certain standard library function is not present in the standard C library; 
  • the definition for this function is present in the header file that is why we do not get a compiler error. 
  • Such kind of errors are called LINKER ERRORS.
  • The .exe file would be created only when LINKER ERRORS are corrected.
(Q. WHAT ARE LINKERS AND LOADERS)


2.7.4  LOGICAL AND RUNTIME ERRORS :
After the program is compiled and linked successfully there are three possibilities : (The errors which are not detected by the compiler or the linker are known as LOGICAL ERRORS).
  1. The program executes and we get correct result. (it means the program is correct)
  2. The program executes and we get wrong results; (means there is some logical mistake in the program= LOGICAL ERRORS) these type of errors are corrected by DEBUGGING. 
  3. The program dos not execute completely and aborts in between. (only detected during execution, known as RUN TIME ERRORS. These errors do not produce the result at all, the execution stops in between 'runtime error' message is flashed on the screen.



EXAMPLE :  2.4  (EXAMPLE OF A  LOGICAL ERROR)

include<stdio.h>                    
main()                              
{                                   
    int a,b,c,sum,avg;     -------Wrong Instruction------> we are using average as Integer
    a=10;                                                                                  which is an error,   
    b=5;                                                                                 
    c=20;                             
    sum=a+b+c;                        
    avg=sum/3;                          
    printf("The Average is %d\n",avg);  
}                                     

NOW OUTPUT IS:                       .      
                  The average is 8   .  But The result should
                                     .  be  8.33 


NOTE :   सी के आरक्षित शब्द, जैसे int, char, float, struct, return आदि को राशियों के नामों के रूप में इस्तेमाल नहीं किया जा सकता। इन आरक्षित पदों की संपूर्ण सूची यह है:-  auto break case chart const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while    उदाहरण:  while, switch, case ये अमान्य नाम हैं, क्योंकि ये सब सी के आरक्षित शब्द हैं।

EXAMPLE : 
#include<stdio.h>
main()
{
  int a,b;
  float c;
  a=10;
  b=10;
  c=(a+b)/(a-b);
  printf("The result is %f\n",c);  
}
The above program will compile and link successfully, it will execute till the first printf statement and we will get the message in this statement, as soon as the next statement is executed we get a RUNTIME ERROR of "Divide by zero" and the program halts (this is a RUNTIME ERROR)



[ In computer science, run time, run-time, runtime, or execution time is the time during which a program is running (executing), in contrast to other phases of a program's lifecycle such as compile timelink timeload time, etc.  ------------  A run-time error is detected after or during the execution of a program, whereas a compile-time error is detected by the compiler before the program is ever executed. Type checkingstorage allocationcode generation, and code optimization are typically done at compile time, but may be done at run time depending on the particular language and compiler  ------- Ref: Wikipedia  for MORE DETAILS CLICK HERE ]

----------------------------------------------
2.8  DIAGRAMMATIC REPRESENTATION OF PROGRAM EXECUTION PROCESS : 
----------------------------------------------

The following figure shows the PROGRAM EXECUTION PROCESS :

CHECK YOUR PROGRESS 3 :
1.  What is the extension of an executable file ?

Ans.     .exe  ( dot EXE)
2.  What is the need for linking a compiled file ?

Ans.    When programmers talk about creating programs, they often say, "it compiles fine" or, when asked if the program works, "let's compile it and see". This colloquial usage might later be a source of confusion for new programmers. Compiling isn't quite the same as creating an executable file! Instead, creating an executable is a multistage process divided into two components: compilation and linking. In reality, even if a program "compiles fine" it might not actually work because of errors during the linking phase. The total process of going from source code files to an executable might better be referred to as a build
3. How do you correct the logical errors in the program? 
Ans: By De-Bugging,  (If using TurboC then there is a Menu named DEBUG).


---------------------------------------------
SUMMARY
---------------------------------------------

  • Difference Between High Level / Low Level Language.
  • What is C ?  Features of C ? Emergence of C.
  • What is Middle Level Language (MLL). Advantage of High Level Language.
  • Converting Algorithm and Flowchart into a C program.
  • Writing and Storing a C Program in a file in case of UNIX as well as DOS.
  • How to compile and Run a C Program.
  • Different Type of Errors in (Logical Errors, Linker Errors, Runtime Errors, Compile Time Errors, Execution Time Errors, etc. etc.)
2.10  SOLUTIONS / ANSWERS :
(Please refer Page No. 35 of MCS-011) - Also see the above Mentioned Answers.

No comments:

Post a Comment