Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Answer: Array is nothing but a pointer in disguise. Array name stores base address of array.
Thus we can say array points to a memory location of fixed size of a particular data type.
arr[1] is same as *(arr+1)
{In fact 1[arr] is same as arr[1] because they both are converted into *(1+arr) and *(arr+1)}
However array is not same as pointer. We may make a pointer point to an arbitrary location
however an array always points to a constant memory location.
Eg.
int a[5]={1,2,3,4,5};
int *p=a;
=>p=&a[2]; or p++; are both correct but similar statements for array 'a' will invoke error.
2. What value is assigned to the below variables?
int X1 = 13/3;
int X2 = 13%3;
Answer:
The value of X1 is 4
The value of X2 is 1
Both auto variable and register variable are local variables. Register variables are stored
in register memory. Whereas, auto variables are stored in main CPU memory.
Register variables will be accessed very faster than the normal/auto variables since they
are stored in register memory rather than main memory.
But, only limited variables can be used as register since register size is very low. (16 bits,
32 bits or 64 bits)
Static variables can retain the value of the variable between different function calls.
But, scope of auto variable is within the function only. It cant retain the value of the
variable between different function calls.
retrieve the value of a particular element, refer to the element number. For example: if you have
a declaration that says intscores[5];, then you have 5 accessible elements, namely: scores[0],
scores[1], scores[2], scores[3] and scores[4].
7.
Can I use int data type to store the value 32768? Why?
Answer: No. int data type is capable of storing values from -32768 to 32767. To store 32768,
you can use long int instead. You can also use unsigned int, assuming you dont intend to
store negative values.
assigned to a variable. When using getche(), the key that was pressed by the user will appear on
the screen, while at the same time being assigned to a variable.
Dangling pointers arise when an object is deleted or deallocated, without modifying the
value of the pointer, so that the pointer still points to the memory location of the deallocated
memory. Following are examples.
Output:"o World"
Long pointer increment by one increases the value of the pointer by 4 bytes. Initially the pointer
was at &str[0]. After increment, the pointer will be at &str[4].
19. What is a self-referential structure?
Answer: A structure containing the same structure pointer variable as its element is called as
self-referential structure.
structure type to a function. This method helps maintain consistency and uniformity in terms of
argument type.
Answer: Some important distance between calloc and malloc are given below:
malloc:
malloc takes only the "size" of the memory block to be allocated as input parameter.
malloc allocates memory as a single contiguous block.
if a single contiguous block cannot be allocated then malloc would fail.
calloc:
calloc takes two parameters: the number of memory blocks and the size of each block of memory
calloc allocates memory which may/may not be contiguous.
all the memory blocks are initialized to 0.
it follows from point 2 that, calloc will not fail if memory can beallocated in non-contiguous
blocks when a single contiguous blockcannot be allocated.
24. What is the difference between String and Array?
Answer: Main difference between String and Arrays are:
A string is a specific kind of an array with a well-known convention to determine its length
where as An array is an array of anything.
In C, a string is just an array of characters (type char)and always ends with a NULL character.
The value of an array is the same as the address of (or a pointer to) the
first element; so, frequently, a C string and a pointer to char are used to mean the same thing.
An array can be any length. If its passed to a function, theres no way the function can tell
how long the array is supposed to be, unless some convention is used. The convention for strings
is NULL termination; the last character is an ASCII NULL character.
Array can hold group of element of the same the type, and which allocates the memeory for each
element in the array,if we want we can obtain the particular value from an array by giving index
value.
Ex: int a[10];
a variable holds the values of integer at max 10.
String can hold any type of data, but it makes together we can't obtain the particular value of the
string.
String s="Pra12";
Arguments available in the funtion definition are called formal arguments. Can be preceded by
their own data types.
We use Actual arguments in the function call.
30. What is the purpose of main( ) function?
Answer: Main function is to be called when the program getting started.
1.It is the first started function.
2.Returns integer value.
3.Recursive call is allowed for main() also.
4.it is user-defined function
5.It has two arguments:
5.1)argument count
5.2) argument vector(strings passed)
6.We can use any user defined name as parameters for main(),behalf of argc and argv.
31. What are the advantages of the functions?
Answer: Some main advantages of the functions are given below:
1.It makes Debugging easier.
2.Using this we can easily understand the programs logic.
3.It makes testing easier.
4.It makes recursive calls possible.
5.These are helpful in generating the programs.
32. What is a pointer variable?
Answer: A pointer variable is used to contain the address of another variable in the memory.
33. What is a pointer value and address?
Answer: It is a data object,it refers to a memory location.we numbered every memory locaion in
the memory.
Numbers that we attached to a memory location is called the address of the location.
Answer: Enumeration is a kind of type.It is used to hold the group of constt values that are given
by programmer.After defining we can use it like integer type.e.g.
enum off,on;
Here,off and on are 2 integer constants.These are called enumerators.By default value assign to
enumurator are off.
44. What is the difference between #include <file> and #include ?file?
Answer: In C we can include file in program using 2 type:
1.#include
2.#include?file?
1.#include:In this which file we want to include surrouded by angled brakets(< >).In this method
preprocessor directory search the file in the predefined default location.For instance,we have
given include variable
INCLUDE=C:COMPILERINCLUDE;S:SOURCEHEADERS;
In this compiler first checks the C:COMPILERINCLUDE directory for given file.When there it
is not found compiler checked the S:SOURCEHEADERS directory.If file is not found there also
than it checked the current directory.
#include method is used to include STANDARDHEADERS, like:stdio.h,stdlib.h.
2.#include?file?:In this which file we want to include surrouded by quotation marks(" ").In this
method preprocessor directory search the file in the current location.For instance,we have given
include variable
INCLUDE=C:COMPILERINCLUDE;S:SOURCEHEADERS;
In this compiler first checks the current directory.If there it is not found than it checks
C:COMPILERINCLUDE directory for given file.When there it is not found compiler,than
checked the S:SOURCEHEADERS directory.
#include?file?method is used to include NON-STANDARDHEADERS.These are those header
files that created by programmers for use .
45. Difference between malloc and calloc?
Answer: Some main and important difference b/n malloc and calloc are given below:
malloc is use for memory allocation and initialize garbage values. where as calloc is same as
malloc but it initialize 0 value.
example:
// This allocats 200 ints, it doesn't initialize the memory:
int *m = malloc(200*sizeof(int));
// This, too, allocates 200 ints, but initializes the memory
// to zero:
int *c = calloc(200,sizeof(int));
46. Can we execute printf statement without using semicolan?
Answer: Yes,We can execute printf statement without using semicolan. When we write printf
inside the if,for,while,do while etc it gives output.
Example:
void main()
{
if(printf("R4R Welcomes you!"))
{
/*statements*/
}
}
void main()
{
while(printf("Welcome"))
{
/*statements*/
}
}
47. Is C is platform dependent or independent?how and why?
49. how to switch the values of two variable without using third variable?
Answer: I have given a example which switch the values of two variables without using third
variable.
#include
void main()
{
int val1,val2;
val1=5;
val2=6;
/* now adding val2 to val1 we have */;
val1=val2+val1;
val2=val1-val2;
/* now here val2 will get value of val1 */;
val1=val1-val2
/*this will give val1 value of val2 */;
}
Answer: Basically header files are the library functions.Header files have definitions of
functions.These are required to execute the specific operation of function.we write header file
with .h extension.Generally these are pre-defined fuctions,But we can also generate
our own header file.
Example:
stdio.h header file is use to perform function like prinf and scanf.
conio.h header file is use to perform function like clrscr and getch.
52. What is macro?
Answer: Macros are the fragment of code.That are used to given a name.When we want to used
thew name it is being replaced by content of macros.we can differentiate macros in terms of what
they look like & when they are used.Macros are of two types:
1.Object-like macros.
2.Function-like macros.
When we used Object-like macros It resembles data objects.Where as when we using
function-like macros it resembles function calls.
In this array of integers are created.Than first member in the array are addressed as Array[0] and
last member has been declared as Array[9].
Example:
#define MAX_SIZE 10
int Array[MAX_SIZE];
int n;
for (n = 1; n <= MAX_SIZE; n++)
{
Array[n] = n;
}
Definition of an Array:
We know that we declare an array like that,
int Array[10];
When an array is external, it must be defined
in any other source files that may need to access it. Because you dont want the compiler
to reallocate the storage for an array, you must tell the compiler that the array is allocated
externally and that you want only to access the array.
60. What are the pointers?How to declare them?
Answer: Pointers are may be of variable or constant.The contains the address of object.Some
important characteristics of pointers are given below:
1.Pointer is used to contain the address of any object.It may be an array,a singular variable,a
union and a structure.
2.It is also used to store the address of a function.
3.It can not hold the address of the constant.
Remember that when a pointer getting incremented than its value is incremented by the sizeof()
the pointer's type.
We can declare and initialize the pointer like that:
int counter= 0;
int *pcounter;
pcounter = &counter;
61. What are the Bit Operators?
Answer: Bit operators are different frm logical operator. So,don't confuse with them.The
keyword TRUE signifies a true bit (or bits) that is set to one,
and FALSE signifies a bit (or bits) that is set to zero.
Some Bitwise Operators are given below:
&: Performs a bitwise AND operation. If both operands are TRUE, the result is TRUE;
otherwise, the result is FALSE.
|: Performs a bitwise OR operation. If either operand is TRUE, the result is TRUE; otherwise, the
result is FALSE.
^: Performs a bitwise exclusive OR operation. If both operands are TRUE or both operands are
FALSE, the result is FALSE. The result is TRUE if one operand is TRUE and the other is
FALSE. Exclusive OR is used to test to see that two operands are different.
<<: Shifts the X operand, Y operand bits to the left. For example, (1 << 4) returns a value of 8. In
bits, (0001 << 4) results in 1000. New
positions to the left are filled with zeroes. This is a quick way to multiply by 2, 4, 8, and so on.
>>: Shifts the X operand, Y operand bits to the right. For example, (8
>> 4) returns a value of 1. In bits, (1000 >> 4) results in 0001. New positions to the right are
filled with ones or zeroes, depending on the value and whether the operand being shifted is
signed. This is a quick way to divide by 2, 4, 8, and so on.
// Using a bitwise AND:
if (x & y)
{
// With x == 1, and y == 2, this will NEVER be TRUE.
}
62. How to use free() function?
Answer: Free() function is used to return the memory to the operating system after use.Because
is most of time very limited source.So,we have to try to made max output.
Example:
#include
#include
int main ()
{
int *R1, *R2, *R3;
R1 = (int*) malloc (10*sizeof(int));
R2 = (int*) calloc (10,sizeof(int));
R3 = (int*) realloc (R2,50*sizeof(int));
free (R1);
free (R3);
return 0;
}
63. How to differentiate local memory and global memory?
Answer: Local and global memory allocation is applicable with Intel 80x86 CPUs.
Some main difference b/n local and global variables are given below:
1.Using local memory allocation we can access the data of the default memory segment,Where
as with global memory allocation we can access the default data,located outside the
segment,usually in its own segment.
2.If our program is a small or medium model program , the default memory pool is local,Where
as If our program uses the large or compact memory model, the default memory pool is global.
64. What are the Text Files and Binary Files?
Answer: Text Files: Text Files are those files that are display on the screen.These files are
readable files.It has not uses only few special characters like,tab and newlines.Text Files
generally introduce by programmers.
Binary Files: Binary Files are those files that can contain any data, including the internal
representation of numbers, special control characters.
65. What is a data type?
Answer: It is means to identify type of data. When we are storing something in our program
then we need to allocate some space for that purpose and while allocating that
space we must also specify what kind of data is to be stored in the
allocated space.
Data types help to solve this problem.
66. Give example of some data types?
Answer: Int, char, float, void etc. are all examples of predefined basic primitive data types.
Structures and Unions are user defined data types.
67. Is it possible to bring about conversion from one data type to other?
Answer: Yes, most programming languages provide ways for data to be converted from one data
type to other. Data conversion can be both implicit and explicit. Implicit type conversion
involves automatic type promotion. Explicit type conversion is also called type casting and may
lead to loss in data precision.
68. What is the meaning of prototype of a function?
Answer: Prototype of a function
Declaration of function is known as prototype of a function. Prototype of a function means
(1) What is return type of function?
(2) What parameters are we passing?
(3) For example prototype of printf function is:
int printf(const char *, );
I.e. its return type is int data type, its first parameter constant character pointer and second
parameter is ellipsis i.e. variable number of arguments.
int i = 0 , a[3] ;
a[i] = i++;
printf (%d",a[i]) ;
}
Answer: The output for the above code would be a garbage value. In the statement a[i] = i++; the
value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get
incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.
71. Why doesn't the following code give the desired result?
ANSWER: int x = 3000, y = 2000 ;
long int z = x * y ;
Answer: Here the multiplication is carried out between two ints x and y, and the result that
would overflow would be truncated before being assigned to the variable z of type long int.
However, to get the correct output, we should use an explicit cast to force long arithmetic as
shown below:
long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.
72. Why doesn't the following statement work?
ANSWER:
Answer: The string function strcat( ) concatenates strings and not a character. The basic
difference between a string and a character is that a string is a collection of characters,
represented by an array of characters whereas a character is a single character. To make the
above statement work writes the statement as shown below:
strcat ( str, "!" ) ;
73. How do I know how many elements an array can hold?
ANSWER: The amount of memory an array can consume depends on the data type of an array.
In DOS environment, the amount of memory an array can consume depends on the current
memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more
than 64 kb. Consider following program, which shows the maximum number of elements an
array of type int, float and char can have in case of Small memory model.
main( )
{
int i[32767] ;
float f[16383] ;
char s[65535] ;
}
74. How do I write code that reads data at memory location specified by segment and offset?
ANSWER: Use peekb( ) function. This function returns byte(s) read from specific segment and
offset locations in memory. The following program illustrates use of this function. In this
program from VDU memory we have read characters and its attributes of the first row. The
information stored in file is then further read and displayed using peek( ) function.
#include
#include
main( )
{
char far *scr = 0xB8000000 ;
FILE *fp ;
int offset ;
char ch ;
if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL )
{
printf ( "\nUnable to open file" ) ;
exit( ) ;
}
// reads and writes to file
for ( offset = 0 ; offset < 160 ; offset++ )
fprintf ( fp, "%c", peekb ( scr, offset ) ) ;
fclose ( fp ) ;
offset (which will be the offset to access the integer variable) but as the actual data object is to be
printed is not present at that memory location so it will print what ever will be the contents of
that memory location.
3. Some compilers check the format string and will generate an error without the proper number
and type of arguments for things like printf(...) and scanf(...).
77. malloc() Function- What is the difference between "calloc(...)" and "malloc(...)"?
ANSWER: 1. calloc(...) allocates a block of memory for an array of elements of a certain size.
By default the block is initialized to 0. The total number of memory allocated will be
(number_of_elements * size).
malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...)
allocated bytes of memory and not blocks of memory like calloc(...).
2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL
if there is insufficient memory available.
calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the
allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to
set the new handler mode.
78. In printf() Function- What is the difference between "printf(...)" and "sprintf(...)"?
ANSWER: sprintf(...) writes data to the character array whereas printf(...) writes data to the
standard output device.
79. Compilation How to reduce a final size of executable?
ANSWER: Size of the final executable can be reduced using dynamic linking for libraries.
80. Linked Lists -- Can you tell me how to check whether a linked list is circular?
ANSWER: Create two pointers, and set both to the start of the list. Update each as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}
}
If a list is circular, at some point pointer2 will wrap around and be either at the item just before
pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.
81. string Processing --- Write out a function that prints out all the permutations of a string. For
example, abc would give you abc, acb, bac, bca, cab, cba.
ANSWER: void PrintPermu (char *sBegin, char* sRest) {
int iLoop;
char cTmp;
char cFLetter[1];
char *sNewBegin;
char *sCur;
int iLen;
static int iCount;
iLen = strlen(sRest);
if (iLen == 2) {
iCount++;
printf("%d: %s%s\n",iCount,sBegin,sRest);
iCount++;
printf("%d: %s%c%c\n",iCount,sBegin,sRest[1],sRest[0]);
return;
} else if (iLen == 1) {
iCount++;
printf("%d: %s%s\n", iCount, sBegin, sRest);
return;
} else {
// swap the first character of sRest with each of
// the remaining chars recursively call debug print
sCur = (char*)malloc(iLen);
sNewBegin = (char*)malloc(iLen);
for (iLoop = 0; iLoop <>
strcpy(sCur, sRest);
strcpy(sNewBegin, sBegin);
cTmp = sCur[iLoop];
sCur[iLoop] = sCur[0];
sCur[0] = cTmp;
sprintf(cFLetter, "%c", sCur[0]);
strcat(sNewBegin, cFLetter);
debugprint(sNewBegin, sCur+1);
}
}
}
void main() {
char s[255];
char sIn[255];
printf("\nEnter a string:");
scanf("%s%*c",sIn);
memset(s,0,255);
PrintPermu(s, sIn);
}