C Prog Lab Programs
C Prog Lab Programs
Lab 3:
Declare variables of different data types (signed char, unsigned char, short, unsigned
short, int, unsigned, long, unsigned long, float, double) and print their sizes using
sizeof.
Set breakpoints and use GDB to inspect the values of these variables.
● Use GDB commands (print, x) to display the content in decimal and hexadecimal
formats.
#include<stdio.h>
int main()
{
/* Print size of different data types in bytes */
// declare variables
int n;
unsigned int p;
float l;
double m;
char x;
unsigned char y;
unsigned long k;
long q;
// print size (Result is unsigned int so %u or %d)
printf("Size of Integer: %u Bytes\n",sizeof(n)); // or sizeof(int)
printf("Size of Unsigned Integer: %u Bytes\n",sizeof(p));
printf("Size of Float: %u Bytes\n",sizeof(l));
printf("Size of Double: %u Bytes\n",sizeof(m));
printf("Size of Character: %u Bytes\n",sizeof(x));
printf("Size of Unsigned Character: %u Bytes\n",sizeof(y));
printf("Size of Unsigned Long: %u Bytes\n",sizeof(k));
printf("Size of Long: %u Bytes\n",sizeof(q));
return 0;
}
Lab 4:
(a) Write a C program that declares variables of different types, uses both single-line
and multi-line comments, and prints their values. Additionally, declare a constant and
print its value.
(b) Write a C program that uses scanf to read an integer, a float, and a character from
the user. Then, use printf to display these values with appropriate format specifiers.
#include<stdio.h>
int main()
{
/* declare and
initialize variables */
signed char c = -'C';
unsigned char uc = '2';
unsigned int uin = 15; //or unsigned short uin = 15;
int in = -30; //or short in = -30; // or signed int in = -30
unsigned long ulo = 429496; // max value = (2^32-1)
long lo = -214748; // max value (-2^31 to 2^31-1)
float fl = 345.235692;
double db = 345.2356928674;
/* declare constants */
const int cin = 1234;
const float cfl = 3.141592;
/* print entries */
printf("\nSigned char entry: %c\n",c);
printf("Unsigned char entry: %c\n",uc);
printf("Unsigned int entry: %u\n",uin);
printf("Unsigned int entry (in octal): %o\n",uin);
printf("Unsigned int entry (in hex): %X\n",uin);
printf("Signed int entry: %d\n",in); // or %i
printf("Unsigned long entry: %lu\n",ulo);
printf("Signed long entry: %ld\n",lo);
printf("Float entry (single precision): %f\n",fl);
printf("Float entry (double precision): %lf\n",db);
printf("Float entry (double, 10 digits precision): %.10lf\n",db);
return 0;
}
#include<stdio.h>
int main()
{
int n;
float l;
char x;
printf("Enter a character\n");
scanf("%c",&x);
printf("Entered character: %c\n",x);
printf("Enter an integer\n");
scanf("%d",&n);
printf("Entered integer: %d\n",n);
printf("Enter a floating point number\n");
scanf("%f",&l);
printf("Entered floating point number: %.4f\n",l);
return 0;
}
Lab 5:
(a) Write a C program that demonstrates the use of assignment and arithmetic
operators. Perform and display the results of addition, subtraction, multiplication,
division, and modulus operations on two integer variables.
(b) Write a C program that uses if-else and cascading if-else constructs to check if a
given integer is positive, negative, or zero. Print the appropriate message based on the
condition.
#include<stdio.h>
int main()
{
/*Operations on two integers*/
int a=3,b=5;
printf("Operations on two integers:\n");
printf("%d+%d = %d\n",a,b,a+b);
printf("%d-%d = %d\n",a,b,a-b);
a += 4; /* a = a+4 */
printf("%d*%d = %d\n",a,b,a*b);
printf("%d/%d = %d\n",a,b,a/b);
printf("%d%c%d = %d\n",a,'\%',b,a%b);
#include<stdio.h>
int main()
{
int z;
printf("Enter any integer\n");
scanf("%d",&z);
if(z>0){
printf("Positive number");
}
else if (z<0){
printf("Negative number");
}
else{
printf("Zero");
}
return 0;
}
Lab 6:
(a) Write a C program that functions as a basic calculator. The program should
prompt the user to enter two numbers and an operator (+, -, *, /). Use a switch
statement to perform the corresponding arithmetic operation based on the operator
provided by the user. Display the result of the operation.
(b) Write a C program that calculates the sum of the first n natural numbers using a
for loop.
#include<stdio.h>
int main()
{
int a,b;
char c;
printf("Enter operator\n");
scanf("%c",&c);
printf("Enter two integers\n");
scanf("%d %d",&a,&b);
switch(c){
case '+': printf("\n%d",a+b);
break;
case '-': printf("\n%d",a-b);
break;
case '*': printf("\n%d",a*b);
break;
case '/': if(b!=0)
printf("\n%d",a/b);
//for floating point use cast: printf("\n%f",a/(float)b);
else
printf("Error: Division by zero is not allowed.\n");
break;
default: printf("\nWrong entry");
break;
}
return 0;
}
#include<stdio.h>
int main()
{
int z,num,sum=0;
printf("Enter a number\n");
scanf("%d",&num);
for(z=0;z<=num;z++){
sum = sum+z;
}
printf("Sum of first %d natural numbers= %d\n",num,sum);
return 0;
}
Lab 7:
(a) Write a C program to calculate the factorial of a given number using a while loop.
(b) Write a C program that prints the multiplication table of a given number using
nested loops.
#include<stdio.h>
int main()
{
#include <stdio.h>
int main()
{
int i, a = 15;
for(i=1;i<=10;i++){
printf("%d * %-d = %3d\n",a,i,a*i);
}
return 0;
}
Lab 8:
(a) Write a C program to find the sum of the digits of a given number using a do-
while loop.
(b) Demonstrate block scope, function scope, and file scope in C.
#include<stdio.h>
int main()
{
int n, sum = 0;
/* Ask user for number */
printf("Enter a number\n");
scanf("%d",&n);
do{
sum += n%10;
n /= 10;
}while(n>0);
printf("Sum of digits = %d\n",sum);
return 0;
}
#include<stdio.h>
#define L 1000 // file scope
int i; // file scope
void f(void) // writing void within ( ) optional
{
const int l = 999; // function scope
extern int i; // declaration optional
printf("Function f called, i set to %d\n",i);
int j = i+1; // function scope
{
int k; // block scope
k = i-1;
printf("k=%d ",k);
}
printf("j=%d\n",j);
printf("Upper case (L) constant = %d\n",L);
printf("Lower case (l) constant = %d\n",l);
}
int main()
{
printf("Enter a positive integer\n");
scanf("%d",&i);
f();
return 0;
}
Lab 9:
(a) Demonstrate the use of automatic, register, static, and external storage classes in C.
(b) Demonstrate array initialization, bounds checking, initializing a 2-dimensional
array, and the memory map of a 2-dimensional array.
#include<stdio.h>
int main()
{
int a[5]={1,8,3,6,5},i;
int b[3][2]={2,1,5,3,7,4},j;
printf("%6s %8s %10s\n","Index","Mem Val","Address");
for(i=0;i<=4;i++){
printf("%3c[%d] %5d %12p\n",'a',i,a[i],&a[i]);
}
printf("\n");
for(i=0;i<=2;i++){
for(j=0;j<=1;j++){
printf("b[%d][%d] %5d %12p\n",i,j,b[i][j],&b[i][j]);
}
}
return 0;
}
Lab 10:
#include<stdio.h>
int main()
{
int a[3][2], b[3][2], i,j;
printf("Enter matrix A (3X2)\n");
for(i=0;i<=2;i++){
for(j=0;j<=1;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B (3X2)\n");
for(i=0;i<=2;i++){
for(j=0;j<=1;j++){
scanf("%d",&b[i][j]);
}
}
printf("Addition of two matrices\n");
for(i=0;i<=2;i++){
for(j=0;j<=1;j++){
printf("%d ",a[i][j]+b[i][j]);
}
printf("\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int a[2][2], b[2][2], i,j,k,sum;
printf("Enter matrix A (2X2)\n");
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B (2X2)\n");
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
scanf("%d",&b[i][j]);
}
}
printf("Multiplication of two matrices\n");
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
sum = 0;
for(k=0;k<=1;k++){
sum += a[i][k]*b[k][j];
}
printf("%d ",sum);
}
printf("\n");
}
return 0;
}
Lab 11:
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two integers\n");
scanf("%d %d",&a,&b);
printf("Sum: %d",sum(a,b));
return 0;
}
#include<stdio.h>
int main()
{
int a;
int b;
printf("Enter base\n");
scanf("%d",&a);
printf("Enter power\n");
scanf("%d",&b);
printf("%d power %d = %d",a,b,power(a,b));
return 0;
}
Lab 12:
(a) A 5-digit positive integer is entered through the keyboard, write a recursive
function to calculate the sum of digits of the 5-digit number.
(b) Write a recursive function to obtain the sum of the first 25 natural numbers.
#include<stdio.h>
int sum_digits(int n); //prototype
int main()
{
int num;
printf("Enter a 5 digit number\n");
scanf("%d",&num);
printf("Sum of digits: %d\n",sum_digits(num));
return 0;
}
int sum_digits(int n)
{
if(n==0)
return 0;
else
return n%10+sum_digits(n/10);
}
#include<stdio.h>
int nat_sum(int limit)
{
if(limit==0)
return 0;
else{
return limit+nat_sum(limit-1);
}
}
int main()
{
int lm = 5;
printf("Sum of first %d natural numbers:%d",lm,nat_sum(lm));
return 0;
}
Lab 2:
Write a "Hello World" C program. Compile the program using gcc and run the
executable.
$ gedit hello.c
#include<stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
gcc -g <filename.c> -o <filename.x> Move onto the next line but don't execute
add -g option to enable built-in (gdb) next
debugging
Print value of variable each time the
Start gdb program stops.
$ gdb <filename.x> (gdb) display <var_name>
Type above once. Thereafter no need to
Run the program use print or display for same variable
(gdb) run
Proceed onto the next breakpoint
List the source code (gdb) continue
(gdb) list
Information about break points
Set a break point (gdb) info breakpoints
(gdb) break <linenumber>
Delete a specified breakpoint
Print variables (gdb) delete <breakpoint_num>
(gdb) print <var_name>
To exit debugger
Execute the next line of code (gdb) quit
(gdb) step
To view stack
(gdb) backtrace
Lab 1:
Navigating Directories:
● List the contents of the root directory and other important directories using the ls
command. Navigate to different directories using the cd command and print the
current directory using the pwd command.
● Commands: ls, cd, pwd
File and Directory Operations:
● Create a new directory in your home directory using mkdir. Create a new file using
touchand open it with nanoor gedit. Copy, move, and delete files using cp, mv, and rm.
● Commands: mkdir, touch, nano, gedit, cp, mv, rm
File Permissions:
● Create a new file and modify its permissions using chmod. Change the ownership
of a file using chown.
● Commands: chmod, chown
System Information and Process Management:
● Display system memory usage with free. Check disk usage with df and du. List
currently running processes with ps and monitor system activity with top.
● Commands: free, df, du, ps, top
$ top
Monitor cpu and memory usage, disk i/o
operations, network activity, port
configuration in real time
SCHEME OF EVALUATION: