Basic Structure of C Program


Here, we are going to learn about the basic structure of a C program. A c program is divided into different sections. There are 6 main sections to a basic c program.

The six sections are:-

  1. Documentation Section
  2. Link Section
  3. Definition Section
  4. Global Declaration Section
  5. Main Section
  6. Declaration Part
  7. Execution Part
  8. User Defined functions/ Subprogram Section

Lets start with sections:

  • Documentation Section:- It consists of comment lines. Comments are added into program to increase its understandability and readability.

                        

  • Link Section:- All the library files that are to be included in the program are mentioned in this section.
#include<stdio.h>
#include<conio.h>
#include<string.h>               

          The compiler is thus informed about the library files to be linked with the program. This process  is called linking.

  • Definition Section:- In this section you will see all the symbolic constants that are to be used in the program.
#define PI=3.14
#define TRUE=1
#define FALSE=0                                   

          "define" keyword is used in this section.

  • Global Declaration Section:-  All the variables used in more than one function are called as global variables and they should be declared before using anywhere in the program.

                                                    

  • Main Section:- Every C program must have exactly one main function. All the statements in the main section must appear between the opening and closing brace and must end with semicolon.
main( )
{
     statement 1;
     statement 2;
}                                    

    • Declaration part:- All the variables to be used in main section are declared here and thus can be used later.  
    • Execution part:- This part consists of main logic which solves the program.         

  •  User Defined Section:- All the user defined functions called by main function are defined here.





Introduction to programming

Today, we use computers in almost every part of our life. Lets think about some of the different ways in which people use computers. 
In school, students use computers for various tasks such as writing letters, searching for information, sending emails, and in online classes and many more. 
At work, people use computers to analyze data, make presentations, conduct business transactions, to make calculations fast, online meetings etc. 
At home, people use computers for tasks such as paying bills,  online shopping, communicating with friends and family, playing computer games etc. Also our cell phone, microwave ovens, refrigerators, smart watches, washing machines and other equipment’s are all using computers in direct or indirect forms. The uses of computers are almost limitless in our everyday lives.

Computers can do such a wide variety of things because they can be programmed. This means that computers are designed to do any job that user tell them to do. A program is a set of instructions that a computer follows to perform certain task. We all are familiar with word processing software’s like Microsoft word, open office documents etc. These are word processing programs those allows you to create, edit, and print documents with your computer. Programs are commonly referred to as software. Software is essential to a computer because it controls everything the computer does. All of the software that we use to make our computers useful is created by individuals working as programmers or software developers. A programmer, or software developer, is a person with the training and skills necessary to design, create, and test computer programs. Computer programming is an exciting and rewarding career.

Programs that make a computer useful for everyday tasks are known as application software. These are the programs that people normally spend most of their time running on their computers. A computer will store the data and instructions in its memory. Computer memory is organized as the collection of bits. Bit is the smallest part of memory which can store two situations called on or off. Memory is generally managed by bytes (Where a byte is a combination of 8 bits). A computer’s CPU can only understand instructions that are written in machine language. Because people find it very difficult to write entire programs in machine language, other programming languages have been invented.

The CPU is an electronic device that is designed to do specific things. In particular, the CPU is designed to perform operations such as the following:

• Reading a piece of data from main memory

• Adding two numbers

• Subtracting one number from another number

• Multiplying two numbers

• Dividing one number by another number

• Moving a piece of data from one memory location to another

• Determining whether one value is equal to another value


As you can see from this list, the CPU performs simple operations on pieces of data. The CPU does nothing on its own, however. It has to be told what to do, and that’s the purpose of a program. A program is nothing more than a list of instructions that cause the CPU to perform operations. Each instruction in a program is a command that tells the CPU to perform a specific operation. To a CPU, however, an instruction to perform an operation is written in 0s and 1s because CPUs only understand instructions that are written in machine language and machine language instructions always have an underlying binary structure.

Although a computer’s CPU only understands machine language, it is impractical for people to write programs in machine language. For this reason, assembly language was created in the early days of computing as an alternative to machine language. Instead of using binary numbers for instructions, assembly language uses short words that are known as mnemonics. For example, in assembly language, the mnemonic add typically means to add numbers, mul typically means to multiply numbers, and mov typically means to move a value to a location in memory. When a programmer uses assembly language to write a program, he or she can write short mnemonics instead of binary numbers.

Although assembly language makes it unnecessary to write binary machine language instructions, it is not without difficulties. Assembly language is primarily a direct substitute for machine language, and like machine language, it requires that you know a lot about the CPU. Assembly language also requires that you write a large number of instructions for even the simplest program. Because assembly language is so close in nature to machine language, it is referred to as a low-level language.

So, a new generation of programming languages known as high-level languages began to appear. A high-level language allows you to create powerful and complex programs without knowing how the CPU works, and without writing large numbers of low-level instructions. In addition, most high-level languages use words that are easy to understand.

Each high-level language has its own set of predefined words that the programmer must use to write a program. The words that make up a high-level programming language are known as keywords or reserved words. Each keyword has a specific meaning, and cannot be used for any other purpose.

Because the CPU understands only machine language instructions, programs that are written in a high-level language must be translated into machine language. Depending on the language that a program has been written in, the programmer will use either a compiler or an interpreter to make the translation. A compiler is a program that translates a high-level language program into a separate machine language program. The machine language program can then be executed any time it is needed.


Overview of C

'C' seems a strange name for a programming language. But this strange sounding language is one of the most popular computer languages today because it is a structured, high-level, machine independent language. It allows software developers to develop programs without worrying about the hardware platforms where they will be implemented.

The root of all modern languages is ALGOL, introduced in the early 1980s. ALGOL was the first computer language to use a block structure. Although it never became popular in USA, it was widely used in Europe. Computer scientists like Corrado Bohm, Guiseppe Jacopini and Edsger Dijkstra popularized this concept during 1960s.
     
History of C
                                                       
In 1967, Martin Richards developed a language called BCPL primarily for writing system software. In 1970, Ken Thompson created a language using many features of BCPL and called it simply B. Both BCPL and B were "typeless" system programming languages. 
C was evolved from ALGOL, BCPL and B by Dennis Ritchie at the Bell Laboratories in 1972. C uses many concepts from these languages and added the concept of data types and other powerful features. Since it was developed along with the UNIX operating system, it is strongly associated with UNIX.

During 1970s, C had evolved into what is now known as "traditional C". The language became more popular after publication of the book 'The C Programming Language' by Brian Kernighan and Dennis Ritchie in 1978. The book was so popular that the language came to be known as "K&R C" among the programming community.

To assure that the C language remains standard, in 1983, American National Standards Institute (ANSI) appointed a technical committee to define a standard for C. The committee approved a version of C in December 1989 which is now known as ANSI C.

During 1990's, C++, a language entirely based on C, underwent a number of improvements and changes and became an ANSI/ISO approved language in November 1977. C++ added several new features to C to make it not only a true object-oriented language but also a more versatile language. During the same period, Sun Microsystems of USA created a new language Java modelled on C and C++.

All popular computer languages are dynamic in nature. They continue to improve their power and scope by incorporating new features and C is no exception. Although C++ and Java were evolved out of C, the standardization committee of C felt that a few features of C++/Java, if added to C, would enhance the usefulness of the language. The result was the 1999 standard for C. This version is usually referred to as C99.


Post a Comment

If you have any doubts, Please let me know

Previous Post Next Post