
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a structure in local scope program using C language
Structure is a collection of different datatype variables, grouped together under a single name.
Features of structure
The features of structure are explained below −
It is possible to copy the contents of all structure elements of different datatypes to another structure variable of its type by using an assignment operator.
For handling complex datatypes, it is better to create a structure within an another structure, which is called as the nested structures.
It is possible to pass an entire structure, individual elements of a structure and an address of structure to a function.
It is also possible to create the structure pointers.
Declaration of structures
The general form of structure declaration is as follows −
datatype member1; struct tagname{ datatype member2; datatype member n; };
Here, struct is the keyword.
tagname specifies name of structure.
member1, member2 are the data items.
For example,
struct book{ int pages; char author [30]; float price; };
Example
Following is the C program for structure in local scope −
#include<stdio.h> struct{ char name[20]; int age; int salary; char add[30]; }emp1,emp2; int manager(){ struct{ char name[20]; int age; int salary; char add[50]; }manager ; manager.age=27; if(manager.age>30) manager.salary=65000; else manager.salary=55000; return manager.salary; } int main(){ printf("enter the name of emp1:"); //gets(emp1.name); scanf("%s",emp1.name); printf("\nenter the add of emp1:"); scanf("%s",emp1.add); printf("\nenter the salary of emp1:"); scanf("%d",&emp1.salary); printf("\nenter the name of emp2:"); // gets(emp2.name); scanf("%s",emp2.name); printf("\nenter the add of emp2:"); scanf("%s",emp2.add); printf("\nenter the salary of emp2:"); scanf("%d",&emp2.salary); printf("\nemp1 salary is %d",emp1.salary); printf("\nemp2 salary is %d",emp2.salary); printf("\nmanager salary is %d",manager()); return 0; }
Output
When the above program is executed, it produces the following result −
enter the name of emp1:hari enter the add of emp1:hyderabad enter the salary of emp1:4000 enter the name of emp2:lucky enter the add of emp2:chennai enter the salary of emp2:5000 emp1 salary is 4000 emp2 salary is 5000 manager salary is 55000
- Related Questions & Answers
- What is a structure at local scope in C language?
- Write an example program on structure using C language
- What are the local and global scope rules in C language?
- How to write a simple calculator program using C language?
- Explain scope of a variable in C language.
- Read/Write structure to a file using C
- Structure declaration in C language
- What are Local Scope Variables in Postman?
- Explain bit field in C language by using structure concept
- Declaring a structure with no members in C language
- Write a program to understand the concept of pointers in C language?
- Can a local variable's memory be accessed outside its scope in C/C++?
- Explain scope rules related to the functions in C language
- What is the scope of local variables in Java?
- What are the local static variables in C language?