In this article, I'll present some interesting 'C' problems for which I will not provide the answer. If you are not bale to solve any problem, then just leave a comment here so that everybody is benefited when the problem is discussed.
1) Will the program execute without any problem?
2) Is there any problem with this piece?
3) Here in this code, the result comes out to be '0', why?
4)This program gives 'seg-fault' when executed, can you tell the reason why ?
5)Here is a visual treat. The output comes out to be 100 but don't you think that it should be '10' ?
6)Somebody was saying that I am in real trouble if any one comes to know this logic behind my binary. Can you tell the reason?
7)Shouldn't the output be 40 here ?
8) I want to change the entry point of my C program. ie I want my function 'func()' to be the first function to run rather than the traditional main() function. Is this possible?
9)I see the output of the following code may vary from machine to machine, whats happening?
10)I am expecting the output to be ONE but I am getting NONE. why?
11)This program is seg-faulting when I am trying to execute it, what could be the reason?
12)Why is the value printed by the two printf's different?
13) In the following program I get some weird output when I print str. why?
14)What should be the output of the following code and why?
15)What should be the order of print statements here ?
16)Why does the following piece of code seg-fault after sometime?
17) Shouldn't the size of array here be 10? Why is it coming out to be 40?
18)Why does this for loop execute only once, shouldn't it execute 10 times?
19)Some times in certain scenarios when you try to run a program, you get an error like "Operation not permitted", do you know why this error is thrown in most of the cases?Like the other day I was using pcap library functions to sniff network traffic but when I ran my program the first error that I got was :
Do you know why these kind of problems occur?
20)You would have observed an error when you try to declare same variable twice in a function but can there be a way to make this happen??
Here were some interesting questions on basic 'C' fundamentals. I'd love to discuss here if anyone has some doubt in any of the problem.
Stay tuned for more!!!
Problems
1) Will the program execute without any problem?
Code:
#include <stdlib.h>
#include <stdio.h>
#define SIZE 5
int main()
{
int *p, i;
p = malloc(SIZE*sizeof(int));
for (i=0; i<SIZE; i++)
*(p + i) = i * i;
for (i=0; i<SIZE; i++)
printf("%d\n", *p++);
free(p);
return 0;
}
Code:
#include <stdio.h>
int main()
{
int* ptr1,ptr2;
ptr1 = malloc(sizeof(int));
ptr2 = ptr1;
*ptr2 = 10;
return 0;
}
Code:
#include<stdio.h>
int func(int a, int b, int *res)
{
*res = b/a;
return 0; // Sum successful
}
int main(void)
{
int a=2,b=6,res;
printf("\n Func returned = [%d], res = [%d]\n",func(a,b,&res), res);
return 0;
}
Code:
#include <stdio.h>
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d\n",n);
printf("You entered %d \n",n);
return 0;
}
Code:
#include <stdio.h>
#include<stdlib.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int main()
{
int y = 100;
int *p;
p = malloc(sizeof(int));
*p = 10;
y = y/*p; /*dividing y by *p */;
PrintInt(y);
return 0;
}
Code:
#include <stdio.h>
int main()
{
char str[80];
printf("Enter the string:");
scanf("%s",str);
printf("You entered:%s\n",str);
return 0;
}
Code:
#define SIZE 10
void size(int arr[SIZE])
{
printf("size of array is:%d\n",sizeof(arr));
}
int main()
{
int arr[SIZE];
size(arr);
return 0;
}
9)I see the output of the following code may vary from machine to machine, whats happening?
Code:
#include<stdio.h>
int main(void)
{
int *ptr1 = (int*)400;
int *ptr2 = (int*)200;
printf("\n Diff is [%ld]\n",(ptr1 - ptr2));
return 0;
}
Code:
#include<stdio.h>
int main()
{
int a=1;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
default:
printf("NONE\n");
}
return 0;
}
Code:
#include<stdio.h>
int main()
{
char str[] = "hello";
char *st = "bye";
str[0] = 'b';
st[0] = 'h';
return 0;
}
Code:
#include<stdio.h>
int* func()
{
static int count;
count ++;
if(count == 2)
{
return NULL;
}
int a = 10, b=20, c = 0;
c = b/a;
return &c;
}
int main()
{
int *ret1 = func();
printf("\n ret = [%d]\n",*ret1);
int *ret2 = func();
if(ret2 == NULL)
{
printf("\n This time function returned NULL\n");
}
printf("\n ret = [%d]\n",*ret1);
return 0;
}
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[] = {'h','e','l','l','o'};
char *st = "bye";
printf("\n st = [%s], str = [%s]\n",st,str);
return 0;
}
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *st = "bye all";
st++;
printf("\n *st = [%c]\n", *((char*)st));
return 0;
}
Code:
#include<stdio.h>
#include<stdlib.h>
int func1(void)
{
printf("\nInside func1\n");
return 0;
}
int func2(void)
{
printf("\nInside func2\n");
return 0;
}
int main()
{
printf("\n Inside main\n");
printf("func1 returned [%d], func 2 returned[%d]",func1(), func2());
return 0;
}
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("\n Inside main\n");
main();
return 0;
}
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[10];
printf("\n sizeof array arr = [%lu]\n",sizeof(arr));
return 0;
}
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i =0;
for(i = 0; i<10; i++);
printf("\n i = [%d]\n",i);
return 0;
}
Quote:
pcap_open_live(): socket: Operation not permitted
20)You would have observed an error when you try to declare same variable twice in a function but can there be a way to make this happen??
Conclusion
Here were some interesting questions on basic 'C' fundamentals. I'd love to discuss here if anyone has some doubt in any of the problem.
Stay tuned for more!!!
lionaneesh, Trinity
likes this




Thanks it really helped!