C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Enter n: 2

Enter string, integer and floating number respectively:

Programming

2

3.2

Enter string, integer and floating number respectively:

Structure

6

2.3

Displaying Information

Programming 2 3.20

Structure 6 2.30

C Programming Structure and Function

In C, structure can be passed to functions by two methods:

1. Passing by value (passing actual value as argument)

2. Passing by reference (passing address of an argument)

Passing structure by value

A structure variable can be passed to the function as an argument as normal variable. If structure is passed by value, change made in structure variable in function definition does not reflect in original structure variable in calling function.

Write a C program to create a structure student, containing name and roll. Ask user the name and roll of a student in main function.

Pass this structure to a function and display the information in that function.

#include <stdio.h>

struct student{

char name[50];

int roll;

};

void Display(struct student stu);

/* function prototype should be below to the structure declaration otherwise compiler

shows error */

int main(){

struct student s1;

printf("Enter student's name: ");

scanf("%s",&s1.name);

printf("Enter roll number:");

scanf("%d",&s1.roll);

Display(s1); // passing structure variable s1 as argument

return 0;

}

void Display(struct student stu){

printf("Output\nName: %s",stu.name);

printf("\nRoll: %d",stu.roll);

}