SlideShare une entreprise Scribd logo
1  sur  7
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 1
DHANALAKSHMI COLLEGE OF ENGINEERING
Tambaram, Chennai
Department of Computer Science and Engineering
OCS752 INTRODUCTION TO C PROGRAMMING
Year / Sem : IV / VII
2 Marks Q & A
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 2
UNIT V
STRUCTURES
Introduction to structures – Declaration – Initialization – Accessing the members – Nested Structures –
Array of Structures – Structures and functions – Passing an entire structure – Exercise programs −
Compute the age of a person using structure and functions (passing a structure to a function) –
Compute the number of days an employee came late to the office by considering his arrival time for 30
days (Use array of structures and functions)
PART – A
1. Define – Structure (N/D – 17)
Structure is defined as a collection of different data types which are grouped together and each element
in a C structure is called member. Structure variable should be declared to access structure members in
C. Keyword struct is used to define the structure.
2. Create a structure called ID_Card to hold the details of a student. (N/D – 15)
Structure to hold the details of a student
struct ID_Card
{
char Name[50];
int roll;
int year;
char dept[50];
int phonenumber;
};
3. What are the uses of structure? (N/D – 16)(N/D – 19)
Uses of structure
1) Structure is used to store different data types.
2) Each element can be accessed individually.
4. Write the syntax of structure.
The keyword struct is used to create a structure.
Syntax:
struct structname
{
datatype member 1;
datatype member 2;
-------------
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 3
datatype member m;
};
5. Specify the use of typedef.
typedef is used to create a new data using the existing type.
Syntax: typedef datatype name;
Example:
typedef int hours;
hours hrs;
6. Write the syntax for initialize a structure variable.
struct structname
{
datatype member 1;
datatype member 2;
-------------
datatype member m;
}structvar={constant1, constant2, …..};
7. Differentiate array from structure. (A/M – 19)
S. No. Array Structure
1 Array is a collection of data items of same data. Structure is a collection of data items of
different type.
2 Array name represents the address of the
starting element.
Structure name is known as tag.
3 Array cannot have bit fields. Structure may contain bit fields
8. List the features of structure. (A/M – 15)
Features of structure
1) All the elements of a structure are stored at contiguous memory locations
2) A variable of structure type can store multiple data items of different data types under the one
name.
9. How will you access the structures member through pointers?
The structures member can be accessed through pointers by the following ways
1) Referencing pointer to another address to access memory
2) Using dynamic memory allocation
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 4
10. What is structure within structure?
Nested structure is structure within structure. One structure can be declared inside other structure as
declaring structure members inside a structure. The structure variables can be a normal structure variable
or a pointer variable to access the data.
Example
struct complex
{
int imagvalue;
float realvalue;
};
struct number
{
struct complex comp;
int real;
} num1, num2;
11. Write a C program to read and display information of a student using a structure within a
structure.
#include <stdio.h>
struct student{
char name[30];
int rollNo;
struct dateOfBirth{
int dd;
int mm;
int yy;
}DOB;
};
void main()
{
struct student std;
gets(std.name);
scanf("%d",&std.rollNo);
scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy);
printf(" %s %d %d/%d/%dn",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy);
}
12. How can a structure member be processed? How is a structure member accessed?
The members of a structure are usually processed individually as separate entities. A structure member
can be accessed by writing variable member name.
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 5
13. Write a C program using structures to find the biggest of three numbers.
#include <stdio.h>
int main()
{
struct numbers
{
int a,b,c, largest;
} n;
scanf("%d %d %d", &n.a, &n.b, &n.c);
n.largest = n.a > n.b ? (n.a > n.c? n.a :n.c) : (n.b > n.c ? n.b :n.c);
printf("%d is the largest number.",n.largest);
}
14. What is the precedence of the period operator? What is its associativity?
This period (.) is an operator. It is a member of the highest precedence group, and its associativity is
left-to-right. The use of the period operator can be extended to arrays of structure by writing
array[expression]. member.
15. What is self-referential structure?
A self-referential structure is one of the data structures which refer to the pointer to points to another
structure of the same type.
Example:
struct node
{
int value;
struct node *next;
};
16. Write a C program for passing a structure to a function.
#include<stdio.h>
struct p
{
int x,y;
}point;
void display(point);
void main()
{
point p1={2,3};
display(p1);
}
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 6
void display(point p1)
{
printf(“%d %d”,p1.x,p1.y);
}
17. What is array of structure?
An array of structure in C is the collection of multiple structures variables, where each variable
contains information about different entities. The arrays of structures in C are used to store information
about multiple entities of different data types.
18. What are the two ways of passing a structure to function in C?
Two ways of passing a structure to function in c
1) Passing structure to a function by value
2) Passing structure to a function by address(reference)
19. Declare a structure to create an inventory method.
struct inventory
{
char productname;
float price;
int stock;
};
20. Write a C program for passing structures through pointers.
#include <stdio.h>
struct student
{
int id;
char name[30];
};
void main()
{
int i;
struct student record1 = {1, “Raju”};
struct student *ptr;
ptr = &record1;
printf(“ Id is: %d n”, ptr−>id);
printf(“ Name is: %s n”, ptr−>name);
}
OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 7
21. Write a C program using typedef keyword.
#include<stdio.h>
struct Point1{
int x;
int y;
};
typedef struct Point1 Point;
void main()
{
Point p1;
p1.x = 1;
p1.y = 3;
printf("%d %d", p1.x, p1.y);
}

