0% found this document useful (0 votes)
44 views8 pages

CPDS Unit 2 QB

The document discusses various C programming concepts like structures, unions, pointers, arrays, files handling, and functions. Structures allow combining different data types while unions allocate space for one member. Pointers store addresses of variables. Arrays store multiple elements of the same type. Functions perform tasks and files allow permanent storage of data.

Uploaded by

kiruba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views8 pages

CPDS Unit 2 QB

The document discusses various C programming concepts like structures, unions, pointers, arrays, files handling, and functions. Structures allow combining different data types while unions allocate space for one member. Pointers store addresses of variables. Arrays store multiple elements of the same type. Functions perform tasks and files allow permanent storage of data.

Uploaded by

kiruba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

REVIEW QUESTIONS

PART A
1. What is a Structure in C?
 Structure is a user-defined datatype in C language which allows us to combine
data of different types together. Structure helps to construct a complex data type
which is more meaningful.
 In structure, data is stored in form of records.
2. How to define a Structure?
 struct keyword is used to define a structure. struct defines a new data type
which is a collection of primary and derived data types.
 Syntax
struct [structure_tag]
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
3. What is Union?
 A union is a special data type available in C that allows to store different data
types in the same memory location.
 You can define a union with many members, but only one member can contain a
value at any given time. Unions provide an efficient way of using the same
memory location for multiple purpose.
4. Give the syntax for creating a union.
union [union name]
{
member definition;
member definition;
...
member definition;
};
5. Difference between Structure and Union.
Structure Union
The Keyword struct is used to define The Keyword union is used to define
the Structure the Union
Structure allocates storage space for all Union allocates one storage space for
its members seperately. all its members.
Structure occupies high memory space Union occupies low memory space
when compared to Structure
We can access all members of Only one member of union can be
Structure at a time accessed at a time.
Altering the value of a member will Altering the value of a member will
not affect other member of a structure alter other member value in union.

6. What are Enumerated Datatypes?


 Enumeration (or enum) is a user defined data type in C. It is mainly used to
assign names to integral constants, the names make a program easy to read and
maintain.
7. What is pointer?
 A pointer is a variable that stores the memory address of another variable as its
value. A pointer variable points to a data type (like int) of the same type and is
created with the * operator.
8. How addresses are assigned to Pointers?
Example
int* p,
a; a= 8;
p = &a;
Here, 8 is assigned to the variable a and the address of a is assigned to the
pointer p.
9. What are the uses of Pointers?
 Pointers are used to return more than one value to the function
 Pointers are more efficient in handling the data in arrays
 Pointers reduce the length and complexity of the program
 They increase the execution speed
 The pointers saves data storage space in memory.
10. What is the difference between an array and pointer?
Arrays Pointers
Array allocates space automatically. Pointer is explicitly assigned to point
to an allocated space.
It cannot be resized. It can be resized using realloc ().
It cannot be reassigned. Pointers can be reassigned.
Sizeof(array name) gives the number Sizeof(pointer name) returns the
of bytes occupied by the array. number of bytes used to store the
pointer variable.

11. What is dangling pointer?


 In C, a pointer may be used to hold the address of dynamically allocated
memory. After this memory is freed with the free() function, the pointer itself
will still contain the address of the released block. This is referred to as a
dangling pointer.
 Using the pointer in this state is a serious programming error. Pointer should be
assigned NULL after freeing memory to avoid this bug.
12. What is ‘C’ functions?
 A function is a self-contained block (or) a sub-program of one or more
statements that performs a special task when called.
 To perform a task repetitively then it is not necessary to re-write the particular
block of the program again and again. The function defined can be used for any
number of times to perform the task.
13. Differentiate library functions and User-defined functions.
Library Functions User-defined Functions
The User-defined functions are the
Library functions are pre-defined set of functions defined by the user according
functions that are defined in C libraries. to his/her requirement.

User can only use the function but User can use this type of function.
cannot change (or) modify this function. User can also modify this function.

14. What are the steps in writing a function in a program?


 Function Declaration (Prototype declaration): Every user-defined function has to
be declared before the main().
 Function Calling: The user-defined functions can be called inside any functions
like main(), user-defined function, etc.
 Function Definition: The function definition block is used to define the user-
