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

Basic Programming Lab

The document discusses the basics of C programming including structures, unions, and storage classes. It provides examples of declaring and using structures to store student data and calculate expenses. It also covers accessing structure members using pointers, arrays of structures, passing structures to functions, and different storage classes like auto, extern, static, register, local and global variables. The document concludes with two programming assignments - one to add distances in feet and inches using structures, and another menu driven program to calculate picnic expenses divided among students using structures.

Uploaded by

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

Basic Programming Lab

The document discusses the basics of C programming including structures, unions, and storage classes. It provides examples of declaring and using structures to store student data and calculate expenses. It also covers accessing structure members using pointers, arrays of structures, passing structures to functions, and different storage classes like auto, extern, static, register, local and global variables. The document concludes with two programming assignments - one to add distances in feet and inches using structures, and another menu driven program to calculate picnic expenses divided among students using structures.

Uploaded by

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

Basic Programming Lab

— — 0x12
Structure
Syntax:

struct structure_name
{
datatype member_1;
datatype member_2;
datatype member_3;
} structure_variable_1, structure_variable_2;

Example:

struct country
{
char name[30];
int population;
char language[15];
}India, South_Korea, Japan;

//Alternative way for structure variable declaration

struct country India, South_Korea, Japan;


Accessing Structure Members
with structure variable
#include <stdio.h>

struct student
{
int roll;
char name[25];
char gender;
float marks;
};

int main()
{
struct student s;
printf("\nEnter the students data:\n");
printf("\nRoll Number: ");
scanf("%d", &s.roll);
printf("\nName: ");
scanf("%s", s.name);
printf("\nGender: ");
scanf(" %c", &s.gender);
printf("\nMarks: ");
scanf("%f", &s.marks);
printf("\nThe students details are\n");
printf("\nRoll number: %d", s.roll);
printf("\nName:%s ", s.name);
printf("\nGender:%c ", s.gender);
printf("\nMarks: %.2f", s.marks);
return 0;
}
Accessing Structure Members
with structure pointer
#include <stdio.h>

struct student
{
int roll;
char name[25];
char gender;
float marks;
};

int main()
{
struct student s, *ptr;
ptr = &s;
printf("\nEnter the students data:\n");
printf("\nRoll Number: ");
scanf("%d", &ptr->roll);
printf("\nName: ");
scanf("%s", ptr->name);
printf("\nGender: ");
scanf(" %c", &ptr->gender);
printf("\nMarks: ");
scanf("%f", &ptr->marks);
printf("\nThe students details are\n");
printf("\nRoll number: %d", ptr->roll);
printf("\nName:%s ", ptr->name);
printf("\nGender:%c ", ptr->gender);
printf("\nMarks: %.2f", ptr->marks);
return 0;
}
Array of Structure
#include <stdio.h>
struct student
{
int roll;
char name[25];
char gender;
float marks;
};
int main()
{
struct student s[100];
int i, n;
printf("\nEnter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\n\nEnter student-%d data:\n", i + 1);
printf("\nRoll Number: ");
scanf("%d", &s[i].roll);
printf("\nName: ");
scanf(" %[^\n]s", s[i].name);
printf("\nGender: ");
scanf(" %c", &s[i].gender);
printf("\nMarks: ");
scanf("%f", &s[i].marks);
}
printf("\nThe student details are\n");
printf("\nSl.No.\tRoll No.
\tName\t\tGender\tMarks\n");
printf("\n====\t======= \t==== \t\t===== \t=====
\n");
for (i = 0; i < n; i++)
{
printf("\n%d\t%d\t\t%s \t\t%c \t%f\n", i + 1,
s[i].roll, s[i].name, s[i].gender, s[i].marks);
}
return 0;
}
Pass & return a structure to
a function
#include <stdio.h>
struct complex
{
float real;
float imag;
};
struct complex add( struct complex x, struct complex y)
{
struct complex temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return (temp);
}
int main()
{
struct complex c1, c2, c;
printf("\nFor 1st complex number \n");
printf("\nEnter real and imaginary part respectively:");
scanf("%f %f", &c1.real, &c1.imag);
printf("\n\nFor 2nd complex number \n");
printf("\nEnter real and imaginary part respectively:");
scanf("%f %f", &c2.real, &c2.imag);
c = add(c1, c2);
printf("\n\nSum = %.1f + %.1fi", c.real, c.imag);
return 0;
}
Union
Syntax:

union union_name
{
datatype member_1;
datatype member_2;
datatype member_3;
} union_variable_1, union_variable_2;

Example:

union country
{
char name[30];
int population;
char language[15];
}India, South_Korea, Japan;

//Alternative way for union variable declaration

union country India, South_Korea, Japan;


Storage Class
//1. Automatic Example

#include <stdio.h>
int main( )
{
auto int j = 3;
{
auto int j = 5;
{
auto int j = 7;
printf ( "%d ", j);
}
printf ( "%d ", j);
}
printf( "%d ", j);

return 0;
}

//2. Extern Example


//In test4.c
#include "abc.txt"

int main( )
{
extern int abc;
printf("\nValue of abc is: %d", abc);

return 0;
}

//In abc.txt
#include<stdio.h>

int abc = 73;


//3. Static Example
#include <stdio.h>

void static_example();

int main()
{
static_example();
static_example();

return 0;
}

void static_example()
{
static int a = 1;
a++;
printf("%d ", a);
}

//Register
register int Number;

//Local Variable & Global Variable


#include <stdio.h>

int global_variable = 10; //Global Variable

int main()
{
int local_variable = 20; // Local Variable
return 0;
}
Assignment
//0x11

//Use scanf for input in Every Program

1. Write a program to add two distances (in inch-


feet). (Use structure)

2. Students of FAkirchand and Lakirchand Trust


University decided to go for a picnic. Write a menu
driven program to read item details used in picnic
and calculate all expenses, divide expenses in all
students equally. (Use structure)

Example:

Enter item-1 details


Item Name: Rice
Price: 50.00
Quantity: 4
Want to add more items (press 1): 1

Enter item-2 details:


Item Name: Dal Fry
Price:35
Quantity: 5
Want to add more items (press 1): 1

Enter item-3 details:


Item Name: Shahi Paneer
Price:55
Quantity: 4
Want to add more items (press 1): 1

Enter item-4 details:


Item Name: Butter Chicken
Price:120
Quantity: 3
Want to add more items (press 1): 1
Enter item-5 details:
Item Name: Ice cream
Price:45
Quantity: 5
Want to more items (press 1): 0

*********************Expenditure Bill**********************
Sl Item Name Price Quantity Total Amount
-- --------- ------ -------- ------------

1 Rice 50.00 4 200.00


2 Dal Fry 35.00 5 175.00
3 Shahi Paneer 55.00 4 220.00
4 Butter Chicken 120.00 3 360.00
5 Ice Cream 45.00 5 225.00

-----------------------------------------------------------

Total Expenses: Rs.1180.00


How many students have to pay the expenses: 5
Each student will have to pay: Rs.236.00
Points to Remember
1. Filetype: .c

2. Naming Convention for File: Question_Y.c


where Y = Question No in that Assignment
example: Question_1.c

3. Write your details in every program

/*
--------------------------------------------------
|Author : Your_Name |
|Roll No: Your_Roll_No |
|Department: Your_Department |
--------------------------------------------------
*/

You might also like