Ads 468x60px

C Language


PROGRAMMING IN “C”

C, the most popular computer language today, is an off-spring of the Basic Combined Programming Language (BCPL) called B developed in the 1960’s at Cambridge. Dennis Ritche was implemented at Bell Laboratories in 1972. The new language was named C.

IMPORTANCE OF C:

1.       Programs written in C are efficient and fast. This is due to the variety of data types and powerful operators.
2.       C is highly portable, ie. C program written for one computer can be run on another with little or no modification.
3.       C language is well suited for structured programming, this requiring the user to think in terms of function modules or blocks. A proper collection of these modules would make a complete program
4.       Another important feature of C is its ability to extend itself. A C program is basically a collection of functions that are supported by C library.

CHARACTER SET:
The characters that can be used to from words, numbers and expressions are called Character Set. The Characters in C are grouped in to

1.       Letters (a to z)
2.       Digits (0 to 9
3.       Special Characters (+, -, *, ; etc)
4.       White space characters (blank space, tab etc)

CONSTANTS:
A constant in C refers to fixed values that do not change during the execution of a program

VARIABLE:
A variable is a data name that may be used to store data value. Unlike constant, a variable may take different values at different times during execution

Eg: average, height, total,  a, b, x, y, etc.

RULES:
1.       They must being with a letter
2.       A length of 31 characters is recognized, but only the first 8 characters are considered significant.
3.       Uppercase and lowercase are significant. Total is different from TOTAL or total.
4.       Variable name should not be a keyword.
5.       White space is not allowed.

DECLARATION OF VARIABLES:
The variables must be declared to the compiler. By declaration, we tell the compiler,

Syntax of declaration is,
                Datatype Variavlename;

Eg:
int  a;
Float x;

DATA TYPES:
C supports the following data types

1.       Integer (int)
2.       Floating point (float)
3.       Character (char)
4.       Double floating point (double)
5.       Long Integer (long)

READING DATA FRONT KEYBOARD
Data is input through the keyboard using the scanf function. The general format is,

scanf(“Control string”, &variable1, &variable2, ……, variable n);

The control string contains the format of data being received. The ampersand symbol (&) before each variable name is an operator that specifies the variable name’s address.

Eg:
scabf(“%d”, &number);

Data Type
Control String
Integer
%d
Float
%f
Character
%c
String
%s

WRITING DATA TO MONITOR:
To print the values of number on the monitor use the printf function. The general format is,

Printf(“Control string”, variable name);

Eg:
printf(“%d”), number);

The first argument %d tells the compiler that the value of the second argument number should be printed as integer. The two arguments are separated by a comma.

EXECUTING A C PROGRAM:
Executing a C program involves a series of steps. There are,

1.       Creating a program:
The program that is entered into file is known as the source program
2.       Compiling the program:
Compilation process converts the source code into an object code with .OBJ extension
3.       Linking the program:
After linking the file is converted to, an executable files with .EXE extension.
4.       Running the program:
The program is then run to get the designed output.


PROGRAM: 1

Write a program that displays, Welcome to Emmess Info

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr(); //(Clear the output screen)
prinf(“Welcome to Emmess Info:);
getch();
}
In this program getch() is used to get a character from the keyboard. It is included in the header file, conio.h

HEADER FILE contain the declarations that are needed by the identifier and the operator. Without these the compiler won’t recognize them.