Contenu connexe

Tendances

Tendances (20)

C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdf
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Access modifiers in Python
Access modifiers in PythonAccess modifiers in Python
Access modifiers in Python
 
1.ppi 8255
1.ppi 8255 1.ppi 8255
1.ppi 8255
 
Function-Definition, Need, Declaration, Definition, Arguments, Return Value
Function-Definition, Need, Declaration, Definition, Arguments, Return ValueFunction-Definition, Need, Declaration, Definition, Arguments, Return Value
Function-Definition, Need, Declaration, Definition, Arguments, Return Value
 
String c
String cString c
String c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
Loops in c
Loops in cLoops in c
Loops in c
 
Digital Electronics Most Essential and Frequently Asked Interview Questions
Digital Electronics Most Essential and Frequently Asked Interview QuestionsDigital Electronics Most Essential and Frequently Asked Interview Questions
Digital Electronics Most Essential and Frequently Asked Interview Questions
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
c++ lab manual
c++ lab manualc++ lab manual
c++ lab manual
 

Similaire à Ocs752 unit 5

Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
babuk110
 
C language.pptx
C language.pptxC language.pptx
C language.pptx
SANJUSANJEEVTOPPO
 
Javaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptxJavaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptx
sanjutoppo93
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 

Similaire à Ocs752 unit 5 (20)

structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structures
StructuresStructures
Structures
 
Structure In C
Structure In CStructure In C
Structure In C
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 
C language.pptx
C language.pptxC language.pptx
C language.pptx
 
Javaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptxJavaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptx
 
Data structure manual
Data structure manualData structure manual
Data structure manual
 
Structure & union
Structure & unionStructure & union
Structure & union
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
Structures in C
Structures in CStructures in C
Structures in C
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
Structures
StructuresStructures
Structures
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 

