0% found this document useful (0 votes)
19 views

4 Codings (XII)

i. The basic structure of a C program includes preprocessor directives, header files, global declarations, function declarations, the main function, and subprograms. ii. The C preprocessor processes and modifies source code according to directives before compiling. Preprocessor directives begin with # and include header files. iii. Header files contain function definitions for library functions like input/output, strings, and math functions. They are included using #include.

Uploaded by

Bijay Kc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

4 Codings (XII)

i. The basic structure of a C program includes preprocessor directives, header files, global declarations, function declarations, the main function, and subprograms. ii. The C preprocessor processes and modifies source code according to directives before compiling. Preprocessor directives begin with # and include header files. iii. Header files contain function definitions for library functions like input/output, strings, and math functions. They are included using #include.

Uploaded by

Bijay Kc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Basic Structure of C Programming

Documentation Section /* Program to Calculate Area of Circle */ Suggested i. C Preprocessor:


It is a program that processes &
#include<stdio.h> Preprocessor Directive & modifies the source code according to
Link Section Mandatory directives supplied before it passes through
#include<conio.h> Header Files
the compiler.

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( );

for(i=0; i<3; i++) String Handling Functions:


{ 1. Strlen( ): A program to find the length of a string.
printf("\nEnter the Roll No : "); #include<stdio.h>
scanf("%d", &s[i].rollno); #include<conio.h>
fflush(stdin); #include<string.h>
printf("\nEnter the Full Name : "); Input
gets(s[i].fullname);
void main ( )
printf("\nEnter the Percentage : ");
scanf("%f", &s[i].percentage); {
printf("\n"); char string[20];
} int length;
clrscr( );
printf(“\nStudent Details : ”); printf("\nEnter a String : ");
for(i=0; i<3; i++) gets(string);
{
length=strlen(string);
printf("\nRoll No : %d",s[i].rollno);
printf("\nFull Name : %s", s[i].fullname); Output printf("\nThe number of characters in %s is %d", string, length);
printf("\nPercentage : %.2f", s[i].percentage); getch( );
printf("\n"); }
} Output:

getch( );

}
4
2. strrev( ): A program to reverse the given string. 3. strcpy( ): A program to copy one string into another string.

#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>

void main( ) void main( )


{ {
char string1[20], string2[20]; char string1[20], string2[20];
clrscr( ); int diff;
printf ("\nEnter First String : "); clrscr( );
gets(string1); printf ("\nEnter First String : ");
printf ("\nEnter Second String : "); gets(string1);
gets(string2); printf ("\nEnter Second String : ");
strcat (string1, string2); gets(string2);
printf ("\nWord after concatenating the two string is %s", string1); diff=strcmp(string1, string2);
getch( );
} if(diff<0)
{
Output: printf ("\n%s comes before %s & ASCII difference is %d", string1, string2, diff);
}

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>

void swap(int x, int y) void swap(int *x, int *y)


{ {
int temp; int temp;
temp=x; temp=*x;
x=y; *x=*y;
y=temp; *y=temp;
printf(“\nThe values within functions are: x=%d\ty=%d”, x, y); printf(“\nThe values within functions are: x=%d\ty=%d”, *x, *y);
} }

void main( ) void main( )


{ {
int a=47, b=74; int a=47, b=74;
clrscr( ); clrscr( );
printf(“\nBefore Function Call: a=%d\tb=%d”, a, b); printf(“\nBefore Function Call: a=%d\tb=%d”, a, b);
swap(a, b); /* Function call by value */ swap(&a , &b); /* Function call by reference */
printf(“\nAfter Function Call: a=%d\tb=%d”, a, b); printf(“\nAfter Function Call: a=%d\tb=%d”, a, b);
getch( ); getch( );
} }

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>

long int factorial(int n) void main( )


{ {
if(n= =0) int num,i;
return(1); long int fact=1;
else clrscr( );
return(n * factorial(n-1)); printf("Enter any positive number : ");
} scanf("%d", &num);

void main( ) if(num= =0)


{ {
int num; printf("The factorial of %d is %ld", num, fact);
clrscr( ); }
printf(“\nEnter any positive number : ”); else
scanf(“%d”, &num); {
printf(“\nThe factorial of %d is %ld”, num, factorial(num)); for(i=1; i<=num; i++)
getch( ); {
} fact=fact*i;
}
printf("The factorial of %d is %ld", num, fact);
Output: }
getch( );
}

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).

Fibonacci Series: #include<stdio.h>


4 7 11 18 29 47……… #include<conio.h>