PREPROCESSOR, DIRECTIVES starts with a number sign(#). It is an instruction to the compiler. # include tells the compiler to insert another file into the source file.

PROGRAM: 2

Write a program to find the sum and average of three numbers

#include<stdio.h>
#include<conio.h>
void main()
{
float a, b, c, sum=0, avg;
clrscr();
printf(“Enter the 3 numbers:”);
scanf(“%f, %f, %f”, &a, &b, &c);
sum = a+B+C;
avg = sum/3;
printf(“The sum of 3 Numbers%f”, sum);
printf(“\nThe Average is %f”, avg);   //(\n: Next Line)
getch();
}

PROGRAM: 3

Write a program to convert the given temperatures in Fahrenheit to Celsius, the formula  C=(F-32)/1.8

#include<stdio.h>
#include<conio.h>
void main()
{
float f, c;
clrscr();
printf(“Enter the temperature in Fahrenheit:”);
scanf(“%f”, &f);
c=(f=32)/1.8;
printf(“The temperature os %f Celsius”, c);
getch();
}


PROGRAM: 4

Write a program to find the net salary of an employee given Basic pay, DA at 20% of basic pay, HRA at 15% of basic pay, TA at 10% of basic pay. Gross salary is basicpay+DA+HRA+TA, PF is 3% of GS(Gross Pay), tax is 1% of GS. Print the Basic Pay, DA, HRA, TAX, GS and PF and NetPay

#include<stdio.h>
#include<conio.h>
void main()
{
float bp, da, hra, ta, tax, gs, pf, ns;
clrscr();
printf(“Enter the Basic Pay:”);
scanf(“%f”, &bp);
da = bp*20/100;
hra = bp*15/100;
ta = bp*10/100;
gs = bp+da+hra+ta;
pf = gs*3/100;
tax = gs*1/100;
ns = (gs+pf)-tax;

printf(“Basic Pay:%f”, bp);
printf(\nDA:%f”, da);
printf(\nHRA:%f”, hra);
printf(\nTA:%f”, ta);
printf(\nGross Pay:%f”, gs);
printf(\nPF:%f”, pf);
printf(\nTAX:%f”, tax);
printf(\nNet Salary:%f”, ns);
getch();
}

OPERATORS:
An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulators. C operator can be classified into the following categories.

1.       Arithmetic Operator
2.       Relational Operator
3.       Logical Operator
4.       Assignment Operator
5.       Increment & decrement Operators
6.       Conditional Operator

1.       ARITHMETIC OPERATORS:
C provides al, the basic arithmetic operators. They are +, -, *, / and % ie; addition, subtraction, multiple, division and modular division.

Modular division gives the reminder of division

Eg:
14 % 3 = 2

2.       RELATIONAL OPERATOR:
Often we compare 2 quantities and depending their relation take certain decision. These comparisons are done with the help of relational operators, which are

> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= not equal to

3.       LOGICAL OPERATOR:
C supports the 3 logical operators, namely ,
(i)                  Logical AND && gives true only if both the conditions are true

Eg: (a>b) && (a>v)

(ii)                Logical OR !! gives true if either of the condition true

Eg: (a>b) !! (a>v)

(iii)               Logical NOT ! gives true if input is false. False input is true

Eg: !(a>b)

4.       ASSIGNMENT OPERATOR:
Assignment operators are to assign the result of an expression to a variable. The assignment operator is =

Eg:          a = a+1;                                a+=1;
                a = a-1;                 a-=1;

5.       Increment and decrement Operator:
The increment operator (++) adds are to the operand while decrement operator (--) subtracts one.

Eg:          m = 10;
                ++m = 11;
                m++ = 11;
While m++ and ++m means the same thing when they form statement independently, they before differently when they are used in expression on the right hand side of m.

(a)    m = 5;
y = ++m;
                in this case, the value of in is assigned to y after incrementing the value of m. Hence the value of y and m will be 6.

(b)   m = 5;
y = m++;
Here, the value of m is first assigned to y and then incremented so m will be 6 and y will be 5.

PROGRAM: 7

Write a program to print two numbers, add, subtract, multiple and divide two numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
float a,b;
printf(:Enter the first Number:”);
scanf(“%f”, &a);
printf(:Enter the second Number:”);
scanf(“%f”, &b);
printf(“a+b gives:%f”, a+b);
printf(“a-b gives:%f”, a-b);
printf(“a*b gives:%f”, a*b);
printf(“a/b gives:%f”, a/b);
getch();
}

6.       CONDITIONAL OPERATOR:
Represented by ?: Its general form is

(expression1) ? (expression2) : (expression3)

If expression1 is true, expression2 is execute, otherwise if expression1 is false, expression3 is execute

Eg: number = (num1>num2) ? num1 : num2;

PROGRAM: 8

Write a program to check the biggest number among 2 numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(:Enter two Number:”);
scanf(“%d %d”, &a, &b);
if (a>b)
                printf(“%d is biggest”, b);
getch();
}

DECISION MAKING AND BRANCHING:
A ‘C’ program is a set of statements, which are normally executed sequentially in the order in which they appear. This involves a kind of decision making to see whether a particular condition has occurred or not, and then direct the computer to execute certain statements accordingly. These statement are known as control statements or decision making statement

