BIIT

C Programing

Introduction:
C is a Procedural Language. A Procedural Language is a computer programming language that follows, an order, a set of commands. Examples of computer procedural languages are BASIC, C, FORTRAN and Pascal. Procedural languages are some of the common types of programming languages used by script and software programmers. They make use of functions, conditional statements, and variables to create programs that allow a computer to calculate and display a desired output.
A Procedural Language gives emphasis on the functions rather than data values.
History of C language
Here we are going to discuss a brief history of the C language.
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the father of the C Language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
Initially, C language was developed to be used in UNIX Operating system. It inherits many features of previous languages such as B and BCPL.
Structure of C Language
1. Documentation section: The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later.
2. Link section : The link section consists of the header files of the functions that are used in the program. The link section provides instructions to the compiler to link functions from the system library.
3. Definition section : The definition section defines all symbolic constants. Macros are known as symbolic constants.
4. Global declaration section : There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions.
5. main () function section : Every C program must have one main function section. This section contains two parts; declaration part and executable part
  a. Declaration part : The declaration part declares all the variables used in the executable part. These variables are local to main function.
  b. Executable part : There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program. All statements in the declaration and executable part end with a semicolon.
6. Subprogram section : The subprogram section contains all the user-defined functions that are called in the main() function. User-defined functions are generally placed immediately after the main() function, although they may appear in any order.
Demonstration
                
                //Documentation Section
                /*This program will accept the radius of circle
                and calculates the area of circle.*/

                //Linking Section
                #include<conio.h>
                #include<stdio.h>

                //Definition Section
                #define pi 3.14

                //Global Declaration Section
                float r;

                //main() function Section
                void main()
                {
                    //Local Declaration Part
                    float a;

                    //Executable Part
                    clrscr();
                    printf("\nEnter the radius of circle ");
                    scanf("%f",&r);
                    a=pi*r*r;
                    printf("\nArea of circle = %f",a);
                    getch();
                }
                
                
Language Fundamentals
To learn C Language we must learn about following:
C Character Set
C Token
C Instruction/Statement
C Program
C Character Set: Like every other language, 'C' also has its own character set. The C character set consists of upper and lowercase alphabets, digits, special characters and white spaces. The alphabets and digits are altogether called as the alphanumeric character.
English Alphabet: a to z, A to Z.
Digits: 0 to 9.
Special Symbols: # + _ - * ! ~ & %........etc.
White Spaces: tab, enter, spacebar.
C Tokens: Token is the smallest unit in a 'C' program. It is each and every word and punctuation that you come across in your C program. The compiler breaks a program into the smallest possible units (tokens) and proceeds to the various stages of the compilation. Every token must follow the rules defined below:
1. It must be a meaningful word.
2. Only 8 characters are allowed to form a C Token but now a days C compilers can support upto 44 characters.
3. Only english alphabets allowed to form a C Token.
4. No special symbol except underscore(_) is allowed.
5. No White spaces are allowed.
6. Digits can be inserted but no token will be started with digit.
Types of Tokens A token is divided into fours different types:
1. Identifiers
2. Variables
3. Constants
4. Keywords
1. Identifier: A token, which is used to identify something, is known as identifier. An identifier can be used for the name of Variable/ Constant/ Keyword/ Function/ Structure/ Union etc.
2. Variable: An identifier, for that values can be changed any time during the execution of program, is known as Variable.
3. Constant: An identifier, for that values can’t be changed throughout whole program, is known as Constant.
4. Keywords: An identifier, that has its specific meaning and can be used to perform some specific task, is known as Keyword. A keyword is a reserved word.
There are 32 keywords used in C Language:
Note: an identifier can be used as a Keyword but not Vice-Versa.
C Instructions/Statements: A meaningful collection of C Tokens is called as C Statements. Every instruction is used to instruct the Computer. Every instruction is ended with semicolon (;).
There are 3 types of instructions used in C Language:
1. Type Declaration Instruction: To declare the type of variables used in a C Program.
2. Arithmetic Instruction: To perform arithmetic operations between constants and variables.
3. Control Instruction: To control the sequence of execution of various statements in a C Program.
C Program: A meaningful set of C Instructions is known as C Program. Every program is developed for performing a specific task. A set of programs is known as software.
Comments:A comment is an explanation or description of the source code of the program. It helps a developer explain logic of the code and improves program readability. At run-time, a comment is ignored by the compiler.
There are two types of comments in C:
1) Multi-line Comment: A comment that starts with a /* and finishes with */ is known as Multi-line Comment. You can place it anywhere in your code, on the same line or several lines.
2) Single-line Comments: A comment that is started with // is known as Single-line comment.
Data Types
A keyword, which is used to define the type of data, is termed as datatype. Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities.
We use 2 different types of data types in C Language:
1. Primary DataType (Primitive/Basic/Fundamental):
 a. Integer
 b. Real
 c. Character
 d. Empty
2. Secondary DataTypes (Non-Primitive/Derived/User Defined): Secondary Datatype is derived from primary datatype. These are also known as User defined datatypes. Actually Derived data types are nothing but primary datatypes but a little twisted or grouped together like :
a. Array
b. Pointer
c. Structure
d. Union
e. Enumeration
Data type determines the type of data a variable will hold. Every variable which is used in the program must be declared as what data-type it is.
short: It is a primary datatype. If a variable x is declared as short. it means x can hold only integer values. Integer values means a collection of digits only according to its range.
int: It is a primary datatype. If a variable x is declared as int. it means x can hold only integer values. Integer values means a collection of digits only according to its range.
long: It is a primary datatype. If a variable x is declared as long. it means x can hold only integer values. Integer values means a collection of digits only according to its range.
float: It is a primary datatype. If a variable x is declared as float. it means x can hold real values. real values means a collection of digits and a special symbol (.) according to its range.
double: It is a primary datatype. If a variable x is declared as double. it means x can hold real values. real values means a collection of digits and a special symbol (.) according to its range.
long double: It is a primary datatype. If a variable x is declared as long double. it means x can hold real values. real values means a collection of digits and a special symbol (.) according to its range.
char: It is a primary datatype. If a variable x is declared as char. it means x can hold character values. character values means any character from C character set (it may be an English alphabet, digit, special symbol).
void: It is a primary datatype. This type means empty data type (ie. no value). This is usually used to specify the type of functions which returns nothing. We will get acquainted to this datatype as we start learning more advanced topics in C language, like functions, pointers etc.
Array: It is a secondary datatype. It can be formed with the help of primary datatype. A collection of homogenous data items is known as array. All the elements in the collection share common name. all the elements stored in the array are stored in contiguous memory locations.
Pointer: It is a secondary datatype. It is a special type of variable which can store the address of another variable.
Structure: It is a secondary datatype. It can be formed with the help of primary datatype. A collection of heterogeneous data items is known as structure. All the elements in the collection shows a structure of an object. Memory of all elements of any structure is stored separately.
Union: It is a secondary datatype. It can be formed with the help of primary datatype. A collection of heterogeneous data items is known as union. All the elements in the collection shows a structure of an object. Unlike structure, memory for all elements of any union is not stored separately. Memory for biggest element is created and all other elements share that space.
Enumeration: It is a secondary datatype. It can be formed with the help of primary datatype. A collection of named integer constants is known as Enumeration. All the elements in the collection have an index to be distinguished each other.
Operators:
C language supports a vast range of operators. An operator is a special symbol that has some specific meaning and can be used to perform some specific operation. Operators are used in programs to manipulate data and variables.
Operands: A variable or constant, that is used with some operator to perform some specific operation, is known as Operand.
Expression: A meaningful set of Operators and Operands is known as Expression. Types of expression will be depending upon types of Operators.
Categories of Operators: There are three categories of operators:
1. Unary Operator: An operator which is used on single operand, is known as Unary operator.
2. Binary Operator: An operator which is used on two operands, is known as Binary operator.
3. Ternary Operator: An operator which is used on three operands, is known as Ternary operator.
C operators can be classified into following types:
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Conditional operators
• Increment/Decrement operators
• Bitwise operators
• Shorthand operators
• Special operators
Arithmetic operators: Operators used for the operations of Mathematics fall in this category.
Relational operators: Operators used for the checking the relation between two operands fall in this category.
Logical operators: Operators used for logics in C Language fall in this category.
Truth Table for Logical Operators:
Note: 0 represents False and 1 represents True.
Assignment operator: This operator is used to assign some value to some operand.
Conditional operator: This is a ternary operator. It is used for decision making.
Syntax:
(Condition) ? Expression 1 : Expression 2;
Condition always returns either true or false value. If at the time of execution condition returns true value, Expression 1 will be executed and Expression 2 will be exempted otherwise Expression 2 will be executed and Expression 1 will be exempted.
Increment/Decrement operators: These two operators are used to increase and decrease the given value of operand by one.
There are two versions of these operators:

Bitwise operators: These operators are used to perform manipulations on data at bit level. These operators also perform shifting of bits from left to right or right to left.
Truth Table for Bitwise Operators:
The bitwise shift operator shifts the bit value. The left operand specifies the value to be shifted and right operand specifies the no of positions that the bits in the value have to be shifted.
Short hand/cut operators: These operators are the combination of two symbols and shorts one operand in expression.
Like : a = a + b; can be written as a += b;
Here += is a short hand operator.
Special operators: Some special operators are also used in C Language.

Precedence & Associativity of Operators in C Language:

Rules determining how to evaluate expressions containing more than one operator with the same precedence example: 15 - 10 – 12 can be expressed as ((15 - 10) - 12) or (15 - (10 - 12)) its important because the values may differ (e.g., -7 or 17 for the above possibilities)
Left associativity: operators are evaluated left to right.
Right associativity: evaluated right to left.
Input & Output Built-in Functions:
Computer system uses two important functions: Input & Output. For same purpose We us separate input and output functions in C language. There are two categories of input/output functions used in C language (Formatted and Unformatted Input Output Functions discussed as below):
1. Formatted functions:
printf(): The printf() function is a formatted output function, which displays all type of data on the screen. This is a Library function, which is defined in stdio.h header file.
The general format of printf() function is:
printf(“control_strings”,variables);
scanf(): The scanf() function for input operation in C language is used for formatted input from standard input device like keyboard. This is a Library function, which is defined in stdio.h header file.
The general format of scanf() function is:
scanf(“controlstrings”,addresslst);
Demonstration
                
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    printf("Welcome to BIIT");
                    getch();
                }
                
                
                 
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    int x;
                    float y;
                    char z;
                    x=5;
                    y=55.4;
                    z='a';
                    printf("\nx = %d",x);
                    printf("\ny = %f",y);
                    printf("\nz = %c",z);
                    getch();
                }
                
                
                
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    int x;
                    float y;
                    char z;
                    printf("Enter the value of x ");
                    scanf("%d",&x);
                    printf("Enter the value of y ");
                    scanf("%f",&y);
                    printf("Enter the value of z ");
                    scanf(" %c",&z);

                    printf("\nx = %d",x);
                    printf("\ny = %0.2f",y);
                    printf("\nz = %c",z);
                    getch();
                }
                
                
                
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    int x;
                    float y;
                    char z;
                    printf("Enter the values of x, y & z ");
                    scanf("%d%f %c",&x,&y,&z);
                    printf("\nx = %d\ny = %f\nz = %c",x,y,z);
                    getch();
                }
                
                
2. Unformatted functions:
Input functions:
getch(): It is an unformatted input function. This function is used to ask a character from the keyboard. It does not send echo on the screen and also does not wait for pressing enter key on the keyboard. It is a library function and defined in the conio.h header file.
The general format of getch() is:
char ch=getch();
getche(): It is an unformatted input function. This function is used to ask a character from the keyboard. It sends echo on the screen and also does not wait for pressing enter key on the keyboard. It is a library function and defined in the conio.h header file.
The general format of getche() is:
char ch=getche();
getchar(): It is an unformatted input function. This function is used to ask a character from the keyboard. It sends echo on the screen and also waits for pressing enter key on the keyboard. It is a library function and defined in the conio.h header file.
The general format of getchar() is:
char ch=getchar();
gets(): It is an unformatted input function. This function is used to input a string from the keyboard. It also accepts whitespaces. It is a library function and defined in the stdio.h header file.
The general format of gets() is:
gets(ch[]);
Output functions:
putch(): It is an unformatted output function. This function is used to print a character on the screen. It is a library function and defined in the conio.h header file.
The general format of putch() is:
putch(ch);
putchar():It is an unformatted output function. This function is used to print a character on the screen like putch(). It is a library function and defined in the conio.h header file.
The general format of putchar() is:
putchar(ch);
puts():It is an unformatted output function. This function is used to print a string on the screen. It is a library function and defined in the conio.h header file.
The general format of puts() is:
puts(str);

Demonstration
                
                #include<stdio.h>
                #include<conio.h>
                //Usage of getchar() & putchar() functions 
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    char ch;
                    printf("Enter any character ");
                    ch=getchar();
                    printf("\nCharacter entered by you is ");
                    putchar(str);
                }
                
                
                
                //Usage of getche() & putchar() functions 
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    char ch;
                    printf("Enter any character ");
                    ch=getche();
                    printf("\nCharacter entered by you is ");
                    putchar(str);
                }
                
                
                
                //Usage of getch() & putch() functions 
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    char ch;
                    printf("Enter any character ");
                    ch=getch();
                    printf("\nCharacter entered by you is ");
                    putch(str);
                }
                
                
                
                //Usage of gets() & puts() functions 
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    char str[50];
                    printf("Enter Your name ");
                    gets(str);
                    printf("\nYour name is ");
                    puts(str);
                }
                
                
