Debugging questions in c with answers


C debugging interview questions
C debugging questions with answers


1. Write a c program too shutdown window operating system   in TURBO C.

void main(void)
{
system("shutdown -s");
}
Save the above .Let file name is close.c and compile and execute the above program. Now close the turbo c compiler and open the directory in window you have saved the close.c (default directory c:\tc\bin) and double click its exe file (close.exe).After some time your window will shutdown.


2. Write a scanf statement which can store one line of string which includes white space?

Answer:

void main() {
char a[30];
clrscr();
scanf("%[^\n]",a);
printf("%s",a);
getch();
}

3. Given the string "MATCHMAKING", write a program to read the string from the terminal and display the same in the following formats:

(a) MATCH MAKING
(b) MATCH
MAKING
(c) M.M.

4.  I need it to write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.

5.  A string (example "I am writing an email") is entered through the keyboard, write a program in C to get its reverse in a column as output i.e.:
email
an
writing
am

Answer:

void main()
{
char str[20];
char *ptr=str,*temp;
int i=0,j;
clrscr();
scanf("%[^\n]",str);
while(*ptr){
i++;
ptr++;
}
for(j=0;j
            if(*ptr==' ')
            {
                  temp=ptr;ptr--;temp++;
                  while((*temp!=' ')&&(*temp!='\0')) {
                       printf("%c",*temp);
                        temp++;
                  }
                  printf("\n");
            }
            else
            {
                  ptr--;
            }
}
while(*ptr!=' ') {
printf("%c",*ptr);
ptr++;
}
getch();
}


6. I want a C program to check whether a string is a palindrome or not where the string to be checked is passed as command line argument during execution.

Answer:

#include<string.h>
void main(int counter,char**string)
{
char *rev;
char str[15];
int i,j;
clrscr();
strcpy(str,string[1]);
printf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
getch();
}

7. How to write a c program to display the source code of the program.

Answer:

If source code is available
#include<stdio.h>
void main()
{
   char str[70];
   FILE *p;
   clrscr();
   if((p=fopen("mod.c","r"))==NULL)
   {
      printf("\nUnable t open file string.txt");
      exit(1);
   }
   while(fgets(str,70,p)!=NULL)
          puts(str);
   fclose(p);
   getch();
}

8. Swapping of two number without using third variable

Answer:

void main()
{
int a=5,b=10;
clrscr();
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d  b=  %d",a,b);
//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d  b=  %d",a,b);
//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d  b=  %d",a,b);
//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d  b=  %d",a,b);
//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d  b=  %d",a,b);
getch();
}

9. How to convert decimal to binary in c program?

Answer

void main()
{
  long int m,no=0,a=1;
  int n,rem;
  clrscr();
  printf("Enter any decimal number->");
  scanf("%d",&n);
  m=n;
  while(n!=0)
  {
          rem=n%2;
          no=no+rem*a;
          n=n/2;
          a=a*10;
  }
  printf("The value %ld in binary is->",m);
  printf("%ld",no);
  getch();
}
  
10. Write a program to accept character and integer n from user and display next n character

Answer:

void main()
{
char c;
int n,i;
clrscr();
printf("insert one character and integer : ");
scanf("%c%d",&c,&n);
for(i=c+1;i<=c+n;i++)
printf("%c ",i);
getch();
}

Output:
insert one character and integer : c 4
d e f g

C debugging questions
Debugging questions in c

13 comments:

  1. For the 1st point, no need to use Turbo C compiler, just add :

    #include

    at the beginning of your programm and you're done.

    ReplyDelete
  2. for shutting down ur system do one thing.
    type shutdown -s on cmd.
    then ur computer will shutdown within 1 minute.

    ReplyDelete
  3. #include
    main()
    {
    char *p;
    static int arr[]={2,3,4};
    p=arr;
    p=(char *)((int *)(p));
    printf("%d",*p);
    p=(int *)(p++);
    printf("%d",*p++);
    }
    o/p=2,0 pls can any1 explain??

    ReplyDelete
  4. line 1 -declaration of pointer p
    line 2 -declaration of int arr
    line 3 -pointer p will point out the
    value 2
    line 4 -pointer p is typecasted in int pointer and then again typecasted in char pointer still it point out 2
    line 5 -value of p is printed which is 2
    line 6 -p is incremented which is address and now it does not point out 2 and then it contains garbage and typecasted in int pointer
    line 7 -then the value of p is printed which is 0

    ReplyDelete
  5. for 8th question
    void main()
    {
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d%d",a,b);
    printf("after swapping");
    a=(b-a)+a;
    b=(a-b)+b;
    printf("%d%d",a,b);
    getch();
    }

    ReplyDelete
    Replies
    1. a = 5, b = 10,
      a = (10-5)+5 = 10;
      b = (10-10)+10 = 10;

      so using this you can't swap two number.

      Delete
    2. YOU CAN USE THIS METHOD

      void main()
      {
      int a,c;
      scanf("%d%d",&a,&b);
      a=a+b;
      b=a-b;
      a=a-b;
      printf("after swapping a=%d and b=%d",a,b);
      getch();
      }

      }

      Delete
  6. void main()
    {
    int a=10,b=5;
    int *x,*y;
    x=&b;
    y=&a;
    printf("a=%d b=%d" ,*x,*y);
    getch();
    }

    ReplyDelete
  7. #include
    void main()
    { long int n;
    int i,r=0,sum=0,sum2=0,r1;
    clrscr();
    scanf("%ld",&n);
    while(n>0)
    {
    r=n%10;
    sum=sum+r;
    n=n/10;
    }
    if(sum>10)
    {
    while(sum>0)
    { r1=sum%10;
    sum2=sum2+r1;
    sum=sum/10;
    }

    }
    else
    {
    printf("%d",sum);
    return 0;
    }
    printf("%d",sum2);
    getch();
    }//65536

    ReplyDelete
  8. Hi All, Please help me for creating a C program & I am new for C development.
    The Task is given by my Team leader , The task description is given below :
    We need an C program that, when given a table name and database name as arguments, will query the informix & Postgres system tables to get all permissions granted to various users on that table. The program will then generate sql statements to grant all these permissions on the table and write these statements to a file which can be run later.

    From my basic research, the information needed can be got from the tables "systabauth" and "syscolauth", the former for table-level privilèges and the latter for column-level privilèges, if any. You will need to do some more research to find out how exactly to get the required information out of these tables.

    ReplyDelete