(AN AUTONOMOUS UNIT OF RANCHI UNIVERSITY FROM 2009)
- Prakash Kumar, Dept. of CA
______________________________________________________________________
C is a general-purpose, high-level
language that was originally developed by Dennis M. Ritchie to develop the UNIX
operating system at Bell Labs. C was originally first implemented on the PDP-11
computer in 1972.
Some
facts about C:
·
C was invented to
write an operating system called UNIX.
·
C is a successor
of B language which was introduced around the early 1970s.
·
The language was
formalized in 1988 by the American National Standard Institute (ANSI).
·
The UNIX OS was
totally written in C.
·
Today C is the
most widely used and popular System Programming Language.
·
Most of the
state-of-the-art software has been implemented using C.
·
Today's most
popular Linux OS and RDBMS MySQL have been written in C.
Take a NOTE:
[C
is a programming language that is powerful, efficient and compact. C combines
the features of high level language with element of the assembler.
C
is the offspring of “Basic Combine Programming Language” called ‘B’ was
developed in 1960 at “Cambridge University”
The
‘B’ language was modified by “Dennis Ritchie” at “Bell Laboratory” in 1972. ]
A C program basically consists of the following parts:
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
#include
<stdio.h>
int main()
{
printf("Hello,
World! \n");
return 0;
}
Character Set
It
is helpful to run any type of program by the ‘words’, number’, and
‘expression’.
There
are four character sets in C:-
·
Letter
·
Digit
·
Special
character
·
White
space
Tokens in C
A C program consists of various tokens and a token is a keyword, an
identifier, a constant, a string literal, or a symbol.
Semicolons
In a C program, the semicolon is a statement terminator. That is, each
individual statement must be ended with a semicolon. It indicates the end of
one logical entity.
Comments
Comments are like helping text in your C program and they are ignored
by the
compiler. They start with /* and terminate with the characters */ as
shown
below:
/* my first
program in C */
Identifiers
A C identifier is a name used to identify a variable, function, or any
other userdefined item. An identifier starts with a letter A to Z, a to z, or
an underscore ‘_’ followed by zero or more letters, underscores, and digits (0
to 9).
C does not allow punctuation characters such as @, $, and % within
identifiers.
C is a case-sensitive programming language. Thus, Marwari
and marwari
are two different identifiers in C.
Rules
for Identifiers:-
- The character
must be an alphabet and underscore.
- It must consist
of only letters, digit or underscore.
- Only 31
characters are significant.
- It cannot use keywords.
- It must not
contain white space.
Keywords
The following list shows the reserved words in C. These reserved words
may not be used as constants or variables or any other identifier names(32
keywords).
Data Types
Data types in
C refer to an extensive system used for declaring variables or functions of
different types. The type of a variable determines how much space it occupies
in storage and how the bit pattern stored is interpreted.
VARIABLES
A variable is nothing but a name given to a storage area that our
programs can manipulate. Each variable in C has a specific type, which
determines the size and layout of the variable's memory; the range of values
that can be stored within that memory; and the set of operations that can be
applied to the variable.
The name of a variable can be composed of letters, digits, and the
underscore character. It must begin with either a letter or an underscore.
Upper and lowercase letters are distinct because C is case-sensitive.
Lvalues and Rvalues in C
There are two kinds of expressions in C:
·
lvalue : Expressions that refer to a memory location are called
"lvalue" expressions. An lvalue may appear as either the left-hand or
right-hand side of an assignment.
·
rvalue : The term rvalue refers to a data value that is stored at some address
in memory. An rvalue is an expression that cannot have a value assigned to it
which means an rvalue may appear on the right-hand side but not on the
left-hand side of an assignment.
Constants and literals
Constants refer to fixed values that the program may not alter during
its execution. These fixed values are also called literals.
Character literals
Character literals are enclosed in single quotes, e.g., 'x' can be
stored in a simple variable of char type.
String literals
String literals or constants are enclosed in double quotes
"". A string contains characters that are similar to character
literals: plain characters, escape sequences, and universal characters.
Eg: “ Marwari
College”
Defining Constants
There are two simple ways in C to define constants:
Using #define
preprocessor
#include<stdio.h>
#define a
10
#define b
20
void main()
{
printf("Value=%d",a*b);
}
Using const keyword
#include<stdio.h>
void main()
{
const
int a;
const
int b;
int x;
printf("Enter the value:");
scanf("%d",&a);
printf("Enter the value:");
scanf("%d",&b);
a=a+2;//Assignment of read only variable a
scanf("%d",&a);
printf("Value=%d",a*b);
}
Storage class
A
storage class defines the scope (visibility) and life time of variables and/or
functions within a C Program.
There
are following storage classes which can be used in a C Program
- auto
- register
- static
- extern
auto - auto is the default
storage class for all local variables.
int main()
{
int Count;
auto int Month;
return 0;
}
|
The
example above defines two variables with the same storage class. auto can only
be used within functions, i.e. local variables.
register -
register is used to define
local variables that should be stored in a register instead of RAM. This means
that the variable has a maximum size equal to the register size.
NOTE:
It cannot have the unary '&' operator applied to it (as it does not have a
memory location).
int main()
{
register int Miles;
return 0;
}
|
Register
should only be used for variables that require quick access - such as counters.
static -
static is the default
storage class for global variables. The two variables below (count and road)
both have a static storage class.
static int Count;
|
Static
variables can be 'seen' within all functions in this source file. Static can
also be defined within a function. If this is done the variable is initialized
at run time but is not reinitialized when the function is called. Inside a
function static variable retains its value during various calls.
NOTE
:
Here keyword void means function does not return anything and it does
not take any parameter. You can memoriese void as nothing. static variables are
initialized to 0 automatically.
extern -
extern is used to give a
reference of a global variable that is visible to ALL the program files. When
you use 'extern' the variable cannot be initialized as all it does is point the
variable name at a storage location that has been previously defined.
When you have multiple files and you
define a global variable or function which will be used in other files also,
then extern will be used in another file to give reference of defined
variable or function. Just for understanding extern is used to declare a
global variable or function in another files.
File 1: main.c
int count=5;
main()
{
write_extern();
}
|
File 2: write.c
void write_extern(void);
extern int count;
void write_extern(void)
{
printf("count is %i \n", count);
}
|
Operator
An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in operators and
provides the following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Operators
Precedence in C
Decision-making structures
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
eg: To find greatest between 3 numbers
#include<stdio.h>
int main()
{
int num1,num2,num3;
printf("\n Enter number1:");
scanf("%d",&num1 );
printf("\n Enter number2:");
scanf("%d",&num2);
printf("\n Enter number3:");
scanf("%d",&num3);
if(num1>num2 && num1>num3)
{
printf("\n Num1 is the largest");
}
else
{
if(num2>num3)
{
printf("\n Num2 is the largest");
}
else
{
printf("\n Num3 is the largest");
}
}
return 0;
}
Switch Statement
A switch statement
allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for
a switch statement
in C programming language is as follows:
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
default : /*
Optional */
statement(s);
}
eg:
#include<stdio.h>
main()
{
char ch;
float A1,A2,x,y,r;
printf("\n1 to find Area of rectangle or 2 to find Area of circle :");
scanf("%c",&ch);
switch(ch)
{
case'1':
printf("\n Enter the length x:");
scanf("%f",&x);
printf("\n Enter the breath y:");
scanf("%f",&y);
A1=x*y;
printf("Area of rectangle=%f",A1);
break;
case'2':
printf("\n Enter the radius r:");
scanf("%f",&r);
A2=3.14*r*r;
printf("Area of Circle=%f",A2);
break;
default:
printf("\n Wrong Choice");
}
}
The ? : Operator:
Conditional operator ? : can be used to replace if...else statements. It has the
following general form:
Exp1? Exp2: Exp3;
Where Exp1,
Exp2, and Exp3 are expressions.
#include<stdio.h>
int main()
{
int a,b;
printf("\n Enter the value of a:");
scanf("%d",&a);
printf("\n Enter the value of b:");
scanf("%d",&b);
((a>b?printf("\n A is greater"):printf("\nB is greater")));
return 0;
}