Friday, December 27, 2013

C program to print 'xay' in place of every 'a' in a string.

This question was asked in one the technical interview( Under graduate level). This is kind of application for  'replace' feature. You can make it more accurate by putting more constraints.

Ans :

#include<stdio.h>
int main()
{
int i=0;
char str[100],x ='x',y='y' ;
printf("Enter the string\n:");
gets(str);
while(str[i]!='\0')
{
if(str[i]=='a')
{
printf("%c ",x);
printf("%c ",str[i++] );
printf("%c ",y);
}
else
{
printf("%c ",str[i++] );
}
}
return 0;

Tuesday, December 24, 2013

How to deallocate dynamically allocate memory without using “free()” function.

NOTE: this is actually puzzle type question nobody  use it realloc() function to free the memory. But I heard, in many interviews this question had been asked.

Standard library function realloc() can be used to deallocate previously allocated memory. Below is function declaration of “realloc()” from “stdlib.h

void *realloc(void *ptr, size_t size);
 
If “size” is zero, then call to realloc is equivalent to “free(ptr)”. And if “ptr” is NULL and size is non-zero then call to realloc is equivalent to “malloc(size)”.

Let us check with simple example.

/* code with memory leak */
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int *ptr = (int*)malloc(10);
 
    return 0;
}
 
Check the leak summary with valgrind tool. It shows memory leak of 10 bytes, which is highlighed in red colour.

 valgrind –leak-check=full ./free
  ==1238== LEAK SUMMARY:
  ==1238==    definitely lost: 10 bytes in 1 blocks.
  ==1238==      possibly lost: 0 bytes in 0 blocks.
  ==1238==    still reachable: 0 bytes in 0 blocks.
  ==1238==         suppressed: 0 bytes in 0 blocks.

Let us modify the above code.
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int *ptr = (int*) malloc(10);
 
    /* we are calling realloc with size = 0 */
    realloc(ptr, 0);
    
 
    return 0;
}
 
Check the valgrind’s output. It shows no memory leaks are possible, highlighted in red color.
  >valgrind –leak-check=full ./a.out
  ==1435== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
  ==1435== malloc/free: in use at exit: 0 bytes in 0 blocks.
  ==1435== malloc/free: 1 allocs, 1 frees, 10 bytes allocated.
  ==1435== For counts of detected errors, rerun with: -v
  ==1435== All heap blocks were freed — no leaks are possible.
  

Saturday, December 21, 2013

SCANF() - Some important features.

scanf()
The scanf()is very powerful function. We can use it to scan the input but we can add something in it's arguments and make it very powerful function.

Assume that we have one variable : a[100];

To read a string:
             scanf("%[^\n]\n", a);
            // it means read until you meet '\n', then trash that '\n'
 
 
To read till a coma:
             scanf("%[^,]", a);
            // this one doesn't trash the coma

             scanf("%[^,],",a);
           // this one trashes the coma
 
If you want to skip some input, use * sign after %. 
For example you want to read last name from "John Smith" :
          scanf("%s %s", temp, last_name);
         // typical answer, using 1 temporary variable

         scanf("%s", last_name);
         scanf("%s", last_name);
        // another answer, only use 1 variable, but calls scanf twice

          scanf("%*s %s", last);
        // best answer, because you don't need extra temporary variable nor 
           calling scanf twice 
 
 

To know, Why should u not use turbo c++ ? click here

 
C language
  

Saturday, December 14, 2013

Why should u not use turbo c++ ?

I have seen that in many under graduate collages turbo c++ is still used.  But now a days it is an outdated IDE.  Please, do not use it. It is totally useless.

Draw backs of Turbo C++
Turbo C++
There are many recent IDEs available which has great features and no bugs like GCC or Eclipse C/C++. Now a days GCC is treated as standard compiler.

some of drawbacks of Turbo C++:
  • Debugging is not as efficient as they are in other IDEs
  • It is not conformed with the standards that are laid down
  • It does not support modern casts, only C-Style casts.
  • I doubt if it may not goes well with 3rd party libraries! eg database or graphics libraries
There are other run time drawbacks also. So it is preferable to not use turbo C++ .