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

Lab 1 sem

The document provides a comprehensive guide on using the C programming language, including setting up an IDE, compiling and running C programs, and understanding the compilation process. It outlines various programming exercises such as finding ASCII values, computing quotient and remainder, swapping numbers, and using control structures like if-else and switch statements. Additionally, it covers advanced topics like reading strings with spaces, using loops, and implementing functions for tasks like calculating factorials and generating Fibonacci series.

Uploaded by

aruneshbabu1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 1 sem

The document provides a comprehensive guide on using the C programming language, including setting up an IDE, compiling and running C programs, and understanding the compilation process. It outlines various programming exercises such as finding ASCII values, computing quotient and remainder, swapping numbers, and using control structures like if-else and switch statements. Additionally, it covers advanced topics like reading strings with spaces, using loops, and implementing functions for tasks like calculating factorials and generating Fibonacci series.

Uploaded by

aruneshbabu1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

C Language Lab Sheets

Day 1. Using an IDE - Turbo C - Compile and Run C Program

To compile and run a C language program, you need a C compiler. A compiler is a software that is used to
compile and execute programs. To set up a C language compiler in your Computer/laptop, there are two ways:
1. Download a full-fledged IDE like Turbo C++ or Microsoft Visual C++ or DevC++, which comes along
with a C language compiler.
2. Or, you can use any text editor to edit the program files and download the C compiler separately and
then run the C program using the command line.
Using an IDE - Turbo C
We will recommend you to use Turbo C or Turbo C++ IDE, which is the oldest IDE for C programming. It is
freely available over the internet and is good for a beginner.
Step 1: Open turbo C IDE(Integrated Development Environment), click on File and then click on New

Step 2: Write a Hello World program that we created in the previous article - C Hello World program.

Step 3: Click on Compile menu and then on Compile option, or press the keys press Alt + F9 to compile the
code.
Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs are first compiled to generate the
object code and then that object code is Run.

Step 5: Output is here.

Difference between Compile and Run in C?


Well, the compilation is the process where the compiler checks whether the program is correct syntax-wise,
and there are no errors in the syntax, and if the code is fine converts the C language source code into machine
understandable object code.
When we run a compiled program, it is just the already compiled code that is run.

Day 2. C Source Code Compilation Process

The C source code compilation process is a multi-step process, which involves pre-processing, compiling of
code, linking of libraries, etc.
The process of converting the source code written in any programming language(generally, mid-level or high-
level language) into machine-level language that is understandable by the computer is known as Compilation.
The software that is used for this conversion is known as a Compiler.
In C language, through compilation, the C language source code is converted into object code.

The compiler takes the input which is the source code and provides the output in the form of object code. The
complete process of compilation in the C language is further divided into four phases:
1. Pre-processing,
2. Compiling,
3. Assembling, and
4. Linking
The compiler checks the source code for any syntax or structural errors and after checking, if the source code
is found error-free, it then generates the object code that has an extension .obj (for Windows) or .o (for Linux).
Let us now take a look at the different stages of the compilation process in the C language.

Compilation Process

Like mentioned above, the different stages of the compilation process are as follows:
 Pre-processing
 Compiling
 Assembling
 Linking
In the flow chart below we have explained how the compilation process work and what are the different stages
of compiling the C language source code.

Let's discuss all these stages of the C language source code compilation in the order they are performed.
Step 0: Pre-processing of the source file
In this phase, pre-processing of the source file is done. The Pre-processor is a program that accepts the C
source code file and then it performs the following tasks:
 It will remove the comments from the source code.
 It will perform the Macro expansion if any Macro is used
 It will perform the expansion of the included header files.
Step 1: Preprocessor
It is a program that processes the source program before passing them on to the compiler. At this step the pre-
processors used in any C program are handled and the source code is made ready for compilation.
 Each preprocessing statement must start with #, where # is called the preprocessor directive.
 Each preprocessing directive is a single-line code statement.
 The word after # is called the preprocessor command.