The decision making statement in C are,

1.       if statement
2.       switch statement
3.       conditional operator statement

1.       IF STATEMENTS:
If statement, the powerful decision making statement in C, which is used to control the flow of execution of statements allows the computer to evaluate the expression first and then depending on whether the value of the expression is true or false, it transfers the control to a particular statement. The if statement may be implemented in different forms

a)      Simple if Statement

Syntax is,
if (test expression)
{
Statement 1;
}
Statement 2;

Statement 1 , may be a single statement or a group of statements. If the expression is true, the statement 1 will be executed, otherwise the statement 1 will be skipped and execution will jump to statement 2.

Eg:
                if (category = ‘sports’)
                {
                marks = marks + bonusmark;
                }
                printf(“%f”, marks);

b)      If… else statement:

Syntax is,
if (test expression)
{
Statement 1;
}
else
{
Statement 2;
}
If the test expression is true, then the statement1 immediately following the if statement are executed otherwise statement2 is executed. In either case statement1 or statement2 will be executed and not both of them.

c)       Nested if … else statement

General form is,
if(test expression1)
{
if(test expression2)
{
Statement1;
}
else
{
Satement2;
}
}
else
{
Satement3;
}

If the expression1 is false, statement1 will be executed otherwise it continues to perform the second test. If expression2 is true, Statement1 will be executed, otherwise statement2 will be executed and then the control is transferred to the next statement after the if else statement.

Eg:
if(a == b)
{
                If(a == c)
                {
                printf(“a and c are equal”);
                }
                else
                {
                printf(“a and c are nor equal”);
                }
                }
                else
                {
                if(b == c)
                {
printf(“b and c are equal”);
}
else
{
printf(“b and c are not equal”);
}


d)      else if ladder
There is another way of putting its together when multipath decision are involved

Syntax is,
if (condition1)
Statement1;
else if (condition2)
Statement2;
else if (condition3)
Statement3;
else
Default Statement;

Conditions are evaluated from the top of ladder, downwards. As soon as a true condition is found, the statement associated with it is executed and the control is transferred skipped the rest of the ladder. When all the n conditions become false, then the default statement will be executed.

PROGRAM: 11

Write a program to check whether a given candidate is eligible or not:

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
printf(“Enter the Age:”);
scanf(“%d”, &age);
if(age>=18)
printf(“Candidate is eligible”);
else
printf(“Candidate is not eligible”);
getch();
}


PROGRAM: 12

Write a program to find the biggest of 3 numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
printf(“Enter the three numbers:”);
scanf(“%d %d %d”, &a, &b, &c);
if((a>b) && (a>c))
printf(“%d is biggest”, a);
else if((b>a) && (b>c))
printf(“%d is biggest”, b);
else
printf(“%d is biggest”, c);
getch();
}

(OR)


PROGRAM: 13

Write a program to find the biggest of 3 numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, big;
printf(“Enter the three numbers:”);
scanf(“%d %d %d”, &a, &b, &c);
if(a>b)
big = a;
else
big = b;
if(c>big)
printf(“%d is the biggest number”, big);
getch();
}

(OR)


PROGRAM: 14

Write a program to find the biggest of 3 numbers:

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, big;
printf(“Enter the three numbers:”);
scanf(“%d %d %d”, &a, &b, &c);
big = a;
if(b>big)
big = b;
if(c>big)
big = c;
printf(“%d is the biggest number”, big);
getch();
}

SWITCH STATEMENT:
Switch statement tests the value of a given variable or expression against a list of case values and when a match is found, the block of statements associated with that case is executed. General form of the switch statement is,

Switch (expression)
{
Case value1:
Block1;
Break;
Case value2:
Block2;
Break;
…………………
…………………
Case value n:
Break;
default:
default block;
break;
}

Expression is an integer expression or even characters. Value1, value2 are constants or constant expression and are known as case labels.

When the switch is executed, the value of the expression is successfully compared against the values value1, value2 etc, It a case is found whose values matches with the values of the expression, then the block of statements that follow the case are executed. The break statement at the end of each block signifies the end of a particular case and causes the exit from switch statement transferring the control to the next statement after switch. Default is an optional case.

PROGRAM: 15

