24
Q:

main()

{

char s[ ] = "man";

int i;

for( i=0; s[ i ]; i++)

printf( "n%c%c%c%c", s[ i ], *(s+i), *(i+s), i[s] );

}

A) mmmm aaaa nnnn B) aaaa mmmm nnnn
C) nnnn aaaa mmmm D) None

Answer:   A) mmmm aaaa nnnn



Explanation:

s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea.Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

Subject: Programming
Q:

If the following program (myprog) is run from the command line as 

myprog 1 2 3 

what would be the output?

main(int argc, char *argv[])

{

    int i, j = 0;

    for (i = 0; i < argc ; i++)

           j = j + atoi ( argv[i]);

    printf ("%d", j);

}

A) 123 B) 6
C) Error D) "123"
 
Answer & Explanation Answer: B) 6

Explanation:

When atoi() tries to convert argv[0] to a number it cannot do so (argv[0] being a file name) and hence returns a zero.

Report Error

View Answer Report Error Discuss

Filed Under: Programming

2 10636
Q:

What would be the output of the following program?

/* sample.c */

main ( int argc, char **argv )

{

     argc = argc - (argc -1);

     printf ("%s", argv[argc - 1]);

}

Answer

C: \SAMPLE.EXE

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2032
Q:

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

# include "stdio.h"

main()

{

     FILE *fp;

     char str[80];

     fp = fopen ("trail", "r");

     while (!feof (fp))

    {

           fgets (str, 80, fp);

           puts (str);

     }

     fclose (fp);

}

Answer

The last line from the file "trial" would be read twice. To avoid this, ues:


       While ( fgets (str, 80, fp) ! = NULL)


               puts (str);

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2712
Q:

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

#include "stdio.h"

main()

{

      unsigned char;

       FILE *fp;

       fp = fopen ("trail", "r");

       while (( ch = getc (fp)) ! = EOF)

               printf ("%c", ch);

       fclose (fp);

}  

Answer

EOF has been defined as #define EOF -1 n the file "stdio.h" and an unsigned char ranges from 0 to 255 hence when EOF is read from the file it cannot be accommodated in ch. Solution is to declare ch as an int.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 2820
Q:

How would you check whether the contents of two structure variables are same or not?

Answer

struct emp


{


     char n[20];


      int age;


};


main()


{


    struct emp e1 = {"Dravid", 23};


   struct emp e2;


   scanf ("%s %d",e2.n, & e2.age);


   if( structcmp (e1,e2) ==0)


        printf ("The structures are equal");


   else


         printf ("The structures are unequal");


}


structcmp ( struct emp x, struct emp y)


{


     if (strcmp (x.n,y.n) ==0)


           if (x.age == y.age)


            return (0);


            return (1);


}


In short, if you nee to compare two structures, you'll have to write your own function to do so which carries out the comparison field by field.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3266
Q:

What would be the output of the following program?

main()

{

   struct emp

   {

        char *n;

        int age;

   };

   struct emp e1 = { "Dravid", 23};

   struct emp e2 = e1;

   strupr (e2.n);

   printf ("\n%s",e1.n);

}

Answer

DRAVID


When a structure is assigned, passed, or returned, the copying is done monolithically. This means that the copies of any pointer fields will point to the same place as the original. In other words, anything pointed to is not copied. Hence, on changing the name through e2.n it automatically changed e1.n

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2182
Q:

If the following structure is written to a file using fwrite(), can fread() read it back successfully?

struct emp

{

    char *n;

    int age;

};

struct emp e = { "Sujay",15};

FILE *fp;

fwrite (&e, sizeof (e), 1, fp);

Answer

No, since the structure contains a char pointer while writing the structure to the disk using fwrite() only the value stored in the pointer n would get written. When this structure is read back the address would be read back but it is quite unlikely that the desired string would be present at this address in memory

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2647
Q:

Point out the error, ifany, in the followingb code?

typedef struct

{

     int data;

     NODEPTR link;

} *NODEPTR;

 

Answer

A typedef defines a new name for a type, and in simpler cases like the one shown below you can define a new structure type and a typedef for it at the same time.


typedef struct


{


    char name[20];


    int age;


} emp;


However, in the structure defined in this question, there is an error because a typedef declaration cannot be used until it is defined. In the given code fragment the typedef declaration is not yet defined at he point where the link field is declared.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1811