Concept of Header files
The functions that are widely used in our C programs, were developed by C developers at the time of development of C Language. These functions are termed as Library or Built-in/In-built functions. These library functions are categorized according to their property and are kept in some files. These files are called as Header files. When a library function, that is kept in header file, is used in your program, you must have to include that header file then only you can use that function.
Header files also contain predefined macros.
Header file can be included in your program using following syntax:
# include <filename.h>
When above line is written in our program, whole code written in the header file filename.h is added to your program before compilation.
Note: The header file, which are required, are only to be included, rest others are to be avoided, as including the same reduces the speed of program.
Decision Control Structures
In real life, we encounter verious situations where we make decisions. Programs are made for real life applications and we encounter various problems where decisions come. To handle these situations we have following 5 statements in C language:
1 if statement.
2 if else statement.
3 else if ladder.
4 nested if.
5 switch statement.

1. if statement

Syntax:
                
                if(Condition)
                {
                    Statement 1;
                    Statement 2;
                    Statement 3;
                    Statement 4;
                }
                
                
Examples:
Program to check whether an input number is even or odd using if statement.
                
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    int n;
                    printf("Enter the number ");
                    scanf("%d",&n);
                    if(n%2==0)
                    {
                        printf("\nEven");
                    }
                    if(n%2!=0)
                    {
                        printf("\nOdd");
                    }
                    getch();                   
                }
                
A Program (using only if statement) to input the percentage of student and show the result as follows:
percentage>=60 then First Division
percentage>=50 then Second Division
percentage>=40 then Third Division
percentage<40 then Fail

                
                #include<stdio.h>
                #include<conio.h>
                void main()
                {
                    int per;
                    printf("Enter your percentage ");
                    scanf("%d", per);
                    if(per>=60)
                    {
                        printf("\nFirst Division");
                    }
                    if(per>=50 && p<60)
                    {
                        printf("\nSecond Division");
	            }
                    if(per>=40 && p<50)
                    {
                        printf("\nThird Division");
                    }
                    if(per< 40) 
                    {
                        printf("\nFail");
                    }
                    getch();
                }
                

2. if else statement

Syntax:
if(Condition)
{
    Statement 1;
    Statement 2;
    Statement 3;
}
else
{
    Statement 1;
    Statement 2;
    Statement 3;
}
Examples:
Program to check whether an input number is even or odd using if else statement.

#include<stdio.h>
#include<conio.h>                
void main()
{
        int n;
	printf("Enter the number ");
	scanf("%d",&n);
	if(n%2==0)
	{
		printf("\nEven");
	}
	else
	{
            printf("\nOdd");
	}
	getch();
}

3. else if Ladder

Syntax:

if(Condition 1)
{
    Statement 1;
    Statement 2;
    Statement 3;
}
    else if(Condition 2)
    {
        Statement 1;
        Statement 2;
        Statement 3;
    }
        else if(Condition 3)
        {
            Statement 1;
            Statement 2;
            Statement 3;
        }
            else 
            {
                Statement 1;
                Statement 2;
                Statement 3;
            }
Examples:

#include<stdio.h>
#include<conio.h>
void main()
{
	int x,y;
	printf("Enter two numbers ");
	scanf("%d%d",&x,&y);
	if(x>y)
		{
			printf("\nx is greater");
		}
		else if(x<y)
			{
				printf("\ny is greater");
			}
			else
				{
					printf("\nBoth are equal");
				}
	getch();
}

A Program (using else if Ladder) to input the percentage of student and show the result as follows:
percentage>=60 then First Division
percentage>=50 then Second Division
percentage>=40 then Third Division
percentage<40 then Fail


#include<stdio.h>
#include<conio.h>
void main()
{
	int per;
	printf("Enter your percentage ");
	scanf("%d",&per);
	if(per>=60)
	{
		printf("\nFirst Division");
	}
        else if(per>=50)
        {
            printf("\nSecond Division");
        }
            else if(per>=40)
            {
                printf("\nThird Division");
            }
                else
                {
                    printf("\nFail");
                }
	getch();
}

4. Nested if else

Syntax:

if(Condition1)
{
    Statement;
    if(Condition2)
    {
        Statement;
    }
    else
    {
        Statement;
    }
}
else 
{
    if(Condition3)
    {
        Statement;
    }
}
Examples:

//Write A Program to input three unequal numbers and show which one is greatest
#include<stdio.h>
#include<conio.h>
void main()
{
	int x,y,z;
	printf("Enter three unequal numbers ");
	scanf("%d%d%d",&x,&y,&z);
	if(x>y)
	{
		if(x>z)
			printf("\nx is greatest");
		else
			printf("\nz is greatest");
	}
	else
	{
		if(y>z)
			printf("\ny is greatest");
		else
			printf("\nz is greatest");
	}
	getch();
}

5. switch Statement

Syntax:

switch(ch)
{
    case val1:
        Statement;
    case val2:
        Statement;
    case val3:
        Statement;
    case val4:
        Statement;
    default:
        Statement;
}
Examples:

//Write A Menu Driven Program for a Restaurant as fallows:
/*  Menu
    1. Dosa
    2. Paw Bhaji
    3. Burgur
    Enter your choice */
#include<stdio.h>
#include<conio.h>
void main()
{
	int ch;
	printf("\nMenu\n1.Dosa\n2.Paw Bhaji\n3.Burgur");
	printf("\nEnter your choice ");
	scanf("%c",&ch);
	switch(ch)
	{
		case 1:
			printf("\nDosa");
			break;
		case 2:
			printf("\nPav Bhaji");
			break;
		case 3:
			printf("\nBurgur");
			break;
		default:
			printf("\nWrong Choice");
	}
	getch();
}

//Write A Menu Driven Program for a Restaurant as fallows:
/*  Menu
    a. Dosa
    b. Paw Bhaji
    c. Burgur
    Enter your choice */
#include<stdio.h>
#include<conio.h>
void main()
{
	char ch;
	printf("\nMenu\na.Dosa\nb.Paw Bhaji\nc.Burgur");
	printf("\nEnter your choice ");
	scanf(" %c",&ch);
	switch(ch)
	{
		case 'a':
		case 'A':
			printf("\nDosa");
			break;
		case 'b':
		case 'B':
			printf("\nPav Bhaji");
			break;
		case 'c':
		case 'C':
			printf("\nBurgur");
			break;
		default:
			printf("\nWrong Choice");
	}
	getch();
}

Loop Control Statements (Iteration through loop)
In real life, we encounter verious situations where we make repetitions. Programs are made for real life applications and we encounter various problems where repetitions come. To handle these situations we have following 3 statements in C language:
1 for loop.
2 while loop.
3 do while loop.

1. for loop

Syntax:
                
                    for(initialization;condition;inc/dec)
                    {
                        statement 1;
                        statement 2;
                        statement 3;
                        statement 4;
                    }
                
Examples:
Program to print 1 to 10 on the screen.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i;
                    for(i=1;i<=10;i++)
                    {
                        printf("\n%d",i);
                    }
                    getch();
                }
                
Program to print table of n on the screen.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,n;
                    printf("\nEnter the number ");
                    scanf("%d",&n);
                    for(i=1;i<=10;i++)
                    {
                        printf("\n%d X %d = %d",n,i,i*n);
                    }
                    getch();
                }
                
Program to print sum of 1 to 10.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,sum;
                    for(i=1;i<=10;i++)
                    {
                        sum=sum+i;
                        printf("\nsum = %d",sum);
                    }
                    getch();
                }
                
Program to print factorial of n.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    long i,f=1,n;
                    printf("Enter the number ");
                    scanf("%ld",&n);
                    for(i=1;i<=n;i++)
                    {
                        f=f*i;
                    }
                    printf("\nfactorial = %ld",f);
                    getch();
                }
                

2. while loop

Syntax:
                
                    while(condition)
                    {
                        statement 1;
                        statement 2;
                        statement 3;
                        statement 4;
                    }
                
Examples:
Program to print sum of digits of number input by user.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,sum=0,r,n;
                    printf("\nEnter the number ");
                    scanf("%d",&n);
                    while(n>0)
                    {
                        r=n%10;
                        n=n/10;
                        sum=sum+r;
                    }
                    printf("\nSum of digits = %d",sum);
                    getch();
                }
                
                
Program to print reverse of number input by user.
                
                #include<stdio.h>
                #include<conio.h> 
                void main()
                {
                    int i,rev=0,r,n;
                    printf("\nEnter the number ");
                    scanf("%d",&n);
                    while(n>0)
                    {
                        r=n%10;
                        n=n/10;
                        rev=rev*10+r;
                    }
                    printf("\nReverse No = %d",rev);	
                    getch();
                }
                
                
Program to input a number and check whether it is palindrome or not.
                
                #include<stdio.h>
                #include<conio.h> 
                void main()
                {
                    int i,rev=0,r,n,org;
                    printf("\nEnter the number ");
                    scanf("%d",&n);
                    org=n;
                    while(n>0)
                    {
                        r=n%10;
                        n=n/10;
                        rev=rev*10+r;
                    }
                    if(org==rev)
                    {
                        printf("\nPalindrome No");
                    }
                    else
                    {
                        printf("\nNon Palindrome No");
                    }
                    getch();
                }
                
                
Program to input a three digit no. and check whether it is armstrong no or not.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,sum=0,r,n,org;
                    printf("\nEnter the number ");
                    scanf("%d",&n);
                    org=n;
                    while(n>0)
                    {
                        r=n%10;
                        n=n/10;
                        sum=sum+r*r*r;
                    }
                    if(org==sum)
                    {
                        printf("\nArmstrong No");
                    }
                    else
                    {
                        printf("\nNot an Armsrong No");
                    }
                    getch();
                }
                
                

3. do while loop(User Controlled Loop)

Syntax:
                
                    do
                    {
                        statement 1;
                        statement 2;
                        statement 3;
                        statement 4;
                    }while(condition);
                
                
Examples:
Program to input the number &check &display whether it is even or odd. Repeat the same process until user wants.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int n;	
                    char ch;	
                    do
                    {
                        printf("Enter the number ");
                        scanf("%d",&n);
                        if(n%2==0)
                        {
                            printf("\nEven");
                        }
                        else
                        {
                            printf("\nOdd");
                        }
                        printf("\nDo you want more ");
                        scanf(" %c",&ch);		
                    }while(ch=='y');
                    getch();
                }
                
                
Differences between while and do while Loop
1. while loop is an Entry Controlled loop whereas do while is an Exit controlled loop.
2. while loop checks the condition first then executes the statements whereas do while loop executes the statements first then checks the condition.
3. while loop does not execute the statements if condition returns false value whereas do while loop executes the statements atleast once.
Nested Loops
if Loops are executed inside anothel loop, this concept is known as Nesting of Loops. Syntax:
                
                    for(initialization;condition;inc/dec)
                    {
                        statement 1;
                        for(initialization;condition;inc/dec)
                        {
                            statement 2;
                            statement 3;
                            statement 4;
                        }
                    }
                
Examples:
Program to print all non-repeating combinations 1, 2 and 3.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j,k;
                    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)
                                printf("\n\t%d%d%d",i,j,k);
                            }
                        }
                    }
                    getch();
                }
                
                
Program to print the tables of 1 to 10.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=1;i<=10;i++)
                    {
                        for(j=1;j<=10;j++)
                        {
                            printf("%d\t",i*j);
                        }
                        printf("\n");
                    }
                    getch();
                }
                
                
Pattern Designing
Program to Design following pattern:
                
                *
                * *
                * * * 
                * * * * 
                * * * * *
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=1;i<=5;i++) //row
                    {
                        for(j=1;j<=i;j++) //col
                        {
                            printf(" *");
                        }
                        printf("\n");
                    }
                    getch();
                }                
                
                
Program to Design following pattern:
                
                * * * * *
                * * * *
                * * * 
                * * 
                *
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=5;i>=1;i--) //row
                    {
                        for(j=1;j<=i;j++) //col
                        {
                            printf(" *");
                        }
                        printf("\n");
                    }
                    getch();
                }
                
                
Program to Design following pattern:
                
                * * * * *
                  * * * *
                    * * * 
                      * *
                        *
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=1;i<=5;i++) //row
                    {
                        for(j=1;j<i;j++) //col
                        {
                            printf("  ");
                        }
                        for(j=1;j<=6-i;j++) //col
                        {
                            printf(" *");
                        }
                        printf("\n");
                    }
                    getch();
                }
                
                
Program to Design following pattern:
                
                        *
                      * *
                    * * *
                  * * * * 
                * * * * *
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=1;i<=5;i++) //row
                    {
                        for(j=1;j<6-i;j++) //col
                        {
                            printf("  ");
                        }
                        for(j=1;j<=i;j++) //col
                        {
                            printf(" *");
                        }
                        printf("\n");
                    }
                    getch();
                }
                
                
Program to print following pattern:
                
                            *
                          * *
                        * * *
                      * * * * 
                    * * * * *
                      * * * *
                        * * * 
                          * *
                            *
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=1;i<=5;i++) //row
                    {
                        for(j=1;j<6-i;j++) //col
                        {
                            printf("  ");
                        }
                        for(j=1;j<=i;j++) //col
                        {
                            printf(" *");
                        }
                        printf("\n");
                    }
                    for(i=1;i<=4;i++) //row
                    {
                        for(j=1;j<=i;j++) //col
                        {
                            printf("  ");
                        }
                        for(j=1;j<=5-i;j++) //col
                        {
                            printf(" *");
                        }
                        printf("\n");
                    }
                    getch();
                }
                
                
