Sunteți pe pagina 1din 5

REG NO : BBIT/55742/2016

NAME : PAUL MUTHURI KIRAMANA

COURSE : BARCHELOR IN BUSINNES INFORMATION TECHNOLOGY

UNIT CODE : BIT1208

UNIT NAME : STRUCTURED PROGRAMMING

TASK : CAT 1

1
1. What are enumerations? [2 Marks]

An enumeration is a user-defined data type that consists of integral constants. To define an


enumeration, keyword enum is used.
An enumerated data type gives you an opportunity to invent your own data type and define what values
the variable of this data type can take. This can help in making the program listings more readable,
which can be an advantage when a program gets complicated.

2. Describe about storage allocation and scope of global, extern, static, local and register variables?
[2 Marks]
Global variables.
Globals have application-scope. They are available in any compilation unit that includes an appropriate
declaration (usually brought from a header file). They're stored wherever the linker puts them, usually a
place called the BSS segment.

Extern.
The extern storage class is used to give a reference of a global variable that is visible to all the program
files. When you use 'extern', the variable cannot be initialized, however, it points the variable name at a
storage location that has been previously defined.

Static
Stored the same place as globals, typically, but only available to the compilation unit that contains
them. If they are block-scope global, they are only available within that block and its sub blocks.

Local
Stored on the stack, typically. Only available in that block and its sub blocks. (Although pointers to locals
can be passed to functions invoked from within a scope where that local is valid.)

Register
The register storage class is used to define local variables that should be stored in a register instead of
RAM. This means that the variable has a maximum size equal to the register size (usually one word) and
can't have the unary '&'operator applied to it (as it does not have a memory location). The register
should only be used for variables that require quick access such as counters.

3. What are register variables? What are the advantages [2 Marks?]

These variables inform the compiler for storing the variables in a register of the CPU.
These variables are stored in the registers, so the speed of processing is to become more than the
normal variables.

Advantages

 Access optimization and speed of program execution: The operations of these variables are
faster by orders of magnitude.
 It is useful when you want to refer a variable frequently.
 It allocates fast memory in the form of a register.
 It helps to speed up program execution.

2
4. What is the use of typedef? [2 Marks]

The keyword typedef is used for defining user defined data types. A new definition of existing data types
is created by using typedef. It is used to define user defined identifiers which can be used in substitution
of type specifiers such as int, float etc. It does not reserve memory space. The names defined by typedef
are synonyms for the data types.

5. Can we specify variable field width in a scanf ()? Out of fgets () and gets () which function is safe to
use and why? [2 Marks]

It is possible to specify variable field width in a scanf () format string by using %s control string.
The %s reads a string of variable field width up to the first white space character.

fgets () is safer than gets (), because we can specify a maximum input length. Neither one is completely
safe, because the compiler can't prove that programmer won't overflow the buffer he pass to fgets ().

6. Difference between strdup and strcpy? [2 Marks]


 The function strcpy () will not allocate the memory space to copy. It should be given a pointer to
the string to copy and a pointer to place to copy.
The function strdup () will occupy / grab itself the memory space for copying the string to copy.
This memory space needs to be freed up later when it is of no use anymore.
 strcpy wants a buffer to copy into. strdup allocates a buffer using malloc().
 Unlike strcpy (), strdup () is not specified by ANSI.

7. What is recursion?

Recursion is the process by which a function calls itself repeatedly until a special condition is satisfied.
To use recursion, two conditions must be satisfied:
 The problem must be written in a recursive form.
 There must be a stopping case (terminating condition).

8. Differentiate between a for loop and a while loop? What are it uses? [2 Marks]

 For loop is used for executing a set of statements fixed number of times. A While loop is used
when the number of iterations to be performed is not known in advance.
 The while loop is usually used when you need to repeat something until a given condition is
true. On the other hand, the for loop is usually used when you need to iterate a given number of
times
9. What is storage class? What are the different storage classes in C? [2 Marks]

The storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope
and linkage.
C storage classes determine how a variable is stored.
The storage classes in c are auto, register, and extern, static, typedef.

3
10. What the advantages of using Unions? [2 Marks]

 The basic advantage is that union will use the memory space of the data type which ha s the
highest memory, hence memory consumption will be less.
 Convert one data type to another
 Allows to store different datatypes in the same memory location.

11. What is the difference between Strings and Arrays? [2 Marks]

 An array is a collection of like variables that share a single name. The individual elements of an
array are referenced by appending a subscript, in square brackets, behind the name. The
subscript itself can be any legitimate C expression that yields an integer value, even a general
expression. Therefore, arrays in C may be regarded as collections of like variables. Although
arrays represent one of the simplest data structures, it has wide-spread usage in embedded
systems.

Strings are similar to arrays with just a few differences. Usually, the array size is fixed, while
strings can have a variable number of elements. Arrays can contain any data type
(char short int even other arrays) while strings are usually ASCII characters terminated with a
NULL (0) character.

 The main difference between the two is that arrays can have any data type of any length while
strings are usually ASCII characters that are terminated with a null character ‘\0’.

12. What is a far pointer? Where we use it? [2 Marks]

A far pointer is typically 32 bit that can access memory outside current segment. To use this, compiler
allocates a segment register to store segment address, then another register to store offset within
current segment.
In a segmented architecture of computer, a far pointer is a pointer which includes a segment selector,
making it possible to point to addresses outside of the default segment.

13. What is a huge pointer? [2 Marks]

Huge pointer is 32bit long containing segment address and offset address. Huge pointers are normalized
pointers so for any given memory address there is only one possible huge address segment: offset pair.
Huge pointer arithmetic is done with calls to special subroutines so its arithmetic slower than any other
pointers.

14. What is a normalized pointer, how do we normalize a pointer? [2 Marks]

It is a 32bit pointer, which has as much of its value in the segment register as possible. Since a segment
can start every 16bytes so the offset will have a value from 0 to F.
How to normalize a pointer
Convert the address into 20bit address then use the 16bit for segment address and 4bit for the offset
address.

4
15. What is near pointer.[2 Marks?]

This is a pointer which can point only 64KB data segment or segment number 8 .Near pointer is used to
store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we can
only access 64kb of data at a time. Near pointer cannot access beyond the data segment like graphics
video memory, text video memory etc. Size of near pointer is two byte. With help keyword near, we can
make any pointer as near pointer.

REFERENCES

 www.careerride.com
 users.ece.utexas.edu
 www.geeksforgeeks
 www.tutorialspoint.com

S-ar putea să vă placă și