what is c++ ?

what is c++ ?
C++ is an object-oriented programming language. It was developed by B.Jarne Strousstrup at AT&T Bell laboratories in Murray Hill, New Jersey, USA, in the early 1980’s. Strousstrup, an admirer of Simula67 and a strong supporter of C, wanted to combine the best of both the languages and create a more powerful language that could support object-oriented programming features and still retain the power and elegance of C. The result was C++, therefore, C++ is an extension of C with a major addition of the class construct feature of Simula67. Since the class was a major addition to the original C language, Strousstrup initially called the new language ‘C with classes’. However, later in 1983, the name was changed to C++. The idea of C++ comes from the increment operator ++, thereby suggesting that C++ is an augmented (incremented) version 0f C.
Comments: C++ introduces a new comment symbol // (double slash). Comments start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in the line, and whatever follows till the end of the line is ignored. Note that is no closing symbol.
Output Operator: The identifier cout (pronounced as ‘C out’) is a predefined object that represents the standard output stream in C++. Here, the standard output stream represents the screen. It is also possible to redirect the output to other output devices.
The operator <<>> is known as extraction or get from operator. It extracts (or takes) the value from the keyboard and assigns it to the variable on its right.












A Look at Procedure – Oriented Programming: Conventional programming, using high level languages such as COBOL, FORTRAN and C, is commonly known as procedure - oriented programming (POP). In the procedure-oriented approach, the problem is viewed as a sequence of things to be done such as reading, calculating and printing. A number of functions are written to accomplish these tasks. The primary focus is on functions.
Some characteristics by procedure-oriented programming are:
Emphasis is on doing things (algorithms).
Large programs are divided into smaller programs known as functions.
Most of the functions share global data.
Data move openly around the system from function to function.
Function transforms data from one form to another.
Employs top-down approach in program design.

Object-Oriented Programming Paradigm:
Some of the striking features of object-oriented programming are:
Emphasis is on data rather than procedure.
Programs are divided into what are known as objects.
Data structures are designed such that they characterize the objects.
Functions that operate on the data of an object are tied together in the data structure.
Data is hidden and cannot be accessed by external functions.
Objects may communicate with each other through functions.
New data and functions can be easily added whenever necessary.
Follow bottom-up approach in program design.

Organization of data functions in OOP:
Basic Concepts of Object-Oriented Programming:
Objects
Classes
Data Abstractions and encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing
OBJECTS: Objects are the basic run-time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represent user-defined data such as vectors, time and lists. Programming problem is analyzed in terms of objects and the nature of communication between them. Program objects should be chosen such that they match closely with the real-world objects. Objects take up space in the memory and have an associated address like a record in Pascal or a structure in C.
Two Ways of representing an object
Classes: The entire set of data and code of an object can be made a user-defined data type with the help of a class. In fact, objects are variables of the data type class. Once a class has been defined, we can create only number of objects belonging to that class. Each object is associated with the data of type class with which they are created. A class is thus a collection of objects of similar type. For example, mango, apple and orange are members of the fruit. Classes are user-defined data types and behave like the built-in types of programming languages. The syntax used to create an object is no different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement.
Fruit mango;
Will create an object mango belonging to the class fruit.

Data Abstraction and Encapsulation:
The wrapping up of data and function into a single unit (called class) is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions, which are wrapped in the class, can access it. These functions provide the interface between the object’s data and the program. This insulation of the data from direct access by the program is called data hiding or information hiding.
Abstraction refers to the act of the representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and functions to operate on these attributes. They encapsulate all the essential properties of the objects that are to be created. The functions that operate on these data are sometimes called methods or member function.
Since the classes use the concept of data abstraction, they are known as Abstract Data Types (ADT).
Inheritance: Inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. For example, the bird ‘robin’ is a part of the class ‘flying bird’, which is again a part of the class ‘bird’. The principle behind this sort of division is that each derived class shares common characteristics with the class from which it is derived.
In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes.

Property inheritance
Polymorphism: Polymorphism is another important OOP concept. Polymorphism, a Greek term, and means the ability to take more than one form. An operation may exhibit different behaviours in different instances. The behavior depends upon the types of data used in the operation. For example, consider the operation of addition. For two numbers, the operation will generate a sum. If the operands were strings, then the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviours in different instances is known as operator overloading. Using a single function name to perform different types of tasks is known as function overloading.
Polymorphism plays an important role in allowing objects having different internal structure to share the same external interface.
Polymorphism:Dynamic Binding: Binding refers to the linking of a procedural call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated polymorphism and inheritance.
Message Passing: Objects communicate with one another by sending and receiving information much the same way as people pass message to one another.
A message for an object is a request for execution of a procedure, and therefore will invoke a function in the receiving object that generates the desired result. Message passing involves specifying the name of the object, the name of the function and the information to be sent.

Benefits of OOP:
Through inheritance, we can eliminate redundant code and extend the use of existing classes.
The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program.
It is possible to map objects in the problem domain to those in the programs.
It is easy to partition the work in a project based on objects.
Object-oriented systems can be easily upgraded from small to large systems.
Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler.
Software complexity can be easily managed.
Object-Oriented Languages: Languages that support programming with objects are said to be object-based programming languages. They do not support inheritance and dynamic binding. Ada is a typical object-based programming language.
Object-oriented programming incorporates all of object-based programming features along with two additional features, namely, inheritance and dynamic binding. The following statement can therefore characterize object-oriented programming:
Object-based features + inheritance + dynamic binding
Languages that support these features include C++, Smalltalk, Object Pascal and Java. There are a large number of object-based and object-oriented programming languages.

Class: A Class is a collection of objects (or)
A class is a collection of data members & member function (or)
A class is a description of an object.
Specifying a class: A class is a way to bind the data and its associated functions
together. It allows the data (and functions) to be hidden, if necessary, from external use. When defining a class, we are creating a new abstract data type
that can be treated like any other built-in data type. Generally, a class specification has two parts:
1. Class declaration
2. Class function definitions.
The class declartation describes the type and scope of its members. The class function definitions describe how the class functions are implemented.
Class class_name
{
private:
variable declaration;
variable declaration;
public:
variable declaration;
variable declaration;
};
The class body contains the declaration of variables and functions.
These functions and variables are collectively called class members. They are usually grouped uner two sections, namely, private and public to denote which of the members are private and which of them are public. The keywords private and public are known as visibility labels. Note that these keywords are followed by a colon.
The class members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed from outside of the class also.
The variables declared inside the class are known as data members and the functions are known as member functions. Only the member functions can have access to the private data members and private functions. However, the public members(both functions and data)can be accessed from outside of the class.
Accessing Class Members : Syntax:
object-name.function-name(actual-arguments);
Ex: x.getadata(100,76.56);
Defining Member Functions:
Member functions can be defined in two places:
1. Outside of the class definition
2. Inside the class definition
Outside of the class definition : An important difference between a member function and a normal function is that a member function incorporates a membership 'identity label' in the header. This 'label' tells the compiler which class the function belongs to. The general form of a member function definition is :
return-type class-name :: function-name(argument declaration)
{
Function body
}
The membership label class-name :: tells the compiler that the function function-name belongs to the class-name. That is, the scope of the function is restricted to the class-name specified in the header line. The symbol :: is called the scope resolution operator.
Inside of the Class : When a function is defined inside a class, it is treated as an inline function. Therefore, all the restrictions and limitations that apply to an inline function are also applicable here. Normally, only small functions are defined inside the class defintions.
Making an Outside function Inline : We can define a member function outside the class definition and still make it inline by just using the qualifier inline in the header line of function definintion.
Nesting of member functions : A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions.
Private Member Functions : A private member function can only be called by another function that is a member of its class. Even an object cannot invoke a private function using the dot (.) operator.
Memory Allocation for Objects : The member functions are created and placed in the memory space only once when they are defined as a part of a class specification. Since all the objects belonging to that class use the same member functions, no separate space is allocated for member functions when the objects are created. Only space for member variables is allocated separately for each object. Separate memory locations for the objects are essential, because the member variables will hold different data values for different objects.
Static Data Members : It is initialized to zero when the first object of its class is created. No other initialization is permitted.
Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
It is visible only within the class, but its lifetim is the entire program.
Static variables are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that records the occurences of all the objects.
Static Member functions : Like static member variable, we can also have static member functions. A member function that is declared static has the following properties : 1. A static function can have access to only other static members (functions or variables) declared in the same class.
2. A static member function can be called using the class name (instead of its objects) as follows :
class-name :: function-name;
Friend Functions: To make an outside function "friendly" to a class, we have to simply declare this function as a friend of the class as shown below:
class SITD
{
private:
...............
...............
public:
...............
...............
friend void cttc(void); //Declaration
};
The function declaration should be preceded by the keyword friend. The function is defined elsewhere in the program like a normal C++ function. The function definition does not use either the keyword friend or the scope operator (::). The functions that are declared with the keyword friend are known as friend functions. A function can be declared as friend in any number of classes. A friend function, although not a member function, has full access rights to the private members of the class.
A friend function possesses certain special characteristics:
1. It is not in the scope of the class to which it has been declared as friend.
2. Since it is not in the scope of the class, it cannot be called using the object of that class.
3. It can be invoked like a normal function without the help of any object.
4. Unlike member functions, it cannot access the member names directly and has to use an object name and dot member ship operator with each member name (E.g.: A.x)
5. It can be declared either in the public or the private part of a class without affecting its meaning.
6. Usually, it has the objects as arguments.
Const Member functions: If a member function does not alter any data in the class, then we may declare it as a const member functions as follows:
void mul (int, int)const;
double get_balance() const;
The qualifier const is appended to the function prototypes (in both declaration). The compiler will generate an error message if such functions try to alter the data values.
Local Classes : Classes can be defined and used inside a function or a block. Such classes are called local classes.

Friend Functions: To make an outside function "friendly" to a class, we have to simply declare this function as a friend of the class as shown below:
class SITD
{
private:
...............
...............
public:
...............
...............
friend void cttc (void); //Declaration
};
The function declaration should be preceded by the keyword friend. The function is defined elsewhere in the program like a normal C++ function. The function definition does not use either the keyword friend or the scope operator (::). The functions that are declared with the keyword friend are known as friend functions. A function can be declared as friend in any number of classes. A friend function, although not a member function, has full access rights to the private members of the class.
A friend function possesses certain special characteristics:
1. It is not in the scope of the class to which it has been declared as friend.
2. Since it is not in the scope of the class, it cannot be called using the object of that class.
3. It can be invoked like a normal function without the help of any object.
4. Unlike member functions, it cannot access the member names directly and has to use an object name and dot member ship operator with each member name (E.g.: A.x)
5. It can be declared either in the public or the private part of a class without affecting its meaning.
6. Usually, it has the objects as arguments.