Program to print following pattern:
                
                A
                A B
                A B C
                A B C D
                A B C D E
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=1;i<=5;i++) //row
                    {
                        for(j=1;j<=i;j++) //col
                        {
                            printf(" %c",64+j);
                        }
                        printf("\n");
                    }
                    getch();
                }
                
                
Program to print following pattern:
                
                A
                B B
                C C C
                D D D D
                E E E E E
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int i,j;
                    for(i=1;i<=5;i++) //row
                    {
                        for(j=1;j<=i;j++) //col
                        {
                            printf(" %c",64+i);
                        }
                        printf("\n");
                    }
                    getch();
                }
                
                
Jump Statements
These statements are used to jump from one place to another places (either downwards or upwards) in the program execution. these are five statements:
1. break Statement.
2. continue Statement.
3. goto Statement.
4. exit() function.
5. return Statement.
1. The break Statement:
it is used to exit form a loop or switch control, by passing to the first statement after the loop or a switch. With loops, break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body. A break within a loop should always be protected within an if statement, which provides the test to control the exit condition.
The break statement abruptly terminates the loop. As soon as it gets executed, program control comes out of the loop and the first statement after the loop gets executed.
General from of break statement:
break;
Example:
Program to input 10 numbers and print the double of number and exit from loop if input number is divisible by 5.
                
                #include<stdio.h>
                #include<conio.h>                
                void main()
                {
                    int n,i;
                    printf("\nBefore Loop (B1)");
                    for(i=1;i<=10;i++)
                    {
                        printf("\nEnter any number(B2) ");
                        scanf("%d",&n);
                        if(n%5==0)
                        {
                            printf("\nOh! No. is divisible by 5(B3)");
                            break;
                        }
                        printf("\nDouble of your number is %d(B4)",n*2);
                    }
                    printf("\nAfter Loop(B5)");
                    getch();
                }
                
                
2. The continue Statement:
it is used to repeat the same operations once again even if it checks the error. This is similar to break statement, but is encountered less frequently. It only works within loops where its effect is to force an immediate jump to the loop control statement. Like a break statement continue statement should always be protected within an if statement.
General form of the continue statement:
continue;
Example:
Program to input 10 numbers and print the double of number except number is divisible by 5.
                    
                    #include<stdio.h>
                    #include<conio.h>                
                    void main()
                    {
                        int n,i;
                        printf("\nBefore Loop (B1)");
                        for(i=1;i<=10;i++)
                        {
                            printf("\nEnter any number(B2) ");
                            scanf("%d",&n);
                            if(n%5==0)
                            {
                                printf("\nOh! No. is divisible by 5(B3)");
                                continue;
                            }
                            printf("\nDouble of your number is %d(B4)",n*2);
                        }
                        printf("\nAfter Loop(B5)");
                        getch();
                    }
                    
                    
3. The goto Statement:
it is used to alter the program execution sequence by transferring the control to some part of the program.
The general form of goto statement:
goto label;
where label is a valid C identifier, which is used to label the destination such that, control could be transferred. There are two ways of using goto statement in the program, namely as a condition goto and unconditional goto.
Unconditional goto statement:
The unconditional goto statement is used just to transfer the control from one part of the program to the another part without checking any condition.
Conditional goto statement:
The conditional goto statement is used just to transfer the control from one part of the program to the another part in certain conditional cases.
Example:
Program to demonstrate goto statements(Conditional and Unconditional).
                    
                    #include<stdio.h>
                    #include<conio.h>                
                    void main()
                    {
                        int n;
                        clrscr();
                        printf("\nEnter any number ");
                        scanf("%d",&n);
                        if(n%2==0)
                        {
                            goto ev;    	//conditional goto
                        }
                        else
                        {
                            goto od;        //conditional goto
                        }
                        ev:
                        printf("\nEven");
                        goto ed;   		//unconditional goto
                        od:
                        printf("\nOdd");
                        ed:
                        printf("\nThank you");
                        getch();
                    }
                    
4. The exit() function: C provides a way to leave a program early with the exit() function.
The general form of exit() function:
exit(status);
where status is an int variable or constant. The status is sent to the operating system’s error level environment variable. Here status can be tested by the batch file.
We must include stdlib.h or process.h header file when using exit() function. These files define the operations of exit() function to our program.
Example:
Program to demonstrate exit() function.
                                        
                    #include<stdio.h>
                    #include<conio.h>                
                    void main()
                    {
                        int ch;
                        float a,b,c;
                        clrscr();
                        printf("\nMenu\n1.Add\n2.Sub\n3.Div\n4.Mul");
                        printf("\nEnter your choice ");
                        scanf("%d",&ch);
                        if(ch>=1&&ch<=4)
                        {
                            printf("\nEnter the two numbers ");
                            scanf("%f%f",&a,&b);
                        }
                        switch(ch)
                        {
                            case 1:
                                c=a+b;
                                break;
                            case 2:
                                c=a-b;
                                break;
                            case 3:
                                c=a/b;
                                break;
                            case 4:
                                c=a*b;
                                break;
                            default:
                                printf("Wrong choice");
                                getch();
                                exit(0);
                        }
                        printf("\nResult is %f",c);
                        getch();
                    }
                    
                    
Functions:
Functions are the subprograms (that means Small codes) that are made for:
1. Making the code Reusable
2. Debugging the Progrma Easily.
3. Making the Program Easier.
Threre are two Categories of Functions:
1. Library Functions.
2. User Defined Functions.
Library Functions:
These are the functions that were defined by the developpers of C Language. These functions are also known as These functions are kept in Header files.
Like:
printf(), scanf()....etc.
When you use these library functions, you have to include their header files.
like:
#include<stdio.h>
User Defined Functions:
These are the functions that are defined by the Users(i.e. Programmers). These functions are kept in your program files after the main() function.
Like:
show(), add()....etc.
There are three things required for any function:
1. Prototype
2. Calling
3. Body
Prototype:
This is the Declaration of function.
Syntax:
return_type function_name (arguments);
Example:
void show();
int add(int, int);
Calliing:
This is the Invoking of function.
Syntax:
function_name (arguments);
Example:
show();
add(a,b);
Body:
This is the Definition of function.
Syntax:
                            
                            return_type function_name (arguments)
                            {
                                Statement 1;
                                Statement 2;
                                Statement 3;                        
                            }
                            
                        
Example:
                    
                    void show()
                    {
                        printf("\nWelcome");
                    }
                    
                    
                    
                    int add(int a, int b)
                    {
                        int c;
                        c=a+b;
                        return c;                        
                    }
                    
                    

Examples:
                    
                    #include<stdio.h>
                    void main()
                    {
                        void show();  	//Prototype (Declaration)
                        show();          //calling(Invoking)	
                    }                        //Body(Definition)
                    void show()
                    {
                        printf("\nWelcome");	
                    }
                    
                    
Type of functions
There are four basic type of functions:
1. Without Return Without Argument
2. Without Return With Argument
3. With Return Without Argument
4. With Return With Argument
1. Without Return Without Argument
                    
                    #include<stdio.h>
                    void main()
                    {
                        void add();				//Prototype
                        add();           		//Calling
                    }
                    void add()    				//Body
                    {
                        int x,y,z;
                        x=55;
                        y=44;
                        z=x+y;
                        printf("\nSum is %d",z);
                    }
                    
                    
2. Without Return With Argument
                    
                    #include<stdio.h>
                    void main()
                    {
                        void add(int,int);		//Prototype
                        int a,b;
                        a=22;
                        b=33;
                        add(a,b);               //Calling
                    }
                    void add(int x,int y)       //Body
                    {
                        int z;
                        z=x+y;
                        printf("\nSum is %d",z);
                    }
                    
                    
3. With Return Without Argument
                    
                    #include<stdio.h>
                    void main()
                    {
                        int add();		//Prototype
                        int r;
                        r=add();                  //Calling
                        printf("Sum is %d",r);
                    }
                    int add()                    //Body
                    {
                        int x,y,z;
                        x=55;
                        y=33;
                        z=x+y;
                        return z;
                    }
                    
                    
4. With Return With Argument
                    
                    #include<stdio.h>
                    void main()
                    {
                        int add(int,int);		//Prototype
                        int p,q,r;
                        p=33;
                        q=11;
                        r=add(p,q);                  //Calling
                        printf("Sum is %d",r);
                    }
                    int add(int x,int y)                    //Body
                    {
                        int z;
                        z=x+y;
                        return z;
                    }
                    
                    
Recursion
A function, that calls itself is known as Recursive Function and this procedure is known as Recursion.
                    
                    #include<stdio.h>
                    void main()
                    {
                        void show();
                        show();
                    }
                    void show()     //Recursive Function
                    {
                        printf("Welcome to BIIT");
                        show();
                    }
                    
                    
Example:
Write A Recursive Function to Calculate Factorial of an input number and use this function in main() function.
                    
                    #include<stdio.h>
                    void main()
                    {
                        int fact(int);
                        int i,f,n;
                        printf("\nEnter the number ");
                        scanf("%d",&n);
                        f=fact(n);
                        printf("\nFactorial of %d = %d",n,f);
                    }
                    int fact(int num)
                    {
                        int fa;
                        if(num==1||num==0)
                        {
                            return 1;
                        }
                        else
                        {
                            fa=num*fact(num-1);
                            return fa;
                        }
                    }
                    
                    
Actual Parameter/Argument
A Parameter written in the Function call statement is known as Actual Parameter.
Formal Parameter/Argument
A Parameter written in the First Line(Signature of Function) of Function's Body is known as Formal Parameter.
                    
                    #include<stdio.h>
                    void main()
                    {
                        void show(int));
                        int x;
                        x=5;
                        show(x);                        
                    }
                    void show(int p)
                    {
                        printf("\nx = %d",p);
                    }
                    
                    
In the Above example x is an Actual Parameter and p is a Formal Parameter
Call By Value (i.e. Pass By Value)
it is a process of passing a copy of Actual Paramaeter to Formal Parameter in such a way that any change made in the Formal parameter does not reflect on Actual Parameter. For Example: Swap two variables using this concept
                    
                    #include<stdio.h>
                    void main()
                    {
                        void swap(int x,int y);
                        int x,y;
                        x=5;
                        y=4;
                        printf("\nBefore swap");
                        printf("\nx = %d",x);
                        printf("\ny = %d",y);
                        swap(x,y);
                        printf("\nAfter swap");
                        printf("\nx = %d",x);
                        printf("\ny = %d",y);
                    }
                    void swap(int x,int y)
                    {
                        int temp;
                        temp=x;
                        x=y;
                        y=temp;
                        printf("\nInside swap");
                        printf("\nx = %d",x);
                        printf("\ny = %d",y);
                    }
                    
                    
Call By Reference (i.e. Pass By Reference)
it is a process of passing a reference(i.e. Address) of Actual Paramaeter to Formal Parameter in such a way that any change made in the Formal parameter will be reflected on Actual Parameter. For Example: Swap two variables using this concept
                    
                    #include<stdio.h>
                    void main()
                    {
                        void swap(int *x,int *y);
                        int x,y;
                        x=5;
                        y=4;
                        printf("\nBefore swap");
                        printf("\nx = %d",x);
                        printf("\ny = %d",y);
                        swap(&x,&y);
                        printf("\nAfter swap");
                        printf("\nx = %d",x);
                        printf("\ny = %d",y);
                    }
                    void swap(int *x,int *y)
                    {
                        int temp;
                        temp=*x;
                        *x=*y;
                        *y=temp;
                        printf("\nInside swap");
                        printf("\nx = %d",*x);
                        printf("\ny = %d",*y);
                    }
                    
                    
Array:
Array is a Linear Data Structure. A collection of homogenous data elements is known as Array. All the elements of an array share common name and stored on contiguous locations.
Arrays are categorized into 3 categories:
1. Single Dimensional Array.
2. Two Dimensional Array.
3. Multi Dimensional Array.
Single Dimensional Array (SDA):
It is also known as One Dimensional Array (1D). All the elements of this type of Array are stored in one dimension only. For example: A collection of Roll Nos of 5 students(int), A collection of marks in 4 subjects(float) etc.
Declaration of SDA:
Syntax:
datatype arrayname[size];

For example:
To declare Roll Nos. of 5 Students(in integer form):
int rn[5];
To declare Marks of 4 Subjects(in float form):
float m[4];
To declare Name of 10 characters(in character form):
char n[10];
Memory Allocation:
Indexing or Subscripting:
Indexing always starts from 0 and goes up to n-1. Where n is the size of array.
Initialization:

Inputting:
            
            int rn[5];
            for(i=0;i<5;i++)
            {
                 printf(“Enter the rn ”);
                 scanf(“%d”,&rn[i]);
            }
            
            

Outputting:
            
            int rn[5];
            for(i=0;i<5;i++)
            {
                printf(“\nYour rno is %d”,rn[i]);
            }
            
            
Two Dimensional Array (2DA):
it is also known as Double Dimensional Array (DDA). All the elements of this type of Array are stored in two dimensions(i.e. rows & columns) only.
For example: A collection of marks of 5 students in 3-3 subjects (float) etc.
Declaration of DDA:
Syntax:
datatype arrayname[row][col];

For example:
To declare Marks of 5 Students in 3-3 subjects (in floating point form):
float m[5][3];
Memory Allocation:
float m[5][3];

Indexing or Subscripting:
Indexing always starts from [0][0] and goes up to [r-1][c-1]. Where r is the no. of rows and c is the no. of columns.

Initialization:

Inputting:
                        
            int m[5][3];
            for(i=0;i<5;i++)
            {
                for(j=0;j<3;j++)
                {
                    printf(“Enter the marks”);
                    scanf(“%f”,&m[i][j]);
                }
            }
            
            

