For More Notes and questions log on to

INTRODUCTION TO C

C has emerged as the most widely used programming language for software development. Its features allow the development of well-structured programs. Most computers directly support its data types and control structures, resulting in the construction of efficient programs. It is independent of any particular machine architecture or operating system, which makes it easy to write portable programs. It is this contribution of rich control structure and data types, portability and conciseness that has contributed to the popularity of C.

History of C

C programming language is basically developed for UNIX Operating System. UNIX was developed in 1969 at bell telephone laboratories. It was entirely written on PDP 7 assembly language. After UNIX has been implemented Ken Thompson implemented a compiler for a new language called B used for transporting UNIX onto other machines. B was heavily influenced by BCPL (Basic Cambridge Programming Language) written for writing system software. B was latter modified by Dennis Ritchie who was also working at bell Labs. He named the successor C. Unix was later rewritten in C by Dennis Ritchie, Thompson and others by 1973.

C Program Structure

A basic fact about computer programming is that all programs can be written using a combination of only three control structures: Sequential, Selective and repetitive. The sequential structure consists of a sequence of program statements that are executed one after another in order, the selective structure consists of a test for a condition followed by alternative paths that the program can follow, and the repetitive structure consists of program statements that are repeatedly executed while some condition holds.

The sequential structure can be pictorially represented as follows

Entry

Statement 1

Statement 2

Statement 3

Exit

All C programs are made up of one or more functions, each performing a particular task. Every program has a special function named main. It is special because the execution of any program starts at the beginning of its main function.

A typical C program has following sections

  1. Preprocessor Directives
  2. Global Variable Declarations
  3. Functions

In a C program, preprocessor directive, if present, should come first followed by global variable definition if any.

Variable Declaration in C

  1. The variable can be 31 characters long.
  2. The variable can be any of a-z, A-Z, 0-9 and the underscore.
  3. Should not be a keyword.
  4. First character must be an alphabet
  5. The variable is case sensitive

Data Types

Every programming language has its own data type. The basic data types in C are

Int - an integer

Float – a single precision floating point number

Char - a character in C character set

Double – a double precision floating point number

Variables

Variables are data objects that are manipulated in a program. Information can be stored in a variable and recalled later. Variables must be declared before they can be used in a program.

Constants

A constant is an entity whose value does not change during program execution. Constants are of five different types

  1. Integer Constants
  2. Floating point Constants
  3. Character Constants
  4. String Constants

C Operators

The operators in C include

  1. Arithmetic
  2. Assignment
  3. Relational
  4. Increment and Decrement
  5. Bit
  6. Logical or Boolean
  7. Conditional expression

INPUT / OUTPUT

The important aspects of C programming language are its ability to handle input and output (I/O). A program using input / output functions must include the standard header file (stdio.h) in it using the directive.

Printf functions (CONIO.H, STDIO.H)

printf – sends formatted output to stdout

fprintf – sends formatted output to a stream

cprintf – sends formatted output to the text window on the screen

Scanf Function

Scanf - reads data from stdin

Fscanf – reads data from stream

The GETCHAR and PUTCHAR Function

Getchar, putchar (STDIO.h)

-getchar is a macro that gets a character from stdin

-putchar is a macro outputs a character on stdout

The GETCH and GETCHE Function

-getch gets a character from console but does not echo to the screen

-getche gets a character from console and echoes to the screen

gets, puts

gets() - gets a string from stdin

puts() – outputs a string to stdout

CONDITIONAL STATEMENTS

If (condition) Statement

When an if statement is encountered in a program, condition is evaluated, if its value is true, then the following statements are executed.

The if statement allows conditional execution of a group of statements.

If-else Statement

SYNTAX

If condition

Statement 1;

Else

Statement 2;

If the condition is true then statement 1 is executed else statement 2 is executed (if it exists). Else part is optional.

LOOPS IN C

WHILE LOOP

While loop provides the mechanism for looping as long as a specified condition is met. The while loop should be used in applications that do not require the modification of any variables at each iteration.

SYNTAX

While (condition)

Statements

The statement may be a single statement or a block of statements that is to be repeated. The condition may be any expression, with true being any non-zero value. The statements are executed while the condition is true. When the condition becomes false, program control passes to the line after the loop code.

FOR LOOP

This is used when the statements are to be executed more than once. This is the most widely used iteration construct. The for loop supported by C is much more powerful than its counterpart in any other programming language.

SYNTAX

For (exp1;exp2;exp3)