defined functions with statements.
15. What is a use of ‘return’ Keyword?
 The ‘return’ Keyword is used only when a function returns a value.
16. What is the purpose of the function main()?
 The function main () invokes other functions within it. It is the first function to
be called when the program starts execution.
 Features of Main method
o It is the starting function.
o It returns an int value to the environment that called the program.
o Recursive call is allowed for main () also.
o It is a user-defined function.
o Program execution ends when the closing brace of the function main() is
reached.
o It has two arguments (a) argument count and (b)argument vector (represents
strings passed.)
17. Compare between Array and Structure
Arrays Structures
An array is a collection of data items A structure is a collection of data items
of same data type. of different data types.
Arrays can only be declared. There is Structures can be declared and defined.
no keyword for arrays. The Keyword for structures is struct.
An array name represents the address A structure name is known as tag. It is a
of the starting element. shorthand notation of the declaration.
An array cannot have bit fields. A structure may contain bit fields.

18. Is it better to use a macro or a function?


 Macros are more efficient (and faster) than function because their corresponding
code is inserted directly at the point where the macro is called. There is no
overhead involved in using a macro like there is in placing a call to a function.
 However, macros are generally small and cannot handle large, complex coding
constructs. In cases where large, complex constructs are to handled, functions
are more suited, additionally; macros are expanded inline, which means that the
code is replicated for each occurrence of a macro.
19. List the characteristics of Arrays.
 All elements of an array share the same name, and they are distinguished form
one another with help of an element number.
 Any element of an array can be modified separately without disturbing other
elements.
20. What are the types of Arrays?
 One-Dimensional Array
 Two-Dimensional Array
 Multi-Dimensional Array
21. What is File Handling in C?
 A file is nothing but a source of storing information permanently in the form of a
sequence of bytes on a disk. The contents of a file are not volatile like the C
compiler memory. The various operations available like creating a file, opening
a file, reading a file, or manipulating data inside a file is referred to as file
handling.
22. What is the need for File Handling in C?
 Reusability: It helps in preserving the data or information generated after
running the program.
 Large storage capacity: Using files, you need not worry about the problem of
storing data in bulk.
 Saves time: There are certain programs that require a lot of input from the user.
You can easily access any part of the code with the help of certain commands.
 Portability: You can easily transfer the contents of a file from one computer
system to another without having to worry about the loss of data.
23. List some of C File Handling Operations.
 Creating a new file: fopen()
 Opening an existing file in your system: fopen()
 Closing a file: fclose()
 Reading characters from a line: getc()
 Writing characters in a file: putc()
 Reading a set of data from a file: fscanf()
 Writing a set of data in a file: fprintf()
 Reading an integral value from a file: getw()
24. Give the syntax for Opening a Text File in C.
Syntax
*fpointer = FILE *fopen(const char *file_name, const char *mode);
 *fpointer is the pointer to the file that establishes a connection between the
file and the program.
 *file_name is the name of the file.
 *mode is the mode in which we want to open our file.
25. How to Read and Write a Text File in C?
 The input/output operations in a file help you read and write in a file.
 The simplest functions used while performing operations on reading and writing
characters in a file are getc() and putc() respectively.
 In order to read and write a set of data in a file, we use the fscanf() and fprintf()
operators.
26. Write short notes on Preprocessor Directives.
 The C preprocessor is a macro processor that is used automatically by the C
compiler to transform your program before actual compilation (Preprocessor
directives are executed before compilation.).
 It is called a macro processor because it allows you to define macros, which are
brief abbreviations for longer constructs.
27. What is macro?
 A macro is a segment of code which is replaced by the value of macro. Macro is
defined by #define directive.
28. List few preprocessor directives in C.
 #include
 Macro's (#define)
 #undef
 #ifdef
 #ifndef
 #if
 #else
PART-B

1. Explain Structure in C with neat program.


2. Write short notes on Union with example program.
3. What is a function? Explain with neat program.
4. Explain call by value and call by reference with example programs.
5. Explain the file handling mechanism in C with programs.
6. Explain preprocessor directives with its types and examples.
7. Explain the concept of pointers with neat programs.
8. Write shorts notes on Arrays.
9. How to write Data into a text file and Read Data from the file? Discuss.
10. How to read and write data to the binary file in a program? Explain.

You might also like