Write a program to check whether the given character is a vowel or not:

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
printf(“Enter the character:”);
scanf(%c”, &c);
switch(c)
case ‘a’:
printf(“The character is vowel a”);
break;
case ‘e’:
printf(“The character is vowel e”);
break;
case ‘i’:
printf(“The character is vowel i”);
break;
case ‘o’:
printf(“The character is vowel o”);
break;
case ‘u’:
printf(“The character is vowel u”);
break;
default:
printf(“%c is not a vowel ”, c);
break;
}
getch();
}

LOOPING STRUCTURES
In looping, sequences of statements are executed until some condition for termination of the loop are satisfied. A program loop therefore consist of 2 statements one known as the body of the loop and the other known as the control statement. A control segment test certain conditions and then directs the repeated execution of the statement contained in the body of the loop.

Depending on the position of the control statement in the loop a control structure may be either as the entry controlled loop or as the exit controlled loop. In the entry controlled loop, the control conditions are tested before the start of the loop execution. If the conditions are not satisfied, then the body of the loop will be executed. In the case of an exit controlled loop, the test is performed at the end of the body of the first time.

C language provides 3 loop constructs for performing loop operation.

1.       While Statement
2.       Do while statement
3.       For statement

1.       While statement:
Simplest of all the looping structure in C is the while statement

Syntax is,

while (test condition)
{
Body of the loop;
}

                While is an entry controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body, the test condition is once again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body of continues until the test condition finally becomes false and the control is transferred out of the loop. The body of the loop may have 1 or more statements.

Eg:

i = 1;
while (i<=3)
{
printf(“%d”, i);
i = i+1;
}

2.       do while statement
While loop construct makes a test of condition before the loop is executed. Therefore the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. On some occasions, it might be necessary to execute the body of the loop before the test is performed. Such situations can be handled with the help of do statement. Syntax is,

do
{
Body of the loop;
}
while(test condition);

First the body of the loop is evaluated and at the end of the loop, the test condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. When the condition becomes false, the loop will be terminated and the control goes to statement that appears immediately after do while statement do while construct is an exit controlled loop and therefore the body of the loop is executed atleast once.

Eg:

                i = 1;
                do
                {
                Printf(“%d”, i);
                i++;
                }
                while(i<=3);

3.       For loop
This entry controlled loop takes the form,

for (initialization; test condition; increment or decrement statement)
{
Body of the loop;
}

The execution of the for loop is as follows.

I.                    Initialization of the control variable is done first
II.                  Value of the control variable is tested using the test condition which is a relational expression that determines when the loop will exist. If the condition is true, the body of the loop is execution continues the loop is terminated and the execution continues with the statement that immediately follows the loop.
III.                When the body of the loop is executed the control is transferred back to the for statement after evaluating the last statement in the loop. The control variable is incremented or decremented and the  new value of the variable is again tested to see whether it satisfies the loop condition.
Eg:
for (x=0;x<=5;i++)
{
printf(“%d”, i);
}

PROGRAM: 16

Write the program to calculate the simple interest of 10 different value of P,N,R

#include<stdio.h>
#include<conio.h>
void main()
{
float p, r, si;
int t=0, n;
while(t<10)
{
printf(“Enter the Principal Amount:”);
scanf(“%f”, &p);
printf(“Enter the No. of Year:”);
scanf(“%f”, &n);
printf(“Enter the Rate of Interest:”);
scanf(“%f”, &r);
si = p*n*r/100
printf(“Simple Interest is %f”, si);
t++;
}
getch();
}


PROGRAM: 17

Write a program to convert the temperature from centigrade to Fahrenheit for different cities:

#include<stdio.h>
#include<conio.h>
void main()
{
float c, f;
int n = 1;
while(n<=15)
{
printf(“Enter the centigrade:”);
scanf(“%f”, &c);
f=(c*9/5)+32;
printf(“Fahrenheit:%f”, f);
n++;
}
getch();
}


PROGRAM: 18

Write a program to find the factorial of n numbers:
(Factorial Numbers are Ć  1! : 1, 2! : 2*1=2, 3! : 3*2*1 = 6 etc..)

#include<stdio.h>
#include<conio.h>
void main()
{
int f, i, n;
f = i = 1;
printf(“Enter the number:”);
scanf(“%d”, &n);
while(i<=n)
{
F*=1;
i++;
}
printf(“Factorial value is %d”, f);
getch();
}

