Basic Programming Lab
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;
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;
#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;
}
int main( )
{
extern int abc;
printf("\nValue of abc is: %d", abc);
return 0;
}
//In abc.txt
#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;
int main()
{
int local_variable = 20; // Local Variable
return 0;
}
Assignment
//0x11
Example:
*********************Expenditure Bill**********************
Sl Item Name Price Quantity Total Amount
-- --------- ------ -------- ------------
-----------------------------------------------------------
/*
--------------------------------------------------
|Author : Your_Name |
|Roll No: Your_Roll_No |
|Department: Your_Department |
--------------------------------------------------
*/