What Is Structure? Explain C Syntax of Structure Declaration With Example
What Is Structure? Explain C Syntax of Structure Declaration With Example
What Is Structure? Explain C Syntax of Structure Declaration With Example
Syntax:
struct struct_name
{
data_type1 member1;
data_type2 member2;
…………………………………..
data_typen membern;
};
struct is a keyword
strut_name is the name of the structure which is userdefined
datatype1,datatyp2,…..datatypen are the basic data types
member1,member2,…..membern are the different data items
Example:
struct student
{
int roll_num;
char name[20];
float marks;
};
Declaration of structure variable:
Syntax: struct struct_name structvar_list;
Example: struct student stud1,stud2;
Example 1: Example 2:
struct date struct employee
{ {
int date; char name[20;
int month; float salary;
int year; struct date
}; {
struct employee int date;
{ int month;
char name[20]; int year;
float salary; } doj;
struct date doj; }emp1;
} emp1;
emp1.doj.date;
emp1.doj.month;
3. Write a c-program using structures to read, write, compute average - marks and display the students
scoring above and below the average marks for a class of N students.
/* Program to read, write, compute average- marks and the students scoring above and below the average
marks for a class of N students using structures */
#include<stdio.h>
struct student
{
int roll;
char name[50];
int marks;
};
void main()
{
struct student s[10];
int i,n,sum=0;
float average=0.0;
printf("\nEnter the number of students:");
scanf("%d",&n);
// Reading details of N students
for(i=0;i<n;i++)
{
printf("Enter the details of student %d\n",i+1);
printf("Enter the roll number:");
scanf("%d",&s[i].roll);
printf("Enter the Name:");
gets(s[i].name);
printf("Enter Total Marks:");
scanf("%d",&s[i].marks);
printf("\n");
}
// compute sum and average marks
for(i=0;i<n;i++)
{
sum=sum+s[i].marks;
}
average=(float)sum/n;
// print the details of N students
printf("Class Average is %g\n",average);
printf("-----------------------------------------------------------------------------------\n");
printf("Record details of students:\n");
printf("\nRollNo\t Student Name\t Total_Marks\t Above_Average(Y/N) \n");
printf("---------------------------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("%4d",s[i].roll);
printf("%15s",s[i].name);
printf("%15d",s[i].marks);
if(s[i].marks>=average)
printf("\tYes");
else
printf("\tNo");
printf("\n");
}
}
Output:
4. What is a pointer? Explain how the pointer variable declared and initialized.
A pointer is a variable that contains an address of another variable.
Declaring a pointer variable:
Syntax:
data_type *pt_name;
The (*) asterisk tells that the variable pt_name is a pointer variable.
Pt_name needs memory location
Pt_name points to a variable of type data_type
Example:
int *p; // declares the variable p as a pointer that points to an integer data type.
Initializing the pointer variable:
Declare a data variable
Declare a pointer variable
Assign the address of a variable to pointer variable using & operator and assignment operator
Syntax:
pointer_var=&data_var;
Examples:
int x; //declare a data variable
int *px; //declare a pointer variable
px=&x // copy the address of data variable to pointer variable
5. Write a program in C to find the sum and mean of all elements in an array using pointers.
/* program to compute the sum, mean and standard deviation of all elements stored in an array of n real
numbers using pointers */
#include<stdio.h>
#include<math.h>
void main()
{
float a[10],*ptr, mean, std, sum=0,sumstd=0;
int n, i;
printf("Enter the no. of elements\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i=0; i<n; i++) // read n real numbers and store in the array a
{
scanf("%f",&a[i]);
}
ptr=a; // assign the base address of array a to pointer variable ptr
for(i=0; i<n; i++) // compute sum
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n; // compute mean
ptr=a;
for(i=0; i<n; i++) // compute standard deviation
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard Deviation=%.3f\t",std);
}
Output
- General form:
#include “filename” or #include<filename>
where filename is the name of the file containing the required definitions or functions.
When the filename is included within the double quotes, the search for the file is made first in the
current directory and then in the standard directories
When the file name is included with angular brackets, the file is searched only in the standard
directories
If an included files is not found, an error is reported and compilation is terminated
Example:
#include<stdio.h>
#include “test.c”
#if
#else
#endif
Example:
#if((9%2)==0)
printf(“number is even”);
#else output: number is odd
printf(“number is odd”);
#endif
7. Explain how the structure variable can be passed as a parameter to a function with an example.
There are 3 methods of passing structure to a function
1. pass each member of the structure as an actual argument of the function call
this is the most elementary method and becomes unmanageable and inefficient when the
structure size is large
Example:
void display(char,int);
struct student
{
char name[20];
int usn;
};
void main() void display(char name[20],int usn)
{ {
struct student s1; printf(“Name:%s\n”,name);
printf(“enter name & usn:”); printf(“USN:%d”,usn);
scanf(“%s%d”,s1.name,&s1.usn); }
display(s1.name,s1.usn);
}
2. Passing a copy of entire structure
The called function works on the copy of the structure, any changes to structure members
within the function are not reflected in the original structure
All compilers may not support this method
Example:
void display(struct student s);
struct student
{
char name[20];
int usn;
};
void main() void display(struct student s1)
{ {
struct student s1; printf(“Name:%s\n”,s1.name);
printf(“enter name & usn:”); printf(“USN:%d”,s1.usn);
scanf(“%s%d”,s1.name,&s1.usn); }
display(s1);
}
3. Pass the address location of the structure to the called function
The function can access indirectly the entire structure and work on it
This method is more efficient as compared to the second one
Example:
void display(struct student *s);
struct student
{
char name[20];
float marks;
};
void main() void display(struct student *r)
{ {
struct student st; printf(“Name:%s”,r->name);
strcpy(st.name,”Raju”); printf(“marks:%f”,r->marks);
st.marks=45; }
display(&st);
}
11. What are pre-processor directives? Explain #define & #include pre-processor directives.
Preprocessor is a program that processes the source code before it passes through the compiler
Preprocessor directives are placed in the source program before the main line
Before the source code passes through the compiler, it is examined by the preprocessor for any
preprocessor directives
Preprocessor directives begin with the symbol # in column one and do not require a semicolon at
the end.
#define:
General form: #define identifier string
- string may be any text, while the identifier must be a valid C name
If this statement is included at the beginning of the program, then the preprocessor replaces
every occurrence of the identifier by the sting
Example: #define PI 3.142
#include:
Used to include an external file containing functions or macro definitions
General format: #include “filename”
where filename is the name of the file containing the required definitions or functions.
This directive can take the form #include<filename> without double quotation marks
If an included files is not found, an error is reported and compilation is terminated
Example: #include<stdio.h>
#include “a.c”
12. Write a ‘C’ Program using pointers to compute the sum, mean & standard derivation of all elements
stored in an array of ‘n’ real numbers.
Refer question number 5