we use the key words "CONSTANT" and "STATIC" while declaring the variables.
lets get in detail:
when we want the scope of the variable to be valid through out the program we declare the variable as static. to be more clear, the changes made to a static variable stands valid even when we move from one function to other function in the same program.
for example if we declare static int x=10 in main function and we incremented x and jumped to some other function and if we print x there, the value will be printed as 11.
coming to the CONSTANT, when we want the value of the variable to be constant all the time and should not be changed through out the program, we declare it as a constant.
hope u have got a clear idea by now. don't get confused with these two keywords as they look alike in sense.
A Little about the blog.....
This blog contains all the interview questions that i face during my interviews with various MNC's. I am trying to post the solutions for almost all the questions and i have covered few important topics which would be frequent topics in interviews. I hope the blog would be useful not only for the job aspirants but also to the one who tries to upgrade his/her knowledge in C and Embedded concepts......
please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....
please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....
Wednesday, November 16, 2011
difference between structures and unions
A structure is a group of variables of different data types whereas even an union is a group of variables of different data types.
what makes the union different from structure is the memory allocated for each variable in an union is the space required for the largest member in the union.
where as in the case of a structure the memory is allocated differently for different data type variables.
more ever,in the case of a union,a union can accommodate only one variable at a time, when a new variable enters, it automatically erases the old variable and new variable is stored in the place of old variable.
Explanation:
A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time.
You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.
A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used at a time. The size of a union is equal to the size of it's largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest, is the most you will need.
what makes the union different from structure is the memory allocated for each variable in an union is the space required for the largest member in the union.
where as in the case of a structure the memory is allocated differently for different data type variables.
more ever,in the case of a union,a union can accommodate only one variable at a time, when a new variable enters, it automatically erases the old variable and new variable is stored in the place of old variable.
Explanation:
A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time.
You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.
A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used at a time. The size of a union is equal to the size of it's largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest, is the most you will need.
print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!!
#include"stdio.h"
#include"conio.h"
int x=59; // ascii value for semicolon
void main()
{
int x;
clrscr();
printf("enter the ascii value");
scanf("%d",&x);
printf("the charcter for ascii value %d: is %c",x,x);
getch();
}
#include"conio.h"
int x=59; // ascii value for semicolon
void main()
{
int x;
clrscr();
printf("enter the ascii value");
scanf("%d",&x);
printf("the charcter for ascii value %d: is %c",x,x);
getch();
}
Tuesday, November 15, 2011
swapping of two numbers without using a temporary variable
#include"stdio.h"
#include"conio.h"
void main()
{
int x=10,y=20;
clrscr();
x=x+y;
y=x-y;
x=x-y;
printf("%d %d",x,y);
getch();
}
#include"conio.h"
void main()
{
int x=10,y=20;
clrscr();
x=x+y;
y=x-y;
x=x-y;
printf("%d %d",x,y);
getch();
}
finding the size of a variable without using the "sizeof()" operator.
int main()
{
double a = 5;
int b,c;
void *p = &a;
void *q = &a + 1;
b=(int)p;
c=(int)q;
printf("%d",c-b);
return 0;
}
{
double a = 5;
int b,c;
void *p = &a;
void *q = &a + 1;
b=(int)p;
c=(int)q;
printf("%d",c-b);
return 0;
}
Thursday, November 10, 2011
Different types of Pointers
The concept of pointers is the Beauty behind the use of C language. We know that, in general pointer refer to some address location in the memory.
There are different types of Pointers in C language that any C aspirant is ought to know about. let's have a brief glance about them.
1. pointer depending upon the type of the variable it is referring to.
ex: integer pointer, Character pointer, float pointer etc;
2. Dangling Pointer.
explanation: It is the pointer to a memory location where is varibale is stored
earlier but not present now ( destroyed or cleared or freed by the program).simply OBJECT DESTROYED BUT PATH PRESENT.
3. NULL Pointer.
explanation: To avoid the errors caused by the dangling pointer, the concept of a NULL POINTER is introduced. Whenever the NULL POINTER is dereferenced/freed, there is no scope for data corruption.
ex: #DEFINE NULL 0 //defining a null value
int *a; // defining a integer pointer
free(a); // the value at a is destroyed, hence a is a Dangling pointer
a=NULL; // now 'a' is a safe pointer
4. VOID Pointer.
explanation: also called as generic pointer. a void pointer can be of any type.
ex: *(int)p, *(float)p, *(char)p
5. CONSTANT POINTER.
explanation: the constant pointer's value cannot be changed once it is declared as CONSTANT.
ex: const int* const p= &a;
6. FUNCTION POINTER.
explanation: To store the address of a function,we need a function pointer.
ex: void main()
{void(*fp)();
fp= calling function name;
fp();
}
There are different types of Pointers in C language that any C aspirant is ought to know about. let's have a brief glance about them.
1. pointer depending upon the type of the variable it is referring to.
ex: integer pointer, Character pointer, float pointer etc;
2. Dangling Pointer.
explanation: It is the pointer to a memory location where is varibale is stored
earlier but not present now ( destroyed or cleared or freed by the program).simply OBJECT DESTROYED BUT PATH PRESENT.
3. NULL Pointer.
explanation: To avoid the errors caused by the dangling pointer, the concept of a NULL POINTER is introduced. Whenever the NULL POINTER is dereferenced/freed, there is no scope for data corruption.
ex: #DEFINE NULL 0 //defining a null value
int *a; // defining a integer pointer
free(a); // the value at a is destroyed, hence a is a Dangling pointer
a=NULL; // now 'a' is a safe pointer
4. VOID Pointer.
explanation: also called as generic pointer. a void pointer can be of any type.
ex: *(int)p, *(float)p, *(char)p
5. CONSTANT POINTER.
explanation: the constant pointer's value cannot be changed once it is declared as CONSTANT.
ex: const int* const p= &a;
6. FUNCTION POINTER.
explanation: To store the address of a function,we need a function pointer.
ex: void main()
{void(*fp)();
fp= calling function name;
fp();
}
Wednesday, November 9, 2011
Things to Remember : Storage classes in C
* SCOPE OF A VARIABLE: A variable defined in a block can be accessed in that block and all its internal blocks only (In default,a variable defined in block cannot be accessed by any other external blocks). It is called as the scope of a variable.
* To make the variable valid and accessible through out our operations, we define the variable with different storage classes as required per our needs.
* In default storage class (AUTO), the variable defined is valid as long as the control is in the block where it is defined. when the control comes out of the loop, the variable dies and the memory allocated is unreserved.
* For STATIC & GLOBAL variables, the memory is allocated as soon as the program gets loaded into the memory (before the execution of main() function) and hence these variables are also called as LOAD TIME VARIABLES. The memory for these variable gets de-allocated just before the termination of the program from the memory.
LIFETIME of a VARIABLE: The period for which after a variable is defined and allocated with memory and the memory allocated is deallocated is called the lifetime of the variable.
Note: The lifetime of a declared element is the period of time during which it is available for use. Variables are the only elements that have lifetime; for this purpose, the compiler treats procedure arguments and function returns as special cases of variables. The lifetime of a variable represents the period of time during which it can hold a value. Its value can change over its lifetime, but it always holds some value.
* To make the variable valid and accessible through out our operations, we define the variable with different storage classes as required per our needs.
* In default storage class (AUTO), the variable defined is valid as long as the control is in the block where it is defined. when the control comes out of the loop, the variable dies and the memory allocated is unreserved.
* For STATIC & GLOBAL variables, the memory is allocated as soon as the program gets loaded into the memory (before the execution of main() function) and hence these variables are also called as LOAD TIME VARIABLES. The memory for these variable gets de-allocated just before the termination of the program from the memory.
LIFETIME of a VARIABLE: The period for which after a variable is defined and allocated with memory and the memory allocated is deallocated is called the lifetime of the variable.
Note: The lifetime of a declared element is the period of time during which it is available for use. Variables are the only elements that have lifetime; for this purpose, the compiler treats procedure arguments and function returns as special cases of variables. The lifetime of a variable represents the period of time during which it can hold a value. Its value can change over its lifetime, but it always holds some value.
Subscribe to:
Posts (Atom)