Outputting:
            
            int m[5][3];
            for(i=0;i<5;i++)
            {
                for(j=0;j<3;j++)
                {
                    printf(“\nYour marks = %f”,m[i][j]);
                }                                            
            }
            
            

Multi Dimensional Array (MDA): it may be 3-D, 4-D … and n-D Array. All the elements of this type of Array are stored in 3-Dimensions, 4-Dimensions, ….n-Dimensions. Now we will consider 3-D Array for an example. Consider A collection of marks of 5 students in 3-3 subjects for 2 Classes (float) etc.
Declaration of 3D Array:
Syntax: datatype arrayname[room][row][col]; For example: To declare Marks of 5 Students in 3-3 subjects for 2 Classes (in floating point form):
float m[2][5][3];
Memory Allocation:

Indexing or Subscripting:
Indexing always starts from [0][0][0] and goes up to [x-1][y-1][z-1]. Where x is the no. of rooms, y is the no. of rows and z is the no. of columns.
Initialization:

Inputting:

Outputting:

Examples:
            
            #include<stdio.h>
            //Initializing the elements of SDA
            void main()
            {
                int rn[]={33,44,55,66,77};
                int i;
                clrscr();
                for(i=0;i<5;i++)
                {
                    printf("\nYour rn is %d",rn[i]);
                }                
            }
            
            
Initializing the elements of SDA
            
            #include<stdio.h>            
            void main()
            {
                int rn[]={33,44,55,66,77};
                int i,n;
                clrscr();
                printf("\nEnter the student number ");
                scanf("%d",&n);
                printf("\nYour rn is %d",rn[n-1]);
            }
            
            
Inputting & Outputting the elements of SDA
            
            #include<stdio.h>            
            void main()
            {
                int rn[5];
                int i;
                clrscr();
                for(i=0;i<5;i++)
                {
                    printf("\nEnter the rn ");
                    scanf("%d",&rn[i]);
                }
                for(i=0;i<5;i++)
                {
                    printf("\nYour rn is %d",rn[i]);
                }
            }
            
            
Inputting the marks of 5 subjects of one student & show the result of same
            
            #include<stdio.h>
            void main()
            {
                float m[5],tot,per,i;
                clrscr();
                for(i=0;i<5;i++)
                {
                    printf("\nEnter your marks ");
                    scanf("%f",&m[i]);
                }
                for(i=0;i<5;i++)
                {
                    printf("\nYour marks is %0.2f",m[i]);
                    tot+=m[i];
                }
                per=tot/5;
                printf("\nYour total of marks %f",tot);
                printf("\nYour percentage of marks %f",per);
                if(per>=60)
                    printf("\nFirst Division");
                else if(per>=50)
                    printf("\nSecond Division");
                else if(per>=40)
                    printf("\nThird Division");
                else
                    printf("\nFail");
            }
            
            
Inputting & Outputting the elements of DDA
            
            #include<stdio.h>
            void main()
            {
                float m[2][3],i,j;
                clrscr();
                for(i=0;i<2;i++)
                {
                    printf("\nStudent");
                    for(j=0;j<3;j++)
                    {
                        printf("\nEnter your marks ");
                        scanf("%f",&m[i][j]);
                    }
                }
                for(i=0;i<2;i++)
                {
                    printf("\nStudent");
                    for(j=0;j<3;j++)
                    {
                    printf("\nYour marks is %0.2f",m[i][j]);
                    }
                }
            }
            
            
Initializing the elements of DDA
            
            #include<stdio.h>
            void main()
            {
                float m[2][3]={{11,22,33},{55,66,77}};
                int i,j;
                clrscr();
                printf("\nEnter the student and subject no ");
                scanf("%d%d",&i,&j);
                printf("\nYour marks is %0.2f",m[i-1][j-1]);
            }
            
            
Inputting the marks of 2 Students in 3-3 Subjects & show the result of same
            
            #include<stdio.h>
            void main()
            {
                float m[2][3],tot,per;
                int i,j;
                clrscr();
                for(i=0;i<2;i++)
                {
                    printf("\nStudent");
                    for(j=0;j<3;j++)
                    {
                        printf("\nEnter your marks ");
                        scanf("%f",&m[i][j]);
                    }
                }
                for(i=0;i<2;i++)
                {
                    printf("\nStudent");
                    tot=0;
                    for(j=0;j<3;j++)
                    {
                    printf("\nYour marks is %0.2f",m[i][j]);
                    tot+=m[i][j];
                    }
                    printf("\nyour total of marks = %f",tot);
                    per=tot/3;
                    printf("\nYour percentage of marks %f",per);
                    if(per>=60)
                        printf("\nFirst Division");
                    else if(per>=50)
                        printf("\nSecond Division");
                    else if(per>=40)
                        printf("\nThird Division");
                    else
                        printf("\nFail");
                }
            }
            
            
Initializing 3-D Array:
Initialize the marks in 4-4 Subjects of 3-3 Students of 2 Classes
            
            #include<stdio.h>
            void main()
            {
                float m[2][3][4]={{{1,1,1,1},{2,2,2,2},{3,3,3,3}},{{4,4,4,4},{5,5,5,5},{6,6,6,6}}};
                int i,j,k;
                for(i=0;i<2;i++)//Z
                {
                    printf("\n\nClass %d",i+1);
                    printf("\n\t\tM1\tM2\tM3\tM4");			
                    for(j=0;j<3;j++)//Y
                    {
                        printf("\nStudent %d",j+1);
                        for(k=0;k<4;k++)//X
                        {
                            printf("\t%0.2f",m[i][j][k]);
                        }
                    }
                }
            }
            
            
Inputting/Outputting 3-D Array:
Input the marks in 4-4 Subjects of 3-3 Students of 2 Classes and show the same
            
            #include<stdio.h>
            void main()
            {
                float m[2][3][4];
                int i,j,k;
                for(i=0;i<2;i++)//Z
                {
                    printf("\nClass #%d#",i+1);
                    for(j=0;j<3;j++)//Y
                    {
                        printf("\nStudent #%d#",j+1);
                        for(k=0;k<4;k++)//X
                        {
                            printf("\nEnter your marks in subject #%d# = ",k+1);
                            scanf("%f",&m[i][j][k]);
                        }
                    }
                }
                for(i=0;i<2;i++)//Z
                {
                    printf("\n\nClass %d",i+1);
                    printf("\n\t\tM1\tM2\tM3\tM4");			
                    for(j=0;j<3;j++)//Y
                    {
                        printf("\nStudent %d",j+1);
                        for(k=0;k<4;k++)//X
                        {
                            printf("\t%0.2f",m[i][j][k]);
                        }
                    }
                }
            }
            
            
Matrix Operations
Representation of 3X3 Matrix on Screen
            
            #include<stdio.h>
            void main()
            {
                int m[3][3];
                int i,j;
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\nEnter element ");
                        scanf("%d",&m[i][j]);
                    }
                }
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m[i][j]);
                    }
                    printf("\n");
                }
            }
            
            
Transpose of a Matrix
            
            #include<stdio.h>
            void main()
            {
                int m[3][3];
                int i,j;
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\nEnter element ");
                        scanf("%d",&m[i][j]);
                    }
                }
                printf("\nOriginal Matrix\n");
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m[i][j]);
                    }
                    printf("\n");
                }
                printf("\nTranspose Matrix\n");
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m[j][i]);
                    }
                    printf("\n");
                }
            }
            
            
Sum of two matrices
            
            #include<stdio.h>
            void main()
            {
                int m1[3][3]={{11,2,3},{4,5,6},{7,8,9}};
                int m2[3][3]={{1,2,3},{4,5,6},{7,8,9}};
                int m3[3][3];
                int i,j;
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        m3[i][j]=m1[i][j]+m2[i][j];
                    }
                    printf("\n");
                }
                printf("\nMatrix 1\n");
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m1[i][j]);
                    }
                    printf("\n");
                }
                printf("\nMatrix 2\n");	
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m2[i][j]);
                    }
                    printf("\n");
                }
                printf("\nMatrix 3\n");	
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m3[i][j]);
                    }
                    printf("\n");
                }	
            }
            
            
Sum of all elements of Matrix
            
            #include<stdio.h>
            void main()
            {
                int m[3][3];
                int i,j,sum=0;
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\nEnter element ");
                        scanf("%d",&m[i][j]);
                    }
                }
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m[i][j]);
                        sum+=m[i][j];
                    }
                    printf("\n");
                }
                printf("\nSum is %d",sum);
            }
            
            
Sum of rows and columns of Matrix
            
            #include<stdio.h>
            void main()
            {
                int m[3][3];
                int i,j,sumr,sumc;
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\nEnter element ");
                        scanf("%d",&m[i][j]);
                    }
                }
                for(i=0;i<3;i++)//row
                {
                    sumr=0;
                    sumc=0;
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m[i][j]);
                        sumr+=m[i][j];
                        sumc+=m[j][i];
                    }
                    printf("\tsum of row = %d\tsum of col = %d\n",sumr,sumc);
                }	
            }
            
            
Sum of all elements on Left and Right Diagonals of Matrix
            
            #include<stdio.h>
            void main()
            {
                int m[3][3];
                int i,j,sumr=0,suml=0;
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\nEnter element ");
                        scanf("%d",&m[i][j]);
                    }
                }
                for(i=0;i<3;i++)//row
                {
                    for(j=0;j<3;j++)//col
                    {
                        printf("\t%d",m[i][j]);
                        if(i==j)
                            suml+=m[i][j];
                        if(i+j==2)
                            sumr+=m[i][j];
                    }
                    printf("\n");
                }	
                printf("\nsum of elements of Left Diagonal = %d",suml);
                printf("\nsum of elements of Right Diagonal = %d",sumr);	
            }
            
            
Product of two matrices(Same Order)
            
            #include<stdio.h>
            void main()
            {
                int m1[3][3]={{1,2,3},{4,5,6},{7,8,9}},m2[3][3]={{1,2,3},{4,5,6},{7,8,9}},m3[3][3];
                int i,j,k;
                for(i=0;i<3;i++)//row
                {		
                    for(j=0;j<3;j++)//col
                    {
                        m3[i][j]=0;
                        for(k=0;k<3;k++)
                        {
                            m3[i][j]+=m1[i][k]*m2[k][j];				
                        }
                        printf("\t%d",m3[i][j]);
                    }
                    printf("\n");
                }	
            }
            
            
Product of two matrices(Different Order)
            
            #include<stdio.h>
            void main()
            {
                int m1[5][5],m2[5][5],m3[5][5];
                int i,j,k,r1,r2,c1,c2;
                printf("\nEnter the no. of rows & columns for matrix 1 = ");
                scanf("%d%d",&r1,&c1);
                printf("\nEnter the no. of rows & columns for matrix 2 = ");
                scanf("%d%d",&r2,&c2);
                if(c1==r2)
                {
                    printf("\nInput For Matrix 1\n");
                    for(i=0;i<r1;i++)//row
                    {
                        for(j=0;j<c1;j++)//col
                        {
                            printf("\nEnter the element : ");
                            scanf("%d",&m1[i][j]);
                        }
                    }
                    printf("\nInput For Matrix 2\n");
                    for(i=0;i<r2;i++)//row
                    {
                        for(j=0;j<c2;j++)//col
                        {
                            printf("\nEnter the element : ");
                            scanf("%d",&m2[i][j]);
                        }
                    }
                    for(i=0;i<r1;i++)//row
                    {
                        for(j=0;j<c2;j++)//col
                        {
                            m3[i][j]=0;
                            for(k=0;k<r2;k++)//for(k=0;k<c1;k++)
                            {
                                m3[i][j]+=m1[i][k]*m2[k][j];
                            }
                        }
                    }
                    printf("\nMatrix 1\n");
                    for(i=0;i<r1;i++)//row
                    {
                        for(j=0;j<c1;j++)//col
                        {
                            printf("\t%d",m1[i][j]);
                        }
                        printf("\n");
                    }
                    printf("\nMatrix 2\n");
                    for(i=0;i<r2;i++)//row
                    {
                        for(j=0;j<c2;j++)//col
                        {
                            printf("\t%d",m2[i][j]);
                        }
                        printf("\n");
                    }
                    printf("\nMatrix 3\n");
                    for(i=0;i<r1;i++)//row
                    {
                        for(j=0;j<c2;j++)//col
                        {
                            printf("\t%d",m3[i][j]);
                        }
                        printf("\n");
                    }		
                }
                else
                {
                    printf("Multiplication of Matrix is not Possible ");
                }
            } 
            
            
Pointers:
A Pointer is a special type of variable that stores the address of another variable.
Pointer Operators:
There are two pointer operators used in C Language.
1. Address of Operator:
It is used to find the address of any variable. for this purpose we use ampersand symbol (&).
2. Value at Operator:
It is used to find the value at any Address. for this purpose we use astrisk symbol (*).
Demonstration of Pointer
                
                    #include<stdio.h>
                    void main()
                    {
                        int x,*ptr;
                        x=55;
                        ptr=&x
                        printf("\nValue of x is %d",x);
                        printf("\nValue of x is %d",*ptr);
                        printf("\nAddress of x is %u",&x);
                        printf("\nAddress of x is %d",ptr);
                        printf("\nAddress of x is %p",ptr);
                    }
                
                
Types of Pointers:

Pointers are categorized into various categories. Some of those are as follows:
1. Pointer to Integer
It is a variable, that stores the address of any integer variable only.
2. Pointer to Float
It is a variable, that stores the address of any float variable only.
3. Pointer to Character
It is a variable, that stores the address of any character variable only.
4. Pointer to Double
It is a variable, that stores the address of any double variable only.
5. Pointer to Long
It is a variable, that stores the address of any long variable only.
6. Pointer to Pointer
It is a variable, that stores the address of any other pointer (ie. address) variable only.
7. Pointer to Void
It is a variable, that stores the address of any type of variable only.
Example
                
                    #include<stdio.h>
                    void main()
                    {
                        int x,*ptr1,**ptr4;	//ptr1 is Pointer to integer & ptr4 is Pointer to Pointer
                        float y,*ptr2;		//ptr2 is Pointer to float
                        char z,*ptr3;		//ptr3 is Pointer to char
                        void *ptr;			//ptr is Pointer to void 
                        x=55;
                        y=45.4;
                        z='p';
                        ptr1=&x;
                        ptr2=&y;
                        ptr3=&z;
                        printf("\nValue of x is %d",*ptr1);
                        printf("\nAddress of x is %d",ptr1);
                        printf("\nValue of y is %f",*ptr2);
                        printf("\nAddress of y is %d",ptr2);
                        printf("\nValue of z is %c",*ptr3);
                        printf("\nAddress of z is %d",ptr3);
                        ptr=&x;
                        printf("\nAddress of x is %d",ptr);
                        ptr=&y;
                        printf("\nAddress of y is %d",ptr);
                        ptr=&z;
                        printf("\nAddress of z is %d",ptr);
                        ptr4=&ptr1;
                        printf("\nAddress of ptr1 is %d",ptr4);	
                        printf("\nValue of ptr1 is %d",*ptr4);	
                        printf("\nValue of x is %d",**ptr4);		
                    }
                
                

Array of Pointers
It is a collection of Similer(Homogenous) Pointers.
In this collection pointers must be of same datatype.
Example
                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int i,*ptr[5];
                        for(i=0;i<5;i++)
                        {
                            ptr[i]=&x[i];
                        }
                        for(i=0;i<5;i++)
                        {
                            printf("\nx[%d]=%d",i,*ptr[i]);
                        }	
                    }
                
                

Pointer Arithmetic
Just like mathematics all the operations of Arithmetic except Multiply/Divide/Modulo Division work in same way.
Operators that work: addition ( + ), subtraction ( - ), pre/post increment ( ++ ), pre/post decrement ( -- ).

Operators that do not work: multiplication ( * ), division (/), modulo division ( % ).
Example
                
                    #include<stdio.h>
                    void main()
                    {
                        int x=5;
                        int *ptr,*pt;
                        ptr=&x;
                        printf("\nx = %d",*ptr);
                        printf("\nptr = %d",ptr);
                        pt=ptr+1;
                        printf("\npt = %d",pt);
                        pt=ptr-1;
                        printf("\npt = %d",pt);
                        pt=ptr+2;
                        printf("\npt = %d",pt);
                        pt=--ptr;
                        printf("\npt = %d",pt);
                        printf("\nptr = %d",ptr);
                        pt=++ptr;
                        printf("\npt = %d",pt);
                        pt=ptr--;
                        printf("\npt = %d",pt);
                        printf("\nptr = %d",ptr);
                        pt=ptr++;
                        printf("\npt = %d",pt);
                        pt=ptr*2;                   //Error
                        printf("\npt = %d",pt);			
                        pt=ptr/2;                   //Error
                        printf("\npt = %d",pt);			
                        pt=ptr%2;                   //Error
                        printf("\npt = %d",pt);			
                    }
                
                

Array Vs Pointers
The name of Array is always a pointer constant, that stores the address of very first element of Array.
Examples
Program 1
                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr;
                        ptr=x;
                        printf("\nx=%d",*ptr);
                        printf("\nAddress of x=%d",ptr);
                    }
                
                
Program 2
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nValue = %d",*ptr+i);  //11,12,13,14,15
                        }
                    }
                
                
Program 3
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nValue = %d",*(ptr+i));//11,22,33,44,55         
                        }
                    }
                
                
Program 4
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nValue = %d",*x+i);    //11,12,13,14,15           
                        }
                    }
                
                
Program 5
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nValue = %d",*(x+i));  //11,22,33,44,55
                        }
                    }
                
                
Program 6
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nValue = %d",*ptr++);  //11,22,33,44,55
                        }
                    }
                
                
Program 7
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nValue = %d",*x++);//error
                        }
                    }
                
                
Program 8
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nAddress = %d",ptr+i); //All Addresses
                        }
                    }
                
                
Program 9
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nAddress = %d",x+i);   //All Addresses
                        }
                    }
                
                
Program 10
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nAddress = %d",ptr++); //All Addresses
                        }
                    }
                
                
Program 11
                                
                    #include<stdio.h>
                    void main()
                    {
                        int x[5]={11,22,33,44,55};
                        int *ptr,i;
                        ptr=x;
                        for(i=0;i<5;i++)
                        {
                            printf("\nAddress = %d",x++);//Error
                        }
                    }
                
                

Pointers & functions

1. Pointers can be passed to a function as arguments
Example
                
                    #include<stdio.h>
                    void main()
                    {
                            int x,*ptr;
                            void show(int*);
                            x=55;
                            ptr=&x;
                            show(ptr);
                    }
                    void show(int *p)
                    {
                        printf("x is %d",*p);
                    }
                
                

2. Pointer can be returned from a function
Example
                
                    #include<stdio.h>
                    void main()
                    {
                            int *ptr;
                            int* get();
                            ptr=get();
                            printf("x = %d",*ptr);
                    }
                    int* get()
                    {
                        int x,*p;
                        x=55;
                        p=&x;
                        return p;
                    }
                
                

3. Pointers can be passed & returned with a function
Example
                
                    #include<stdio.h>
                    void main()
                    {
                            int x,*ptr;
                            int* show(int*);
                            x=45;
                            ptr=&x;
                            printf("\nx = %d",*ptr);//45
                            ptr=show(ptr);
                            printf("\nx = %d",*ptr);//70
                    }
                    int* show(int *p)
                    {
                        int *pt,xx;
                        printf("\nx is %d",*p);//45
                        xx=*p+25;
                        pt=&xx;
                        return pt;
                    }
                
                

Array & functions

Array can be passed to a function as an argument
Example
                
                    #include<stdio.h>
                    void main()
                    {
                        void showArray(float[],int);
                        float m[5];
                        int i,n=5;
                        for(i=0;i<n;i++)
                        {
                            printf("\nEnter your marks = ");
                            scanf("%f",&m[i]);
                        }
                        showArray(m,n);
                    }
                    void showArray(float mm[],int size)
                    {
                        int i;
                        for(i=0;i<size;i++)//row
                        {
                            printf("\nYour marks = %0.2f",mm[i]);
                        }
                    }
                
                

Array can be returned from a function
Example
                
                    #include<stdio.h>
                    int n;
                    void main()
                    {
                        float *getArray();
                        float *m;
                        int i;
                        m=getArray();
                        for(i=0;i<n;i++)
                        {
                            printf("\nYour marks = %0.2f",*m++);
                        }
                    }
                    float *getArray()
                    {
                        int i;
                        static float marks[5];
                        n=5;
                        for(i=0;i<5;i++)//row
                        {
                            printf("\nEnter your marks = ");
                            scanf("%f",&marks[i]);
                        }
                        return marks;
                    }
                
                

Array can be passed & returned with a function
Examples
                
                    #include<stdio.h>
                    int n=5;
                    void main()
                    {
                        float *show(float[],int);
                        float *ptr,m[5];
                        int i;
                        for(i=0;i<n;i++)
                        {
                            printf("\nEnter your marks = ");
                            scanf("%f",&m[i]);
                        }
                        ptr=show(m,n);
                        for(i=0;i<n;i++)
                        {
                            printf("\nYour marks = %0.2f",*ptr++);
                        }
                    }
                    float *show(float mm[],int size)
                    {
                        int i;
                        static float marks[5];
                        for(i=0;i<size;i++)//row
                        {
                            marks[i]=mm[i]*2;
                        }
                        return marks;
                    }
                
                
Strings:
A String is a collection of characters, that means we can say that an array of characters is also known as String. String always terminated with an escape sequence character '\0' (pronounced as backslash zero, which is also known as null. Every string is represented within double quotes (" ")
for example:
"Welcome to BIIT"strong>
Above string can be represented in memory as follows:
'W''e''l''c''o''m''e'' ''t''o'' ''B''I''I''T''\0'
Demonstration of String Handling Operations
1. Initializing String
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str[10]="BIIT";
                                    printf("\nString = %s",str);
                                }
                            
                            
2. Inputting String
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str[10];
                                    printf("Enter the string ");
                                    scanf("%s",str);
                                    printf("\nString = %s",str);
                                }
                            
                            

3. Using gets() & puts() Library functions
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str[30];
                                    printf("Enter the string ");
                                    gets(str);
                                    printf("\nString = ");
                                    puts(str);
                                }
                            
                            

4. Using strlen() Library function
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str[20];
                                    int l;
                                    printf("\nEnter any String ");
                                    gets(str);
                                    l=strlen(str);
                                    printf("\nLength of your String = %d",l);
                                }    
                            
                            

5. Using strcpy() Library function
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str1[20]="",str2[20]="";
                                    printf("\nEnter any String ");
                                    gets(str1);
                                    printf("\nBefore Copy");
                                    printf("\nStr1 = %s",str1);
                                    printf("\nStr2 = %s",str2);
                                    strcpy(str2,str1);
                                    printf("\nAfter Copy");
                                    printf("\nStr1 = %s",str1);
                                    printf("\nStr2 = %s",str2);
                                }
                            
                            

6. Using strcat() Library function
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str1[10]="",str2[20]="";
                                    printf("\nEnter any two Strings ");
                                    gets(str1);
                                    gets(str2);
                                    printf("\nBefore Concatenate");
                                    printf("\nStr1 = %s",str1);
                                    printf("\nStr2 = %s",str2);
                                    strcat(str2," ");
                                    strcat(str2,str1);
                                    printf("\nAfter Concatenate");
                                    printf("\nStr1 = %s",str1);
                                    printf("\nStr2 = %s",str2);
                                }
                            
                            
7. Using strcmp() Library function
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str1[20]="",str2[20]="";
                                    int dif;
                                    printf("\nEnter any two Strings ");
                                    gets(str1);
                                    gets(str2);
                                    dif=strcmp(str1,str2);
                                    printf("\nDifference = %d",dif);
                                    if(dif>0)
                                        printf("\n%s is greater than %s",str1,str2);
                                    else if(dif<0)
                                        printf("\n%s is less than %s",str1,str2);
                                    else
                                        printf("\n%s is equal to %s",str1,str2);
                                }
                            
                            
8. Using strrev() Library function
                            
                                #include<stdio.h>
                                void main()
                                {
                                    char str1[20];
                                    printf("\nEnter any String ");
                                    gets(str1);
                                    strrev(str1);
                                    printf("\nStr1 = %s",str1);
                                }
                            
                            
Character based Library functions
Use of isalnum(),isalpha(), isupper(), islower(), tolower(), toupper(), isspace() Library functions
                            
                                #include<stdio.h>
                                #include<ctype.h>
                                #include<conio.h>
                                void main()
                                {
                                    char ch;
                                    clrscr();
                                    printf("\nEnter the character ");
                                    ch=getche();
                                    if(isalnum(ch)!=0)
                                    {
                                        printf("\nAlphabet or Digit");
                                        if(isalpha(ch)!=0)
                                        {
                                            printf("\nEnglish Alphabet");
                                            if(isupper(ch)!=0)
                                            {
                                                printf("\nUpper Case Letter");
                                                printf("\nLower equivalent = %c",tolower(ch));
                                            }
                                            else
                                            {
                                                printf("\nLower Case Letter");
                                                printf("\nUpper equivalent = %c",toupper(ch));
                                            }
                                        }
                                        else
                                        {
                                            printf("\nDigit");
                                        }
                                    }
                                    else
                                    {
                                        printf("\nNeither Alphabet nor Digit");
                                        if(isspace(ch)!=0)
                                        {
                                            printf("\nWhite Space");
                                        }
                                        else
                                        {
                                            printf("\nSpecial Character");
                                        }
                                    }
                                    getch();
                                }
                            
                            
A Program to find the length of string without using library function
                            
                                #include<stdio.h>
                                #include<conio.h>
                                void main()
                                {
                                    int i;
                                    char str[10];
                                    printf("\nEnter the string ");
                                    gets(str);
                                    for(i=0;str[i]!='\0';i++);
                                    printf("\nLength of string = %d",i);
                                    getch();
                                }
                            
                            
A Program to find the length of string using a User defined function(designed by you)
                            
                                #include<stdio.h>
                                #include<conio.h>
                                void main()
                                {
                                    int length(char*);
                                    int l;
                                    char str[20];
                                    printf("\nEnter the string ");
                                    gets(str);
                                    l=length(str);
                                    printf("\nLength of string = %d",l);
                                    getch();
                                }
                                int length(char *st)
                                {
                                    int i;
                                    for(i=0;st[i]!='\0';i++);
                                    return i;
                                }
                            
                            
Design a copy function for copying a string into another and also use it in the main function. (Do not use strcpy() library function)
                            
                                #include<stdio.h>
                                #include<conio.h>
                                void main()
                                {
                                    void copy(char*,char*);
                                    char str1[20]="",str2[20]="";
                                    printf("\nEnter the string ");
                                    gets(str1);
                                    printf("\nBefore Copy");
                                    printf("\nStr1 = %s",str1);
                                    printf("\nStr2 = %s",str2);
                                    copy(str2,str1);
                                    printf("\nAfter Copy");
                                    printf("\nStr1 = %s",str1);
                                    printf("\nStr2 = %s",str2);
                                    getch();
                                }
                                void copy(char *st1,char *st2)
                                {
                                    int i;
                                    for(i=0;st2[i]!='\0';i++)
                                    {
                                        st1[i]=st2[i];
                                    }
                                    st1[i]='\0';
                                }
                            
                            
