Lab 1 sem
Lab 1 sem
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.
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);
#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
#include<stdio.h>
int main()
{
double first, second, temp;
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.
The format specifier "%[^\n]" tells to the compiler that read the characters until "\n" is not found.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
scanf("%[^\n]",name);
Output
Enter name: Alex Thomas
Name is: Alex Thomas
Explanation :
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;
}
#include <stdio.h>
int main( )
{
int c;
return 0;
}
Using gets(),puts()
#include <stdio.h>
int main( )
{
char str[100];
return 0;
}
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");
}
}
}
int main()
{
int i = 0; // declaration and initialization at the same time
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;
}
int main()
{
#include<stdio.h>
int main()
{
int i = 0; // declaration and initialization at the same time
return 0;
}
#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();
}
#include<stdio.h>
#include<conio.h>
// Enter number of terms 6 011235
if(num >= 2)
printf("%d\t%d",a,b);
#include <stdio.h>
int main()
{
int num; //Declare the nummber
printf("Enter the number\n");
scanf("%d",&num); //Initialize the number
return 0;
}
Output:
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;
int main()
{
int a[50], size, i, big, small;
return 0;
}
return 0;
}
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);
/*
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];
/*
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];
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]);
}
}
Output:
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;
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>
void main()
{
int a, b;
printf("Enter a number...");
scanf("%d", &a);
b = factorial(a); //calling the function named factorial
printf("%d", b);
}
return r;
}
# include<stdio.h>
# include<conio.h>
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);
swapbyref(&x,&y);
printf("\n After call by ref swap x value is:%d y value is :%d",x,y);
getch();
}
Output: find swapping not possible with swapbyvalue function, swapping possible with swapbyref
function
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
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");
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;
fpbw= fopen("bin2.exe", "wb"); // Opening file bin2.exe in “wb” mode for writing*/
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>
a = atoi(argv[1]);
b = atoi(argv[2]);
sum = a+b;
return 0;
}