2. 1
LAB MANUAL FOR
PROGRAMMING IN IP LAB
1st
Semester
Department of Computer Science
Prepared by
SAFAD ISMAIL
Asst.Professor, Dept of CSE
College of Applied Science Perissery ,IHRD
Chengannur-689126
3. 2
CS1142: C PROGRAMMING LAB
SYLLABUS
1. Familiarization of important DOS/Windows/Linux features
2. Practice on basic features of word processor, spread sheet and
presentation software.
Part A
The C laboratory work will consist of 25-30 Experiments 1-15.
Testing out and interpreting a variety of simple programs to demonstrate
the
syntax and use of the following features of the language: basic data types,
operators and control structures.
Part B
16. 1-D Arrays: A variety of programs to declare, initialise, read, print and
process 1-D arrays of various basic data types. Processing to include,
selection,
sum, counting, selective sum, selective counting, reversing etc.
17. Pointers: A large number of trivial programs involving all possible data
types to familiarize the syntax of pointers in a variety of situations and to
draw
memory diagrams based on the observations.
18. Structures: A variety of programs to declare, initialise, read, print and
process structures made up of a variety of data types and structures.
19. 2-D Arrays: A variety of programs to declare, initialise, read, print and
process 2-D arrays of various basic data types. Processing to include,
selection,
4. 3
sum, counting, selective sum, selective counting, reversing etc.
20. Array of Structures and Structure of Arrays: Programs to demonstrate
declaration and processing of structure of arrays and array of structures. 21.
Pointers to Arrays: A number of programs to demonstrate handling of 1-D
and
2-D arrays using pointers and to draw memory diagrams based on the
observations.
22. Pointers to Structures: A number of programs to demonstrate use of
pointers to structures and to draw memory diagrams based on the
observations.
23. Functions –I: Simple Examples of declaring and using functions of the
following categories (i)no argument, no return, (ii) argument, no return, (iii)
no
argument, return, (iv) argument, return, all pass by value
24. Functions –II: Declaring and using functions with pass by reference,
Passing
and Returning structures, Recursive functions.
25. Files: Simple Example involving use of multiple files: declaring, opening,
closing, reading from and writing to text files.
26. Files: Example involving use of multiple files: declaring, opening, closing,
reading from and writing to binary files.
27. Library functions: A variety of Examples demonstrating (i) string
processing
functions (ii) a variety of selected library functions
28. Debugging programs involving syntactic and/or logical errors
5. 4
29-30: Developing programming solutions to problems including program
design, algorithm development and data structure selection Developing
programming solutions to problems including program design, algorithm
development and data structure selection.
Syllabus link: https://www.keralauniversity.ac.in
6. 5
CONTENTS
Si:no Name of the Experiment Page no Date
Introduction to C 6-8
1 Sum of First N Natural Numbers 9
2 Greatest of three Numbers 10
3 Fibonacci Sequence Up to a Certain Number 11
4 Amstrong Number or Not 12
5 Reverse of a Number 13
6 Quadratic Equation 14
7 Write a program to find whether a character is
consonant or vowel using switch statement.
15
8 Simple Calculator using Switch 16
9 Sorting of an Array 17-18
10 Illustration of Linear Search in C 19-20
11 Illustration of Binary Search 21-22
12 Addition of Two Matrices 23-24
13 Multiplication of Two Matrices 25-26
14 Factorial of a number using Recursion 27
15 Swapping of two numbers using call by value 28
16 Swapping of two numbers using call by reference 29
17 Sum of elements in an array using pointers 30-31
18 String Handling Functions 32
19 Usage of Structure 33-37
20 Coping a file to another file 38
How to Write and Run a C Program in Linux
Step 1: Write a simple C program
$ gedit sampleProgram.c
Step 2: Compile the C program with gcc Compiler
$ gcc [programName].c -o programName
Step 3: Run the program
$ ./programName
7. 6
OBJECTIVES
1. To introduce students to the basic knowledge of programming
fundamentals of C language.
2. To impart writing skill of C programming to the students and solving
problems.
3. To impart the concepts like looping, array, functions, pointers, file,
structure.
COURSE OUTCOME
After completing this lab course you will be able to:
1. Understand the logic for a given problem.
2. Write the algorithm of a given problem.
3. Draw a flow chart of a given problem.
4. Recognize and understand the syntax and construction of C programming
code.
5. Gain experience of procedural language programming.
6. Know the steps involved in compiling, linking and debugging C code.
7. Understand using header files.
8. Learn the methods of iteration or looping and branching.
9. Make use of different data-structures like arrays, pointers, structures and files.
10. Understand how to access and use library functions.
11. Understand function declaration and definition.
12. Understand proper use of user defined functions.
13. Write programs to print output on the screen as well as in the files.
14. Apply all the concepts that have been covered in the theory course, and
15. Know the alternative ways of providing solution to a given problem.
INTRODUCTION ABOUT LAB
Steps involved in program development:-
To develop the program in high level language and translate it into machine level
language following steps have to be practised.
1. Writing and editing the program.
2. Linking the program with the required library modules.
3. Compiling the program.
4. Executing the program.
8. 7
ABOUT C LANGUAGE
C is a programming language developed by Dennis Ritchie at AT&T‟s BELL
Laboratory of USA in 1972. Because of its reliability, C is very popular. C is
highly portable & it is well suited for structured programming. C program
consists of collection of functions.
HISTORY OF C
The milestones in C's development as a language are listed below:
1. BCPL - a user friendly OS providing powerful development tools developed
from BCPL c. 1967. Assembler was tedious, long and error prone.
2. A new language “B” was attempted c. 1970 by Ken Thompson at Bell Labs.
3. UNIX was developed c. 1970 using “B” - DEC PDP-7 Assembly Language.
4. A totally new language “C”, a successor to “B” was developed c. 1971.
5. By 1973, UNIX OS was almost totally written in “C”.
Hardware Requirement: Desktop Computer / laptop computer.
Software Requirement: Linux Operating System with GCC / TURBO C in
WINDOWS OS / TURBO C++ in WINDOWS OS.
GCC
GCC is a Linux-based C compiler released by the Free Software Foundation
which is usually operated via the command line. It often comes distributed freely
with a Linux installation, so if you are running UNIX or a Linux variant you will
probably have it on your system. You can invoke GCC on a source code file
simply by typing:- gcc filename
The default executable output of GCC is "a.out", which can be run by typing
“./a.out”. It is also possible to specify a name for the executable file at the
command line by using the syntax “ -o outputfile” , as shown in the following
example : - gcc filename -o outputfile
Again, you can run your program with "./outputfile". (The ./ is there to ensure
you run the program for the current working directory.)
Note: If you need to use functions from the math library (generally functions
from “math.h” such as sin or sqrt), then you need to explicitly ask it to link with
that library with the “ –l ” flag and the library “m”: gcc filename -o outputfile -
lm
Turbo C/C++
Open Turbo C/C++ from your Desktop or Programs menu. Select “File” from
Menu bar and select option “New” and Save C program with filename „.C‟
extension. To do compiling – Select -> Compile from menu and click-> compile.
If the compilation is successful – you will see a “success” message. Else you will
9. 8
see the number of errors. To RUN the program – you may select ->Run from
menu and click -> Run Now you will see the output screen.
STRUCTURE OF “C‟ PROGRAM :
C program is a collection of several instructions where each instruction is written
as a separate statement. The C program starts with a main function followed by
the opening braces which indicates the start of the function. Then follows the
variable and constant declarations which are followed by the statements that
include input and output statements.
C program may contain one or more sections as shown below:
DOCUMENTATION SECTION
LINK SECTION
DEFINITION SECTION
GLOBAL DECLARATION SECTION
Main()
Function section
{
Declaration part
Executable part
}
SUBPROGRAM SECTION
User defined functions
SALIENT FEATURES OF C
C language has some characteristics that define the language and also have led
to its popularity as a programming language.
Small size.
Extensive use of function calls.
Structured language.
Low level (Bitwise) programming readily available.
Pointer implementation - extensive use of pointers for memory, array, structures
and functions.
It has high-level constructs.
It can handle low-level activities.
It produces efficient programs.
It can be compiled on a variety of computers
10. 9
EXPERIMENTS
**********************************************
1.Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
Output:
Enter an integer number
15
Sum of first 15 natural numbers = 120
11. 10
2.Greatest of Three Numbers
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
} else {
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
return 0;
}
Output:
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
12. 11
3.Fibonacci Sequence Up to a Certain Number
#include <stdio.h>
int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);
// displays the first two terms which is always 0 and 1
printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while (nextTerm <= n) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Output:
Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
13. 12
4.Amstrong Number or Not
#include <stdio.h>
int main()
{
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0)
{
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
// removing last digit from the orignal number
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Output:
Enter a three-digit integer: 371
371 is an Armstrong number.
14. 13
5. Reverse of a Number
#include <stdio.h>
int main()
{
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
Output:
Enter an integer: 2345
Reversed number = 5432
15. 14
6.Quadratic Equation
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart,
realPart, imagPart);
}
return 0;
}
Output:
Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
16. 15
7. Write a program to find whether a character is consonant or vowel
using switch statement.
#include <stdio.h>
void main()
{ char ch;
printf(“Enter any alphabet:”); //input alphabet from user
scanf(“%c”, &ch);
switch(ch)
{ case ‘a’:
case ‘A’:
printf(“Vowel”);
break;
case ‘e’:
case ‘E’:
printf(“Vowel”);
break;
case ‘I’:
case ‘I’:
printf(“Vowel”);
break;
case ‘o’:
case ‘O’:
printf(“Vowel”);
break;
case ‘u’:
case ‘U’:
printf(“Vowel”);
break;
default:
printf(“Consonant”);
}
}
17. 16
8. Simple Calculator using Switch
#include <stdio.h>
int main() {
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
Output:
Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
18. 17
9.Sorting of an Array
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Displaying elements of original array
printf("Elements of original array: n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
//Sort the array in ascending order
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
19. 18
printf("n");
//Displaying elements of array after sorting
printf("Elements of array sorted in ascending order: n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
Elements of original array:
5 2 8 7 1
Elements of array sorted in ascending order:
1 2 5 7 8
20. 19
10.Illustration of Linear Search in C
linear search, i.e
Start from the leftmost element of arr[] and one by one compare x with
each element of arr[]
If x matches with an element, return the index.
If x doesn’t match with any of elements, return -1.
Hint:
// C code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
Code:
#include <stdio.h>
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver code
int main(void)
{
21. 20
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, n, x);
(result == -1)
printf("Element is not present in array")
printf("Element is present at index %d;", result);
return 0;
}
Output:
Element is present at index 3
22. 21
11.Illustartion of Binary Search
Binary Search: Search a sorted array by repeatedly dividing the search interval
in half. Begin with an interval covering the whole array. If the value of the search
key is less than the item in the middle of the interval, narrow the interval to the
lower half. Otherwise narrow it to the upper half. Repeatedly check until the value
is found or the interval is empty.
Example :
// C program to implement recursive Binary Search
#include <stdio.h>
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
23. 22
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present in array")
: printf("Element is present at index %d",
result);
return 0;
}
Output:
Element is present at index 3
24. 23
12. Program to find sum of two matrices
#include<stdio.h>
#include<conio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("nEnter elements of 1st matrix:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("nSum of two matrices: n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("nn");
}
}
return 0;
}
25. 24
Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
Enter elements of 1st matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3
Sum of two matrices:
-2 8 7
26. 25
13.Matrice multiplication:
1. #include<stdio.h>
2. #include<stdlib.h>
3. int main(){
4. int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
5. system("cls");
6. printf("enter the number of row=");
7. scanf("%d",&r);
8. printf("enter the number of column=");
9. scanf("%d",&c);
10.printf("enter the first matrix element=n");
11.for(i=0;i<r;i++)
12.{
13.for(j=0;j<c;j++)
14.{
15.scanf("%d",&a[i][j]);
16.}
17.}
18.printf("enter the second matrix element=n");
19.for(i=0;i<r;i++)
20.{
21.for(j=0;j<c;j++)
22.{
23.scanf("%d",&b[i][j]);
24.}
25.}
26.printf("multiply of the matrix=n");
27.for(i=0;i<r;i++)
28.{
29.for(j=0;j<c;j++)
30.{
31.mul[i][j]=0;
32.for(k=0;k<c;k++)
33.{
34.mul[i][j]+=a[i][k]*b[k][j];
35.}
36.}
37.}
38.//for printing result
39.for(i=0;i<r;i++)
40.{
28. 27
14. Factorial of a number using Recurssion
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
Output:
Enter a positive integer: 6
Factorial of 6 = 720
29. 28
15. Swapping of two numbers using call by value
1. #include <stdio.h>
2. void swap(int , int); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %dn",a,b); // p
rinting the value of a and b in main
8. swap(a,b);
9. printf("After swapping values in main a = %d, b = %dn",a,b); // The v
alue of actual parameters do not change by changing the formal paramete
rs in call by value, a = 10, b = 20
10.}
11.void swap (int a, int b)
12.{
13. int temp;
14. temp = a;
15. a=b;
16. b=temp;
17. printf("After swapping values in function a = %d, b = %dn",a,b); // Fo
rmal parameters, a = 20, b = 10
18.}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
30. 29
16. Swapping of two numbers using call by reference
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %dn",a,b); // printing
the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %dn",a,b); // The values of
actual parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %dn",*a,*b); // Formal
parameters, a = 20, b = 10
}
31. 30
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
32. 31
17. Sum of elements in an array using pointers
#include <stdio.h>
void s(int* a, int len) {
int i, s_of_arr = 0;
for (i = 0; i < len; i++)
s_of_arr = s_of_arr + *(a + i);
printf( "sum of array is = %d" ,s_of_arr);
}
int main() {
int arr[] = { 1,2,4,6,7,-5,-3 };
s(arr, 7);
return 0;
}
Output:
Sum of array = 12
33. 32
18.String Handling Functions
function working
strlen() computes string's length
strcpy() copies a string to another
strcat() concatenates(joins) two strings
strcmp() compares two strings
strlwr() converts string to lowercase
strupr() converts string to uppercase
Code: strlen()
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','0'};
// using the %zu format specifier to print size_t
printf("Length of string a = %zu n",strlen(a));
printf("Length of string b = %zu n",strlen(b));
return 0;
}
Output:
Length of string a = 7
Length of string b = 7
Code: strcpy()
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20];
34. 33
// copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
return 0;
}
Output:
C programming
Code: strcat()
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";
// concatenates str1 and str2
// the resultant string is stored in str1.
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output:
This is programiz.com
programiz.com
Code: C strcmp()
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
35. 34
printf("strcmp(str1, str2) = %dn", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %dn", result);
return 0;
}
Output:
strcmp(str1, str2) = 1
strcmp(str1, str3) = 0
Hint:
strings str1 and str2 are not equal. Hence, the result is a non-zero integer.
strings str1 and str3 are equal. Hence, the result is 0.
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "COLLEGE OF APPLIED SCIENCE PERISSERY";
// converting the given string into lowercase.
printf("%sn",strlwr (str));
return 0;
}
Output:
College of applied science perissery
Code: strupr( )
37. 36
19.Usage of Structure
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %sn", Book1.title);
printf( "Book 1 author : %sn", Book1.author);
printf( "Book 1 subject : %sn", Book1.subject);
printf( "Book 1 book_id : %dn", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %sn", Book2.title);
printf( "Book 2 author : %sn", Book2.author);
printf( "Book 2 subject : %sn", Book2.subject);
printf( "Book 2 book_id : %dn", Book2.book_id);
return 0;
}
Output
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
38. 37
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
39. 38
20.Write a program to create a file called emp.rec and store information
about a person, in terms of his name, age and salary.
Code
#include <stdio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
/* open for writing */
fptr = fopen("emp.rec", "w");
if (fptr == NULL)
{ printf("File does not exists n"); return;
}
printf("Enter the name n");
scanf("%s", name);
fprintf(fptr, "Name = %sn", name);
printf("Enter the agen");
scanf("%d", &age);
fprintf(fptr, "Age = %dn", age);
printf("Enter the salaryn");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2fn", salary);
fclose(fptr);