Operators and control Structures Flashcards

1
Q

void:

its uses:

A
1) to specify the return
type of a function when it is not returning any value, and (2) to indicate an empty argument list to a
function. Example:
void funct1(void);
Another interesting use of void is in the declaration of generic pointers. Example:
void *gp; // gp becomes generic pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

generic Pointer:

Rules:

A

A generic pointer can be assigned a pointer value of any basic data type, but it may not be dereferenced. For example,
int *ip; // int pointer
gp = ip; // assign int pointer to void pointer
are valid statements. But, the statement,
*ip = *gp;
is illegal. It would not make sense to dereference a pointer to a void value.

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

dereference:

A

To access the value or object located in a memory location stored in a pointer or another value interpreted as such; to access a value being referenced by something else. Attempting to dereference a null pointer often results in a crash.

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

Assigning void pointer in C and C++:

A

Assigning any pointer type to a void pointer without using a cast is allowed in both C++ and ANSI C.
In ANSI C, we can also assign a void pointer to a nonvoid pointer without using a cast to non-void
pointer type. This is not allowed in C++. For example,
void *ptr1;
char *ptr2;
ptr2 = ptr1;
are all valid statements in ANSI C but not in C++. A void pointer cannot be directly assigned to other
type pointers in C++. We need to use a cast operator as shown below:
ptr2 = (char *)ptr1;

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

Difference between Union and Structure:

Who is preferred when?

A

The size of a structure type is equal to the sum of the sizes of individual member types.
However, the size of a union is equal to the size of its largest member
element.

In simple words, we can say
that unions are memory-efficient alternatives of structures particularly in situations where it is not
required to access the different member elements simultaneously.
Union members share common memory space
for their exclusive usage.

I don’t understand:

All members of a structure can be manipulated
simultaneously.
The members of a union can be manipulated
only one at a time.

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

C++ structures and unions:

A
Structures in C++ behave just like a class.
Almost everything that can be achieved with a
class can also be done with structures.

Unions retain their core functionality in C++
with slight add-ons like declaration of
anonymous unions.

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

enums in C++ :

3 differences:

2 out of those 3

A

An enumerated data type is another user-defined type which provides a way for attaching names to
numbers, thereby increasing comprehensibility of the code.
The enum keyword (from C) automatically enumerates a list of words by assigning them values 0,1,2, and so on. This facility provides an
alternative means for creating symbolic constants.

Syntax:
enum shape{circle, triangle = 3,square};
[circle will be 0 by default, square will however be now 4 as subsequent initialized enumerators are larger by one than their predecessors.]

The enumerated data types differ slightly in C++ when compared with those in ANSI C. In C++,
the tag name shape become new type names. By using these tag names,
we can declare new variables. Examples:
shape ellipse; // ellipse is of type shape.

ANSI C defines the types of eums to be ints. In C++, each enumerated data type retains its own
separate type. This means that C++ does not permit an int value to be automatically converted to an
enum value.

Examples:
colour background = blue; // allowed
colour background = 7; // Error in C++
colour background = (colour) 7; // OK
However, an enumerated value can be used in place of an int value.
int c = red; // valid, colour type promoted to int

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

3rd difference:

A

C++ also permits the creation of anonymous enums (i.e., enums without tag names).
Example:
enum{off, on};
Here, off is 0 and on is 1. These constants may be referenced in the same manner as regular
constants. Examples:
int switch_1 = off;
int switch_2 = on;

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

Applications:

and scopes in C and C++?

A

In practice, enumeration is used to define symbolic constants for a switch statement.
ANSI C permits an enum to be defined within a structure or a class, but the enum is globally visible.
In C++, an enum defined within a class (or structure) is local to that class (or structure) only.

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

Storage class:

Keyword

A

auto
extern
static
Register

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

Array in C and C++:

A

When initializing a character array in ANSI C, the compiler will allow us to declare the
array size as the exact length of the string constant. For instance,
char string[3] = “xyz”;
is valid in ANSI C.
It assumes that the programmer intends to leave out the null character \0 in the definition.
But in C++, the size should be one larger than the number of characters in the string.
char string[4] = “xyz”; // O.K. for C++

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

