Structures, Unions, Enums for C Programming MCQ Practice

Structures, Unions, Enums for C Programming MCQ Practice

Examination Preparation and Interview questions on Structures, Unions, Enums for C Programming
What you’ll learn

  • Structures, Unions, Enums for C Programming
Requirements
  • Anyone who wants to learn C Programming
  • Advanced C Programming Users
Description

A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address. The struct data type can contain other data types so is used for mixed-data-type records such as a hard-drive directory entry (file length, name, extension, physical address, etc.), or other mixed-type records (name, address, telephone, balance, etc.)

The C struct directly references a contiguous block of physical memory, usually delimited (sized) by word-length boundaries. It corresponds to the similarly named feature available in some assemblers for Intel processors. Language implementations that could utilize half-word or byte boundaries (giving denser packing, using less memory) were considered advanced in the mid-1980s. Being a block of contiguous memory, each field within a struct is located at a certain fixed offset from the start.

Because the contents of a struct are stored in contiguous memory, the sizeof operator must be used to get the number of bytes needed to store a particular type of struct, just as it can be used for primitives. The alignment of particular fields in the struct (with respect to word boundaries) is implementation-specific and may include padding, although modern compilers typically support the #pragma pack directive, which changes the size in bytes used for alignment

A union can be pictured as a chunk of memory that is used to store variables of different data types. Once a new value is assigned to a field, the existing data is overwritten with the new data. The memory area storing the value has no intrinsic type (other than just bytes or words of memory), but the value can be treated as one of several abstract data types, having the type of the value that was last written to the memory area

In type theory, a union has a sum type; this corresponds to disjoint union in mathematics

Depending on the language and type, a union value may be used in some operations, such as assignment and comparison for equality, without knowing its specific type. Other operations may require that knowledge, either by some external information, or by the use of a tagged union

An enumerated type (also called enumeration, enum, or factor in the C programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. An enumerated type can be seen as a degenerate tagged union of unit type. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but are not specified by the programmer as having any particular concrete representation in the computer’s memory; compilers and interpreters can represent them arbitrarily

For example, the four suits in a deck of playing cards may be four enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named suit. If a variable V is declared having suit as its data type, one can assign any of those four values to it

Although the enumerators are usually distinct, some languages may allow the same enumerator to be listed twice in the type’s declaration. The names of enumerators need not be semantically complete or compatible in any sense. For example, an enumerated type called color may be defined to consist of the enumerators Red, Green, Zebra, Missing, and Bacon. In some languages, the declaration of an enumerated type also intentionally defines an ordering of its members; in others, the enumerators are unordered; in others still, an implicit ordering arises from the compiler concretely representing enumerators as integers

Some enumerator types may be built into the language. The Boolean type, for example is often a pre-defined enumeration of the values False and True. Many languages allow users to define new enumerated types

Values and variables of an enumerated type are usually implemented as fixed-length bit strings, often in a format and size compatible with some integer type. Some languages, especially system programming languages, allow the user to specify the bit combination to be used for each enumerator. In type theory, enumerated types are often regarded as tagged unions of unit types. Since such types are of the form 1+1+…+1, they may also be written as natural numbers

These questions will give you basic idea for Examination Preparation and/or interview on Structures, Unions, Enums for C Programming.

Please Note:

  1. These questions are only for practice and understanding level of knowledge only. It is not necessary that these questions may or may not appear for examinations and/or interview questions
  2. In this practice test, because of large amount of questions (around 53 questions) some of questions may have repeated
  3. I had to put as 70% pass rate because there may also be wrong answers from my side
Who this course is for:
  • C Programming
  • Structures, Unions, Enums for C Programming

Tags:

Tutorial Bar
Logo