Constructors: A Constructor is a special function which is invoked whenever an object is created.
The constructor name and the class name should be same. A Constructor cannot return any type of value even void. Constructors cannot be declared is static, const, and virtual.
The constructors can have arguments.
Types of Constructors:
1. Default Constructor: If a constructor with no arguments then the constructor is known as default constructor.
2. Parameter Constructor: If a constructor contains arguments then the constructor is known as parameter constructor.
3. Dynamic Constructor: If a constructor accepts argument values at run time then the constructor is known as dynamic constructor.
4. Default value Parameter Constructor: If a constructor contains default values as arguments then the constructor is known as default value parameter constructor.
5. Copy Constructor: When one object is copied into or assigned into another object then automatically a bit by bit copy is performed. This can be passed by using copy constructor. In the copy constructor a member by member copy is performed.

Destructors: A Destructor is a special function which is invoked whenever an object is destroyed.
Conditions of Destructors:
1. The destructor name and the class name should be same.
2. A class can have contains only one destructor.
3. Destructor can't have any arguments.
4. Destructors can't return any value even void.
5. Destructor must preceded by ~ (Tilde Symbol).
6. Destructors can't be declared is const, static, but destructors are can be virtual.

INHERITANCE: Deriving one class features into another class.
Introduction: Once a class has been written and tested, it can be adapted by other programers to suit their requirements. This is basically done by creating new classes, re-using
the properties of the existing ones. The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as the base class and the new one is called the derived class or subclass.
The derive class inherits some are all of the traits from the base class. A class can also inherit properties from more than one class or from more than one level. A derived class with only one base class, is called single inheritanceand one with several base classes is called multilevel inheritance. On the other hand, the traits of one class may be inherited by more than one class. This process is known as hierarchical inheritance. The mechanism of deriving a class from another 'derived class' is known as multilevel inheritance.
Defining Derived Classes: A derived class can be defined by specifying its relationship with the base class in addition to its own details. The general form of defining a derived class is:
Class derived_classname : visibility_mode base_classname
{
............. //
............. // members of derived class
............. //
}
The colon indicates that the derived-class name is derived from the base-class. The visibility-mode is optional and, if present, may be either private or public. The default visibility-mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
When a base class is privately inherited by derived class, 'public members' of the base class become 'private members' of the derived class. When the base class is publicly inherited, 'public members' of the base class become 'public members' of the derived class.
Inheritance: We can also create a new class from already existing class without disturbing the existing class features. This is known as inheritance.
The newly created class is known as subclass or derived class. The existing class is known as super class or base class.
Types of Inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance.
1. Single Inheritance : When a class is derived from only one base class then the inheritance is known as single inheritance. The base class in the single inheritance is known as direct base class to the derived class.
2. Multilevel Inheritance : When a class is derived from a subclass of another base class then the inheritance is known as multilevel inheritance. The base class is known as indirect base class to the derived class and direct base class to the subclass. The subclass is also known as direct base class to the derived class.
3. Hierarchical Inheritance: When a multiple classes are derived from only one base class then the inheritance is known as hierarchical inheritance.
4. Multiple Inheritance: When a class is derived from more than one base classes then the inheritance is called as multiple inheritance.
5. Hybrid Inheritance: It is a combination of more than one inheritance concepts.

Advantages:
1. Reusability of the code.
2. Here base class functions and members are inherited to derived class. Hence the base class functions and members can be used in the derived class, without rewriting the class.
3. Time is saved is the time involved in designing base class is eliminated since they inherited to derived class.
4. Memory is sav edc which incurred in developing base class members and functions eliminated.
5. Since base class is an existing class and we can retriving base class functions and members into derived class.


Polymrphism : Polymorphism is one of the crucial features of OOP. It simply means 'one name, multiple forms'. We have already seen how the concept of polymorphism is implemented using the overloaded functions and operators. The overloaded member functions are 'selected' for by matching arguments,both type and number. This information is known to the compiler at the compile time and, therefore, compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early binding or Static binding or static linking. Also known as compile time polymorphism, early binding simply means that an object is bound to its function call at compile time.
They are two types of polymorphism.
1. Compile time polymorphism (Early or Static Binding)
a. Function Overloading
b. Operator Overloading
2. Run time polymorphism (Late or Dynamic Binding)
a. Virtual Functiuons
b. Pure Virtual Functions

Virtual Functions: When a function is declared as virtual then the function as virtual function. A function cannot be static functions.
The base class pointer refers the current object address then the function is virtual in the base class.
Pure Virtual Functions: When a function is declared with virtual and initialized with 0 (zero). Then the function is known as pre virtual function.
Syntax of a pure virtual function :-
virtual return_type function_name(arguments) = 0;
Abstract Class : If a class contains one or more pure virtual functions then the class is known as abstract class. We cannot create any objects to the abstract class. Because it is an incomplete (base) class. We should implement the pure virtual function it is the sub class of the parent class.

