Home Other Languages C Basic C Questions - 3

Login Form




Basic C Questions - 3 Print E-mail

1. Does there exist any other function which can be used to convert an integer or a float to a string?

Some implementations provide a nonstandard function called itoa(), which converts an integer to string.

#include

char *itoa(int value, char *string, int radix);

DESCRIPTION
The itoa() function constructs a string representation of an integer.

PARAMETERS
value :
Is the integer to be converted to string representation.

string :
Points to the buffer that is to hold resulting string.
The resulting string may be as long as seventeen bytes.

radix :
Is the base of the number; must be in the range 2 - 36.

A portable solution exists. One can use sprintf():

char s[SOME_CONST];
int i = 10;
float f = 10.20;

sprintf ( s, “%d %f\n”, i, f );

 

2. What will print out?

main()
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%s\n”,p2);
}

The pointer p2 value is also increasing with p1 .
*p2++ = *p1++ means copy value of *p1 to *p2 , then increment both addresses (p1,p2) by one , so that they can point to next address . So when the loop exits (ie when address p1 reaches next character to “name” ie null) p2 address also points to next location to “name” . When we try to print string with p2 as starting address , it will try to print string from location after “name” … hense it is null string ….

eg :
initially p1 = 2000 (address) , p2 = 3000
*p1 has value “n” ..after 4 increments , loop exits … at that time p1 value will be 2004 , p2 =3004 … the actual result is stored in 3000 - n , 3001 - a , 3002 - m , 3003 -e … we r trying to print from 3004 …. where no data is present … thats why its printing null .

 

Answer : empty string

 

3. What will be printed as the result of the operation below:

main()
{
char *ptr = ” Cisco Systems”;
*ptr++; printf(“%s\n”,ptr)
; ptr++;
printf(“%s\n”,ptr);

}


1) ptr++ increments the ptr address to point to the next address. In the prev example, ptr was pointing to the space in the string before C, now it will point to C.

2)*ptr++ gets the value at ptr++, the ptr is indirectly forwarded by one in this case.

3)(*ptr)++ actually increments the value in the ptr location. If *ptr contains a space, then (*ptr)++ will now contain an exclamation mark.

 

Answer : Cisco Systems

 

4. What will be printed as the result of the operation below:

         int x;
         int modifyvalue()
         {
             return(x+=10);
         }

         int changevalue(int x)
         {
             return(x+=1);
         }

         void main()
         {
             int x=10;
             x++;
             changevalue(x);
             x++;
             modifyvalue();
             printf("First output:%d\n",x);

             x++;
             changevalue(x);
printf("Second output:%d\n",x);
             modifyvalue();
             printf("Third output:%d\n",x);

         }


Answer : 12 , 13 , 13

 

5. Which bit wise operator is suitable for checking whether a particular bit is on or off?

The bitwise AND operator. Here is an example:

enum {
KBit0 = 1,
KBit1,

KBit31,
};

if ( some_int & KBit24 )
printf ( “Bit number 24 is ON\n” );
else
printf ( “Bit number 24 is OFF\n” );