An introduction to C
Language
C is a high level
programming language developed at AT & T’s (American Telegraph and
Telephone) Bell Laboratories of USA in 1972. It was designed and written by a
man named Dennis Ritchie. In the late seventies C began to replace the more
familiar languages of that time like PL/I, ALGOL. It is general purpose high
level programming language, which is used to develop business programs, text
processing programs, database management programs and even to develop Operating
System (OS). It is also known as structured programming language.
Structured Programming
is a programming methodology that produces programs with clean flow, clear
design, and a degree of modularity or hierarchical structure. Benefits of
structured programming include ease of maintenance and ease of readability by
other programmers.
Advantages of
structured programming language
A large and complex
program can be divided into several simpler and manageable sub modules.
Support simultaneously
coding of modules by multiple programmers at a time.
Modules and functions
once built here can be used in other programs.
It reduced testing and
debugging time.
The program can be
easily modified.
It is portable and
occupies less memory space in computer.
Features/Characteristics
of C language
It is structured
programming language.
It is general purpose
programming language.
It contains rich and
powerful set of operators.
It contains rich and
powerful set of declaration and data types.
It allows manipulation
of internal process registers.
It is a middle level
programming language that supports both high and low level programming
languages. So, it is used to develop system software and application software.
Similarities in QBASIC
and C language
Both languages can be
used to develop structured programs.
Both languages support
local and global variables.
Both languages support
procedures.
Differences between
QBASIC and C language
QBASIC is high level
language whereas C language is a middle level language.
QBASIC supports both
sub procedure and function procedure whereas C language supports only function
procedure.
QBASIC is basically
used for developing application software whereas C language is used to develop
system software and application software.
Basic elements of C
language are listed below
i. C Language character
set
The C character set is
a set of characters which are allowed to represent information in C language.
The C character set consists of the alphabets (both upper case and lower case),
the digits (0-9) and certain special symbols (+ - * & ^ % $ # @ ! ? >
< etc ).
ii. Identifiers in C
Language
In a C program every
word is either classified as an identifier or a keyword. Identifiers are used
to identify or name various program-elements such as variables, symbolic
constants, functions, etc.
iii. keywords in C
Language
Keywords are reserved
words which have special meaning for the
C compiler. Keywords are not allowed to use as identifiers. Keywords are also known as reserved words.
There are 32 keywords in C. The keywords of C language are auto, double, int,
struct, break, else, long, switch, case, enum, register, typedef, char, extern,
return, union, const, float, short, unsigned, continue, for, signed, void,
default, goto, sizeof, volatile, do, if, static, while.
iv. Data type in C
Language
The data type in C
language defines the amount of storage allocated to variables and the values
that variables can accept. C language supports basic four data types. They are
char, int, float and double.
The space occupies by
int is 2 bytes, char is 1 byte, float is 4 bytes and double is 8 bytes.
Data type modifier
The keyword that
modifies the size and range of the basic data types is known as Data Type
Modifier. The modifiers define the amount of storage allocated to the variable.
The C language uses four types of data type modifiers. They are signed,
unsigned, long and short. The data type modifier changes the size and range of
the data that can be stored in a variable.
v. Variables in C
Language
In C language, you need
to declare variables before you use them in a program. A variable declaration
tells the compiler the name and type of a variable. If a program attempts to
use a variable that has not been declared, the compiler generates an error
message.
In C language to store
a character, a Character variable is used, to store string, an array variable
is used and to store numeric values, numeric variables like integer variables
and floating point variables are used.
vi. Operators in C
Language
An operator is a symbol
that operates on a certain data type and manipulates them to produce the
output. The operators used in C are listed below.
Arithmetic Operator
The arithmetic operator
performs arithmetic operation on two operands. The following arithmetic operators
are used in C +, -, /, *, % .
Unary Operators
A unary operator is an
operator, which operates on one operand. The commonly used unary operators are
increment (+ +) and decrement (- -) operators. The ++ operator increments the
value of the variable by one, whereas the - - operator decrements the value of
the variable by one. For example, x ++ is equivalent to x = x + 1 and x- - is
equivalent to x = x-1.
Relational Operators
Relational operators
compare two expressions and return result in term of True or False. The
following relational operators are used in C language.
operators meaning
expression <, <= , >= ,= = , ! = ,>
Logical Operators
A logical operator is
used to combine relational expressions and returns the result either in True or
False. The logical operators used in the C language are given below.
&&, ||, !.
Assignment Operator
An assignment operator
(=) is used to assign a constant or a value of one variable to another.
Conditional Operator
A conditional operator
checks for an expression, which returns either a true or a false value.
Header files in C
Language
A header file is a
standard file that contains definitions of variables and functions necessary
for the functioning of a program. A header file is included in a program by
using ‘#include’ directive. The header files in C language have .h extension.
Some header files are listed below.
i. stdio.h
This is the standard
input output header file that provides functions for performing input and
output operations. It contains the declarations of I/O functions like printf (
), scanf( ), putchar(), getchar() etc.
ii. string.h
The string header file
provides many functions useful for manipulating strings which contains the
declarations of library functions like strrev(), strlwr(), strupr(), strchr (
), strcmp( ), strlenr( ),etc.
iii. math.h
The math.h file is
standard header file that handles mathematical functions like sqrt(), pow(),
etc.
iv. ctype.h
The ctype.h header file
contains declarations for character classification functions like isspace(),
isupper(), tolower() etc.
Some C Language
programs are given below
/* this program
displays the product of two numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, p;
clrscr();
a = 20;
b
=16;
p= (a * b);
printf(“product of two numbers is %d”, p);
getch();
}
/* to check whether an
input number is even or odd */
#include<stdio.h>
#include<conio.h>
void main()
{
int n, r;
clrscr();
printf(“Enter an integer”);
scanf(“%d”,&n);
r = n% 2;
if(r==0)
printf (“%d is even number”,n);
else
printf(“%d is odd number”,n);
getch();
}