Structure Programming - C Language

Filter Course


Structure Programming - C Language

Published by: BhumiRaj Timalsina

Published date: 11 Jan 2022

Structure Programming - C Language

C Language

C is a structured programming language developed by Dennis Ritchie at Bell Laboratories in 1972. Since C is a structured programming language, a program in C language can be divided into small logical and functional modules. C++ language is an expanded version of C.

Characteristics of ‘C’ Language

 

  • Small size
  • Extensive use of function calls
  • Loose typing - unlike PASCAL
  • Structured language
  • Low level (BitWise) programming readily available
  • Pointer implementation - extensive use of pointers for memory, array, structures, and functions.

 Now, it had become a widely used professional language because of the following reasons.

  • It has high-level constructs.
  • It can handle low-level activities.
  • It produces efficient programs.
  • It can be compiled on a variety of computers.


Importance of C language

 

  • It is a robust language with a rich set of built-in functions and operators that can be used to write any complex program.
  • The C compiler combines the capabilities of an assembly language with features of a high-level language.
  • Programs written in C are efficient and fast. This is due to its variety of data type and powerful operators.
  • It is many time faster than BASIC.
  • C is highly portable this means that programs once were written can be run on another machine with little or no modification.
  • Another important feature of the C program is its ability to extend itself.
  • A-C program is basically a collection of functions that are supported by C library. We can also create our own function and add it to C library.
  • There are  32 keywords; several standard functions are available which can be used for developing a program.
  • C language is the most widely used language in operating systems and embedded system development today.


Keyword


It is a reserved word, some meaning is already available to that word and that meaning will be recognized by the compiler.
In ‘C’ programming language total no. of keywords are ‘32’
E.g.:- If, else, while, const, break….

Keywords=32, operators=44, separators=14.

Structure of a C program 


After the above discussion, we can formally assess the structure of a C program. By structure, it is meant that any program can be written in this structure only. Writing a C program in any other structure will hence lead to a Compilation Error.

1) Header Files Inclusion: The first and foremost component is the inclusion of the Header files in a C program. 
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. Some of C Header files are given below:

  • stddef.h – Defines several useful types and macros.
  • stdint.h – Defines exact width integer types.
  • stdio.h – Defines core input and output functions
  • stdlib.h – Defines numeric conversion functions, pseudo-random network generator, memory allocation
  • string.h – Defines string handling functions
  • math.h – Defines common mathematical functions

2) Main Method Declaration: The next part of a C program is to declare the main() function. The syntax to declare the main function is:

int main()
{}

Variable Declaration: The next part of any C program is the variable declaration. It refers to the variables that are to be used in the function. Please note that in the C program, no variable can be used without being declared. Also in a C program, the variables are to be declared before any operation in the function.
 Example: 

int main() {

int x;

 

Body: The body of a function in the C program, refers to the operations that are performed in the functions. It can be anything like manipulations, searching, sorting, printing, etc.
Example: 
int main() {

int a;

printf("%d", a);

 

Types of operator

  • Arithmetic operator: The operators which perform mathematical expressions like addition, subtraction, multiplication and division.
  • Relational operator: Relational operators are symbols used to compare two different expressions. The result of the comparison is either True or False.
  • Logical operator: Logical operators performs tests on multiple relation. Example: A < B and A > B
  • Unary operator: C also supports two very important operators called increment and decrement operators.

Loop

A loop causes a section of a program to be repeated a certain number of times. The repetition continues while the condition set for it remains true. When the condition becomes false, the loop ends the control is passed to the statement following the loop. A computer is the most suitable machine for performing repetitive tasks, and it can perform a task thousands of times. Every programming language has the feature to instruct to do such repetitive tasks with the help of certain statements. The process of repeatedly executing a collection of statements is called looping. The statements get executed many numbers of times based on the condition. But if the condition is given in such logic that the repetition continues any number of times with no fixed condition to stop looping those statements, then this type of looping is called infinite looping.

Variables

A variable in simple terms is a storage place which has some memory allocated to it. Basically, a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations which can be applied to them. Variables can be changed according to the need of the programmer.

Constant

As the name suggests the name constants is given to such variables or values in C programming language which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constants like integer, float, octal, hexadecimal, character constants etc. Every constant has some range. The integers that are too big to fit into an int will be taken as a long. Now there are various ranges that differ from unsigned to signed bits. Under the signed bit, the range of an int varies from -128 to +127 and under the unsigned bit, int varies from 0 to 255.Those values which can't change during the program execution is called constant. Example: A = 10.

Rule for naming variables in C

  • Every variable name should start with alphabets or underscore (_).
  • No spaces are allowed in variable declaration.
  • Except underscore (_) no other special symbol are allowed in the middle of the variable declaration (not allowed -> roll-no, allowed -> roll_no).
  • Maximum length of variable is 8 characters depend on compiler and operation system.
  • Every variable name always should exist in the left hand side of assignment operator (invalid -> 10=a; valid -> a=10;).
  • No keyword should access variable name (int for <- invalid because for is keyword).