What is the use of typedef?

typedef declaration helps to make source code of a C program more readable. Its purpose is to redefine the name of an existing variable type. It provides a short and meaningful way to call a data type. typedef is useful when the name of the data type is long. Use of typedef can reduce length and complexity of data types.

Note: Usually uppercase letters are used to make it clear that we are dealing with our own data type.

Example:

struct employee { char name[20]; int age;

};

struct employee e;

The above declaration of the structure would be easy to use when renamed using typedef as:

struct employee { More >

What is an Enumeration?

Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable. enum definition is similar to that of a structure.

Example: consider light_status as a data type. It can have two possible values – on or off.

enum light_status

{

on, off; };

enum light_status bulb1, bulb2;

/* bulb1, bulb2 are the variables */

Declaration of enum has two parts:

a) First part declares the data type and specifies the possible values, called ‘enumerators’.

b) Second part declares the variables of this data type. We can give values to these variables:

bulb1 = More >

What is Pass by Reference? Write a C program showing this concept. Pass by Reference ?

In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses, we would have an access to the actual arguments and hence we would be able to manipulate them. Pass by reference is accomplished using pointers.

Example:

#include<stdio.h>

/* function definition */

void swap(int *x, int *y) { int t;

t = *x; /* assign the value at address x to t */ *x = *y; /* put the value at y into x */ *y = t; /* put the value at to y */ }

main() {

int m = 10, n = More >

What is Pass by Value? Write a C program showing this concept.

Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling function.

Example:

#include<stdio.h>

swap(int x, int y) { int t;

t = x; x = y; y = t; }

main() {

int m = 10, n = 20;

printf(“\n Before executing swap m=%d n=%d”, m, n); swap(m, n);

printf(“\n After executing swap m=%d n=%d”, m, n); }

Output:

Before executing swap m=10 n=20 After executing swap More >

What is scope & storage allocation of static, local and register variables? Explain with an example.

Register variables: belong to the register storage class and are stored in the CPU registers. The scope of the register variables is local to the block in which the variables are defined. The variables which are used for more number of times in a program are declared as register variables for faster access.

Example: loop counter variables.

register int y=6;

Static variables: Memory is allocated at the beginning of the program execution and it is reallocated only after the program terminates. The scope of the static variables is local to the block in which the variables are defined.

Example:

void decrement {

static int a=5; a–;

printf(“%d”, a); More >

What is scope & storage allocation of global and extern variables? Explain with an example

Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The scope of the extern variables is Global.

Example:

//program in file f1.c

#include<stdio.h>

extern int x; main() {

printf(“value of x %d”, x); }

//program in file f2.c int x = 3;

Here, the program written in file f1.c has the main function and reference to variable x. The file f2.c has the declaration of variable x. The compiler should know the datatype of x and this is done by More >

What are the advantages of using unions?

Union is a collection of data items of different data types. It can hold data of only one member at a time though it has members of different data types. If a union has two members of different data types, they are allocated the same memory. The memory allocated is equal to maximum size of the members. The data is interpreted in bytes depending on which member is being accessed.

Example:

union pen { char name; float point; };

Here name and point are union members. Out of these two variables, ‘point’ is larger variable which is of float data type and it would need 4 More >

To which numbering system, can the binary number 1101100100111100 be easily converted to?

1101100100111100 can be easily converted to hexadecimal numbering system. Hexa-decimal integer constants consist of combination of digits from 0 to 9 and alphabets ‘A’ to ‘F’. The alphabets represent numbers 10 to 15 respectively. Hexadecimal numbers are preceeded by ’0x’.

1101,1001,0011,1100

1101 = D

1001 = 9 0011 = 3 1100 = C

1101,1001,0011,1100 = 0xD93C

Thus the given binary number 1101100100111100 in hexadecimal form is 0xD93C 8.

How to swap two numbers using bitwise operators?

Program:

#include<stdio.h> main() {

int i = 65; int k = 120;

printf(“\n value of i=%d k=%d before swapping”, i, k); i = i ^ k;

k = i ^ k; i = i ^ k;

printf(“\n value of i=%d k=%d after swapping”, i, k); }

Explanation:

i = 65; binary equivalent of 65 is 0100 0001

k = 120; binary equivalent of 120 is 0111 1000 i = i^k;

i…0100 0001

k…0111 1000

val of i = 0011 1001

k = i^k

i…0011 1001

k…0111 1000

val of k = 0100 0001 binary equivalent of this is 65

———(that is the initial value of i)

i = i^k

i…0011 1001

k…0100 0001

val of i = 0111 1000 binary More >

How to print below pattern?

1

2 3

4 5 6

7 8 9 10

Program:

#include<stdio.h>

main() {

int i, j, ctr = 1;

for (i = 1; i < 5; i++) {

for (j = 1; j <= i; j++)

printf(“%2d “, ctr++);

printf(“\n”);

}

}

Explanation:

for accessing name field of student2

There are two loops, a loop inside another one. Outer loop iterates 5 times. Inner loop iterates as many times as current value of i. So for first time outer loop is executed, inner loop is executed once. Second time the outer loop is entered, inner loop is executed twice and so on. And every time the program enters inner loop, value of variable ctr is printed More >