21 February 2012

Let us C


Do you know what a pointer is?

A pointer is a special type of variable in C. Pointer Is used to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as '*', '%' operators. It follows a special arithmetic called as pointer arithmetic.

A pointer is declared as:
int *ap;
int a = 5;
In the above two statements an integer a was declared and initialized to 5.
A pointer to an integer with name ap was declared.
Next before ap is used
ap=&a;

This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.
Operations on a pointer:
Dereferencing operator ' * ':
This operator gives the value at the address pointed by the pointer . For example after the above C statements if we give
printf("%d",*ap);
Actual value of a that is 5 would be printed. That is because ap points to a. Addition operator ' + ': Pointer arithmetic is different from ordinary arithmetic. Expression ap=ap+1; Would not increment the value of ap by one byte but would increment it by the number of bytes of the data type it is pointing to. Here ap is pointing to an integer variable hence ap is incremented by 2 or 4 bytes depending upon the compiler.


What does a static variable mean?

I already described about static variable when we are discussing about keywords. Refer that for more. Here I just define the static variables

A static variable is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. A static variable can be initialized by using keyword static before variable name.

For Example:
static int a = 5;

A static variable behaves in a different manner depending upon whether it is a global variable or a local variable. A static global variable is same as an ordinary global variable except that it cannot be accessed by other files in the same program / project even with the use of keyword extern. A static local variable is different from local variable. It is initialized only once no matter how many times that function in which it resides is called. It may be used as a count variable.

Example:
[code]
void count(void) {
static int count1 = 0;
int count2 = 0;
count1++;
count2++;
printf("\nValue of count1 is %d Value of count2 is %d", count1,
count2);
}
//In Main function:
main() {
count();
count();
count();
}
[\code]
Output would be:
Value of count1 is 1 Value of count2 is 1
Value of count1 is 2 Value of count2 is 1
Value of count1 is 3 Value of count2 is 1

Do you know structure in C?

A structure is a collection of pre-defined data types to create a user-defined data type. Let us say we need to create records of students. Each student has three fields:

int roll_number;
char name[30];
int total_marks;

This concept would be particularly useful in grouping data types. You could declare a
Structure student as:

struct student {
int roll_number;
char name[30];
int total_marks;
} student1, student2;

The above snippet of code would declare a structure by name student and it initializes two objects student1, student2. Now these objects and their fields could be accessed by saying student1.roll_number for accesing roll number field of student1 object Similarly student2.name for accessing name field of student2 object.

Can u print below pattern?

1
2 3
4 5 6
7 8 9 10

Program:
[code]
#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");
}
}
[\code]

Explanation:

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 and is incremented by 1. %2d ensures that the number is printed in two spaces for proper alignment.

How to swap two numbers using bitwise operators?

Program:
[code]
#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);
}
[\code]

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 equivalent of this is 120
---------(that is the initial value of k)

What is recursion? Write a program using recursion (factorial)?

Recursion: A function is called 'recursive' if a statement within the body of a function calls the same function. It is also called 'circular definition'. Recursion is thus a process of defining something in terms of itself.

Program: To calculate the factorial value using recursion.
[code]
#include<stdio.h>
int fact(int n);
main() {
int x, i;
printf("Enter a value for x: \n");
scanf("%d", &x);
i = fact(x);
printf("\nFactorial of %d is %d", x, i);
}
int fact(int n) {
/* n=0 indicates a terminating condition */
if (n <= 0) {
return (1);
} else {
/* function calling itself */
return (n * fact(n - 1));
/*n*fact(n-1) is a recursive expression */
}
}
[\code]
Output:
Enter a value for x:
4
Factorial of 4 is 24
Explanation:
fact(n) = n * fact(n-1)
If n=4
fact(4) = 4 * fact(3) there is a call to fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) = 1
fact(1) = 1 * 1 = 1
fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6
Thus fact(4) = 4 * 6 = 24
Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the program enters into an infinite loop.

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 preceded 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

Can you differentiate structures and unions?

Structures and Unions are used to store members of different data types.

STRUCTURE

a)Declaration:
[code]
struct
{
data type member1;
data type member2;
};
[\codde]

b)Every structure member is allocated memory when a structure variable is defined.

Example:
[code]
struct emp {
char name[5];
int age;
float sal;
};
struct emp e1;
[\code]

Memory allocated for structure is
1+2+4=7 bytes. 1 byte for name, 2 bytes for age and 4 bytes for sal.

c)All structure variables can be initialized at a time
[code]
struct st {
int a;
float b;
};
struct st s = { .a=4, .b=10.5
};
[\code]
Structure is used when all members are to be independently used in a program.

UNION

a)Declaration:
[code]
union
{
data type member1;
data type member2;
};
[\code]

b)The memory equivalent to the largest item is allocated commonly for all members.

Example:
[code]
union emp1 {
char name[5];
int age;
float sal;
};
union emp1 e2;
[\code]

Memory allocated to a union is equal to size of the largest member. In this case, float is the largest-sized data type. Hence memory allocated to this union is 4 bytes.

c)Only one union member can be initialized at a time
[code]
union un {
int a;
float b;
};
union un un1 = { .a=10 };
[\code]
Union is used when members of it are not required to be accessed at the same time.

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:
[code]
union pen {
char name;
float point;
};
[\code]

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 bytes of memory. Therefore 4 bytes space is allocated for both the variables. Both the variables have the same memory location. They are accessed according to their type. Union is efficient when members of it are not required to be accessed at the same time.

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:
[code]
//program in file f1.c
#include<stdio.h>
extern int x;
main() {
printf("value of x %d", x);
}
[\code]
//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 data type of x and this is done by extern definition.

Global variables: are variables which are declared above the main( ) function. These variables are accessible throughout the program. They can be accessed by all the functions in the program. Their default value is zero.

Example:
[code]
#include<stdio.h>
int x = 0;/* variable x is a global variable */
/* It can be accessed throughout the program */
void increment(void) {
x = x + 1;
printf("\n value of x: %d", x);
}
main() {
printf("\n value of x: %d", x);
increment();
}
[\code]

No comments:

Post a Comment