Chp 15 Miscellaneous Features Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Enumerated Data Types:

A

Enables us to invent our data types and define what values the variable of this data type can take.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

enum definition:

A
enum mar_status
{
    single, married, divorced, widowed
};
enum mar_status person1, person2;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Enumerators:

A

Possible values the variable of our invented data type can take.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What do parts of enum declaration do:

A
1.
enum mar_status
{
    single, married, divorced, widowed
};
 Declares the data type and specifies its possible values (enumerators).
  1. enum mar_status person1, person2;
    declares variables of this data type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

internally how does the compiler treat enumerators:

A

compilers treats enumerators as integers.
Each value on the list of permissible values corresponds to an integer starting from 0 or
it can be overridden by initialising the enumerators to diff integer as :

enum mar_status
{
    single = 100, married = 200, divorced=300, widowed=400
};
enum mar_status person1, person2;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Use of Enumerated Data Type:

A

Clarify the operation of program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Weakness of enum:

A

There is no way to use enumerate values directly in input/output functions like printf() and scanf().
It will print the integer corresponding to it.
look into scanf().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A way to achieve exactly what enum does?

why use enums?

A
Using macro
#define SINGLE 0

MACROS are global.
enum can be global( if declared outside all functions) or local ( declared inside a function).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Renaming Data types with typedef:
define it.
rules.. uses..

A

typedef unsigned long int TWOWORDS;
Its purpose is to redefine the name of an existing variable type.
Preferred uppercase to show that we are dealing with a renamed data type.
Used especially when long data type names such as structure.
Its also used to rename pointer datatype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Use of typedef for structures:
and pointer:
Syntax

A

struct employee
{ … };
typedef struct employee EMP;
EMP e1,e2;

OR
typedef struct employee
{ ... } EMP;
EMP e1,e2;
OR
struct employee
{ ... int age; ...};
typedef struct employee *PEMP;
PEMP p;
p -> age = 32;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Typecasting:

A

To explicitly convert the value of an expression to a particular type.

example: (int)a;

It doesn’t permanently change. Rather it is the value of the expression that undergoes type conversion whenever the cast appears.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Bit Fields:

A

There are no 1 bit, 2 bit, 3-bit data type available in C. However when there are several values whose maximum value are small enough to pack in a single memory location, we can use ‘bit fields’ to store several values in a single integer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

declare a structure using bit field:

A

struct employee
{
unsigned gender : 1 ; unsigned mar_stat : 2;
};

The colon tells the compiler we are talking about bit fields and the number followed by it tells how many bits to allot for the field.
Notice Unsigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Pointers to Function:

A

int ( *func)()
{….}

Its prototype int (*func)();
func = funcName;
Means func is a pointer to a function, which returns int.
To call it: (*func)();

Address of function: funcName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Pointers to every type of variable? is it true?

A

Every type of variable has an address with exception of register.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Use of pointer to function:

A
  1. In writing memory resident programs.

2. Writing viruses, or vaccines to remove viruses.

17
Q

Functions returning pointer:

A

prototype: int *fun();
Declare:
int *func()
{ …. ; return &i ; }

Specially used to copy strings. Write a code to do so and check page 527 for ans.

18
Q

Functions With Variable Number of Arguments:

Define that funct:

A

For this include “stdarg.h”
which includes macro va_start, va_list, va_arg.
va_start is used to initialize a pointer to the beginning of the list of optional arguments.
va_start( ptr, num);
va_arg(ptr, int); /* int or char or float */

va_arg is used to advance the pointer to next arg (maybe return to where its pointing).
va_list ptr; declares a pointer of type va_list.

int findmax(int tot_num, ... )
{
}

Prototype: int func(int, … );

( … ) Ellipses indicate variable args.

19
Q

Unions, what are they?

How are they diff from structure?

A

Union offers a way for a section of memory to be treated as a variable of one type on one occasion, and as a diff type on another occasion.
In structure u can treat diff vars stored in diff memory locations.
Value stores in the element of union is same, u cant assign diff values. Union provides a way to look at same data in several diff ways.

20
Q

Declare Union:

and access union element:

A
union a
{ 
    int i;  char ch[2];
} ;
union a key;
key.i = 512;
21
Q

Memory Map of union:

A

union a
{ int i; char ch[2]; } ;
union a key;
key.i = 512;

0000 0000 0000 0010

int i takes two bytes, but low byte stored before high byte.
so, key.ch[0] = 0 and key.ch[1] = 2

22
Q

Union Of Structure:

A

Union can be nested.
union z
{ struct a key; struct b data; };
union z strange;

23
Q

Utility of Unions:

A

If u want to store data of employess, structure will be used. But if 2 elements are required for a type of emp and other 2 for other type, then those 4 elements are never going to be used simultaneously. Waste of memory.
So create union between those sets ( containing 2 structures) and then create a structure which has that union as element.

24
Q

The Volatile Quantifier:

A

When we define variables in a function the compiler may optimize the code that uses the variable, by using CPU register to store its value rather than stack.

volatile int j;
Declaring var as volatile, tells compiler not to optimize the code.
Then value of variable would be loaded from memory into register, operations would be performed on it and then result written back to the memory location allocated.

We might wanna use this when variable is not within program’s control and is changed from outside ( example digital thermometer changes temp ).

More info:
Volatile for better communications, u will have to access memory everytime to check var, this will allow external things to modify the var, say the temp changes, so the program can access the changed value.