C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Find output of C programs Questions with Answers
(Set - 3)

Submitted by Abhishek Jain, on June 17, 2017

This section contains C language find output programs on mixed topics with correct output and explanation.

Question - 1

#include<stdio.h>

int main()
{
	int a[5] = {1,2,3,4,5};
	int i;

	for (i = 0; i < 5; i++)
		if ((char)a[i] == '5')
			printf("%d\n", a[i]);
		else
			printf("FAIL\n");

	return 0;
}

Output

FAIL
FAIL
FAIL
FAIL
FAIL

Explanation

This is because i.) if-else are conditional statement thus for loop consider them as single line statement(always only one statement will be true). ii) In condition of If statement (char)a[i] will returns the character values equivalent to 1,2,3,4,5 while ‘5’ will return the ASCII value of character '5'(i.e.53).

Question - 2

#include<stdio.h>
int main()
{
	char chr;
	chr = 128;
	printf("%d\n", chr);
	return 0;
}

Output

-128

Explanation

Any character store values from -128 to 127. If character value exceeds the range then it will be round-off to fit it in the range. That’s why, 128 round-off to -128.

Question - 3

#include <stdio.h>
int main()
{
	float f1 = 0.1;
	
	if (f1 == 0.1)
		printf("equal\n");
	else
		printf("not equal\n");
	
	return 0;
}

Output

not equal

Explanation

Generally float values have 4 or 6 decimal digit of precision (digits after decimal point). Therefore f1=0.100000 which is not equal to 0.1.

Question - 4

#include<stdio.h>
int main() 
{
	int a = 0, i = 0, b;
	
	for (i = 0;i < 5; i++)
	{ 
		a++;
		if (i == 3)
			break;
	}
	printf("%d",a);
	
	return 0; 
}

Output

4

Explanation

The for loop run 4 times, incrementing a by 1 each time and then break.

Question - 5

#include<stdio.h>
int main()
{
	int i = 0;
	char c = 'a';

	while(i < 2)
	{
		i++;
		switch (c)
		{
			case 'a':
				printf("%c", c);
				break;
			break;
		}
	}
	printf(" after while\n");
	
	return 0;
}

Output

aa after while

Explanation

Here, while loop executed 2 timesbecause after first break statement it does not go for second break. While loop prints aa and then print after while.

Question - 6

#include<stdio.h>
int main()
{
	int k=0;

	for(k)
	 printf("Hello");

	return 0;
}

Output

Compiler Error

Explanation

Invalid syntax of for loop for(k).

Question - 7
If ASCII value of y is 121, then what is the value of H, if H=('y'-'x')/3

Output

0

Explanation

ASCII value of y and x are 121 and 120 respectively. Thus ('y'-'x') equals to 1 and H=1/3 = 0.









COMMENTS