Introduction : The mechanism of giving such special meanings to an operator is known as operator overloading.
Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators.
Def : To define an addtional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function, called operator function.
return type classname :: operator op(arglist)
{
Function Body //task defined
}
Operator fuctions must be either member functions(non-static) or friend functions. A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators,while a member function has no arguments for unary operators and only for binary operators. This is because the object used to invoke the member function is passed implicitly and therfore is available for the member function. This is not the case with friend functions. Arguments may be passed either by value or by reference.

RULES FOR OVERLOADING OPERATORS :
1. Only existing operators can be ooverloaded. New operators cannot be created.
2. The overloaded operator must have at least one operand that is of user-defined type.
3. We cannot change the basic meaning of an operator. That is to say, we cannot redefine the plus(+) operator to subtract one value from the other.
4. Overloaded operators follow the syntax rules of the original operators. They cannot be overridden.
5. There are some operators that cannot be overloaded.
Sizeof -> Size of Operator
. -> Membership Operator
.* -> Pointer-to-Member Operator
:: -> Scope resolution Operator
?: -> Conditional Operator
6. We cannot use friend functions to overload certain operators.
= -> Assignment Operator
() -> Function call Operator
[] -> Subscripting Operator
-> -> Class member access operator
However, member functions can be used to overload them.
7. Unary operators, overloaded by means of amember function, take no explicit arguments and return no explicit values, but, those oveloaded by means of a friend function, take one reference argument. (The object of the relevant Class).
8. Binary operators overloaded through a member function, take one explicit argument and those which are overloaded through a friend function take two explicit arguments.
9. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class.
10. Binary arithmetic operators such as +, -, *, and / must explicitly return a value. They must not attempt to change their own arguments.
Function Overloading : The same function name can be used file different
purposes. This is known as function overloading.
Rules of Function Overloading :
1. Function name should be same.
2. The function should contain different number of arguments.
3. A function should contain different types of arguments.
4. Function overloading can't depend on the return type of the function.
Function Overloading or Method Overloading and Function Declaration:
More than one function same name with different types of arguments is called
function overloading.
Here parameters can be differentiate either number of parameters or datatypes
of parameters.

Exception Handling:
Introduction :
The two most common types of bugs are logic errors and syntatic errors. The logic errors occurs due to poor syntatic errors arise due to poor understanding of the language itself. We can detect these errors by using exhaustive debugging and testing procedures.
We often come across some peculiar problems other than logic or syntax errors. They are known as exceptions.
Excption Handling Mechanism :
Basics of Exception Handling :
Excepitions are of two kinds, namely, synchronous exceptions and asynchronous exceptions. Errors such as "out-of-range index" and "over-flow" belong to the synchronous type exceptions. The errors that are caused by events beyond the control of the program (such as keyboard interrupts) ae called asynchronous exceptions. The proposed exception handling mechanism in C++ is designed to handle only synchronous exceptions.
The purpose of the exception handling mechanism is to provide means to detect and report an "exceptional circumstance" so that appropriate action can be taken. The mechanism suggests a separate error handling code that performs the following tasks:
1. Find the problem (Hit the exception).
2. Inform that an error has occured (Throw the exception).
3. Receive the error information (Catch the excepion).
4. Take corrective actions (Handle the exception).
The error hanling code basically consists of two segments, one to detect errors and to throw exceptions and to take appropriate actions.
Exception Handling Mechanism :
C++ exception handling mechanism is basically built upon three keywords, namely, try, throw, and catch. The keyword try is used to preface a block of statements (surrounded by braces) which may generate as try block. When an exception is detected, it is thrown using a throw statement in the keyword catch 'catches' the exception block, and handles it appropriately.
The catch block that catches an exception must immediately folow the try block that throws the exception. The general form of these two blocks are as folows.