PROGRAM: 19

Write a program to check whether a given no is Armstrong or not:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n, t, s=0, d;
printf(“Enter the number:”);
scanf(“%d”, &n);
t=n;
while(n!=0)
{
d = n%10;
s = s+pow(d,3); //use <math. h> header
n = n/10;
}
if (s == t)
printf(“%d is Armstrong”, t);
else
printf(“%d is not Armstrong”, t);
getch();
}


ARRAYS
An Array is group of related data items that share a common name. A list of items can be given one variable name using only one subscript and such a variable is called a single subscripted variable and the array as a one dimensional array. General form is

                Datatype variablename[size];
Datatype specifies the type of the elements that will be contained in the array such as int, float, char etc.,

Eg:
                float height[50];
                char name[10];
If we want to represent a set of 5 numbers by an array variable number, then we can declare the array variable as

                int number[10’;

Computer stores the values in 5 storage locations in its memory

                number[0];
                number[1];
                number[2];
                number[3];
                number[4];

Array variable can be initialized as int number[5] = [10,12,22,27,94];

PROGRAM-20
Write a program to input ‘n’ elements into an array and print the same.

#include<stdio.h>
#include<conio.h>
void main()
{
int I, n, a[50];
printf(“Enter the array size:”);
scanf(“%d”, &n);
printf(“Enter the array elements:”);
for(i=0; i<n; i++)
{
scanf(“%d”, &a[i];);
}
for(i=0; i<n; i++)
{
Printf(“&d”, a[i]);
}
getch();
}

PROGRAM-21
Write a program to print an array of ‘n’ elements in reverse order

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, a[50];
printf(“Enter the number of elements is array:”);
scanf(“%d”, &n);
for(i=0; i<n; i++)
{
scanf(“%d”, &a[i];);
}
for(i=n-1; i>=0; i--)
{
Printf(“&d”, a[i]);
}
getch();
}

PROGRAM-22
Write a program to find average marks obtained by 30 students in a class

#include<stdio.h>
#include<conio.h>
void main()
{
int i, m[50];
int total=0;
float avg;
printf(“Enter the marks of students:”);
for(i=0; i<30; i++)
scanf(“%d”, &m[i]);
for(i=0; i<30; i++)
{
Total=total+m[i];
}
avg=total/30;
print(Average &f”, avg);
getch();
}

PROGRAM-23
Write a program to find the biggest element from an array of ‘n’ elements.

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, big, a[50];
printf(“Enter the array size:”);
scanf(“%d”, &n);
big = a[0];
for(i=1; i<=n; i++)
{
if(a[i]>big)
big=a[i];
}
printf(“The Biggest no:%d”, big);
getch();
}

STRINGS
String is an array of Characters. Any group of characters defined between double quotation mark is a constant string. A string variable is a constant string. A string variable is any valid C variable name and is always declared as an array.

Syntax is
                char stringname[size];
Size determines the no of characters in the string.

Eg:
                char name[15];

When the compiler assigns a character string to a characters value, it automatically supplies a null character(10) at the end of the string. Therefore the results should be equal to the maximum no of characters +1

READING STRINGS
The input function scanf can be used with %s format to read in a string.

Eg:
                char name[15];
                scanf[“%s”, name);
scanf function determinates the input on the first white space it finds. In the case of characters arrays ampersand (&) sign is not required before the variable name.

WRITING STRINGS TO SCREEN
printf function with %s format is used to print the strings to screen

Eg:

#include<stdio.h>
#include<conio.h>
void main()
{
char a[]=”Emmess”;
int i=0;
while(i<=5)
{
Printf(“%c”, a[i]);
I++;
}
getch();
}

STRING HANDLING FUNCTIONS
C library supports a large no of string handling functions that can be used to carry out many of the string manipulations. Following are the most commonly used string function.

1.
Strcat()
Concatenates 2 strings
2.
Strcmp()
Compare 2 strings
3.
Strcpy()
Copies one string over another
4.
Strlen()
Finds the length of the string
strcat()
strcat function joins 2 strings together. The format is,

strcat(string1, string2);

string1 and string2 are character arrays when the functions strcat is executed.

strcmp()
strcmp function compares 2 strings identified by the arguments and a value, zero if they are equal. It they are not, it has the numeric difference between the first numeric characters in the strings. The format is strcmp(str1, str2);