Write a program to input a string and check whether it is a palindrome or not. design your own functions, do not use any library function defined in string.h header file.
                            
                                #include<stdio.h>
                                #include<conio.h>
                                int chkpalin(char*);
                                int length(char*);
                                void main()
                                {
                                    char str[20]="";
                                    int res;
                                    printf("\nEnter the string ");
                                    gets(str);
                                    res=chkpalin(str);
                                    if(res==0)
                                    {
                                        printf("\nPalindrome");
                                    }
                                    else
                                    {
                                        printf("\nNon Palindrome");
                                    }
                                    getch();
                                }
                                int chkpalin(char *st)
                                {
                                    int i,len,flag=0;
                                    len=length(st);
                                    for(i=0;i<=len/2;i++)
                                    {
                                        if(st[i]!=st[len-1-i])
                                        {
                                            flag=1;
                                            break;
                                        }
                                    }
                                    return flag;
                                }
                                int length(char *st)
                                {
                                    int i;
                                    for(i=0;st[i]!='\0';i++);
                                    return i;
                                }
                            
                            
Structure:
A Structure is a collection of heterogenous data items.
for example:
A collection of rollnumber(long), name(String), marks(float)..... etc. for a single student.
Syntax:
struct struct_name
{
...........
...........
...........
};

Example:
struct student
{
long rollnumber;
char name[];
float marks;
};

Creating an Object:
Syntax:
struct struct_name object_name;
Example:
struct student stu;
Note: a dot operator (.) is used to access the variables of a structure using pointer to object.
Demonstration of Structure
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n;
                    }s;
                    //struct student s;
                    void main()
                    {
                    //	struct student s;
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s.rn,&s.n,&s.m);
                        printf("\nYour Roll No. is %d",s.rn);
                        printf("\nYour Name is %c",s.n);
                        printf("\nYour Marks is %f",s.m);
                    }
                
                
Declaring a structure inside main() function
                
                    #include<stdio.h>
                    #include<conio.h>
                    void main()
                    {
                        struct student
                        {
                            int rn;
                            float m;
                            char n;
                        }s1,s2;
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s1.rn,&s1.n,&s1.m);
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s2.rn,&s2.n,&s2.m);
                        printf("\nYour Roll No. is %d",s1.rn);
                        printf("\nYour Name is %c",s1.n);
                        printf("\nYour Marks is %f",s1.m);
                        printf("\nYour Roll No. is %d",s2.rn);
                        printf("\nYour Name is %c",s2.n);
                        printf("\nYour Marks is %f",s2.m);
                    }
                
                
Array of Objects of Structure:
A collection of objects of similer structure(ie objects of same structure) is known as Array of Objects
Demonstration for Array of Objects:
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n;
                    }s[2];
                    void main()
                    {
                        int i;
                        for(i=0;i<2;i++)
                        {
                            printf("Enter the Roll No., Name and Marks ");
                            scanf("%d %c%f",&s[i].rn,&s[i].n,&s[i].m);
                        }
                        for(i=0;i<2;i++)
                        {
                            printf("\nYour Roll No. is %d",s[i].rn);
                            printf("\nYour Name is %c",s[i].n);
                            printf("\nYour Marks is %f",s[i].m);
                        }
                    }
                
                
Array within Structure
Arrays can be used inside structure for various purposes, Like a student can have marks in more than one subject.
For Example:
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m[5];
                        char n[10];
                    }s;
                    void main()
                    {
                        int i;
                        printf("Enter the Roll No. & Name ");
                        scanf("%d%s",&s.rn,s.n);
                        for(i=0;i<5;i++)
                        {
                            printf("Enter your Marks ");
                            scanf("%f",&s.m[i]);
                        }
                        printf("\nYour Roll No. is %d",s.rn);
                        printf("\nYour Name is %s",s.n);		
                        for(i=0;i<5;i++)
                        {
                            printf("\nYour Marks is %0.2f",s.m[i]);
                        }
                    }
                
                
WAP to input the roll no, name and marks in 5 subjects of a student and show the same with Result.
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m[5];
                        char n[10];
                    }s;
                    void main()
                    {
                        int i;
                        float total=0,per;
                        printf("Enter the Roll No. & Name ");
                        scanf("%d%s",&s.rn,&s.n);
                        for(i=0;i<5;i++)
                        {
                            printf("Enter your Marks ");
                            scanf("%f",&s.m[i]);
                        }
                        printf("\nYour Roll No. is %d",s.rn);
                        printf("\nYour Name is %s",s.n);		
                        for(i=0;i<5;i++)
                        {
                            printf("\nYour Marks is %0.2f",s.m[i]);
                            total+=s.m[i];
                        }
                        printf("\nYour total of marks = %0.2f",total);
                        per=total/5;
                        printf("\nYour percentage of marks = %0.2f",per);
                        if(per>=60)
                            printf("\nFirst Division");
                        else if(per>=50)
                            printf("\nSecond Division");
                        else if(per>=40)
                            printf("\nThird Division");
                        else 
                            printf("\nFail");	
                    }    
                
                
WAP to input the roll no, name and marks in 5 subjects of 20 students and show the same with their Results.
                
                    #include<stdio.h>
                    #include<conio.h>
                    #define size 20
                    struct student
                    {
                        int rn;
                        float m[5];
                        char n[10];
                    }s[size];
                    void main()
                    {
                        int i,j;
                        float total,per;
                        for(i=0;i<size;i++)
                        {
                            printf("Enter the Roll No. & Name ");
                            scanf("%d%s",&s[i].rn,s[i].n);
                            for(j=0;j<5;j++)
                            {
                                printf("Enter your Marks ");
                                scanf("%f",&s[i].m[j]);
                            }
                        }
                        for(i=0;i<size;i++)
                        {
                            total=0;
                            printf("\nYour Roll No. is %d",s[i].rn);
                            printf("\nYour Name is %s",s[i].n);		
                            for(j=0;j<5;j++)
                            {
                                printf("\nYour Marks is %0.2f",s[i].m[i]);
                                total+=s[i].m[j];
                            }
                            printf("\nYour total of marks = %0.2f",total);
                            per=total/5;
                            printf("\nYour percentage of marks = %0.2f",per);
                            if(per>=60)
                                printf("\nFirst Division");
                            else if(per>=50)
                                printf("\nSecond Division");
                            else if(per>=40)
                                printf("\nThird Division");
                            else 
                                printf("\nFail");	
                        }
                    }
                
                
Pointer to Structure/Object:
it is a pointer variable which can store the adderess of object of same structure. Syntax:
struct struct_name
{
...........
...........
...........
}Object_name,*Pointer;
Pointer=&Object_name;

Example:
struct student
{
long rollnumber;
char name[];
float marks;
}s,*ptr;
ptr=&s;

Note: an arrow operator (->) is used to access the variables of a structure using pointer to object.
Demonstration
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n;
                    }s,*ptr;
                    void main()
                    {
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s.rn,&s.n,&s.m);
                        ptr=&s;
                        printf("\nYour Roll No. is %d",ptr->rn);
                        printf("\nYour Name is %c",ptr->n);
                        printf("\nYour Marks is %0.2f",ptr->m);
                    }
                
                
Using typedef keyword for a structure
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n;
                    };
                    typedef struct student stud;
                    void main()
                    {
                        stud s;
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s.rn,&s.n,&s.m);
                        printf("\nYour Roll No. is %d",s.rn);
                        printf("\nYour Name is %c",s.n);
                        printf("\nYour Marks is %f",s.m);
                    }
                
                
Object of Structure can be passed in a function
Example 1
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n;
                    }s;
                    void show(struct student);
                    void main()
                    {
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s.rn,&s.n,&s.m);
                        show(s);
                    }
                    void show(struct student st)
                    {
                        printf("\nYour Roll No. is %d",st.rn);
                        printf("\nYour Name is %c",st.n);
                        printf("\nYour Marks is %0.2f",st.m);
                    }
                
                
Example 2
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n;
                    };
                    typedef struct student stud;
                    void show(stud);
                    void main()
                    {
                        stud s;
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s.rn,&s.n,&s.m);
                        show(s);
                    }
                    void show(stud st)
                    {
                        printf("\nYour Roll No. is %d",st.rn);
                        printf("\nYour Name is %c",st.n);
                        printf("\nYour Marks is %0.2f",st.m);
                    }
                
                
Object of Structure can be returned from a function
                
                    #include<stdio.h>
                    #include<conio.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n;
                    };
                    typedef struct student stud;
                    stud get();
                    void main()
                    {
                        stud st;
                        st=get();
                        printf("\nYour Roll No. is %d",st.rn);
                        printf("\nYour Name is %c",st.n);
                        printf("\nYour Marks is %0.2f",st.m);
                    }
                    stud get()
                    {
                        stud s;
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d %c%f",&s.rn,&s.n,&s.m);
                        return s;	
                    }
                
                
Object of Structure can be passed & returned with a function
                
                    #include<stdio.h>
                    #include<conio.h>
                    #include<string.h>
                    struct student
                    {
                        int rn;
                        float m;
                        char n[10];
                    };
                    typedef struct student stud;
                    stud show(stud);
                    void main()
                    {
                        stud st,rst;
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%d%s%f",&t.rn,&st.n,&st.m);
                        rst=show(st);
                        printf("\nYour Roll No. is %d",rst.rn);
                        printf("\nYour Name is %s",rst.n);
                        printf("\nYour Marks is %0.2f",rst.m);
                        getch();
                    }
                    stud show(stud s)
                    {
                        stud rs;
                        rs.rn=s.rn+2;
                        strcpy(rs.n,s.n);
                        strcat(rs.n," Kumar");
                        rs.m=s.m+20;
                        return rs;	
                    }

                
                
Union:
A Union is also a collection of heterogenous data items like structure.
for example:
A collection of rollnumber(long), name(String), marks(float)..... etc. for a single student.
Syntax:
union union_name
{
...........
...........
...........
};

Example:
union student
{
long rollnumber;
char name[];
float marks;
};

Creating an Object:
Syntax:
union union_name object_name;
Example:
union student stu;
Demonstration of Union
                
                    #include<stdio.h>
                    #include<conio.h>
                    union student
                    {
                        int rn;
                        float m;
                        char n;
                    }s;
                    void main()
                    {
                        printf("Enter the Roll No., Name and Marks ");
                        scanf("%f %c%d",&s.m,&s.n,&s.rn);
                        printf("\nYour Roll No. is %d",s.rn);
                        printf("\nYour Name is %c",s.n);
                        printf("\nYour Marks is %0.1f",s.m);
                    }
                
                
Difference between structure & union
To show the Difference between structure & union we have a following program:
                
                    #include<stdio.h>
                    #include<conio.h>
                    union student_un
                    {
                        int rn;
                        float m;
                        char n;	
                    };
                    struct student_str
                    {
                        int rn;
                        float m;
                        char n;	
                    };
                    void main()
                    {
                        union student_un s1;
                        struct student_str s2;
                        printf("\nSize of object s1(ie for union) = %d",sizeof(s1));
                        printf("\nSize of object s1(ie for union) = %d",sizeof(union student_un));	
                        printf("\nSize of object s2(ie for struct) = %d",sizeof(s2));
                        printf("\nSize of object s2(ie for struct) = %d",sizeof(struct student_str));	
                    }
                
                
typedef Keyword:
This keyword is used to define a new name for any previously defined datatype.
Syntax:
typedef old_datatypename new_datatypename;
for example:
                            
                                #include<stdio.h>
                                #include<conio.h>
                                typedef int integer;
                                void main()
                                {
                                    integer x;
                                    x=5;
                                    printf("\nx = %d",x);		
                                }
                            
                            
sizeof() Operator:
it is a keyword that can be used for finding the size of any variable / datatype any time when it is required. It is also kept in the category of Operators
Syntax:
sizeof(type)
for example:
                            
                                #include<stdio.h>
                                #include<conio.h>
                                void main()
                                {
                                    int x;
                                    float y;
                                    char z;
                                    double w;
                                    printf("\nSize of x is %d",sizeof(x));
                                    printf("\nSize of int is %d",sizeof(int));
                                    printf("\nSize of y is %d",sizeof(y));
                                    printf("\nSize of float is %d",sizeof(float));
                                    printf("\nSize of z is %d",sizeof(z));
                                    printf("\nSize of char is %d",sizeof(char));
                                    printf("\nSize of w is %d",sizeof(w));
                                    printf("\nSize of double is %d",sizeof(double));
                                    getch();
                                }    
                            
                            
Enumerations:
An Enumeration is a collection of named integer constants. its syntax is just like the syntax of structure.
for example:
A collection of names of week days.
Syntax:
enum enum_name
{
...........
...........
...........
};

Example:
enum week { sunday,monday,tuesday,wednesday,thursday,friday,saturday};

Creating an Object:
Syntax:
enum enum_name object_name;
Example:
enum week a;
Demonstration of Enumeration:
Example1:

	#include<stdio.h>
	#include<conio.h>
	enum week{sunday,monday,tuesday,wednesday,thursday,friday,saturday};
	void main()
	{
		enum week a,b;
		int dif;
		a=thursday;
		b=monday;
		dif=a-b;
		printf("\nIndex value for thursday = %d",thursday);
		printf("\nIndex value for monday = %d",monday);
		printf("\nDifference between thursday and monday = %d",dif);
		getch();		
	}    

