Programming in C

Structured Programming

The computer is a man-made electronic machine. It needs instructions to perform any task. There are many programming languages that enable us to write a set of instructions. There are many instructions in a program that are needed to arrange in a certain order. Normally when we run a program, the execution of instructions takes place from the top to bottom, one after another. So, a programming technique in which small programs are integrated in order to form a complete program is known as structured programming or modular programming. The structured programming follows a Top-Down designing model. Top-Down design means the process of dividing large and difficult problems into small and simple blocks.
 
 
Some of the structured programming languages are C, Java, Pascal, Ada, and so on.
 
Programming in C and Structured Programming || Basic Overview
Fig. Programming in C
 
 

Advantage of Structured programming

  • Effective programs can be made.
  • More than one programmer can work at a time in a program.
  • Easy to understand and modify the program block.
  • The same block or module can be used more than once within a program and in another program as well.
  • Each block can be tested separately. So debugging is easy and fast.
 

Introduction to C language

C is a high-level structure programming language. It was developed by Dennis Ritchie at Bell Telephone Laboratories in 1972 AD. C allows controlling the computer hardware and peripherals. It is also called middle-level language because it has both features of low-level language and high-level language. It is used to develop the system software. UNIX operating system was written using C language. Many other high-level languages have been developed by using C. Perl is a popular programming language for designing web pages that was based on C. C++ language is an extended version of C.
 
C is a compiler-based programming language. A compiler translates the source code into object code, which is executable form. The process of converting the high-level program (source code) into the machine-level program (object code) is called compiling.
 

Some popular compilers of C.

  1. Turbo C compiler (TC)
  2. Borland C compiler (BC)
  3. Sun V compiler (CC)
  4. GNO C compiler (GCC)
 

Characteristics of C language

C has lots of characteristics that make this language more powerful and popular. Some of the characteristics are given below:
  1. It is a high-level programming language. So, it is easy to understand and write programs in C.
  2. C is a structured programming language.
  3. It has enough built-in functions and operations.
  4. It has a variety of data types to represent different kinds of data.
  5. It occupies less memory space.
  6. It runs on different machines.
 

Advantages of C language

  1. Readability: Programs are easy to read.
  2. Maintainability: Programs are easy to maintain.
  3. Portability: Programs are easy to port across different computers.
  4. Reusability: Programs written in C can be reused.

Data Types in C

A data type defines a set of values that a variable can store for calculation. Each and every variable and constant that we are using in our program must be declared before its use. The basic types of data types used in C are given below:
 
Data TypeMeaningMemory Size
charCharacter1 byte
intInteger2 bytes
floatsingle-precision floating-point4 bytes
doubledouble-precision floating-point8 bytes
 

Basic elements of C language

Character Set

The valid characters which can be used in C programming are called character set. The following table shows the valid character sets in C.
 
AlphabetUppercase (A,B, … ,Y, Z)
Lowercase (a, b, … , y, z)
Numbers0, 1, 2, … , 8, 9
Special Symbols+ – * / = () {} [] <> ' " ! # % & _ | ^ ~ . , ; : ?
White space charactersb n t and so on.
 
 

Identifiers

Identifiers can be defined as the name of the variables, functions, arrays, structures, and others created by the programmer. These are tokens which are a sequence of letters, digits, and underscore. keywords can’t be used as identifiers and it does not have spaces between them.
 

Keywords or Reserved Words

Keywords are the words whose meaning has already been explained to the C compiler. The keywords can’t be used as a variable name. The keywords are also called “reserved words”. There are 32 keywords available in C. Different versions of C may have some more keywords. The keywords available in C are given below:
 
autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile
 
 

Types of specifier/Modifier

Format Specifiers are the characters starting with the % sign and followed with a character. While the data is being inputted or output, it must be used. It specifies the type of data is being processed. It is also called conversion specification.
 

Variable

A name given to the location in memory is called variable. The value of the variable may change throughout the program execution. Each and every variable must be declared before its use.
 

Rules for declaring variable names

  • A variable name is a combination of alphabets, digits, or underscore.
  • The first character in the variable name must be an alphabet.
  • No comma or blank spaces are allowed within a variable name.
  • A keyword can not be used as a variable name.
 
Data TypeFormat Specifiers
Integer%d
Unsigned integer%u
Octal%o
Hexadecimal%x
Float (Simple)%f
Float (Scientific or exponential notation)%e
Character%c
String%s
 
 

Constants

A data item that does not change during the execution of the program is called constant. Constant is sometimes referred to as literal. We can define constant value using #define directive before main() function. This is called the preprocessor directive. The semi-colon (;) is not used after the value of constant. For general understanding, constant names are written in uppercase.
 

Operators

Operators are special types of symbols or characters used to manipulate the data in the computer system. Some operators used in C programming are given below:
  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment operators

Arithmetic Operators

The operators which are used for the manipulation of digits are called arithmetic or mathematical operators. Some arithmetic operators are given below:
 
OperatorMeaningExample
+Additionx=a+30
SubtractionA=r-5
*MultiplicationArea=L*B
/DivisionV=x/R
%ModulusC=W%12
 

Relational Operator

The operators which are used to compare two or more values are called relational operators. The relational operators used in C are given below.
 
OperatorsMeaning
==Equal to
!=not equal to
<less than
>greater than
<=less than or equal to
>=greater than or equal to
 

Logical Operator

The operators which are used to combine two or more relational operators are called logical operators. It gives either true or false results after evaluation. Logical operators used in C are given below.
 
OperatorsMeaning
&&AND
||OR
!NOT
 

Ternary Operator

The operator which checks the condition and gives either true or false value is called a ternary or conditional operator. It has three operands.
 

Assignment Operator

The operator which is used to assign value to the variable is called the assignment operator.
 

Input and output Function

In C programming we use scanf() and printf() for input and output function respectively. To use these functions we must use #include
 

scanf() function

The library function is used to get input from the user. its syntax is given below:
 
scanf(control string,arg1,arg,2,…)
 

printf() function

This function is used to display the output to the user. its syntax is given below:
 
printf(control string,arg1,arg2,…)
 

Header Files

The first statement in C language is preprocessor directive. The preprocessor directive includes the following header files.
 
Header FilesMeaning
< stdio.h>Basic input/output function
< stdlib>Standard library function
< string.h>String manipulation function
< math.h>mathematical function
< graphic.h>Graphical function
< malloc.h>Memory allocation function
< time.h>Time computing function
< ctype.h>Character manipulation function
< dos.h>Function linking DOS routines
 
 

Some Practical Examples of C Programming

WAP to find the sum of any two values (5 and 10).

#include 
int main(void)
{
int a=5, b=10, c;
c=a+b;
printf(“sum of two numbers is %d”,c);
getchar();
return 0;
}

 WAP to find the sum of any two numbers supplied by the user.

 
    #include&lt;stdio.h>
    #include&lt;conio.h>
    void main(){
    int a,b,s;
    clrscr();
    printf("Enter two numbers");
    scanf("%d%d",&amp;a,&amp;b);
    s=a+b;
    printf("the sum is %d",s);
    getch();
    }
 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *