You are on page 1of 7

 How would you obtain the current time and difference between two times?

#include<time.h>
#include<stdio.h>
void main()
{
time_t t;
time(&t);
printf("%s",ctime(&t));
}
 How would you use the functions randomize() and random()?
 #include<iostream.h>
 #include<conio.h>
 #include<stdlib.h>

 int main()
 {
 int n;
 randomize();
 n=random(91)+10;
 cout<<"The randomly selected number is :"<<n;
 getch();
 return 0;
 }

 How would you implement a substr() function that extracts a sub string from a given string?

 #include <stdio.h>
 #include <malloc.h>

 char* substring(char*, int, int);

 int main()
 {
 char string[100], *pointer;
 int position, length;

 printf("Enter a string\n");
 gets(string);

 printf("Enter the position and length of substring\n");
 scanf("%d%d",&position, &length);

 pointer = substring( string, position, length);

 printf("Required substring is \"%s\"\n", pointer);

 free(pointer);

 return 0;
 }

 /*C substring function: It returns a pointer to the substring */

 char *substring(char *string, int position, int length)
 {
 char *pointer;
 int c;

 pointer = malloc(length+1);

 if (pointer == NULL)
 {
 printf("Unable to allocate memory.\n");
 exit(EXIT_FAILURE);
 }

 for (c = 0 ; c < position -1 ; c++)
 string++;

 for (c = 0 ; c < length ; c++)
 {
 *(pointer+c) = *string;
 string++;
 }

 *(pointer+c) = '\0';

 return pointer;
 }

 What is the difference between the functions rand(), random(), srand() and randomize()?
RAND: Rand uses a multiplicative congruential random number generator with period232 to return successive pseudo-
random numbers in the range 0 to RAND_MAX.

Return Value: Rand returns the generated pseudo-random number.
RANDOM(): Random returns a random number between 0 and (num-1).random(num) is a macro defined in STDLIB.H.

RANDOMIZE(): Randomize initializes the random number generator with a random value. Because randomize is
implemented as a macro that calls the time function prototyped in TIME.H, you should include TIME.H when you use this
routine

SRAND(): The random number generator is reinitialized by calling srand with an argument value of 1.The generator can
be set to a new starting point by calling srand with a given seed number.

Return Value: None

 What is the difference between the functions memmove() and memcpy()?
void *memmove(void *DST, const void *SRC, size_t LENGTH);
This function moves LENGTH characters from the block of memory start-
ing at `*SRC' to the memory starting at `*DST'. `memmove' reproduces
the characters correctly at `*DST' even if the two areas overlap.

void* memcpy(void *OUT, const void *IN, size_t N);

This function copies N bytes from the memory region pointed to by IN
to the memory region pointed to by OUT.
If the regions overlap, the behavior is undefined.
Memcpy is faster ..
 How do you print a string on the printer?
------------------------------------------------------------------------------------------------------------------------------------------

 Can you use the function fprintf() to display the output on the screen?

 Format of sprintf()


difference between #define and typedef?
A typedef is just a new name for an already existing type and defines are handled by the preprocessor while typedefs
are handled by the C compiler itself.
#define can be used to rename anything not just a type like typedef.................for e.g.

#define AND && and we cant use typedef for that like typedef && AND; its not possible with typedef...... some more
examples like #define PRINT printf("hello") or #define sum(a,b) (a+b)....all this is not possible with
typedef...................

we all know this very well so i am not going to explain this but what will happen if we use #define to rename a
type................

suppose if we have
typedef char * ptr;
#define PTR char *

both of them are looking similar......


lets understand the difference with the help of an example............

typedef char * ptr;
#define PTR char *
int main()
{
ptr a,b,c;
PTR x,y,z;
printf( "sizeof a:%u\n" ,sizeof(a) );
printf( "sizeof b:%u\n" ,sizeof(b) );
printf( "sizeof c:%u\n" ,sizeof(c) );
printf( "sizeof x:%u\n" ,sizeof(x) );
printf( "sizeof y:%u\n" ,sizeof(y) );
printf( "sizeof z:%u\n" ,sizeof(z) );
return 0;
}

Output:
sizeof a:2
sizeof b:2
sizeof c:2
sizeof x:2
sizeof y:1
sizeof z:1


 How does free() function work ?
The free command does two things:
1. The block of memory pointed to by the pointer is unreserved and given back to the free memory on the
heap. It can then be reused by later new statements.
2. The pointer is left in an uninitialized state, and must be reinitialized before it can be used again.

 Expand a[3][2][5]=6;

1. Which are the four storage classes in C.
Answer : static, extern, register, auto

2. Given a program:

int i;//global data segment
int main()
{
int j; //stack
int *k = (int *) malloc (sizeof(int));//k in stack and allocated in heap

}
Where are each of these variables stored?




#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
int coef;
int exp;
struct node* next;
} node;

void get_input(node** head)
{
node* temp,*ptr;
ptr = *head;
temp = (node*)malloc(sizeof(node));

printf("\nEnter coef : ");
scanf("%d",&(temp->coef));
printf("\nEnter exp : ");
scanf("%d",&(temp->exp));

if(NULL == *head)
{
*head = temp;
(*head)->next = NULL;
}
else
{
while(NULL != ptr->next)
{
ptr = ptr->next;
}
ptr->next = temp;
temp->next = NULL;
}
}

void display(node* head)
{
while(head->next != NULL)
{
printf("(%d.x^%d)+",head->coef,head->exp);
head = head->next;
}
printf("(%d.x^%d)",head->coef,head->exp);
printf("\n");
}

void add(node* poly, node* poly1, node* poly2 ) //poly==result
{
while(poly1->next && poly2->next)
{
if(poly1->exp > poly2->exp)
{
poly->coef = poly1->coef;
poly->exp = poly1->exp;
poly1 = poly1->next;
}
else if(poly1->exp < poly2->exp)
{
poly->coef = poly2->coef;
poly->exp = poly2->exp;
poly2 = poly2->next;
}
else
{
poly->coef = poly1->coef + poly2->coef;
poly->exp = poly1->exp;
poly1 = poly1->next;
poly2 = poly2->next;
}
poly->next = (node*)malloc(sizeof(node));
poly = poly->next;
poly->next = NULL;


if(poly1==NULL&&poly2!=NULL)
{
if(poly1->next)
{
poly->coef = poly1->coef;
poly->exp= poly1->exp;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->coef = poly2->coef;
poly->exp= poly2->exp;
poly2 = poly2->next;
}
poly->next = (node*)malloc(sizeof(node));
poly = poly->next;
poly->next = NULL;
}
}

int main()
{
node* head1 = NULL;
node* head2 = NULL;
node* head = (node*)malloc(sizeof(node));
int ch;

do
{
get_input(&head1);
printf("\nEnter more node in poly1? (1,0) :");
scanf("%d",&ch);
}while(ch);

do
{
get_input(&head2);
printf("\nEnter more node in poly2? (1,0) :");
scanf("%d",&ch);
}while(ch);

add(head,head1,head2);
display(head1);
display(head2);
display(head);

return 0;
}