C Interview Questions | StudyObject

how to prepare for c interview

C interview questions are basic questions but are tricky. We are providing the list of 10 C interview questions with answers.

C questions are mostly of program type so practice these questions.

1.What is the difference between sizeof and strlen in C

#include<stdio.h>
int main()
{
char a[]=”hello”;
char *p=”hello”;
printf(“%d—%d—“,sizeof(a),strlen(a));
printf(“%d—%d”,sizeof(p),strlen(p));
return 0;
}
Output:
6—5—4—5
Pointer always returns its size with sizeof
Sizeof operator includes “null” at the end of string

2.write a program to do string reversal without pointer.

#include<stdio.h>
#include<string.h>int main() {
char A[100]=”ITTINA”, temp;
int i, j = 0;printf(“nEnter the string : %s” , A);i = 0;
j = strlen(A) – 1;while (i < j) {
temp = A[i];
A[i] = A[j];
A[j] = temp;
i++;
j–;}printf(“nafter reversal: %s” , A);}

3.write a program for string reversal using pointers
write a program to reverse the string using pointers

#include<stdio.h>
#include<string.h>void reverse(char* p);int main()
{char p[]=”hello”;reverse(p);printf(“%s”,p);return 0;
}
void reverse(char* p)
{
char *start = p;
char *end = p+strlen(p)-1;
char tmp;
while(end>start)
{
tmp=*end;
*end=*start;
*start=tmp;
start++;
end–;
}
}

4.write a program to reverse a string using recursion

#include<stdio.h>char * rev(char *);
int main()
{
char A[10] = “HELLO”;
printf(“%s”,A);
char *p = rev(A);
printf(“—–>%s”,p);return 0;
}char* rev(char *p)
{
static int i=0;
static char m[10];if(*p)
{
rev(p+1);
m[i++]=*p;}return m;
}

5.what happens when NULL pointer is derefernced.

#include<stdio.h>
int main()
{
int *p=NULL;
printf( “printing p %d”,*p);
}
NULL points to 0x00 which is protected by kernal so it does nt allow it to derefence
Output:-
Segmentation fault

6.what is the output of below program

#include<iostream>int main()
{char *p=”HELLOn”;printf(“%s”,p);printf(p);printf(“HELLO”);}Output:-
HELLO
HELLO
HELLO

7.write a program to find if a processor is little endian or big endian

#include<stdio.h>
int main()
{
int a= 0x12345678;
char *p=(char*)&a;
printf(“%x”,*p);
if (*p==0x78)
printf(“little endian”);
}
Output:
78little endian

8.write your own atoi program

atoi converts string to integer value
#include <stdio.h>
int Atoi(char *mystring)
{
int result = 0;
int i;
for ( i = 0; mystring[i] != ‘�’; ++i)
{
result = result*10 + mystring[i] – ‘0’; // ‘0’ is 48 – we need to subtract 48 from each assci to get the integer
}
return result;
}
int main()
{
char mystring[] = “194567”;
int finalvalue = Atoi(mystring);
printf (“%d “, finalvalue);
return 0;
}
Output-
194567

9.compare your own atoi with original atoi

Always remember original atoi in std lib does nt process characters like abcd and if given alongwith integer it silently ignores it
#include <stdio.h>
#include <stdlib.h> /* atoi */
int Atoi(char *mystring)
{
int result = 0;
int i;
for ( i = 0; mystring[i] != ‘�’; ++i)
{
result = result*10 + mystring[i] – ‘0’;
}
return result;
}
int main()
{
char mystring[] = “123a”;
int finalvalue = Atoi(mystring);
printf (“%dn “, finalvalue);
int at =atoi(mystring);
printf (“%d “, at);
return 0;
}
Output:-
1279
123

10.Explain function pointer by writing a program

#include<stdio.h>
int display();
int main()
{
int (*functionPtr)(); //declaration of function pointer
functionPtr=display;
(*functionPtr)(); //making a call to function via function pointer
}
int display()
{
printf(“Function pointer explained”);
}
Output:-
Function pointer explained

StudyObject © 2020. All rights reserved.
X