5
Q:

What will be output of the following c program ?

#include

int main()

{

    int max-val=100;

    int min-val=10;

    int avg-val;

    avg-val =( max-val + min-val ) / 2;

    printf( "%d", avg-val );

    return 0;

}

A) 55 B) 105
C) 60 D) Compilation error

Answer:   D) Compilation error



Explanation:

We cannot use special character – in the variable name.
Variable name can have only underscore.

Subject: Programming
Q:

How would you use the function memmove()?

Answer

#include "mem.h"


#include "alloc.h"


main()


{


      int area;


      char *dest;


      char src[] = "Life is the camera and you are the target"


                                  "so keep smiling always";


      area = sizeof (src);


      dest = malloc (area);


      memmove (dest, src, area);


      printf("\n%s", dest);


      printf("\n%s",src);


}

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 3177
Q:

How would you use qsort() function to sort an array of structures?

Answer

#include "string.h"


#include "stdlib.h"


struct stud


{


       int rollno;


       int marks;


       char name[30];


};


int sort_m (struct stud *, struct stud *);


int sort_name (struct stud *, struct stud *);


int sort_marks (struct stud *, struct stud *);


 


main()


{


static struct stud ss[] = {


                                            { 15, 96, "Akshay" },


                                            { 2, 97, "Madhuri" },


                                            { 8, 85, "Aishvarya" },


                                            { 10, 80, "Sushmita" }


                                   };


int x,w;


clrscr();


w = sizeof (struct stud);


 


printf ('\nIn order of roll numbers:");


qsort (ss, 4, w, sort_rn);


for(x=0; x<4;x++)


     printf ("\n%d%s%d", ss[x].rollno, ss[x].name,ss[x].marks);


 


printf("\n\nIn order of names:");


qsort(ss, 4, sort_name);


 


for (x=0; x<4;x++)


      printf("\n%d%s%d",ss[x].rollno, ss[x].name,ss[x].marks);


printf("\n\nIn order of marks:");


qsort(ss,4,w,sort_marks);


 


for (x=0;x<4;x++)


      printf ("\n%d%s%d",ss[x].rollno,ss[x].name,ss[x].marks);


}


int sort_rn (struct stud *t1, struct stud *t2)


{


     return (t1->rollno-t2->rollno);


}


 


int sort_name (struct stud *t1, struct stud *t2)


{


     return (strcmp(t1->name,t2->name));


}


int sort_marks (struct stud *t1, struct stud *t2)


{


     return (t2->marks-t1->marks);


}


 


 


 

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3720
Q:

What do the functions atoi(), itoa() and gcvt () do? Show how would you use them in a program.

Answer

atoi()         Converts a string to an integer.


itoa()         Convert an integer to a string


gcvt()        Converts a floating-point number to a string


 


#include "stdlib.h"


main()


{


      char s[] = "12345";


      char buffer [15], string[20];


      int i;


      


      i = atoi (s);


      printf("\n%d",i);


 


       gcvt (20.141672, 4, buffer);


       printf ("\n%s", buffer);


 


       itoa(15, string,2);


        printf ("\n%s", string);


}

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

2 6017
Q:

What would be the output of the following program?

main()

{

        char huge * near * far *ptr1;

        char near * far * huge *ptr2;

        char far * huge * near *ptr3;

         printf ("%d%d%d", sizeof (ptr1), sizeof (ptr2), sizeof (ptr3));

}

Answer

4  4  2

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 3611
Q:

If I use the following printf() to print a long int why I am not warned about the type mismatch?

printf ("%d",num );

Answer

When a function accepts a variable number of arguments , its prototype cannot provide any information about the number of arguments and type of those variable arguments. Hence the compiler cannot warn about the mismatches. The programmer must make sure that arguments match or must manually insert explicit typecast.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3397
Q:

Point out the error, if any, in the following program.

#include "stdarg.h"

main()

{

     display ( 4, 12.5, 13.5, 14.5, 44.3);

}

display(int num, ...)

{

       float c; int j;

        va_list ptr;

        va_start (ptr, num);

        for ( j = 1; j <= num; j++)

        {

            c = va_arg ( ptr, float );

            printf ("\n%f", c);

         }

}

Answer

While extracting a float argument using va_arg we should have useed 


c = va_arg (ptr, double)

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 4785
Q:

What is the difference between malloc() and calloc() functions?

Answer

As against malloc(), calloc() needs two arguments, the number of elements  to be allocated and the size of each element. For example,


 p = (int *) calloc (10, sizeof (int));


would allocate space for a 10- integer array. Additionally, calloc() would also set each of this element with a value 0.


Thus the above call to calloc() is equivalent to:


p = (int *) malloc (10 * sizeof (int));


memset (p, 0, 10 * sizeof( int ));

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3921
Q:

Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?

Answer

Yes, using the realloc() function as shown below:


main()


{


        int *p;


        p = ( int *) malloc (20) ;


        t = p;


        t = (int *) realloc ( p, 40);


        if ( t == NULL )


        Printf (" Cannot reallocate, leaves previous allocated region unchanged ");


       else


       {


              if ( p ==t )


              ;  / * the array expanded at the same region */


             else


            { 


                 free ( p ); / * deallocate the original array */


                 p = t;  /* set p to newly allocated region */


             }


      }


}  

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 3488