Dernier

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Dernier (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

Ocs752 unit 5

  • 1. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 1 DHANALAKSHMI COLLEGE OF ENGINEERING Tambaram, Chennai Department of Computer Science and Engineering OCS752 INTRODUCTION TO C PROGRAMMING Year / Sem : IV / VII 2 Marks Q & A
  • 2. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 2 UNIT V STRUCTURES Introduction to structures – Declaration – Initialization – Accessing the members – Nested Structures – Array of Structures – Structures and functions – Passing an entire structure – Exercise programs − Compute the age of a person using structure and functions (passing a structure to a function) – Compute the number of days an employee came late to the office by considering his arrival time for 30 days (Use array of structures and functions) PART – A 1. Define – Structure (N/D – 17) Structure is defined as a collection of different data types which are grouped together and each element in a C structure is called member. Structure variable should be declared to access structure members in C. Keyword struct is used to define the structure. 2. Create a structure called ID_Card to hold the details of a student. (N/D – 15) Structure to hold the details of a student struct ID_Card { char Name[50]; int roll; int year; char dept[50]; int phonenumber; }; 3. What are the uses of structure? (N/D – 16)(N/D – 19) Uses of structure 1) Structure is used to store different data types. 2) Each element can be accessed individually. 4. Write the syntax of structure. The keyword struct is used to create a structure. Syntax: struct structname { datatype member 1; datatype member 2; -------------
  • 3. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 3 datatype member m; }; 5. Specify the use of typedef. typedef is used to create a new data using the existing type. Syntax: typedef datatype name; Example: typedef int hours; hours hrs; 6. Write the syntax for initialize a structure variable. struct structname { datatype member 1; datatype member 2; ------------- datatype member m; }structvar={constant1, constant2, …..}; 7. Differentiate array from structure. (A/M – 19) S. No. Array Structure 1 Array is a collection of data items of same data. Structure is a collection of data items of different type. 2 Array name represents the address of the starting element. Structure name is known as tag. 3 Array cannot have bit fields. Structure may contain bit fields 8. List the features of structure. (A/M – 15) Features of structure 1) All the elements of a structure are stored at contiguous memory locations 2) A variable of structure type can store multiple data items of different data types under the one name. 9. How will you access the structures member through pointers? The structures member can be accessed through pointers by the following ways 1) Referencing pointer to another address to access memory 2) Using dynamic memory allocation
  • 4. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 4 10. What is structure within structure? Nested structure is structure within structure. One structure can be declared inside other structure as declaring structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data. Example struct complex { int imagvalue; float realvalue; }; struct number { struct complex comp; int real; } num1, num2; 11. Write a C program to read and display information of a student using a structure within a structure. #include <stdio.h> struct student{ char name[30]; int rollNo; struct dateOfBirth{ int dd; int mm; int yy; }DOB; }; void main() { struct student std; gets(std.name); scanf("%d",&std.rollNo); scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy); printf(" %s %d %d/%d/%dn",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy); } 12. How can a structure member be processed? How is a structure member accessed? The members of a structure are usually processed individually as separate entities. A structure member can be accessed by writing variable member name.
  • 5. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 5 13. Write a C program using structures to find the biggest of three numbers. #include <stdio.h> int main() { struct numbers { int a,b,c, largest; } n; scanf("%d %d %d", &n.a, &n.b, &n.c); n.largest = n.a > n.b ? (n.a > n.c? n.a :n.c) : (n.b > n.c ? n.b :n.c); printf("%d is the largest number.",n.largest); } 14. What is the precedence of the period operator? What is its associativity? This period (.) is an operator. It is a member of the highest precedence group, and its associativity is left-to-right. The use of the period operator can be extended to arrays of structure by writing array[expression]. member. 15. What is self-referential structure? A self-referential structure is one of the data structures which refer to the pointer to points to another structure of the same type. Example: struct node { int value; struct node *next; }; 16. Write a C program for passing a structure to a function. #include<stdio.h> struct p { int x,y; }point; void display(point); void main() { point p1={2,3}; display(p1); }
  • 6. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 6 void display(point p1) { printf(“%d %d”,p1.x,p1.y); } 17. What is array of structure? An array of structure in C is the collection of multiple structures variables, where each variable contains information about different entities. The arrays of structures in C are used to store information about multiple entities of different data types. 18. What are the two ways of passing a structure to function in C? Two ways of passing a structure to function in c 1) Passing structure to a function by value 2) Passing structure to a function by address(reference) 19. Declare a structure to create an inventory method. struct inventory { char productname; float price; int stock; }; 20. Write a C program for passing structures through pointers. #include <stdio.h> struct student { int id; char name[30]; }; void main() { int i; struct student record1 = {1, “Raju”}; struct student *ptr; ptr = &record1; printf(“ Id is: %d n”, ptr−>id); printf(“ Name is: %s n”, ptr−>name); }
  • 7. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 7 21. Write a C program using typedef keyword. #include<stdio.h> struct Point1{ int x; int y; }; typedef struct Point1 Point; void main() { Point p1; p1.x = 1; p1.y = 3; printf("%d %d", p1.x, p1.y); }