C Programming Interview Questions and Answers-7

Q: – Write a program to reverse a string with out using any array?

#include<stdio.h>
int main()
{
char *str="Mahesh";
char *ptr=str+strlen(str);
while(ptr>=str)
{
printf("%c",*ptr);
ptr--;
}
}

Q: – How do you find IP adress of ur system?
ipconfig – in windows cmd mode.
ifconfig -a in linux and hostname|nslookup in linux.
Q: – What is the output of this program?

#include<stdio.h>
void Allocate(char *str, int size)
{
str =(char *)malloc(size);
}
int main()
{
char *str;
Allocate(str, 20);
strcpy(str, "HCL");
strcat(str, "Technologies");
printf("%sn",str);
}

Answer:Run time error(segmentation fault).
Reason: in function Allocate, we have allocated memory by using malloc function and assigned adress of that block to local pointer str. when control returns from that function the pointer (str) dies.so it will not affect in the main function. in main function,we are trying to read memory by using printf statement. so run time error occur.
Q: – What is the output of this program?

#include<stdio.h>
main()
{
char *ptr ="HCL Technologies";
(*ptr)++;
printf("%sn",ptr);
ptr++;
printf("%sn",ptr);
}

Answer:segmentation fault.because we are trying to modify the const string.
Q: – What is the output of this program?

#include<stdio.h>
void show()
{
static int i=1;
printf("%dn",i++);
}
int main()
{
show();show();show();
}

Answer:1 2 3. static variables values remain in function calls.if i(variable) in function show() islocal variable the out put would be 1 1 1.

Tagged , . Bookmark the permalink.

Leave a Reply