{

statements;

…………….

}

Generally exp1 is an initialization, exp2 is condition checking; exp3 is either an increment or decrement statement.

The initialization is usually an assignment statement that is used to set the loop control variable. The condition is a relational expression that determines when the loop will terminate. The increment determines how the loop control variable change each time the loop is repeated.

1. Write a C program to determine the sum of odd and even numbers.

# include<stdio.h>

# include<conio.h>

main()

{

int n, i, seven=0, sodd=0;

int a[25];

clrscr();

printf(:\n Enter the total number to be entered:”);

scanf(“%d”,&n);

printf(“\n Enter the values”);

for(i=0;i<n;i++)

{

if(a[ i]%2==0)

seven=seven+a[i];

else

sodd=sodd+a[I];

}

printf(“\n The Sum of Even number is %d”,seven);

printf(“\n The Sum of Odd number is %d”,sodd);

getch();

}

  1. Write a C program to count the number of positive, negative and zero number in the given list of numbers.

# include <stdio.h>

# include <conio.h>

main()

{

int n, i, npos=0, nneg=0, nzero=0;

int a[25];

clrscr();

printf(:\n Enter the total number to be entered:”);

scanf(“%d”,&n);

printf(“\n Enter the values”);

for(i=0;i<n;i++)

{

if(a[ i]>0)

npos=npos+1;

if(a[I]<0)

nneg=nneg+1;

else

nzero=nzero+1;

}

printf(“\n The number of positive value is %d”,npos);

printf(“\n The number of negative value is %d”,nneg);

printf(“\n The number of zeros is %d”,nzero);

getch();

}

3. Write a C program for temperature conversion.

#include<stdio.h>

#include<conio.h>

main()

{

int faren,cen;

clrscr();

printf(“\n Enter the farenheit value :”);

scanf(“%d”,&faren);

cen=(faren-32)+5/9;

printf(“\n The equivalent Centigrade value is %d”,cen);

getch();

}

4. Write a C program to check whether the number is prime or not.

#include<stdio.h>

#include<conio.h>

main()

{

int n, i;

clrscr();

printf(:\n Enter the total number to be entered:”);

scanf(“%d”,&n);

for(i=2;i<=n/2;i++)

{

if(n%i= =0)

printf(“\n the given number is not prime”);

break;

}

if(n%i)

printf(“\n the given number is prime”);

getch();

}

5. Write a C program to find whether the given number is palindrome or not.

#include<stdio.h>

#include<conio.h>

main()

{

int n, i;

int p,s,e;

clrscr();

printf(:\n Enter the number :”);

scanf(“%d”,&n);

e=0;

p=n;

while(p!=0)

{

s=p%10;

e=(e*10)+s;

p=p/10;

}

if(e= = n)

printf(“\n the given number is palindrome”);

else

printf(“\n the given number is not a palindrome”);

getch();

}

6. Write a C program to find the sum of digits.

#include<stdio.h>

#include<conio.h>

main()

