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

C Prog Lab Programs

The document outlines lab programs for a C Programming with Linux course at the East West Institute of Technology for the academic year 2024-25. It includes various lab exercises that cover topics such as variable declaration, data types, arithmetic operations, control structures, functions, recursion, and matrix operations. Each lab provides example code snippets and instructions for students to follow.

Uploaded by

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

C Prog Lab Programs

The document outlines lab programs for a C Programming with Linux course at the East West Institute of Technology for the academic year 2024-25. It includes various lab exercises that cover topics such as variable declaration, data types, arithmetic operations, control structures, functions, recursion, and matrix operations. Each lab provides example code snippets and instructions for students to follow.

Uploaded by

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

EAST WEST INSTITUTE OF TECHNOLOGY

(An Autonomous Institute under VTU)


LAB PROGRAMS

Dept: ECE Academic Year: 2024-25


Course Title: C Programming with Linux - I
Course Code: ACSP105L Sem: 1
Duration: 2 hours per week Course Instructor: Pratik Chatterjee

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

printf("\nConstant integer entry: %d\n",cin);


printf("Constant float entry: %f\n",cfl);

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

/*Operations on integer and float*/


int c=3;
float d=5.1, e = c/d;
printf("Operations on an integer and float:\n");
printf("%d-%4.2f = %4.2f\n",c,d,c-d);
printf("%d/%4.2f = %4.2f\n",c,d,e);
return 0;
}

#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()
{

int num, i=1, fact=1;


printf("Enter a number:\n");
scanf("%d",&num);
while(i<=num){
fact = fact*i;
i++;
}
printf("Factorial of %d = %d",num,fact);
return 0;
}

#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> /* Use of automatic storage class */


int main()
{
auto int i = 1;
{
auto int i = 2;
{
auto int i = 3;
printf("%d\n",i);
}
printf("%d\n",i);
}
printf("%d\n",i);
auto int j;
printf("Default initial value: %d",j); // garbage
return 0;
}

#include<stdio.h> /* Use of register storage class */


int main()
{
register int i;
printf("Default initial value: %d\n",i); //garbage
for(i=0;i<5;i++){
printf("%d\n",i);
}
return 0;
}

#include<stdio.h> /* Use of static storage class */


void increment()
{
static int i = 1; // will be initialized once
printf("%d\n",i);
i = i+1;
}
int main()
{
increment();
increment();
increment();
return 0;
}
#include<stdio.h> // Use of external storage class
int i; // external variable declaration
void increment()
{
i = i+1;
printf("on incrementing, i = %d\n",i);
}
void decrement()
{
i = i-1;
printf("on decrementing, i = %d\n",i);
}
int main()
{
extern int i; // declaration inside function optional
printf("Default initial value:%d\n",i);
increment();
increment();
decrement();
decrement();
return 0;
}

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

(a) Write a C program to add two matrices.


(b) Write a C program to multiply two matrices.

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

(a) Program to add two numbers using a function.


(b) Write a function power ( a, b ), to calculate the value of a raised to b.

#include<stdio.h>

int sum(int x, int y); /* prototype*/

int main()
{
int a,b;
printf("Enter two integers\n");
scanf("%d %d",&a,&b);
printf("Sum: %d",sum(a,b));
return 0;
}

int sum(int x, int y)


{
return x+y;
}

#include<stdio.h>

int power(int base, int n)


{
int i,p;
p = 1;
for(i=1;i<=n;i++)
p = p*base;
return p;
}

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 hello.c -o hello


$ ./hello
Or
$ gcc hello.c -o hello.x
$ ./hello.x
Or
$ gcc hello.c
$ ./a.out

For Basic Debugging: (add -g option)


$ gcc -g program_name.c -o program_name
$ ./program_name

● Write a simple C program with a logical error.


● Start GDB and run the program.
● Set breakpoints and step through the code.
● Commands: gdb ./<program_name>, run, break, step, next
Inspecting Variables and Memory:
● Use print and display commands to inspect variable values.
● List the source code with the list command and display
information about the program with info.
● Commands: print, display, list, info
Advanced Debugging:
● Use backtrace to view the call stack.
● Continue program execution with continue and quit GDB with quit.
● Commands: backtrace, continue, quit
GDB AND ASSOCIATED COMMANDS

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

$ ls $ chown <owner/user>:<group name>


List files and directories inside current <filename>
directory Change group name
Example:
$ cd $ chown admin1:plugdev prog1.c
Change directory
$ chmod <user><+ or -><permission>
$ cd .. <filename>
Move one level up Change permission
Example:
$ pwd $ chmod g-w prog1.c
Display present working directory
$ rm <filename>
$ mkdir <dirname> Remove a file
Create directory
$ rmdir <dirname>
$ touch <filename> Remove a directory
Create file
$ free - h
$ gedit <filename> display amount of free and used memory
Create and edit file in human readable format

$ cp <filename> <destination path> $ df -h


Copy file report file system space usage (each file
resides on file system)
$ mv <filename> <destination path>
Move file $ du -h
amount of disk space used by files and
$ ls -l <filename> directories
List permission of a file
$ ls -ld <dirname> $ ps -(e or A)
List permissions of a directory report a snapshot of the current processes

$ top
Monitor cpu and memory usage, disk i/o
operations, network activity, port
configuration in real time

SCHEME OF EVALUATION:

Conduction during class: 15 M


Observation book: 10 M
Conduction during exam: 20 M
Viva: 5M
Total: 50 M

You might also like