Language in C Interview Questions and Answers
Ques 66. How do I define a pointer to a function which returns a char pointer?
char * ( *p )( ) ;
or
typedef char * ( * ptrtofun )( ) ;
ptrtofun p ;
Here is a sample program which uses this definition.
main( )
{
typedef char * ( * ptrtofun ) ( ) ;
char * fun( ) ;
ptrtofun fptr ;
char *cptr ;
fptr = fun ;
cptr = (*fptr) ( ) ;
printf ( "nReturned string is "%s"", cptr ) ;
}
char * fun( )
{
static char s[ ] = "Hello!" ;
printf ( "n%s", s ) ;
return s ;
}
Ques 67. What's wrong with the following declaration: char* ptr1, ptr2 ; get errors when I try to use ptr2 as a pointer.
char * applies only to ptr1 and not to ptr2. Hence ptr1 is getting declared as a char pointer, whereas, ptr2 is being declared merely as a char. This can be rectified in two ways :
char *ptr1, *ptr2 ;
typedef char* CHARPTR ; CHARPTR ptr1, ptr2 ;
Ques 68. How to use scanf( ) to read the date in the form of dd-mm-yy?
To read the date in the form of dd-mm-yy one possible way is,
int dd, mm, yy ;
char ch ; /* for char '-' */
printf ( "nEnter the date in the form of dd-mm-yy : " ) ;
scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ;
Another way is to use suppression character * is as follows:
int dd, mm, yy ;
scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ;
The suppression character '*' suppresses the input read from the standard input buffer for the assigned control character.
Ques 69. Why the output of sizeof ( 'a' ) is 2 and not 1 ?
Character constants in C are of type int, hence sizeof ( 'a' ) is equivalent to sizeof ( int ), i.e. 2. Hence the output comes out to be 2 bytes.
Ques 70. Can we use scanf( ) function to scan a multiple words string through keyboard?
Yes. Although we usually use scanf( ) function to receive a single word string and gets( ) to receive a multi-word string from keyboard we can also use scanf( ) function for scanning a multi-word string from keyboard. Following program shows how to achieve this.
main( )
{
char buff[15] ;
scanf ( "%[^n]s", buff ) ;
puts ( buff ) ;
}
In the scanf( ) function we can specify the delimiter in brackets after the ^ character. We have specified 'n' as the delimiter. Hence scanf( ) terminates only when the user hits Enter key.
Most helpful rated by users:
- What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
}
int x = 3000, y = 2000 ;
long int z = x * y ;
' target='_blank'>Why doesn't the following code give the desired result?
int x = 3000, y = 2000 ;
long int z = x * y ;
char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
' target='_blank'>Why doesn't the following statement work?
char str[ ] = "Hello" ;
strcat ( str, '!' ) ;- How do I know how many elements an array can hold?
- How do I compare character data stored at two different memory locations?