strcpy()
strcpy function works like a string assignment operator. Format is

strcpy(str1, str2);
It assigns the contents of s2 to s1
Eg:
strcpy(name, “Salim”);

This statement will assign the string Salim to the string variable, name.

Eg:
strcpy(name1, name2);
This statement will assign the contents of string variable name2 to the string variable name1

strlen()
strlen function counts and returns the receives the value of the length of the string. Format is

N=strlen(stringname);
Here n is an integer variable which receives the value of the length of the string. The counting ends at the first all characters.

PROGRAM-24
Write a program to input a string and then print the reverse of the string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n, I;
char s[20];
printf(“Enter the string:”);
scanf(“%s”, s);
n=strlen(5);
for(i=n-1;i>=0;i--)
printf(“%c”, s[i]);
getch();
}

PROGRAM-25
S1, S2 and S3 are the three string variables. Write a program to read 2 strings. Constants S1 and S2 and compare whether they are equal or not. If they are not join them together. Then copy the contents of S1 to S3. At the end the program should print the contents of all the three variables and find their length

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n1, n2, n3, n4;
char s1[20], s2[20], s3[20];
scanf(“%s%s”, s1, s2);
n4=strcmp(s1, s2);
if(n4!=0)
strcat(s1, s2);
strcpy(s3, s1);
n1=strlen(s1);
n2=strlen(s2);
n3=strlen(s3);
printf(“Length of %s is %d”, s1, n1);
printf(“Length of %s is %d”, s2, n2);
printf(“Length of %s is %d”, s3, n3);
getch();
}

PROGRAM-26
Write a program to find the number of times character is present in a given string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0, k=0;
char a[20], b;
printf(“Enter the string:”);
do
{
i++;
scanf(“%c”, &a[i]);
}
While(a[i]!=’in’);
i--;
printf(“enter the character to be checked:”);
scanf(“%c”, &b);
while(i>=0)
{
if(a[i]==b)
{
k++;
}
i++;
}
Printf(“The chatacter %c is present %d times”, b, k);
getch();
}

