Data Types Revisited Flashcards

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

Can 16-bit program work on 32-bit processor?

A

It would if it supports it, act like 16 bit processor. But some like Intel Itanium processors have withdrawn support for 16 bit code.

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

Storage of an integer?

A

Out of 2/4 bytes used to store the integer , the highest bit (16th/32nd) is used to store sign of the integer.
1 : -ve
0 : +ve

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

int data type :
and their diff and uses:

what about constants?

A

short int :
2 byte long.
shorts are never bigger than ints.
helps speed up, less memory.

Long Int:
4 byte long.

Declaration:
long int i; OR long i;
short int a; OT short a;
longs are never shorter than ints.
Gives Big range, but program runs slower.

Sometimes, constant is small enough to be an int, but still we want to give it more storage. For that we add suffix ‘L’ or ‘l’.

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

Unsigned Integer:

A

When we are sure an int will be positive in that case:
unsigned int i; unsigned i;
This gives double range, as highest bit (sign bit) is now free to be part of magnitude. 0 - 65535.
there exists:
short unsigned int and long unsigned int
both are signed by default.

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

Format specifiers for the data type learnt:

A
%c : char, unsigned char
%d : int, short int
%u : unsigned int, unsigned short int.
%ld : long int
%lu : unsigned long int
%f : float
%lf : double
%Lf : long double.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Chars, signed and unsigned:

A
Signed char, ordinary
range: -128 to 127.
a char in both occupy 1 byte.
Unsigned char ch;
from 0 to 255.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Floats and doubles:

A

Float:
occupies 4 byte, -3.4e38 to 3.4e38

Double:
occupies 8 bytes, -1.7e308 to 1.7e308

Long double:
10 bytes, -1.7e4932 to 1.7e4932

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

Accuracy of floating-point operation on diff compilers:

A

22.0/7.0
More accurate by VC++ compiler as compared to TC/TC++ compilers as TCs target compiled code to 8088/8086 (16bit) microprocessors which do not offer floating point support, TCs need a Floating point emulator which has limitations and thus less accurate.
This emulator becomes a part of EXE file, hence its size increase.
Increased size - performance penalty, since bigger code take more time to execute.

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

how are negative numbers stored?

A

2’s complement
= 1’s complement + 1
so 1000 0000 = - 128
129 = -127
128 will have nine bit value : 0 1000 0000
If we exceed the range from positive side we end up on the negative side.

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

Storage Classes In C:

What do they tell us?

A

there are 4 types:
- Automatic - Register -Static -External
To fully define a var this should also be defined, it does have a default value though.
Compiler assumes it depending on context in which var is used.
It tells us about:
- Where var is stored.
- It’s initial value if not declared.
- Scope of var
- Life of var.

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

Where are variables value stored:

A
  • Memory

- CPU registers

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

Automatic Storage Class

And declare it

A
  • Storage : Memory
  • Initial value : garbage
  • Scope : Local, in the block where declared only.
  • Life : Till control remains in the block

auto int i;

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

Register Storage Class:

A
  • Storage : CPU registers.
  • Garbage Initial Value.
  • Local block scope.
  • life till control in block.

loops using counter vars can be these for fast access.

register int i;

If no space on register available then, its considered as auto by the compiler.

If 16-bit microprocessor then they can’t hold float, double which req 4 bytes n 8 bytes so even if declared as registered compiler treats them as auto.

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

What is the benefit of storing var on CPU register?

but what hinders it?

A

We can access it faster.
So if a var is used frequently, its preferred to declare them as register.
Hindrance being, register has limited space not always available.

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

Static Storage class:

A

Memory.
Zero default initial value.
Scope is local to the block.
Value persists between different function calls.

static int i;

The initialization of var is done only once, for multiple function calls.

Avoid their use, as they take up memory since their value persists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
main() {
int *j;  int *func();
j = func()
printf( "%d", *j)
}
int *func()
{
int k = 35;
return (&k)
}
A

Func returns address of k.
j is pointer of k.
Garbage value is printed, since the stack does not stay the same when statement is executed, if it were static, it would print 35.

And text in book is lil confusing.. go through it again.

17
Q

External Storage Class:

A

Memory
Zero default initial value.
Scope : Global
life: Exists until program execution is terminated.
Available to functions defined in other files.= if in other files it is declared as extern.

extern int y;

or outside of all func:
int x;

18
Q

What if a static var is defined outside all functions?

A

Its scope is only tht file, its treated as an extern.

19
Q

how many times can u declare and define?

show which are declare n which r define:

A

U can declare many times.
but define only once.
if x is globally declared and defined
and then also locally, then local statements will prefer that local value.

During declaration, no memory is allocated. During definition it is.

Definitions:
auto int i;
static int i;
register int k;

Declaration:
extern int i;

extern int i = 0; // here memory allocated

20
Q

Function consisting of static vars.

those static vars , can they use stack or what?

A

Those static vars do not created on stack, but in a place in memory called ‘Data Segment’.
they die when program execution ends.

21
Q

Economise

when to use which storage class:

A
  • Use static only if its value needed to be persisted btw diff func calls.
  • Use register only for those which are used often as CPU register are scarce.
  • Use extern only for vars which are used by almost by all func. this would avoid unnecessary passing of these vars as arguments when making a func call. They remain active throughout so takes memory.
  • if no needs mentioned above, then go for auto. We usually don’t mind losing them.