Some of the preprocessor directives are as follows:
1. #include
To include a particular header using the name of the header file into the C language program code.
2. #define
This is used to define a MACRO in the C language.
3. #error
This preprocessor command is used to print the error message.
Just like the above three, there are many other preprocessors, we will cover them in detail after.
Hence, the preprocessor expands the source code(adds the required information) and then this expanded source
code is passed on to the compiler.
It gives the (.i) extension to the source code file which is initially with (.c) extension.
Step 2: Compiler
The expanded code by the preprocessor is then passed on to the compiler. As we know a compiler is a program
that converts the high-level language(or mid-level language) code to the assembly code, which is then
converted into the machine code, which the machine can understand.
Therefore the preprocessed code given by the preprocessor to the compiler is then converted into assembly code
by the compiler, which is then passed on to the Assembler.
The source file which got the (.i) extension in the previous step gets converted into (.s) extension by the
compiler.
Step 3: Assembler
The assembler converts the assembly code that it gets from the compiler into the object code. The extension of
the file in this step becomes (.obj).
Don't think that Assembler is a separate program generating the object code. The Assembler is part of the
compilation process of the C language source code.
When in laymen language, we say, the C code is compiled, it means the complete compilation process, covering
all these steps, is done.
Step 4: Linker
A linker is a tool that is used to link all the parts of a program together in order of execution. The code after
this stage becomes Executable Machine code.
There might be some situations when our program refers to the functions that are defined in some other files.
Or, if the code for some program is too big, we can break it into two files, which will be compiled separately
and then linked using the Linker.
In the C language compilation process, the Linker plays a very important role.
If your C program includes a header file, and you are using some function defined in that header file, then the
Linker will link the required object code for the function in the library, to the object code of your program
and package them together.
Similarly, if your program code is too big and you break it into two files, then both the source code files will be
converted into object code separately and then the Linker will link them and make the code ready for execution.
This is also called Separate Compilation.

Day 3.
program to find ASCII value of any input character

#include<stdio.h>
#include<conio.h>

int main()
{
printf("\n\n\t\t S V R M College, Nagaram - Best place to learn\n\n\n");

char c;
clrscr();
printf("Enter a character : ");
scanf("%c" , &c);
printf("\n\nASCII value of %c = %d",c,c);

printf("\n\n\t\t\tEnd of the program!\n\n\n");


return 0;
}

Program to Compute Quotient and Remainder

#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);

// Computes quotient
quotient = dividend / divisor;

// Computes remainder
remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);
return 0;
}

Day 4: write a program To Swap two numbers

#include<stdio.h>
int main()
{
double first, second, temp;

printf("Enter first number: ");


scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);

// value of first is assigned to temp


temp = first;

// value of second is assigned to first


first = second;

// value of temp (initial value of first) is assigned to second


second = temp;

// %.2lf displays number up to 2 decimal points


printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}

C program to read string with spaces using scanf() function


#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
scanf("%s",name);

printf("Name is: %s\n",name);


return 0;
}

Output (Enter name with space)


Enter name: Alex Thomas
Name is: Alex

In both cases variable name stored only "Alex"; so, this is clear if we read a string by using "%s" format
specifier, string will be terminated when white space found.

How to read string with spaces in C?

Read string with spaces by using "%[^\n]" format specifier

The format specifier "%[^\n]" tells to the compiler that read the characters until "\n" is not found.

Consider the program

#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
scanf("%[^\n]",name);

printf("Name is: %s\n",name);


return 0;
}

Output
Enter name: Alex Thomas
Name is: Alex Thomas

Explanation :

Here, [] is the scanset character.

^\n tells to take input until newline doesn’t get encountered.

Here we used ^ (XOR -Operator ) which gives true until both characters are different. Once the character is
equal to New-line (‘\n’), ^ (XOR Operator ) gives false to read the string. So we use “%[^\n]s” instead of “%s”.
So to get a line of input with space we can go with scanf(“%[^\n]s”,name);

#include <stdio.h>
int main()
{
char str[20];
gets(str);
printf("%s", str);
return 0;
}