PROGRAM-27
Write a program to input a line of text and count the number of vowels in it:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int c=0, i, n;
char s[30];
n=strlen(s1);
for(i=0;i<n);i++)
{
if((s[i]==’a’)|| ((s[i]==’e’)|| ((s[i]==’i’)|| ((s[i]==’o’)|| ((s[i]==’u’))
c=c++;
}
printf(“No of vowels in the line of text are:%d”, c);
getch();
}

PROGRAM-28
Write a program to check whether a given string is palindrome or not:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n, i, j=0;
char s1[40], s2[40];
printf(“Enter the string:”);
scanf(“%s”, s1);
n=strlen(s1);
for(i=n-1;i>=0;i--;j++)
{
s2[j]=s1[i];
}
S2[j]=’\o’;
If(strcmp(s1, s2)==0)
printf(“%s” is a palindrome”, s1);
else
printf(“%s” is not palindrome”, s1);
getch();
}

FUNCTIONS
C functions can be classified into 2 categories namely, library function and user defined function. Main is an example of user defined function. printf() and scanf() belong to the category of library functions. The main distinction between these 2 categories is that the library functions are not required to be written by the user where as a user defined function has to be developed by the user at time of writing a program. If a program is divided into functional parts, then each part may be independently coded and later combined into a single unit. These subprograms are much easier to understand, debug and test. Any C program contain atleast one function. In that case it must be the main function. There is no limit on the number of functions. Any function can be called from any other function. A function name is followed by a semicolon.

Form of C function
datatype functionname(argument list)
argument declaration;
{
local variable declaration;
statement part;
return (expression);
}

Function Name
A function must follow the same rules of formation as other variable name in C. Additional are must be taken to avoid duplicating the library rountines, names or operating system commands

Argument list
The argument list contains valid variable names, separated by commas. The list must be surrounded by parenthesis. No semicolon follows the closing parenthesis. The argument variable receive values from the calling function, thus providing a means of data communication from calling function to called function

RETURN VALUES AND THEIR TYPES
A function may or may not send back any value to the calling function. If it does, it is done through the return statement. While it is possible to pass to the called function any number of values, the called function can only return one value per call at the most. The return statement can take one of the following forms

return;

or

return (expression)
When a return statement is encountered the control immediately passed back to the calling function

Eg:
multi(x, y);
int x, y;
{
int p;
p=x * y;
return p;
}
A function may have more than one return statement. This situation arises when the value returned is based on certain conditions.

Eg:
if(x<=0)
return 0;
else
return 1;

All function by default return integer type data.

CALLING A FUNCTION
A function depending on whether the arguments are present or not and whether a value is retuned or not, may belong to one of the following categories.

1.       Functions with no argument and no return values
2.       Functions with arguments and no return values
3.       Functions with arguments and return values

a.       Functions with no argument and no return values:
When a function has no arguments, it does not receive any data from calling function. Similarly when it does not return a value, the calling function does not receive any from the called function. In effect, there is no data transfer between the calling and the called function.

b.      Functions with arguments and no return values
In this case, we could make the calling function to read data from the terminal and pass it on to the called function.

Eg:
printline(ch);
values(p, r, n);
Here, the arguments ch, p, r, n are called the formal arguments

Eg:
value(500, 0.12, 5);
The values 500, 0.12 and 5 are the actual arguments which becomes the values of the formal arguments inside the called function.

c.       Functions with arguments and return values
In this case, the called function receives data from the calling function and also it returns the value to the calling function

PROGRAM-29
Write a program to find the sum of 2 numbers. Using function. (Write the 3 category of function programs).
Category 1 (Function with no arguments and no return values)

#include<stdio.h>
#include<conio.h>
void sum();
void main()
{
sum();
}
void sum()
{
int a, b, s;
printf(“Enter the 2 numbers:”);
scanf(“%d”, &a, &b);
s=a+b;
printf(“Sum of the no is %d”, s);
getch();
}

Category 2 (Function with arguments and no return values)

#include<stdio.h>
#include<conio.h>
void sum(int, int);
void main()
{
int a, b;
printf(“Enter the two nos to be added:”);
scanf(“%d%d”, &a, &b);
sum(a,b);
}
void sum(int x, int y);
{
int p;
p=x+y;
printf(“Sum of two numbers is %d”, p);
getch();
}

Category 3 (Function with arguments and return values)

#include<stdio.h>
#include<conio.h>
void sum(int, int);
void main()
{
int a, b, s;
printf(“Enter the two nos to be added:”);
scanf(“%d%d”, &a, &b);
s=sum(a+b);
printf(“Sum of two numbers is %d”, s);
}
int sum(int x, int y)
{
int p;
p=x+y;
return p;
}

PROGRAM-30
Write a program to find the biggest of 3 nos. Using function

#include<stdio.h>
#include<conio.h>
void big(int, int, int);
void main()
{
int a, b, c, x;
printf(“Enter the three nos:”);
scanf(“%d%d%d”, &a, &b, &c);
x=big(a, b, c);
printf(“The biggest no:”%d”, x);
}
int big(int p, int q, int r)
{
int b;
b=p;
if(q>b)
b=q;
if(r>b)
b=r;
return b;
}

STRUCTURE
If we want to represent a collection of data items of different types using a single name, then we cannot use an array. C supports a constructed data type known as structure which is a method for packing data of different types. A structure is a convenient tool for handling a group of logically related data items.

Structure definition
A structure definition creates a format that may be used to declare structure variable

Eg:
Struct book
{
char title[20];
char author[15];
int pages;
float price;
};

The keyword struct declares a structure to hold the details of the four fields. These fields are called structure elements or members. Each member may belong to a different type of data. Book is the name of the structure and is called structure tag. The tag name may be used frequently to declare variables that have the tag structure. The general format of structure definition is an follows

Struct tagname
{
datatype member1;
datatype member2;
datatype member3;
--------------------------
--------------------------
--------------------------
};

The member of a structure are not variables and they do not occupy any memory until they are associated with a structure.

Giving values to members
The members should be linked to the structure variable in order to make them meaningful. For example the word title has no meaning where as the phrase ‘title of books’ has a meaning. The link between a member and a variable is established using the dot operator (.)

Eg:

struct book
{
char title[20];
char author[15];
int pages;
float price;
};
main()
{
struct book book3
book3.title = “Peace of Emmess”;
book3.author = “M.S.Salim”
book3.pages = 2015;
book3.price = 810.00;
}

Or else the input statement for the structure members can be written as,

scanf(“%s”, book3.title);
scanf(“%s”, book3.author);
scanf(“%d”, book3.pages);
scanf(“%d”, book3.price);

PROGRAM-31
Write a program to get name, regdno of a student and print it. Using structure

#include<stdio.h>
#include<conio.h>
struct stud
{
char name[20];
int regdno;
};
void main()
{
struct stud s1;
printf(“Enter the details\n”);
scanf(“%s%d”, s1.name, &s1.regdno);
printf(“%s%d”, s1.name, s1.regdno);
getch();
}

POINTERS
Whenever we declare a variable, the system allocates somewhere in the memory a location to hold the value of the variable. This location has its own address number. Such variable is, therefore nothing but a variable that contains an address which is a location of another variable in memory. Since the pointer is also a variable, its value is also stored in the memory in another location. For example, consider the statement
‘int quantity = 180;

This statement instructs the system to find a location for the integer variable quantity and puts the value 180 in that location. Let us assume that the system has chosen the address location 5000 for quantity. During the execution of the program, the system always associates the name quantity with the address 5000. We may have access to the value 180 by using either the name quantity or the address 5000. We can determine the address of a variable with the help of the operator &. For example, the statement,

P=&quantity;

Word assigns the address 5000 to the variable p. The operator & can be remembered as ‘address of’,

Declaring and initializing pointers
The declaration of a pointer variable takes the following form,

datatype * pointername;

This tells the compiler three things about the variable pointer name.

1.       The asterisk (*) tells the compiler that the variable pointer name is a pointer variable.
2.       Pointer name needs a memory location.
3.       Pointer name points to a variable of type data type.

int *p;

declares the variables p as a pointer variable that points to an integer data type. The type int refers to the data type of the variable being pointed to by a p and not the type of the value of the pointer. Similarly,

float *x;
declare x as a pointer be a floating point variable.

Accessing a variable through its pointer
The value of the variable can be accessed by using another unary operator asterisk (*), usually known as the indirection operator. Consider the following statements,

int quantity, *p, n;
quantity  = 180;
p = &quantity;
n = *p;

The first line declares quantity and n as integer variables and p as pointer variable pointing to an integer. The second line assigns the value 180 to quantity and the third line assign the address of quantity to the pointer variable p. When the operator * is placed before a pointer variable in an expression the pointer returns the value of the variable of which the pointer value is the address.
C allows us to add integer to or subtract integer from pointers as well as to subtract one pointer from another. If p1 and p2 are pointers, the expressions such as pi>p2, p1==p2 and p1!=p2 are allowed we may not use pointers in addition or multiplication. Similarly, two pointers cannot be added.

PROGRAM-32
Write a program using pointers to determine the length of a string.

#include<stdio.h>
#inlude<conio.h>
void main()
{
char *name;
int lengh;
char *cptr = name;
name = “ASARISTREET”;
while(*cptr!=’\o’)
{
printf(“%c\n”, *cptr);
cptr++;
{
Length = cptr-name;
printf(“\nLength of the String=%d\n”, Length);
getch();
}

FILES
Defining and operating a File
If we want to store data in a file in the secondary memory, we must specify certain things about the file to the OS. They include

         i.            File Name
       ii.            Data Structure
      iii.            Purpose

The general format for declaring or opening a file is

FILE *fp;
Fp = fopen(“filename”, “mode”);

The first statement declares the variable fp as a pointer to the data type FILE. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. The pointer which contains all the information about the file is subsequently used as a communication between the system and the program. The statement also specifies the purpose of opening the file. The mode does this job. Mode can be one of the following,

a)      r = Open the file reading only
b)      w = Open the file writing only
c)       a = Open the file for appending
data to it.

Both the filename and the mode are specified as strings. They should be enclosed in double quotation marks.

Program
Write a program to accept 10 students names and age, and write it into a file

#include<stdio.h>
void main()
{
FILE *fp;
char s[30], age I;
fp = fopen(“stud.txt”, ”w”);
for(i=0;i<10;i++)
{
scanf(“%s%d”, s, &age);
}
for(i=0;i<10;i++)
{
fprintf(fp, “%s%d”, s, age);
}
fclose(fp);
}
Emmess Info Tech