Example2:

	#include<stdio.h>
	#include<conio.h>
	enum student
	{
		rakesh=100,mukesh,suresh,harish=200,ramesh,indresh,munesh,jagesh
	};
	void main()
	{
		printf("\nIndex for rakesh = %d",rakesh);//100
		printf("\nIndex for harish = %d",harish);//200
		printf("\nIndex for munesh = %d",munesh);//203
		printf("\nIndex for jagesh = %d",jagesh);//204
		getch();		
	}
			
			
File Management System:

Demonstration of File Management System:
Example 1:
To Write a character into a file in writing mode
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		f=fopen("biit.txt","a");
		putc('A',f);
		printf("\nData is written");	
		fclose(f);
	}    
	
	
Example 2:
To read Character from a file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		char ch;
		f=fopen("biit.txt","r");
		ch=getc(f);
		printf("\nYour Data is %c",ch);
		fclose(f);
	}
	
	
Example 3:
To Write multiple characters into a file in writing mode
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		char ch,cho;	
		f=fopen("biit.txt","w");
		do
		{
			printf("\nEnter the character ");
			ch=getche();
			fputc(ch,f);
			printf("\nDo you want more ");
			cho=getche();
		}while(cho=='y');
		printf("\nData is written");
		fclose(f);
	}
	
	
Example 4:
To read Multiple Characters from a file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		char ch;
		f=fopen("biit.txt","r");
		do
		{
			ch=fgetc(f);
			printf("%c",ch);
		}while(!feof(f));
		fclose(f);
	}
	
	
Example 5:
To Write a character into a file in append mode
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		f=fopen("biit.txt","a");
		putc('P',f);
		printf("\nData is written");	
		fclose(f);
	}
	
	
Example 6:
To Write an integer into a file in writing mode
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		f=fopen("biit.txt","w");
		putw(7847,f);
		printf("\nData is written");
		fclose(f);
	}
	
	
Example 7:
To Read an integer from file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		int i;
		f=fopen("biit.txt","r");
		i=getw(f);
		printf("\nYour Data is %d",i);
		fclose(f);
	}
	
	
Example 8:
To Write multiple Integers into a file in writing mode
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		char cho;
		int num;	
		f=fopen("biit.txt","w");
		do
		{
			printf("\nEnter the number ");
			scanf("%d",&num);
			putw(num,f);
			printf("\nDo you want more ");
			cho=getche();
		}while(cho=='y');
		printf("\nData is written");
		fclose(f);
	}
	
	
Example 9:
To Read multiple integer from file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		int i;
		f=fopen("biit.txt","r");
		i=getw(f);		
		do
		{
			printf("\nYour Data is %d",i);
			i=getw(f);		
		}while(!feof(f));
		fclose(f);
	}
	
	
Example 10:
To Write an integer into a file in append mode
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		f=fopen("biit.txt","a");
		putw(125,f);
		printf("\nData is written");
		fclose(f);
	}
	
	
Example 11:
To Write a record(Collection of heterogenous Data) into a file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		int rn;
		float m;
		char n[10];
		FILE *f;
		f=fopen("biit.txt","w");
		printf("\nEnter the rn, name and marks ");
		scanf("%d%s%f",&rn,n,&m);
		fprintf(f,"%d %f %s",rn,m,n);
		printf("\nData is written");	
		fclose(f);
	}
	
	
Example 12:
To Read a record(Collection of heterogenous Data) from a file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		int rn;
		float m;
		char n[10];
		FILE *f;
		f=fopen("biit.txt","r");
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		fclose(f);
	}
	
	
Example 13:
To Write multiple records(Collection of heterogenous Data) into a file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		int rn;
		float m;
		char n[10],ch;
		FILE *f;
		f=fopen("biit.txt","w");
		do
		{
			printf("\nEnter the rn, name and marks ");
			scanf("%d%s%f",&rn,n,&m);
			fprintf(f,"%d %f %s ",rn,m,n);
			printf("\nDo you want more ");
			ch=getche();
		}while(ch=='y');
		printf("\nData is written");
		fclose(f);
	}
	
	
Example 14:
To Read multiple records(Collection of heterogenous Data) from a file
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		int rn;
		float m;
		char n[10];
		FILE *f;
		f=fopen("biit.txt","r");
		fscanf(f,"%d%f%s",&rn,&m,n);
		do
		{
			printf("\nYour rn is %d",rn);
			printf("\nYour name is %s",n);
			printf("\nYour marks is %f",m);
			fscanf(f,"%d%f%s",&rn,&m,n);
		}while(!feof(f));
		fclose(f);
	}
	
	
Example 15:
Use of ftell() function
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		FILE *f;
		int rn;
		float m;
		char n[10];
		f=fopen("biit.txt","r");
		printf("\nCurrent position = %ld",ftell(f));
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
		fclose(f);
	}
	
	
Example 16:
To FIND Following:
1. No. of records
2. Size of one Record
3. Total Size of all Records occupied in File
4. Starting Byte No.
5. Ending Byte No.
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		int rn,stpos,endpos,size,nor=0,sz;
		float m;
		char n[10];
		FILE *f;
		f=fopen("biit.txt","r");
		stpos=ftell(f);
		fscanf(f,"%d%f%s",&rn,&m,n);
		do
		{
			printf("\nYour rn is %d",rn);
			printf("\nYour name is %s",n);
			printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
				nor++;
			fscanf(f,"%d%f%s",&rn,&m,n);
		}while(!feof(f));
		endpos=ftell(f);
		size=endpos-stpos;
		sz=size/nor;
		printf("\nStart = %d",stpos);
		printf("\nEnd = %d",endpos);
		printf("\nNor = %d",nor);
		printf("\nSize = %d",size);
		printf("\nSz = %d",sz);	
		fclose(f);
	}
	
	
Example 17:
Use of fseek() and ftell() functions
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		int rn,num;
		long int btn;
		float m;
		char n[10];
		FILE *f;
		f=fopen("biit.txt","r");
		printf("\nFrom Begining of File");
		printf("\nEnter the record number ");
		scanf("%d",&num);
		printf("\nCurrent position = %ld",ftell(f));
		btn=13*(num-1);
		fseek(f,btn,0);
		printf("\nCurrent position = %ld",ftell(f));
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
		printf("\nFrom End of File");
		printf("\nEnter the record number ");
		scanf("%d",&num);
		btn=13*(num);
		fseek(f,-btn,2);
		printf("\nCurrent position = %ld",ftell(f));
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
		printf("\nFrom Current Position of File in Forward Direction ");
		printf("\nEnter the record number ");
		scanf("%d",&num);
		btn=13*(num-1);
		fseek(f,btn+1,1);
		printf("\nCurrent position = %ld",ftell(f));
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
		printf("\nFrom Current Position of File in Backward Direction ");
		printf("\nEnter the record number ");
		scanf("%d",&num);
		btn=13*(num+1);
		fseek(f,-btn,1);
		printf("\nCurrent position = %ld",ftell(f));
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
		fclose(f);
	}
	
	
Example 18:
Use of rewind() function
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		int rn,num;
		long int btn;
		float m;
		char n[10];
		FILE *f;
		f=fopen("biit.txt","r");
		printf("\nFrom Begining of File");
		printf("\nEnter the record number ");
		scanf("%d",&num);
		printf("\nCurrent position = %ld",ftell(f));
		btn=13*(num-1);
		fseek(f,btn,0);
		printf("\nCurrent position = %ld",ftell(f));
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
		rewind(f);
		printf("\nAfter rewind Current position = %ld",ftell(f));
		fscanf(f,"%d%f%s",&rn,&m,n);
		printf("\nYour rn is %d",rn);
		printf("\nYour name is %s",n);
		printf("\nYour marks is %f",m);
		printf("\nCurrent position = %ld",ftell(f));
		fclose(f);
	}
	
	
Dynamic Memory Allocation:

malloc() function
free() function
Example 1:
To store single integer using malloc() function

	#include<stdio.h>
	#include<conio.h>
	void main() 
	{  
		int *elm; //pointer to int
		elm=(int*)malloc(sizeof(int)); 
		printf("Enter element ");
		scanf("%d",elm); 
		printf("\nElement = %d",*elm); 
		free(elm);
	} 

Example 2:
To store elements of array using malloc() function

	#include<stdio.h>
	#include<conio.h>
	void main() 
	{ 

		int *arr,*pt; 
		int n,i; 
		printf("Enter number of elements: ");
		  scanf("%d",&n); 
		arr=(int*)malloc(n*sizeof(int)); 
		pt=arr;
		  for(i=0;i<n;i++) 
		  {
			printf("Enter element ");
			scanf("%d",arr++); 
		}     
		arr=pt;
		for(i=0;i<n;++i)
		  { 
			printf("\nElement = %d",*arr++); 
		} 
	}

Example 3:
Pointer to Structure/Object

	#include<stdio.h>
	#include<conio.h>
	struct student
	{
		int rn;
		float m;
		char n;
	}*ptr;
	typedef struct student std;
	void main()
	{
		ptr=(std*)malloc(sizeof(std));
		printf("Enter the Roll No., Name and Marks ");
		scanf("%d %c%f",&ptr->rn,&ptr->n,&ptr->m);
		printf("\nYour Roll No. is %d",ptr->rn);
		printf("\nYour Name is %c",ptr->n);
		printf("\nYour Marks is %0.2f",ptr->m);
	}

calloc() function:
Example 1:
To store elements of array using calloc() function

	#include<stdio.h>
	#include<conio.h>
	void main() 
	{  
		int *arr,*pt; 
		int n,i; 
		printf("Enter number of elements: ");
		scanf("%d",&n); 

		arr=(int*)malloc(n*sizeof(int)); 
		printf("\nElement =%d",*arr);


		pt=arr;
		for(i=0;i<n;i++) 
		{
			printf("Enter element ");
			scanf("%d",arr++); 
		}     
		arr=pt;
		for(i=0;i<n;++i)
		  { 
			printf("\nElement = %d",*arr++); 
		} 
	} 

Example 2:
To store multiple object of structure using Calloc() function

	#include<stdio.h>
	#include<conio.h>
	struct student
	{
		int rn;
		float m;
		char n;
	}*ptr,*pt;
	typedef struct student std;
	void main()
	{
		int i,num;
		printf("\nEnter the no. of objects to store ");
		scanf("%d",&num);
		ptr=(std*)calloc(num,sizeof(std));
		pt=ptr;
		for(i=0;i<num;i++)
		{
			printf("Enter the Roll No., Name and Marks ");
			scanf("%d %c%f",&ptr->rn,&ptr->n,&ptr->m);
			ptr++;
		}
		ptr=pt;
		for(i=0;i<num;i++)
		{
			printf("\nYour Roll No. is %d",ptr->rn);
			printf("\nYour Name is %c",ptr->n);
			printf("\nYour Marks is %0.2f",ptr->m);
			ptr++;
		}
	}

realloc() function Example 1:
To store elements of array using malloc() function and reallocate the size of array using realloc() function

	#include<stdio.h>
	#include<conio.h>
	void main() 
	{ 
		int *ptr; 
		int n,i; 
		printf("Enter number of elements: ");
		scanf("%d",&n); 
		ptr=(int*)malloc(n*sizeof(int)); 
		for(i=0;i<n;i++)
		{ 
			printf("Enter element ");
			scanf("%d",&ptr[i]); 
		} 
		for(i=0;i<n;i++)
		{ 
			printf("\nElement = %d",ptr[i]); 
		} 
		printf("\nEnter number of elements to Reallocate: ");
		scanf("%d",&n); 
		ptr=realloc(ptr,n*sizeof(int)); 
		for(i=0;i<n;i++)
		{ 
			printf("Enter element ");
			scanf("%d",&ptr[i]); 
		} 
		for(i=0;i<n;i++)
		{ 
			printf("\nElement = %d",ptr[i]); 
		} 
	} 

Example 2:
To store elements of array using calloc() function and reallocate the size of array using realloc() function

	#include<stdio.h>
	#include<conio.h>
	void main() 
	{ 
		int *ptr; 
		int n,i; 
		printf("Enter number of elements: ");
		scanf("%d",&n); 
		ptr=(int*)calloc(n,sizeof(int)); 
		for(i=0;i<n;i++)
		{ 
			printf("Enter element ");
			scanf("%d",&ptr[i]); 
		} 
		for(i=0;i<n;i++)
		{ 
			printf("\nElement = %d",ptr[i]); 
		} 
		printf("\nEnter number of elements to Reallocate: ");
		scanf("%d",&n); 
		ptr=realloc(ptr,n*sizeof(int)); 
		for(i=0;i<n;i++)
		{ 
			printf("Enter element ");
			scanf("%d",&ptr[i]); 
		} 
		for(i=0;i<n;i++)
		{ 
			printf("\nElement = %d",ptr[i]); 
		} 
	} 