Day 5: Using getchar(), putchar()

#include <stdio.h>
int main( )
{

int c;

printf( "Enter a value :");


c = getchar( );

printf( "\nYou entered: ");


putchar( c );

return 0;
}

Using gets(),puts()
#include <stdio.h>
int main( )
{

char str[100];

printf( "Enter a value :");


gets( str );

printf( "\nYou entered: ");


puts( str );

return 0;
}

Using ternary operator:


#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int max = a > b ? a : b;
cout << "Maximum value = " << max << "\n";
return 0;
}

Day 6: Using if..else


#include <stdio.h>

void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}

Using else if :
#include <stdio.h>

void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}
Day 7: using switch statement
#include<stdio.h>
void main( )
{
int a, b, c, choice;
while(choice != 3)
{
/* Printing the available options */
printf("\n 1. Press 1 for addition");
printf("\n 2. Press 2 for subtraction");
printf("\n Enter your choice");
/* Taking users input */
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter 2 numbers");
scanf("%d%d", &a, &b);
c = a + b;
printf("%d", c);
break;
case 2:
printf("Enter 2 numbers" );
scanf("%d%d", &a, &b);
c = a - b;
printf("%d", c);
break;
default:
printf("you have passed a wrong key");
printf("\n press any key to continue");
}
}
}

Using while : printing 100 natural numbers


#include<stdio.h>

int main()
{
int i = 0; // declaration and initialization at the same time

printf("\nPrinting numbers using while loop from 0 to 9\n\n");

while(i<100)
{
printf("%d\n",i);

/*
Update i so the condition can be met eventually
to terminate the loop
*/
i++; // same as i=i+1;
}

return 0;
}

Day 8:Using Do..while


#include<stdio.h>

int main()
{

int i = 10; // declaration and initialization at the same time

do // do contains the actual code and the updation


{
printf("i = %d\n",i);
i = i-1; // updation
}
// while loop doesn't contain any code but just the condition
while(i > 0);

printf("\n\The value of i after exiting the loop is %d\n\n", i);


return 0;
}
Using for Loop

#include<stdio.h>