Templates : It is a new concept which enable us to define generic classes and functions and thus provides support for generic programming. Generic programming is an approach where generic types are used as parameters in algorthims so that they work for a variety of suitable data types and data structures.
A template can be used to create a family of classes or functions. For example, a class template for an array class would enable us to create arrays of various data types such as int array and float array. Similarly, we can define a template for a function, say mul(), that would help us create various versions of mul() for multiplying int, float and double type values.
A template can be considered as a kind of macro. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type. Since a template is defined with a parameter that would be replaced by a specified data type at the time of the class or function, the templates are sometimes called parameterized classes or functions.
In C++ there are two types of templates.
1. Class template.
2. Function template.
Class Template :
The general format of a class template is:
template
class classname
{
//...............
//class member specification
//with anonymous type T
//wherever appropriate
//...............
};
A class created from a class template is called a template class. The syntax for defining an object of a template class is
classname objectname(arglist);
This process of creating a specific class from a class template is called instantiation.
Function Template : Like class templates, we can also define function templates that could be used to create a family of functions with different argument types.
template
returntype functionname (arguments of type T)
{
//..........
//Body of function
//with type T
//wherever appropriate
//..........
}
Function template with multiple parameters : Like template classes, we can use more than one generic data type in the template statement, using a comma-separated list as shown below:
template
returntype functionname(arguments of types T1,T2, .....)
{
..........
.......... (Body of function)
..........
}
Example for Templates :
void swap(int a,int b)
{
cout<<"Before swapping the values in a,b are :" <<<", "<<<"\nAfter swapping the values in a,b are :" <<<", "<<<"Before swapping the values in a,b are :" <<<", "<<<"\nAfter swapping the values in a,b are :" <<<", "<<<"Before swapping the values in a,b are :" <<<", "<<<"\nAfter swapping the values in a,b are :" <<<", "<
void swap(T x,T y)
{
T temp;
temp=x;
x=y;
y=temp;
}
The above template generic function swaps integers, floats, characters etc,.
Rules for generic function :-
1. The templates cannot be used inside the main function
2. The generic function template should be written before the main
3. The generic function cannot have prototype (function declaration).

Type Conversion : We know that when constants and variables of different types are mixed in an expression, C applies automatic type conversion to the operands as per certain rules. Similarly, an assignment operation also causes the automatic type conversion. The type of data to the right of an assignment operator is automaticaly converted to the type of the variable on the left. For example, the statements
int A;
float J = 3.4523;
A=J;
convert J to an integer before its value is assigned to A. Thus, the fractional part is truncated. The type conversions are automatic as long as the data types invloved are built-in types.

Files : The I/O system of C++ handles file operations which are very much similar to the console input and output opearations. It uses file streams as an interface between the programs and the files. The stream that supplies data to the program is known as input stream and the one that receives data from the program is known as output stream. In other words, the input stream extracts (or reads) data from the file and the output stream inserts (or writes) data to the file.

(read data) --> I/P stream --> (data I/P)--> program --> (data O/P)--> O/P stream --> (writedata) -->disk files

c - language

C – LANGUAGE

Introduction to programming languages:-
All the programming languages can be divided into two categories.
Machine oriented languages (or) Low level languages:-
These languages have been designed to give a better machine efficiency,
i.e., Faster program execution.
Eg. : Assembly language, Machine Language.
Problem Oriented Languages (or) High Level Languages :-
These languages have been designed to give a better programming efficiency,
i.e., faster program development.
Eg. : FORTRAN, BASIC, PASCAL etc.,
Steps to learning English language:-

Steps to learning computer (programming) language :-



Introduction to C Language :-
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named “Dennis Ritchie”.
C stands in between the above two [(a) & (b)] categories of languages. That is why it is often called a middle level language, since it was designed to have both: a relatively good programming efficiency (as compared to Machine Oriented Languages) and a relatively good machine efficiency (as a compared to problem oriented languages).
The C character set :
A character denotes any alphabet, digit, or a special symbol used to represent information. Following table shows the valid alphabets, numbers and special symbols allowed in C:

Alphabets A, B, C ... Z
and
a, b, c ... z
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols ~ à Tilde
! à Exclamation mark
# à Harsh
% à Percentage
^ à Cap or Crown or Carrot
& à Ampersand
* à Asterisk
( à Open parentheses
) à Closing parentheses
_ à Underscore
- à Minus
+ à Plus
= à Equal
| à Pipe
\ à Backward slash { à Open brace
} à Closing brace
[àOpen bracket
] àClosing bracket
: à Colon
; à Semi colon
“à Double quotation mark
’ à Single quotation mark
< à Less than
> à Greater than
, à Comma
. à Dot
? à Question mark
/ à Forward slash

Constants, Variables (Identifiers): -
The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Let us see what are constants and variables in C. A constant is a quantity that does not change. This quantity can be stored at a location in the memory of the computer. A variable can be considered as a name given to the location in memory where this constant is stored. Naturally that content of the variable can change.
Eg. : 3x2 + 4x + 5 = 0
Here, 3,4, 5 and 0 cannot change, they are called constants, whereas the quantities x can vary or change hence this is called a variable.
Types of C constants :-
C constants can be divided into two major categories:
Primary constants,
Secondary constants

Rules for constructing Integer Constants :
An integer constant must have at least one digit.
It must not have a decimal point
It could be either positive or negative
If no sign precedes an integer constant it is assumed to be positive
No commas and blanks are allowed within an integer constant.
The allowable range for integer constants is –32768 to +32767 (Because an integer constant occupies 2 bytes of memory in computer).
Integer constants must fall within this range because the IBM compatible microcomputers are usually 16 bit computers which cannot support a number falling out side the above mentioned range. For a 32 bit computer of course the range would be much larger.
E.g. 450, + 590, -267
Rules for constructing Real Constants:-
Real constants are often called floating point constants. The real constants could be written in two forms, Fractional form and Exponential form.


Rules for fractional form:-
A real constant must have at least one digit
It must have a decimal point
It could be either positive or negative. Default sign is positive
No commas & blanks are allowed within a real constant.
E.g. 296.0, +1.272, -48.01
Rules for Exponential form:-
The exponential form of representation of real constants is usually used if the value of the constant is either too small or too large. If however does not restrict us in any way from using exponential form of representation for other real constants.
In exponential form of representation, the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called exponent.
The mantissa part and the exponential part should be separated by a letter ‘e’
The mantissa part may have a positive or negative sign. Default sign is positive.
The exponent must have at least one digit which must be a positive or negative integer. Default sign is positive.
Range of real constants expressed in exponential form is –3.4e38 to 3.4e38. (Because a floating point constant occupies 4 bytes of memory in computers).
Eg. : +5.2e-7,
4.5e8,
-0.2e+3
Note: 5.2e-7 = 5.2 x 10-7
4.5e8 = 4.5 x 108
Rules for constructing character constants :-
A character constant is either a single alphabet, a single digit or a single special symbol enclosed within single inverted commas both the inverted commas should point to the left( ’ ).
Eg. : ‘A’ is not a valid character constant
’A’ is a valid character constant.
The maximum length of a character constant can be 1 Character.
Range of character constants expressed in signed integers is –128 to +127 (or) expressed in unsigned integers is 0 to 255. Because a character constant occupies 1 byte of memory in computer.
Rules for constructing variable names :-
A variable name is any combination of 1 to 8 alphabets, digits, or underscores ( _ ). Some compilers allow variable names whose length could be up to 40 characters. Still, it would be safer to stick to the rule of 8 characters.
The 1st character in the variable name must be an alphabet.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore ( _ ) can be used in a variable name.
Eg. : empname, bas_sal, varl …. etc.
Data Types of C Variables :-
In C, a quantity which may vary during program execution is called a variable. Variable names are names given to locations in the memory of computer where different data are stored. These locations can contain integer, real or character constants. In any language, the type of variables that it can support depends on the types of data that is can handle. This is because a data stores in a location with a particular type of variable name can hold only that type of data. For example, a data stored in a memory location with an integer variable name, must be an integer data, one stored in location with a real variable name must be a real data and the one stored in location with a character variable name must be a character data. The following are the keywords for these data types.
Integer data type à int
Real data type à float
Character data type à char


C Keywords:
Keywords are the words whose meaning has already been explained to the C compiler (or in abroad sense to the computer). The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. The keywords are also called “Reserved Words”. There are only 32 keywords available in C, following is the list of keywords in C.
auto double if static
break else int struct
case enum long switch
char extern near typedef
const float register union
continue far return unsigned
default for short void
do goto signed while
C Instructions ( Expressions (or) Statements):
There are basically four types of instructions in C.
(a) Type Declaration Instruction
(b) Input / Output Instruction
(c) Arithmetic Instruction
(d) Control Instruction
Type Declaration Instruction :
This instruction is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is usually written at the beginning of the C program.
Eg. : int basic;
float hra, ta, da;
char gender;


Input / Output Instruction :
These are used to perform the function of supplying input data to a program and obtaining the output results from it.
Eg. : scanf (“%d”,&basic); /* input instruction .*/
printf (“%d”, basic); /* output instruction.*/
Arithmetic Instructions :
A C arithmetic instruction consists of a variable name on the left hand side of = and variable names and constants on the right hand side of = the variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /.
Eg. : perimet = 2 * (b+l);
Here, *, + are arithmetic operators,
= is the assignment operator,
2 is an integer constant,
perimet, b, l are the variables, which are declared in declaration section.
The variables and constants together are called ‘operands’ which are operated upon by the “arithmetic operators” and the result is assigned, using the assignment operator (=), to the variable on left hand side.
Note :
* C allows only one variable on left hand side of =
i.e. z = k*l is legal,
Whereas k*l = z is illegal
* A statement similar to arithmetic instruction is many times used for storing character constants in character variables.
i.e. a = ’F’ is also an arithmetic instruction.
* Arithmetic operations can be performed on ints, floats, and chars.
i.e. z = ’x’ + ’y’ is perfectly valid.
Since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII (American Secret (Standard) Code for Information Interchange) of ‘x’ and ‘y’ are 120 and 121, and hence can definitely be added.
Operators:-
1) Unary Operators :-
Any operator which operates on only one operand is called Unary operator. These are :
- à Unary minus, used to negate any number
++ à Increment by 1
-- à Decrement by 1
sizeof (variable) à This is used to return the memory size of variable in no. of bytes.
Eg. : (i) a = -a;
a++; This is called post increment
++a; This is called pre increment
a--; This is called post decrement
--a; This is called pre decrement
a = sizeof (int); which returns 2
i.e., The memory size of integer.
(2) Binary (or) Arithmetic Operators :-
Any operator which operates on two operands is called binary operator. Arithmetic operators are used to calculate arithmetic expressions. The operators are :
* à Multiplication
/ à Division
% à Modulus (or) Remainder Operator
+ à Addition
- à Subtraction
. Eg. : a = 25%4; which gives the value 1 for a
(3) Relational Operators :-
These operators are used to compare two variables.
< à Less than
< = à Less than & equal to
> à Greater than
> = à Greater than or equal to
= = à Equal to
! = à Not equal to
(4) Logical Operators :
These operators are used to compare multiple conditions at a time.
&& à And
|| à Or
! à Not
Assignment Operators :
These operators are used to store values of some variables into another.
=, *=, /=, %=, +=, - =, &=, ^=, |=, <<=, >>=
Eg. : (i) a = 15%4;
(ii) a * = 4; which is also written as a=a*4; which means that a is multiplied by 4 and restores it.
Bitwise Operators :
These operators are used to operate with bits (i.e. 0’s and 1’s).
~ à One’s complement
<< à Left Shift
>> à Right Shift
& à Bitwise AND
^ à Bitwise XOR (Exclusive OR)
| à Bitwise OR
Structure of C :
C language has a structured way for constructing a C program, that is why C is called a structured programming language.



[ Documentation Section ]
[ Linkage Section ]
[Global declaration section ]
< main ( )
{
Local declaration section;
……………………..
……………………..
Body of main ( ) ;
……………………..
……………………..
} >

[ Function definition section ]
Documentation section :
This section is used to give the information about the program to other users.
This is also called comments section
Comment about programme should be enclosed within /*...*/.
Any no. of comments can be given at any place in the program.
Comments cannot be nested
i.e., /* calculation of /* simple x/ interest */ is invalid.
A comment can be split over more than one line.
Eg. : /* calculation of simple interest.
Author : Y. Kanitkar
Date : 25-07-2005 */
Linkage Section:
This linkage section is used to link one file to another. For this we use the preprocessor directive #include. This directive causes one file to be included in another. The preprocessor command for file inclusion books like this :
# include “filename” (or) # include <>
And it simply causes the entire contents of filename to be inserted into the source code at that point in the program.
The meanings of each of these forms :
# include “filename” : This command would look for the given file in the current directory as well as the specified list of directories as mentioned in the include search path that might have been set up.
E.g:- # include “C:\TC\decimal.C”
# include : This command would look for the given file in the specified list of directories only.
Eg. : # include
Global declaration section :
This section is used to declare the global variables i.e., This global variables are used in entire program.
Eg. : int sal;
float hra;
In this section we have another feature, that is macro definition #define directive or more commonly just a “macro”.
Syntax for macro definition.
# define
Eg. : #define PI 3.1415
Here, PI is called macro template,
3.1415 is called macro expansion
In C programming it is customary to use capital letters for macro templates.
Macro templates and macro expansions are separated by blanks or tabs.
Space between # and define is optional
Macro definition is never to be terminated by a semi colon (;).
main ( ) :
Any C program is nothing but a combination of functions. main ( ) is one such functions. Empty parentheses {( )} after main are necessary.
The set of statements belonging to a function are enclosed within a pair of braces ({}).
Any variable used in the program must be declared before using it.
Eg. : int p, n;
float r, si;
Function definition section :
This section is used to define user defined functions, if necessary.
In the above structure of C, the documentation section, linkage section, global declaration section, and function definition section are optional, and the main ( ) block is essential for every C program.
Note :
Statement in between [ ] is optional
Statement in between < > is essential
Rules for constructing C program :
Blank spaces may be inserted between two words to improve the readability of the statement. However, the blank spaces are not allowed within a variable, constant or keyword.
Usually all statements are entered in small case letters.
C has no specific rules for the position at which a statement is to be written. That is why it is often called a free-form language.
Any C statement always ends with a semi colon (;)
First C Program :
/* Purpose : Calculation of simple interest.
Author : Y. Kanitkar
Date : 25-7-2005
*/
# include <>
main ( )
{
int p, n;
float r, si;
p = 5000; /* Principle value */
n = 5; /* No. of years */
r = 8.5; /* Rate of interest */
si = p*n*r/100; /* Formula for simple interest */
printf (“Simple Interest is :%f”, si);
}
Note :
printf ( ) : it is used to print values of the variables on the screen.
The general form,
printf (“”, );
Format string could be
%f for real values (float)
%d for integer values
%c for character values.
Calculation of simple interest, using scanf() function.
main ( )
{
int p, n;
float r, si;
printf (“\n Enter principle, no.of years, rate:”) ;
scanf (“%d %d %f”, &p, &n, &r);
si = p*n*r/100;
printf (“\n simple interest = Rs. %f /-“, si);
}
Note :
scanf ( ) : This function is used to take values from keyboard. The ampersand (&) before the variable in the scanf statement is a must. & is a pointer operator, it is called address of value operator.
CONTROL INSTRUCTIONS IN C
As the name suggests the “Control Instructions” enables us to specify the order in which the various instructions in a program are to be executed by the computer. In other words the control instructions determine the “flow of control” in a program. There are 4 types of control instructions in C. They are :
Sequence control instruction,
Selection or Decision control instruction,
Repetition or loop control instruction,
Case control instruction.
(a) Sequence Control Instruction :
The Sequence Control Instruction ensures that the instructions are executed in the same order in which they appear in the program.
The flow of control looks like this :









(b) Selection or Decision Control Instruction :
Decision control instruction allow the computer to take a decision as to which instruction is to be executed next depending on condition, hence is called conditional statement.
if :-
Syntax :- if ()
Statement(s);
Here, if the given condition is true then the statements in if block will execute, otherwise that statements will ignore by the compiler.
Eg. : if (a = = o)
printf (“ a is vanish”);

(2) if .. else :-
Syntax : if ()
statement(s);
else
statement(s);


Here, if the given condition is true then the if block will execute, or if the condition is false then the else block will executed.
Eg : if (a < 0)
printf (“a is a –ve value”);
else
printf (“a is a +ve value”);
(3) if.. else if :-
syntax : if ()
statements1;
else if ()
statements2;
else
statements3;
Eg. : if (a < 0)
printf (“a is –ve”);
else if (a>0)
printf (“a is +ve”);
else
printf (“a is zero”);
(4) Nested if … else :-
Syntax :- if ()
{
statements ;
if ()
statements;
……
……
else
statements;
}
else
statements;
else if ladder :-
Syntax :-
if ()
Statements;
else if ()
Statements;
else if ()
Statements;
……….
……….
else
Statements;
Conditional operator (or) Ternary Operator :-
The conditional operators ? and : are some times called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if .. then .. else.
Syntax :- Expression 1? Expression 2 : expression 3;
Here, if expression 1 is true (that is if the value is non-zero), then the value returned will be expression 2 otherwise the value returned will be expression 3.
Eg. : (i) (a<0) ? printf (“a is –ve”) : printf (“a is +ve”);
(ii) y = ((a>=65 && a<=90) ? 1:0);
Repetition & Loop Control Instructions :-
The Loop Control Instructions helps computer to execute a group of statements repeatedly. The versatility of the computer lies in its ability to perform a set of instruction repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control structure.
C allows us three types of loop contract structures viz.,
while
do…while
for
while :-
Syntax : -
[initialize counter;]
while (condition)
{
Body of loop;
[Increment/decrement counter;]
}
Here, the statements within the pair of braces called body of the loop. The parentheses after the while contains a condition. So long as this condition remains true all statements within the body of the loop keep getting executed repeatedly. The variable counter is many a times called either a ‘Loop Counter’ or an ‘index variable’.
Eg. : This loop is used to print 1 to 1000 numbers on the screen.
i = 1;
while (i< = 1000)
{
printf (“\n %d”, i);
i++;
}
do … while :-
Syntax :- do
{
body of do…while;
}while (condition);
The do..while loop works like a while loop. The do…while tests the condition after having executed the statements within the loop. This means that do…while would execute its statements at least once, even if the condition fails for the 1st time itself.
Eg. :- i = 1
do
{
printf (“\n %d”, i);
i ++;
}while (i<=1000);


for :-
This is probably the most popular looping control. The for allows us to specify three things about in a single line :
setting a loop counter to an initial value.
Testing the loop counter to determine whether its value has reached the number of repetitions desired.
Increasing the value of loop counter each time the program segment within the loop has been executed.
Syntax :-
for ([initialize counter]; ; [increment/decrement counter])
{
body of loop;
}







Eg. :
for (i = 1; i< = 1000; i++)
printf (“\n %d”, i);
Multiple initializations in the for loop :
* The initialize counter expression can contain more than one statement separated by a comma,
Eg. : for (i = 2, j = 1; j<=10; j++)
* Multiple statements can also be used in the increment /decrement counter expression of loop.
Eg. : for (i = 2; j = 1; j< = 10; i++; j++)
* Only one expression is allowed in condition expression. This expression may contain several conditions linked together using logical operators.
Eg. : for (i = 2, j = 1; (j<=10 && i<=20); i++, j++)
Nesting of Loops :-
Any loops within another loops are called nested loops.
Eg. :
for (i = 1; i < = 3; i++)
{
for (j = 1; j<=3; j++)
printf (“ %d”, i);
printf (“\n”);
}
The Odd Loop :
The loops that we have used so far executed the statements within them a finite number of times, i.e, decided by user at runtime are called an odd loop.
Eg. :
while (another = = ’y’)
{
printf (“Hello!”);
printf (“Do you want to another display(y/n):”);
scanf (“%c”, &another);
}
break Statement :-
The keyword break allows us to jump out of a loop instantly, without waiting to get back to the conditional test.
Eg. :
i = 2;
while (i<=num-1)
{
if (num %i = = 0)
{
printf (“Given number is not a prime number”);
break;
}
i++;
}
if (i = = num)
printf (“\nGiven number is a prime number”);
continue Statement :
The keyword continue allows us to take the control to the beginning of the loop, bypassing the statements inside the loop which have not yet been executed.
Eg. : /* To print all combinations of 1,2,3 */
for (i = 1; i<=3; i++)
for (j = 1; j<=3; j++)
for (k=1; k<=3; k++)
{
if(i = = j || j = = k || i = = k)
continue;
printf (“%d %d %d”, i, j, k);
}
d) Case control instruction :-
The control statement which allows us to make a decision from the number of choices is called a switch, or more correctly a switch-case-default, since these three keywords go together to make up the control statement.




Syntax :
switch (choice variable)
{
case constant 1 : statements; break;
case constant 2 : statements; break;
……….………………………………
………….……………………………
case constant n : statements; break;
default : statements;
}
Here, Choice variable à integer/character variable
Constants à integer/character constant
Eg. :
switch (ch)
{
case 1 : a = b + c; break;
case 2 : a = b – c; break;
case 3 : a = b * c; break;
case 4 : a = b/c; break;
case 5 : a = b%c; break;
default : printf (“\n invalid choice …!”);
}

Note:-
We can use cases in any order.
We can mix integer and character constants in different cases of a switch.
Some times there may not be any statement in some of the cases in switch, but still they might turn out to be useful.
Even if there are multiple statements to be executed in each case there is no need to enclose these within a pair of braces.
If we have no default case, then the program simply falls through the entire switch and continues with the next instruction that follows the control structure.
The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch as one is likely to believe.
A switch may occur within another switch, which is called nested switch statements.
The switch statement is very useful while writing menu driven programs.
We can have after the case is an int constant or a char constant. Even a float is not allowed.
goto statement :
This is used to jump from any statement to another statement in a C program.
Syntax : goto