#include<stdio.h> void main( )


#include<conio.h> {
int a=4, b=7, c, n, i;
int fibo(int n); /*function prototype*/ clrscr( );
printf("Enter the number of terms of Fibonacci Series : ");
void main( ) scanf("%d", &n);
{
int i, terms; for(i=0; i<n; i++)
clrscr( ); {
printf("\nEnter the number of terms of Fibonacci Series : "); printf("%d\t", a);
scanf("%d", &terms); c=a+b;
for(i=1; i<=terms; i++) a=b;
printf("%d\t", fibo(i)); b=c;
getch( ); }
} getch( );
}
int fibo(int n) /*function definition*/
{
if(n= =1)
return 4; Output:
else if(n= =2)
return 7;
else
return(fibo(n-1)+fibo(n-2));
}

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>

void main( ) void main( )


{ {
FILE *fp; FILE *fp;
char ch; char text[100];
clrscr( ); clrscr( );
fp=fopen("demo.txt", "w+"); fp=fopen("demo.txt", "w+");
printf("\nEnter any no. of character & press Enter to stop : "); fputs("We should never give up in life.", fp);
while ((ch=getchar( ))!='\n') rewind(fp);
{ fgets(text,100,fp);
fputc(ch, fp); printf("\nThe content from the file is: ");
} printf("\n%s", text);
rewind(fp); fclose(fp);
printf("\nThe content from the file is : \n"); getch( );
while((ch=fgetc(fp))!=EOF) }
{
printf("%c", ch);
}
fclose(fp);
getch( );
}

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>

void main( ) struct student


{ { int roll;
FILE *fp; char name[20];
int roll, i; float gpa;
char fname[20], lname[20]; }s[3];
float gpa;
void main( )
clrscr( );
{
fp=fopen("demo.txt", "w+");
FILE *fp;
int i;
for(i=0; i<3; i++) /* for 3 sets of data */ clrscr( );
{ fp=fopen("demo.txt", "w+");
printf("\nEnter the Roll No : "); for(i=0; i<3; i++)
scanf("%d", &roll); {
fflush(stdin); printf("Enter the Roll No : ");
printf("\nEnter the First Name : "); scanf("%d", &s[i].roll);
gets(fname); fflush(stdin);
printf("\nEnter the Last Name : "); printf("Enter the Full Name : ");
gets(lname); gets(s[i].name);
printf("\nEnter the GPA : "); printf("Enter the GPA : ");
scanf("%f", &gpa); scanf("%f", &s[i].gpa);
fprintf(fp, "\n%d %s %s %f", roll,fname,lname, gpa); printf("\n");
printf("\n"); }
} fwrite(&s, sizeof(s), 3, fp);
rewind(fp); rewind(fp);
printf("Student Details:"); fread(&s, sizeof(s), 3, fp);
while((fscanf(fp, "%d %s %s %f",&roll,fname,lname,&gpa))!=EOF) printf("\nStudent Details:");
{ for(i=0; i<3; i++)
{
printf("\nRoll No : %d", roll);
printf("\nRoll No : %d", s[i].roll);
printf("\nName : %s %s", fname, lname);
printf("\nName : %s", s[i].name);
printf("\nGPA : %f", gpa);
printf("\nGPA : %f", s[i].gpa);
printf("\n"); printf("\n");
} }
fclose(fp); fclose(fp);
getch( ); getch();
} }

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( );

/* Input Data Into the Array of Structure */


for(i=0; i<n; i++)
{
printf("\nEnter the Employee's Id : ");
scanf("%d", &e[i].emp_id);
fflush(stdin);
printf("\nEnter the Employee's Name : ");
gets(e[i].emp_name);
printf("\nEnter the Employee's Salary : ");
scanf("%ld", &e[i].emp_salary);
printf("\n");
}

/* 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.

#include<stdio.h> printf("\nEmployee Details : \n");


#include<conio.h>
/*Output Data From the Array of Structure*/
/* Structure Creation */ 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], temp; }
getch( );
void main( )
{ }
int i, j, n=3;
clrscr( );
/*Input Data Into the Array of Structure*/
for(i=0; i<n; i++)
{
printf("\nEnter the Employee's Id : ");
scanf("%d", &e[i].emp_id);
fflush(stdin);
printf("\nEnter the Employee's Name : ");
gets(e[i].emp_name);
printf("\nEnter the Employee's Salary : ");
scanf("%ld", &e[i].emp_salary);
printf("\n");
}

/* 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

You might also like