int main()
{
int i = 0; // declaration and initialization at the same time

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


{
printf("i = %d\n", i);

printf("\n\The value of i after exiting the loop is %d\n\n", i);

return 0;
}

Day 9: Program to print Factorial of a Number

#include<stdio.h>
#include<conio.h>
void main()
{
int fact, i, n;
fact = 1;
printf("Enter the number\t");
scanf("%d" , &n);
for(i = 1; i <= n; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d", n , fact);
getch();
}

Program to print the Fibonacci Series

#include<stdio.h>
#include<conio.h>
// Enter number of terms 6 011235

void fibonacci(int num);


void main()
{
int num = 0;
clrscr();
printf("Enter number of terms\t");
scanf("%d", &num);
fibonacci(num);
getch();
}
void fibonacci(int num)
{
int a, b, c, i = 3;
a = 0;
b = 1;
if(num == 1)
printf("%d",a);

if(num >= 2)
printf("%d\t%d",a,b);

while(i <= num)


{
c = a+b;
printf("\t%d", c);
a = b;
b = c;
i++;
}
}

Day 10: Program to check prime no or not

#include <stdio.h>

int main()
{
int num; //Declare the nummber
printf("Enter the number\n");
scanf("%d",&num); //Initialize the number

int count=0; //Declare a count variable

for(int i=2;i<num;i++) //Check for factors


{
if(num%i==0)
count++;
}
if(count!=0) //Check whether Prime or not
{
printf("Not a prime number\n");
}
else
{
printf("Prime number\n");
}

return 0;
}

Write a program to print a table using goto statement:


#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10) goto table;
}

Output:

Enter the number whose table you want to print?10


10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Day 11: Write a program to print the given number is palindrome or not :
#include<stdio.h>
#include<conio.h>

void main()
{
int a, b, c, s = 0;
clrscr();
printf("Enter a number:\t");
scanf("%d", &a);
c = a;

// the number is reversed inside the while loop.


while(a > 0)
{
b = a%10;
s = (s*10)+b;
a = a/10;
}

// here the reversed number is compared with the given number.


if(s == c)
{
printf("The number %d is a palindrome", c);
}
else
{
printf("The number %d is not a palindrome", c);
}
getch();
}
Output: Enter the number: 121
The number 121 is a palindrome
Write a program to print the biggest and smallest no in an given array:
#include<stdio.h>

int main()
{
int a[50], size, i, big, small;

printf("\nEnter the size of the array: ");


scanf("%d", &size);

printf("\n\nEnter the %d elements of the array: \n\n", size);


for(i = 0; i < size; i++)
scanf("%d", &a[i]);

big = a[0]; // initializing


/*
from 2nd element to the last element find the bigger element than big and
update the value of big
*/
for(i = 1; i < size; i++)
{
if(big < a[i]) // if larger value is encountered
{
big = a[i]; // update the value of big
}
}
printf("\n\nThe largest element is: %d", big);

small = a[0]; // initializing


/*
from 2nd element to the last element find the smaller element than small and
update the value of small
*/
for(i = 1; i < size; i++)
{
if(small>a[i]) // if smaller value is encountered
{
small = a[i]; // update the value of small
}
}
printf("\n\nThe smallest element is: %d", small);

return 0;
}

Day 12: Write a program to sort an array:


#include <stdio.h>
int main()
{
int i, j, temp, n, arr[30];
printf("Enter the number of elements in your array: \n");
scanf("%d", &n);
printf("Enter the array elements: \n");
for (i = 0; i < n; ++i)
scanf("%d", &arr[i]);

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


{
for (j = i + 1; j < n; ++j)
{
if (arr[i] > arr[j])
//to check if current element greater than i+1th element, if yes; perform swap.
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

printf("\nThe array sorted in ascending order is as given below: \n");


for (i = 0; i < n; ++i)
printf("%d\n", arr[i]);

return 0;
}

Write a program to add/subtract two matrices


#include<stdio.h>

int main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);


for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &second[c][d]);

/*
printing the first matrix
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}

/*
printing the second matrix
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}

/*
finding the SUM of the two matrices
and storing in another matrix sum of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];

// printing the elements of the sum matrix


printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}

/*
finding the DIFFERENCE of the two matrices
and storing in another matrix difference of the same size
*/
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];

// printing the elements of the diff matrix


printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}

Output:
Day 13: Matrix multiplication program in C -extra

In matrix multiplication first matrix one row element is multiplied by second matrix all column elements.

Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices by the figure given below:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output:

enter the number of row=3


enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the figure given below:
Day 14: Write a c program to reverse the given string
#include <stdio.h>
int main()
{
char str[100], rev[100];
int i, j, count = 0;
scanf("%s", str);
printf("\nString Before Reverse: %s", str);
//finding the length of the string
while (str[count] != '\0')
{
count++;
}
j = count - 1;

//reversing the string by swapping


for (i = 0; i < count; i++)
{
rev[i] = str[j];
j--;
}

printf("\nString After Reverse: %s", rev);


}

Hello String Before Reverse: Hello

String After Reverse: olleH

Write a program to find biggest no using functions


#include<stdio.h>

int greatNum(int a, int b); // function declaration

int main()
{
int i, j, result;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
result = greatNum(i, j); // function call
printf("The greater number is: %d", result);
return 0;
}
int greatNum(int x, int y) // function definition
{
if(x > y)
{
return x;
}
else
{
return y;
}
}

Day 15: Write a program to print factorial of given number using Recursion
#include<stdio.h>

int factorial(int x); //declaring the function

void main()
{
int a, b;

printf("Enter a number...");
scanf("%d", &a);
b = factorial(a); //calling the function named factorial
printf("%d", b);
}

int factorial(int x) //defining the function


{
int r = 1;
if(x == 1)
return 1;
else
r = x*factorial(x-1); //recursion, since the function calls itself

return r;
}