{

int n,q,r,s=0;

clrscr();

printf(“\n Enter the no");

scanf(“%d”,&n);

while(n!=0)

{

q=n/10;

r=n-q*10;

s=s+r;

n=q;

}

printf(“\n the sum of digits :%d”,s);

getch();

}

7. Write a program to find whether the given number is perfect or not.

#include<stdio.h>

#include<conio.h>

main()

{

int a = 0;

int m;

printf(“Enter a number to check whether it is a perfect number or not \n”);

printf(“ Enter a number \n”);

scanf(“%ld”,&n);

for (m=0;m<n;m++)

{

if (n % m = = 0 )

a = a + m;

}

if (a = = n)

printf(“the given number is perfect number \n”);

else

printf(“the given number is not a perfect number \n”);

getch();

8.Write a program to find whether the given number is Armstrong or not.

#include<stdio.h>

#include<conio.h>

main()

{

int s = 0;

int c= 0;

int m,n,b;

printf(“Enter a number to check whether it is a perfect number or not \n”);

printf(“ Enter a number \n”);

scanf(“%ld”,&b);

n = b;

while (b>0)

{

c = b % 10;

s = s + (c*c*c);

b = b / 10;

}

if (s = = n)

printf(“the given number is armstrong number \n”);

else

printf(“the given number is not a armstrong number \n”);

getch();

}

9. Write a C program to find the given number using linear search method.

#include<stdio.h>

#include<conio.h>

main()

{

int n,a[30],sea,flag;

clrscr();

printf(“\n Enter the number of terms :”);

scanf(“%d”,&n);

printf(“\n Enter the values:”);

for(i=0;i<n;i++)

scanf(“%d”,a[i]);

printf(“\n Enter the number to be searched :”);

scanf(“%d”,&sea);

for(i=0;i<n;i++)

{

if(a[i] = = sea)

{

flag=1;

break;

}

else

flag=0;

}

if(flag= = 1)

printf(“\n The given number %d is present in the position number %d”,sea,i);

else

printf(“\n The given number is not present”);

getch(); }

10. Write a C program to find the given number using binary search method.

#include<stdio.h>

#include<conio.h>

main()

{

int n,a[30],sea,flag,x,y,t;

int low,high,mid;

clrscr();

printf(“\n Enter the number of terms :”);

scanf(“%d”,&n);

printf(“\n Enter the values:”);

for(i=0;i<n;i++)

scanf(“%d”,a[i]);

for(x=0;x<n-1;x++)

for(y=x+1;y<n;y++)

{

if(a[x]>a[y])

{

t=a[x];

a[x]=a[y];

a[y]=t;

}

}

printf(“\n The sorted numbers are :”);

for(x=0;x<n;x++)

printf(“%d\n”,a[x]);

printf(“\n Enter the number to be searched :”);

scanf(“%d”,&sea);

low=0;

high=n;

while(low<=high)

{

mid=(low+high)/2;

if(t<a[mid])

high=mid-1;

if(t>a[mid])

low=mid+1;

if(t= = a[mid])

{

printf(“\n the number %d is present in the position %d”,t,mid);

flag=0;

break;

}

if(mid = =1 | | mid= = n)

break;

}

if(flag)

printf(“\n The given number is not present”);

getch();

}

  1. Write a program to print fibonacci series using functions

#include <STDIO.H>

#include <CONIO.H>

void main()

{

int n;

void fibo(int);

clrscr();

printf(“\t\t PROGRAM TO PRINT THE FIBONACCI SERIES \n”);

printf(“\n Enter the number of terms to be in the series \n “);

scanf(“%d”,&n);

fibo(n);

getch();

}

void fibo(int num)

{

int I=1,ct,ft,st;

ft = 0;

st = 1;

printf(“\t %d \t %d”,ft,st);

while(I<=num-2)

{

ct = ft + st;

ft = st;

st = ct;

printf(“\t%d”,ct);

I++;

}

}

12. Program to perform the matrix additions

#include <stdio.h>

#include <conio.h>

void main()

{

int a[10][10],b[10][10],c[10][10],row,col,r,co,I,j,k;

clrscr();

printf(“\t\t Matrix Addition\n”);

printf(“Enter Row order of Matrix A : “);

scanf(“%d”,&row);

printf(“Enter Column order of Matrix A : “);

scanf(“%d”,&col);

printf(“Enter Row order of Matrix B : “);

scanf(“%d”,&r);

printf(“Enter Column order of Matrix B : “);

scanf(“%d”,&co);

if ((row!=r) || (col != co) )

{

printf(“Matrix Multiplication is impossible\n”);

getch();

}

else

{

printf(“Enter First Matrix Elements : “);

for (I=0;I<row;I++)

for (j=0;j<col;j++)

scanf(“%d”,&a[I][j]);

printf(“Enter Second Matrix Elements : “);

for (I=0;I<r;I++)

for (j=0;j<co;j++)

scanf(“%d”,&b[I][j]);

for (I=0;I<row;I++)

for (j=0;j<col;j++)

c[I][j] = a[I][j] + b[I][j];

printf(“The resultant matrix is \n”);

for (I=0;I<row;I++)

{

for (j=0;j<col;j++)

{

printf(“%t%d”,c[I][j]);

}

pritnf(“\n”);

}

}

13 . Program to print the factorial number

#include <stdio.h>

#include <conio.h>

void main()

{
int n;

clrscr();

printf(“program to print the factorial\n”);

printf(“\n\n Enter the number : “);

scanf(“%d”,&n);

factorial(n);

getch();

}

void factorial(double x)

{

double fact = 1;

for(I=1;I<=n;I++)

{

fact = fact * I;

printf(“The factorial of a given number is %d\n”,fact);

}

14. Program to implement the Tower of Hanoi

#include <stdio.h>

#include <conio.h>

void main()

{

void transfer(int,char,char,char);

int n;

clrscr();

printf(“\t\t TOWERS OF HANOI \n”);

printf(“How many disks ? “);

scanf(“%d”,&n);

transfer(n,’1’,’r’,’c’);

getch();

}

void transfer(int n, char from, char to, char temp)

{

if(n>0)

{

transfer(n-1,from,temp,to);

printf(“Move disk %d from %c to %c \n”,n,from,to);

transfer(n-1,temp,to,from);

}

return;

}

15. Program to count the number of vowels, consonants, digits, white space characters and

in a line of text using pointers

#include <stdio.h>

main()

{

char line[80];

int vowels = 0;

int cons = 0;

int digits = 0;

int ws = 0;

int other = 0;

void scan_line(char line[], int *pv, int *pc, int pd, int *pw, int *po);

printf(“Enter a line of text \n”);

scanf(“%[^\n],line);

scan_line(line, &vowels, &cons, &digits, &ws, &other);

printf(“%d %d %d %d %d”,vowels,cons,digits,ws,other);

return(0);

}

void scan_line(char line[], int *pv, int *pc, int *pd, int *pw,int *po)

{

char c;

int count = 0;

while((c = toupper(line[count])) != ‘\0’)

{

if (c = = ‘A’ | | c = = ‘E’ | | c = =’I’ || c = = ‘O’ || c = = ‘U’)

++ *pv;

else if (c > = ‘A’ & c < = ‘Z’)

++ *pc;

else if ( c > = ‘0’ & c < = ‘9’)

++ *pd ;

else if (c = = ‘ ‘ | | c = = ‘\0’)

++ *pw;

else

++ *po;

++ count;

}

return;

}

16.Program to implement to Floyds Triangle

#include <stdio.h>

void main()

{

int n,i,j,x=1;

clrscr();

printf("\t\t\tFloyds Triangle\n");

printf("\t\t\t======\n");

printf("Enter the no of Lines:");

scanf("%d",&n);

for(i=0;i<n;i++)

{

for(j=0;j<=i;j++)

{

printf("%4d",x);

x++;

}

printf("\n");

}

getch(); }

17.Program to implement to Pascal Triangle

#include <stdio.h>

void main()

{

int i=1,j,k,m,n;

clrscr();

printf("\t\t\tPascal Triangle\n");

printf("\t\t\t======\n");

printf("Enter the no of Lines:");

scanf("%d",&n);

for(j=0;j<n;++j)

{

for(k=35-2*j;k>0;k--)

printf(" ");

for(m=0;m<=j;++m)

{

if((m==0)||(j==0))

i=1;

else

i=(i*(j-m+1))/m;

printf("%4d",i);

}

printf("\n");

}

getch();

}

18. Program to implement sine series

#include <stdio.h>

#include <math.h>

void main()

{

float d,x,sum=0,fact(int);

int terms,sign=1,i;

clrscr();

printf("\t\t\t Sine Series \n");

printf("\t\t\t ======\n");

printf("\nEnter the X value:");

scanf("%f",&d);

printf("\nEnter the number of terms:");

scanf("%d",&terms);

x=3.14/180*d;

for(i=1;i<=terms;i+=2)

{

sum=sum+sign*pow(x,i)/fact(i);

sign=-sign;

}

printf("\nThe value of sine(%4.2f)is %8.4f",d,sum);

getch();

}

float fact(int n)

{

float f=1;

int i;

for(i=1;i<=n;++i)

f*=i;

return(f);

}

19. Programs on Manipulations on strings

#include <stdio.h>

void main()

{

int ch,i,j,l,m,sign,c,l1,k;

char name[80],name1[80],name2[80],namer[80],nameff[80],ans='y';

clrscr();

printf("\t\t\tManipulations on Strings\n");

printf("\t\t\t======\n");

printf("1.Concatenation\n");

printf("2.Reverse\n");

printf("3.Find\n");

printf("4.Replace\n");

printf("5.Length\n");

printf("Choice:");

scanf("%d",&ch);

switch(ch)

{

case 1:

{

printf("\t\tConcatenation\n");

printf("\t\t======\n");

printf("Enter the first string \n");

scanf("%s",name);

printf("Enter the second string \n");

scanf("%s",name1);

i=j=0;

while(name[i]!='\0')

{

name2[i]=name[i];

i++;

}

while(name1[j]!='\0')

{

name2[i]=name1[j];

i++;

j++;

}

name2[i]='\0';

printf("Resultant String in name2 is%s",name2);

break;

}

case 2:

{

printf("\t\tReverse\n");

printf("\t\t======\n");

printf("Enter the string \n");

scanf("%s",name);

i=j=0;

while(name[i]!='\0')

i++;

while(--i>=0)

name1[j++]=name[i];

name1[j]='\0';

printf("\nThe reversed String is%s",name1);

break;

}

case 3:

{

printf("\n\t\tFind\n");

printf("\t\t====\n");

printf("\nEnter first string:");

scanf(" %[^\n]",name);

printf("Enter search string:");

scanf(" %[^\n]",name1);

l=strlen(name);

l1=strlen(name1);

for(i=0;i<l;++i)

{

c=0;

if(name[i]==name1[c])

{

m=i;

sign=0;

while(name1[c]!='\0'&sign!=1)

{

if(name[m]==name1[c])

{

m++;

c++;

}

else

sign=1;

}

if(sign==0)

{

printf("The given string is present");

printf("\nIts starting position is%d",i+1);

exit(1);

k=-1;

}

}

if(k<0)break;

}

if(sign!=0)

printf("The given string is not present");

break;

}

case 4:

{

i=0;

j=0;

strcpy(nameff," ");

puts("Enter the string:");

scanf(" %[^\n]",name);

fflush(stdin);

puts("Enter find string");

scanf(" %[^\n]",name1);

fflush(stdin);

puts("Enter replace string:");

scanf(" %[^\n]",namer);

fflush(stdin);

l=strlen(name);

strcat(namer," ");

while(i<l)

{

j=0;

for(k=0;k<80;++k)

name2[k]=' ';

while(name[i]!=' '&name[i]!='\0')

{

name2[j]=name[i];

++i;

++j;

}

name2[j]='\0';

++i;

if((strcmp(name2,name1))==0)

{

strcat(nameff," ");

strcat(nameff,namer);

}

else

{

strcat(nameff," ");

strcat(nameff,name2);

}

}

puts("string after replacement");

puts(nameff);

break;

}

case 5:

{

i=0;

printf("Enter String:");

scanf(" %[^\n]",name);

while(name[i]!='\0')

i++;

printf("\nThe length of the given string is%d",i);

break;

}

}

getch();

}

Data Structures

An introduction to C:

Dennis Ritchie at AT & T Bell laboratory, Murray Hill, New Jersey, developed the programming language C in 1972. The languages BCPL and B mainly influenced it. It was named as C to present it as the successor of B language which was Designed earlier by Ken Thompson in 1970 for the first UNIX system on the DECPDP-7 Computer.

How to run C program:

  1. From the Ms Dos prompt start C by typing ‘tc’.
  2. Open a file by selecting File | Open | File name from the IDE menu. Or press

F3 Key

  1. Run the program by selecting Run | Run, Or press

Ctrl+F9 Key

  1. To see the program’s output select Window | User screen or press

Alt+F5 Key.

We may compile and run the programs from the Dos command

Line like tcc Filename <Enter>.

After the program is compiled, we may run it and view the output by typing

Filename <Enter>

Problem solving using computer:

To solve a problem using a computer, the following steps are required :

A program is developed using a high level programming language (program development)

The developed program is entered into a commuter (Program editing).

The edited program is translated and is produced as an executable machine code.

The Executable machine code is run in the computer to carry out the actual task (execution).

To implement the above steps, the programmer develops a program and the developed program is entered and edited with the help of an editor. Normally the editor is provided along with the compiler. After editing the program, the compilation commands us used for the translation process. Then the execution command is used to run the program to get the desired output.

Compilation:

High-level languages allow some English –like words and mathematical expressions that facilitate the better understanding of the logic involved in a program. High-level languages are machine independent. Since a computer system cannot follow programs written in a high language, high language programs are translated into low-level language programs and then executed.

Translation of a high-level language program to allow level language program is done by software known as Compiler. Object code is an intermediate code between the source code and the executable code.

Linking:

Linker performs the linking of libraries with the object code, to make the generated object code into an executable machine code. Thus the object code becomes an input to the linker, which produces an executable machine code. Sometimes programs are divided into modules and these modules are compiled separately and then linked by the linker and executed.

When running a program, the following files will be created automatically.

 OBJ (Object file)

 EXE (Executable file)

 Bak (Backup file)

 SWP (Swap file)

Data Structures Definition

Data Structure is a specialized format for storing data so that the data’s can be organized in an efficient way.

Classification

Primitive Non – Primitive

Example: -