4 Codings (XII)
4 Codings (XII)
Definition Section #define PI 3.14 /* Defining Symbolic Constant */ Optional ii. Preprocessor Directive:
It is simply an instruction to the
Global Declaration Section float r; /* Declaring Global Variable */ Optional preprocessor & begins with ‘#’.
Example:
#include à Specifies a file to be included
Function Declaration Section float area_of_circle(float r); /* Function Prototype */ Optional in the program
#define à Defines a macro substitution &
symbolic constant
void main( )
{ iii. Header Files:
Main Function Section float area; /* Declaring Local Variable */
These are the files that contain the
1. Local Declaration Part clrscr( ); Mandatory function definition of library functions.
2. Executable Part area = area_of_circle(3.0); Example:
Executable
printf(“The area of circle is %f ”, area); stdio.h à Standard Input/Output Functions
} Part
printf( ), scanf( ) etc.
conio.h à Console Input/Output Functions
clrscr( ), getch( ) etc.
Sub Program Section string.h à String Handling Functions
/* Function Definition*/ strlen( ), strcpy( ) etc.
Function1 math.h à pow( ), sqrt( ), sin( ) etc.
float area_of_circle(float r)
Function2 { User-Defined Optional
return(PI * r * r); Function
………… }
…..
FunctionN
1
Programming in C:
There are four steps in writing program in C. Each step has its own /* A sample program to input 2 numbers & display the sum. */
importance and must be completed stepwise.
#include <stdio.h>
Step 1 Coding #include<conio.h>
.C void main( )
{
Step 2 Compiling int a,b,c;
.obj
clrscr( ); /* To clear the previous contents of the screen */
printf ("Enter the first number : ");
Step 3 Linking scanf("%d", &a);
printf ("\nEnter the second number : ");
.exe scanf("%d", &b);
Step 4 Executable File c=a+b;
printf("\nSum of %d and %d is %d.”,a, b, c);
getch( );
Step 1: }
At first, the program is written in C Compiler editor which is known
as source code. This source code can be saved as a program file with an The above C program asks any two numbers from the
extension .C. It can be edited at any time. keyboard and displays their sum. In this program, three variables
Step 2: a, b and c are used and their types are declared as integer (int).
The second step is to compile the source code. During compilation, Hence, we need to declare the type and name of the variables in
syntax error is checked and removed from the source code. After compiling, the beginning of the program.
the source code is changed into binary format and creates a new file with
the extension .obj. It is called object code which cannot be edited. Note: If we use int as a return type in main function, we have
Step 3:
to return an integer value at last unlike in the above program.
The third step is called linking process. In this process, the required i.e. int main( )
libraries are linked to the program. Libraries prepare an appropriate {
environment to execute the C program. ………..
return 0;
Step 4: }
After the linking process, an executable file is created with the
extension .exe. This executable file can be run in any other computer
without compiler.
2
Array: 2. Write a program to input ‘n’ numbers and sort them in descending order.
#include<stdio.h>
1. Write a program to input ‘n’ numbers and sort them in ascending order.
#include<conio.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
void main( )
int num[50],i,j,n,temp;
{ int num[50],i,j,n,temp;
clrscr( );
clrscr( );
printf("\nEnter the size of array not more than 50 : ");
printf("\nEnter the size of array not more than 50 : ");
scanf("%d",&n);
scanf("%d",&n);
printf("\nEnter the numbers : ");
printf("\nEnter the numbers : ");
for(i=0; i<n; i++)
for(i=0; i<n; i++)
{
{
scanf("%d",&num[i]);
Input scanf("%d",&num[i]);
}
}
for(i=0; i<n-1; i++)
{
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
{ Nested for-loop {
for(j=i+1; j<n; j++)
if(num[i]<num[j]) /*Less than Sign*/
{
{
if(num[i]>num[j])//*Greater than Sign*/
temp=num[i];
{
num[i]=num[j];
temp=num[i];
num[j]=temp;
num[i]=num[j]; Swapping }
num[j]=temp;
}
}
}
}
printf("\nThe numbers in descending order are : \n");
}
for(i=0; i<n; i++)
printf("\nThe numbers in ascending order are : \n"); {
printf("%d\t",num[i]);
for(i=0; i<n; i++) In Ascending: }
{ Smallest= num[0]
Output getch( );
printf("%d\t",num[i]);
Largest=num[n-1] }
}
getch(); In Descending:
} Smallest:
Output: printf(“\nThe smallest number is %d”, num[n-1] );
Largest:
printf(“\nThe largest number is %d”, num[0]);
3
Structure Array Implementation:
1. Write a program to input rollno, fullname & percentage of 3 students
and display them in proper format.
#include<stdio.h>
#include<conio.h>
struct student
{ Structure’s Output
int rollno;
char fullname[25]; Structure Creation
float percentage;
};
void main( )
{
struct student s[3]; /* Structure’s Array Variable Declaration */
int i;
clrscr( );
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
#include<string.h>
#include<string.h>
void main( )
void main( )
{
{
char string1[20], string2[20];
char string[20];
clrscr( );
clrscr( );
printf("\nEnter a String to be copied : ");
printf ("\nEnter a String : ");
gets(string1);
gets(string);
strcpy(string2, string1);
strrev(string);
printf("\nThe value of string2 after copy is %s", string2);
printf ("\nReverse of the given string is %s", string);
getch( );
getch( );
}
}
Output:
Output:
5
4. strcat( ): Aprogram to concatenate two strings into one string. 5. strcmp( ): A program to compare two strings.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<string.h> #include<string.h>
else if(diff>0)
{
printf ("\n%s comes after %s & ASCII difference is %d", string1, string2, diff);
}
else
{
printf ("\nBoth strings are same");
}
getch( );
}
Output 1:
6
Output 2: 7. strupr( ): A program to convert the given string into upper case.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ( )
Output 3: {
char string[20];
clrscr( );
printf("\nEnter a String : ");
gets(string);
printf("\nThe string in uppercase is %s", strupr(string));
getch( );
}
Output:
6. strlwr( ): A program to convert the given string into lower case.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
char string[20];
clrscr( );
printf("\nEnter a String : ");
gets(string);
printf("\nThe string in lowercase is %s", strlwr(string));
getch( );
}
Output:
7
Function Call by value: Function Call by reference:
Write a program to illustrate function call by value. Write a program to illustrate function call by reference.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
Output: Output:
8
Recursive Function:
1. Write a program to calculate Factorial using recursive function. 2. Write a program to calculate Factorial without using recursive function
Factorial: 4! à 4*3*2*1=24 (i.e. Using Loop or Iteration)
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
Output:
9
3. Write a program to display Fibonacci series up to nth terms using 4. Write a program to display Fibonacci series up to nth terms without
recursive function. using recursive function (i.e. Using Loop or Iteration).
Output:
10
File Handling:
1. Write & Read Character by Character using fputc( ) & fgetc( ) from the 2. Write & Read String or Block of strings(Sentences) using fputs() &
same program into the data file. fgets( ) from the same program into the data file.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
11
3. Write & Read Formatted Data (integer, float, character, string)
using fprintf() & fscanf() from the same program into the data file. 4. Write & Read the Record(Structure’s data) using fwrite() &
fread() from the same program into the data file.
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
12
Extra Questions from Structure:
i. Displaying records of the structure alphabetically based on string.
/* Output Data Using Array of Structure */
#include<stdio.h> printf("\nEmployee Details : \n");
#include<conio.h>
#include<string.h> /*To use String Function strcmp( ) */ for(i=0; i<n; i++)
{
struct employee printf("\nEmployee's Id : %d", e[i].emp_id);
{ int emp_id; printf("\nEmployee's Name : %s", e[i].emp_name);
char emp_name[20]; printf("\nEmployee's Salary : %ld", e[i].emp_salary);
long int emp_salary; printf("\n");
}e[3]; }
getch( );
void main( ) }
{
struct employee temp;
int i, j, n=3;
clrscr( );
/* Name(String) Sorting */
for(i=0; i<n-1; i++)
{ for(j=i+1; j<n; j++)
{
if(strcmp(e[i].emp_name, e[j].emp_name)>0)
{ temp=e[i];
e[i]=e[j];
e[j]=temp;
}
}
}
13
ii. Displaying records of the structure either in ascending or descending order
of specified numeric data.
/* Salary Sorting */
for(i=0; i<n-1; i++)
{ for(j=i+1; j<n; j++)
{ if(e[i].emp_salary>e[j].emp_salary) /* > is for Ascending Sort
& < is for Descending
Sort */
{ temp=e[i];
e[i]=e[j];
e[j]=temp;
}
}
}
14