write a C program to swap two numbers using Functions


show the difference between call by value and call by reference */

# include<stdio.h>
# include<conio.h>

void swapbyvalue(int a, int b); /* call by value prototype*/


void swapbyref(int *a, int *b); /* call by reference prototype */

void main (void)


{
int x,y;

clrscr();
printf("enter value for x and y :"); scanf("%d%d",&x,&y);
printf("\n Before swap x value is:%d y value is :%d",x,y);

/* Call by value Function call*/


swapbyvalue(x,y);
printf("\n after call by value swap x value is:%d y value is :%d",x,y);

/* Call by Reference Function call */

swapbyref(&x,&y);
printf("\n After call by ref swap x value is:%d y value is :%d",x,y);

getch();
}

void swapbyvalue(int a, int b) /* Call by value Function body */


{
int c;
c=a;
a=b;
b=c;
printf("\n In call by value Function swap x value is:%d y value is :%d",a,b);
}

void swapbyref(int *a, int *b) /* call by reference Function body */


{
int *c=NULL;
*c=*a;
*a=*b;
*b=*c;
}

Output: find swapping not possible with swapbyvalue function, swapping possible with swapbyref
function

Day 16: Write a Program to Store Information of Students Using Structure


#include <stdio.h>
struct student
{
char Name[50];
int roll;
float marks;
} s[5];

int main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].Name);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].Name);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}

Output

Enter information of students:

For roll number1,


Enter name: sri
Enter marks: 98
.
.
.
Displaying Information:

Roll number: 1
Name: sri
Marks: 98
.
.
.

Day 17: Write a program to read and write individual characters to a file.
#include<stdio.h>

int main()
{
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
while( (ch = getchar()) != EOF)
{
putc(ch, fp);
}
fclose(fp);

fp = fopen("one.txt", "r");

while( (ch = getc(fp)! = EOF)


printf("%c",ch);

fclose(fp); // closing the file pointer

return 0;
}

Write a program to Read and Write to File using fprintf() and fscanf()
#include<stdio.h>

struct emp
{
char name[10];
int age;
};

void main()
{
struct emp e;
FILE *p,*q;
p = fopen("one.txt", "a");
q = fopen("one.txt", "r");
printf("Enter Name and Age:");
scanf("%s %d", e.name, &e.age);
fprintf(p,"%s %d", e.name, e.age);
fclose(p);

do
{
fscanf(q,"%s %d", e.name, e.age);
printf("%s %d", e.name, e.age);
}
while(!feof(q));
}

Day 18: Write a program to demonstrate Reading and Writing Binary Files in C
#include <stdio.h>
int main()
{
char ch;

FILE *fpbr, *fpbw; // Pointers for both binary files

fpbr = fopen("bin1.exe", "rb"); // Open for bin1.exe file in rb mode

if (fpbr == NULL) /* test logic for successful open*/


{
puts("Input Binary file is having issues while opening");
}

fpbw= fopen("bin2.exe", "wb"); // Opening file bin2.exe in “wb” mode for writing*/

if (fpbw == NULL) /* Ensure bin2.exe opened successfully*/


{
puts("Output binary file is having issues while opening");
}

while(1) /*Read & Write Logic for binary files*/


{
ch = fgetc(fpbr);
if (ch==EOF)
break;
else
fputc(ch, fpbw);
}
/* Closing both the binary files */
fclose(fpbr);
fclose(fpbw);

return 0;
}

Day 19 : Write a Program to find the sum of two integer numbers using command line arguments in C

#include <stdio.h>
#include <conio.h>

int main(int argc, char *argv[])


{
int a,b,sum;
clrscr();
if(argc!=3)
{
printf("please use \"prg_name value1 value2 \"\n");
return -1;
}

a = atoi(argv[1]);
b = atoi(argv[2]);
sum = a+b;

printf("Sum of %d, %d is: %d\n",a,b,sum);

return 0;
}

Run: on command prompt: ex_pro_name 10 20

You might also like