Phases of Problem Sovling
We are given problem to solve the same. We have to work on some phases to solve the same problem. Phases can be defined as follows:
There are four phases of process of problem solving.
1. Understanding the problem
2. Making the plan of solution
3. Carrying out the plan
4. Looking back i.e. verifying.
Understanding the problem
Needless to say that if you do not understand the problem, you can never solve it. It is also often true that if you really understand the problem, you can see a solution.
Making the plan of solution
When you understood the problem, next phase comes to make the plan of solution. In this phase of planning a group of people makes the different-2 plans for solving the problem.
Carrying out the plan
When you planned about the solution of problem, next step is to carry out the plan. In this phase we work out on the solution of problem in planned way.
Looking back i.e. verifying
When a solution of problem is prepared, next step is to look back on the solution. In this phase we check whether a solution is up to the mark or not. If it is ok then we say that problem is finished and if not ok again make the plan and work out for solution of problem again.
Problem Sovling Techniques
Various ways to solve the problem are followed. There are some algorithms for solving the problem are discussed as follows:
Trial and Error Algorithm:
Some complex problems can be solved by a technique that is called trial and error. Trial and error method is typically good for the problems where you have multiple chances to get the correct solution. However, this is not a good technique for problems that don’t give you multiple chances to find a solution.
Trial and error is also a great way to gain knowledge. Basically, a person that uses the trial and error method will try to a method to see if it is a good solution. If it is not a good solution, they try another option. If the method works, the person using it has acquired the correct solution to a problem. However, there are some situations where there are too many options, and it is not feasible for a person to go through all of them to find out which one works the best. In this event, a person will want to use the option that has the best possible chance of succeeding. If this doesn’t work, they can try the next best option until they find a good solution.
Brainstorming Algorithm:
Brainstorming can be an effective way to generate the lots of ideas on specific issue and then determine which idea or ideas is the best solution. Brainstorming is most effective with group of 8-12 people and should be performed in a relaxed environment. If participants feel free to relax and joke around, they will stretch their minds further and therefore produce more creative ideas. Brainstorming works the best with a varied group of people. Participants should come from various departments across the organization and have different backgrounds. Even in specialist areas, outsiders can bring fresh ideas that can inspire the experts. There are numerous approaches to brainstorming, but the traditional approach is generally the most effective because it is most energetic.
Divide and Conquer Algorithm
Divide and Conquer is an important algorithm design paradigm based on multi branched recursion. A divide and conquer algorithm works recursively breaking down a problem into two or more sub-problems of the same type. Until these become simple enough to be solved directly. The solution to the sub-problems are then combined to give a solution to the original problem. This technique is the basis of efficient algorithms for all kinds of problems, such as sorting (quick sort and merge sort).
Steps in Problem Sovling
There are seven steps to follow when you try to solve the problem. These steps are as follows:
Define & identify the problem:
The first step of solving the problem is to define the problem. It is essential for each group member to clearly understand the problem so that all energy will be focused in the same direction. A good way to define the problem is to write down a concise statement which summarizes the problem. The objective is to get as much information about the problem as per as possible.
Analyze the problem
In this stage of problem solving, question should be asked and information gathered and sifted. Do not make the mistake of assuming you know what is causing the problem without an effort to fully investigate the problem you have defined. Try to view the problem from a verity of viewpoints, not just how it affects you but think about how the issue affects others.
Identifying the possible solutions:
“The best way to have good idea is to have a lot of ideas”. Identifying possible solutions to the problem is sometimes referred to as finding “optional solutions” because the goal is to complete a list of all conceivable alternatives to the problem. To get possible solutions different views are asked to all group members to give their ideas to solve the problem.
Selecting the best solution
In the last step we have got the verity of solutions, from that variety we have to select the one solution. The decision making of project leader makes it possible to select the best idea from that variety of solutions.
Evaluating solution:
There are several ways to evaluate the chose solution and writing them all down will help the group to choose the best solution to the problem.
Develop an action plan:
When we get the best solution we plan our action to perform the best solution. According to that action plan we work out until we get solved our problem.
Implement the solution:
When we get solved our problem, we implement it where it is required.
Time Complexity
The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notations, which suppresses multiplicative constants and lower order terms. When expressed this way, the time complexity is said to be described asymptotically, i.e. as the input size goes to infinity. For example if the time required by an algorithm on all inputs of size n is at most 5n3+3n, the time complexity is O(n3).
Storage Classes
The various information related to a datatype or a variable are:
1. Name of the variable.
2. Amount of space required to store that variable is created.
3. Address of the memory location where the variable is created.
Further there are two kinds of locations in a computer where the data in the variable is stored: Memory and CPU Registers. The storage class specifiers of the variable determines in which of the two locations the value would be stored. In fact storage class specifier is another modifier which can be added to the variable declaration.
There are four storage class specifiers in C and C++.
1. Automatic 2. Static 3. Register 4. External
General Syntex is:
Storage_specifier datatype varname;
Demonstration Programs
Example 1:
	
	#include<stdio.h>
	#include<conio.h>
	int w;
	void main()
	{
		auto int x;
		static int y;
		register int z;
		extern int w;
		x=5;
		y=55;
		z=555;
		w=5555;
		printf("\nx(auto) =%d",x);
		printf("\ny(static) =%d",y);
		printf("\nz(register) =%d",z);
		printf("\nw(extern) =%d",w);	
	}
	
	
Example 2:
	
	#include<stdio.h>
	#include<conio.h>
	void main()
	{
		void showinc();
		showinc();	
		showinc();	
		showinc();		
	}
	void showinc()
	{
		auto int x=0;
		static int y=0;
		printf("\nCalling Time = #%d#",(y+1));
		printf("\nx =%d",x);
		printf("\ny =%d",y);
		x++;
		y++;
		printf("\nx =%d",x);
		printf("\ny =%d",y);		
	}
	
	
Example 3:
	
	#include<stdio.h>
	#include<conio.h>
	 int a = 11;
	int b;
	void main() 
	{
	   void func();
	   auto int c=22,d;
	   extern int b;
	   register int e;
	   static int f;
	   printf("\nThe value of extern variable (a): %d",a);//11
	   printf("\nThe value of extern variable (b): %d",b);//0
	   printf("\nThe value of auto variable (c): %d", c);//22
	   printf("\nThe value of auto variable (d): %d", d);//Garbage
	   printf("\nThe value of register variable (e): %d",e);//Garbage
	   printf("\nThe value of static variable (f): %d",f);//0
	   a = 111;
	   b = 222;
	   c = 333;
	   d = 444;
	   e = 555;
	   f = 666;
	   printf("\nAfter modification");
	   printf("\nThe value of extern variable (a): %d",a);
	   printf("\nThe value of extern variable (b): %d",b);
	   printf("\nThe value of auto variable (c): %d", c);
	   printf("\nThe value of auto variable (d): %d", d);
	   printf("\nThe value of register variable (e): %d",e);
	   printf("\nThe value of static variable (f): %d",f);
	   func();
	}
	void func()
	{
		printf("\nInside function");
		a=1111;
		b=2222;
		c=3333;
		d=4444;
		e=5555;
		f=6666;
		printf("\nThe value of extern variable (a): %d",a);
		printf("\nThe value of extern variable (b): %d",b);
		printf("\nThe value of auto variable (c): %d", c);
		printf("\nThe value of auto variable (d): %d", d);
		printf("\nThe value of register variable (e): %d",e);
		printf("\nThe value of static variable (f): %d",f);
	}
	
	
Scope
of a variable can be defined with some scope rules and a region of a program in which a variable is available for use, is called the scope of a variable. And also Visibility of a variable in the program is the ability to access the variable from the memory.
The Life time of a variable is the duration of time in which a variable exists in the memory during the execution.
Rules of scope:
1. The scope of a global variable is the entire program file.
2. The scope of local variable begins at the point of declaration and ends with the end of block of function in which it is declared.
3. The scope of a formal function argument is its own function.
4. The life time of an auto variable declared in the main() is the entire program execution time, although it’s scope is only main() function.
5. The life of an auto variable declared in the function ends when the function is exited.
6. A static local variable, although it’s scope is limited to its function, it’s life time extends till the program execution.
7. All variable have visibility in their scope provided they are not declared again.
8. If a variable is redeclared within its scope again, it loses, it’s visibility in the scope of re declared variable.
Example 1:
	
	#include<stdio.h>
	#include<conio.h>
	int x;  //Global Variable
	void main()
	{
		int y;//Local Variable & Auto Variable
		static int z;//Static Variable
		y=55;
		{
			int y;
			y=33;
		}

	}
	void func(int p) //p:Formal Function Argument
	{
		int q;//Local Variable & Auto Variable
		static int r;//Static Variable
	}
	
	
Example 2:
	
	#include<stdio.h>
	#include<conio.h>
	int x=11;  //Global Variable
	void main()
	{
		void func(int);
		int y;//Local Variable & Auto Variable
		static int z=33;//Static Variable
		y=22;
		printf("\nx is %d",x);
		{
			int y;
			y=44;
			printf("\ny(inside) is %d",y);		
		}	
		printf("\ny(outside) is %d",y);
		printf("\nz is %d",z);
		func(y);
	}
	void func(int p) //p:Formal Function Argument
	{
		int q;//Local Variable & Auto Variable
		static int r=55;//Static Variable
		q=p;
		printf("\nInside function");
		printf("\np is %d",p);
		printf("\nq is %d",q);
		printf("\nr is %d",r);
	}
	
	
Preprocessors
The preprocessor is a program that processes to the source code before it passes through the compiler. It operates under the control of what is known as preprocessor command line or directives.

Preprocessor directives are in the source program that are declared before the main line. Before the source code passes through it is examined by the preprocessor for any preprocessor directives. If there are any, appropriate actions are taken and then the source program is handed over to the compiler.
There are nine preprocessors defined below:
1. #include
2.#define
3.#undef
4.#if
5.#else
6.#elif
7.#end if
8.#ifdef
9.#ifndef
These directives can be divided into three categories:
1. Macro Substitution Directives.
2. File Inclusion Directives
3. Compiler Control Directives.
Macro Substitution Directives:
Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens. The preprocessor accomplishes this task under the direction of #define statement. This statement is known as macro definition.
General format of define is:
#define identifier string
There are three different forms of macro substitution:
a. Simple macro substitution
b. Argumented macro substitution
c. Nested macro substitution.
Simple macro substitution:
#define pi 3.14
Argumented macro substitution:
#define cube(x) x*x*x
Nested macro substitution:
#define one 1
#define two 2
#define three one+two
Undefining a macro:
a defined macro can be undefined using the following statement:
#undef pi
This is very useful when we want to restrict the definition only to a particular portion of the program.
File Inclusion Directives
An external file containing function or macro definition can be included as a part of a program so that we need not to rewrite those
functions or macro definitions.
This is achieved by the preprocessor directive:
#include “filename”
Where the filename is the name of the file containing the required definitions and functions. At this point the preprocessors insert the entire contents of filename into the source code of the program. When the filename is included within the double quotation marks the search for the file made first in the current directory and then in the standard directories. Alternatively this directive can take the form without double quotation marks.
#include < filename >
In this case, the file is searched in the standard directory.
Compiler Control Directives
These directives can be used as follows:
1. #if
2. #else
3. #elif
4. #end if
5. #ifdef
6. #ifndef Example 1:
	
	#include<stdio.h>
	#include<conio.h>
	#define one 1		//Macro	
	void main()
	{
		printf("One = %d",one);
	}
	
	
Example 2:
	
	#include<stdio.h>
	#include<conio.h>
	#define one 1
	#define two 2
	#define three one+two //Nested Marco
	void main()
	{
		printf("\nOne = %d",one);
		printf("\nTwo = %d",two);
		printf("\nThree = %d",three);
	}
	
	
Example 3:
	
	#include<stdio.h>
	#include<conio.h>
	#define add(x,y) x+y  	//Argumented Macro
	void main()
	{
		int a=5,b=6;
		float c=1.2,d=7.1;
		printf("\nadd(3,4) = %d",add(3,4));
		printf("\nadd(a,b) = %d",add(a,b));
		printf("\nadd(c,d) = %0.1f",add(c,d));	
	}
	
	
Example 4:
	
	#include<stdio.h>
	#include<conio.h>
	#define one 1		//Macro	
	void main()
	{
		printf("\nOne = %d",one);
		#undef one
		int one=12;
		printf("\nOne = %d",one);		
	}
	
	
Example 5:
demoheader.h(A header file)
	
	void show()
	{
		printf("I am in demoheader.h Header file");
	}
	
	

	
	#include<stdio.h>
	#include<conio.h>
	#include "demoheader.h"
	void main()
	{
		show();
		printf("\nNow i am in preprocessor.c (Current) File");
	}
	
	
Example 6:
	
	#include<stdio.h>
	#include<conio.h>
	#define one 1		//Macro	
	void main()
	{
		#ifdef one
			printf("One = %d",one);
		#endif
	}
	
	
Example 7:
	
	#include<stdio.h>
	#include<conio.h>
	#define one 1		//Macro	
	void main()
	{
		#ifdef one
			printf("One = %d",one);
		#endif
		#undef one
		#ifndef one
			printf("\nOne is not Defined");
		#endif
	}
	
	
Example 8:
	
	#include<stdio.h>
	#include<conio.h>
	#define x 5
	#define y 3
	void main()
	{
		#if (x>y)
			printf("x is greater");
		#endif	
	}
	
	
Example 9:
	
	#include<stdio.h>
	#include<conio.h>
	#define x 15
	#define y 13
	void main()
	{
		#if (x>y)
			printf("x is greater");
		#else
			printf("y is greater");	
		#endif	
	}
	
	
Example 10:
	
	#include<stdio.h>
	#include<conio.h>
	#define x 213
	#define y 113
	void main()
	{
		#if (x>y)
			printf("x is greater");
		#elif (x<y)
			printf("y is greater");	
		#else
			printf("Both are Equal");	
		#endif	
	}
	
	
Example 11
	
	#include<stdio.h>
	#include<conio.h>
	void end();
	#pragma startup start
	#pragma exit end
	void main()
	{
	   printf("\n Now I am in main function");
	} 
	void start()
	{
	   printf("\nI am in Starting of Program");
	} 
	void end()
	{
	   printf("\nI am in the End of Program");
	}