Pointer:

Additional feature in C++:

A
C++ adds the concept of constant pointer and pointer to a constant.
char * const ptr1 = “GOOD”;           // constant pointer

We cannot modify the address that ptr1 is initialized to.
int const * ptr2 = &m; // pointer to a constant

ptr2 is declared as pointer to a constant. It can point to any variable of correct type, but the contents
of what it points to cannot be changed.

We can declare both the pointer and the variable as constants:
const char * const cp = “xyz”;
This statment declares cp as a constant pointer to the string which has been declared a constant.
In this case, neither the address assigned to the pointer cp nor the contents it points to can be
changed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create Symbollic Constants:

A

There are two ways of creating symbolic constants in C++:
• Using the qualifier const, and
• Defining a set of integer constants using enum keyword.

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

const in C and C++
regarding type constants:

and naming conventions:

A
. In C++, we can use const in a constant
expression, such as
const int size = 10;
char name[size];
This would be illegal in C. const allows us to create typed constants instead of having to use
#define to create constants that have no type information.
As with long and short, if we use the const modifier alone, it defaults to int. For example,
const size = 10;
means
const int size = 10;
The named constants are just like variables except that their values cannot be changed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Const

Intialising and scope
in c and c++

A

C++ requires a const to be initialized. ANSI C does not require an initializer; if none is given, it
initializes the const to 0.

The scoping of const value differs. A const in C++ defaults to the internal linkage and therefore,
it is local to the file where it is declared. In ANSI C, const values are global in nature. They are visible
outside the file in which they are declared. However, they can be made local by declaring them as
static. To give a const value an external linkage so that it can be referenced from another file, we
must explicitly define it as an extern in C + + . Example:
extern const total = 100;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Define integers const without using keyword const:

A
Another method of naming integer constants is by enumeration as under;
enum {X,Y,Z};
This defines X, Y and Z as integer constants with values 0, 1, and 2 respectively. This is equivalent to
const X = 0;
const Y = 1;
const Z = 2;
We can also assign values to X, Y, and Z explicitly. Example:
enum{X=100, Y=50, Z=200};
17
Q

TYPE COMPATIBILITY:

reason ur statement.

char constant storage in C and C++:

A

In C++, the types of values must be the same for
complete compatibility, or else, a cast must be applied.

These restrictions in C++ are necessary in
order to support function overloading where two functions with the same name are distinguished
using the type of function arguments.

Another notable difference is the way char constants are stored. In C, they are stored as ints, and
therefore,
sizeof (‘x’)
is equivalent to
sizeof(int)
in C. In C++, however, char is not promoted to the size of int and therefore,
sizeof(‘x’)
equals
sizeof(char)
18
Q

DECLARATION OF VARIABLES

A

they saying in C we declared all at the beginning of the scope, then we at the time of use we had to go up and check what type as well, so in C++ we declare them at the time of use but here disadvan being that at a glance we cant tell all vars used, but I am confused cua I thought in C we did both ways..!!

19
Q

New Literal Constant in C++:

A

L‘ab’ // wide-character constant

The wchar_t type is a wide-character literal introduced by ANSI C++ and is intended for character
sets that cannot fit a character into a single byte. Wide-character literals begin with the letter L

20
Q

Strings in C++

A
C++ supports two types of string representation — the C-style character string and the string class type
introduced with Standard C++. Although the use of the string class type is recommended, it is advisable
to understand and use C-style strings in some situations. The string class type strings support many
features and are discussed in detail in Chapter 13.
21
Q

DYNAMIC INITIALIZATION OF VARIABLES:

A

In C, a variable must be initialized using a constant expression, and the C compiler would fix the
initialization code at the time of compilation. C++, however, permits initialization of the variables at run
time. This is referred to as dynamic initialization. In C++, a variable can be initialized at run time using
expressions at the place of declaration.

int n = strlen(string);
…..
float area = 3.14159 * rad * rad;
Thus, both the declaration and the initialization of a variable can be done simultaneously at the place
where the variable is used for the first time.

float average; // declare where it is necessary
average = sum/i;
can be combined into a single statement:
float average = sum/i; // initialize dynamically at run time