KEMBAR78
C Output Problem | PDF | Integer (Computer Science) | Software Development
0% found this document useful (0 votes)
18 views5 pages

C Output Problem

The document provides a series of C programming output problems along with their explanations and expected outputs. It covers various topics such as data types, pointer arithmetic, control structures, and operator precedence. Each problem includes code snippets, the output, and a detailed explanation of the results.

Uploaded by

Rohit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

C Output Problem

The document provides a series of C programming output problems along with their explanations and expected outputs. It covers various topics such as data types, pointer arithmetic, control structures, and operator precedence. Each problem includes code snippets, the output, and a detailed explanation of the results.

Uploaded by

Rohit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C Programming Output Problems

Cloud IT Online- Class -5


STUDY MATERIAL: Cloud IT Online (Handbook: C output Problem, Practice Book: C ,
Exercise Book: C Programming Problem chapter- 1, Lecture Sheet : For C Programming
Theory)

Previous Year Output Problems: Cloud IT Online: Handbook: C Output Problems

1. How can we store 5.95 as a float?


A. use 5.95f
B. use 5.95
C. use 5.95ld
D. none of the above.

Output
A) use 5.95f
Explanation
In case of C and C++ programming 5.95 treat as double value by default. To convert double value
into float value we have to use ‘f’ at the end of value. To specify 5.95 as long double we have to
use character ‘L’ at the end of value.

#include <stdio.h>

int main()
{
char ch='A';
printf("%d",ch);
return 0;
}

Output
65
Explanation
Here, value of "ch" is 'A' and we are printing the value "ch" in integer format (using "%d" format
specifier). Thus, the output will be 65 (which is the ASCII code uppercase character 'A').

#include <stdio.h>

int main()
{
char ch='A';
printf("%c-
%d",ch+32,ch+32);
return 0;
}
Output
a-97
Explanation
Variable contains ASCII code of 'A' which is 65. And we are printing "ch+32" which will be 97 and the
format specifier "%c" will print "a" and "%d" will print 97.

int main()
{
int x[10]={0,1,2,3,4,5,6,7,8,9};
int *ptr1,*ptr2;
ptr1=&x[0];
ptr2=&x[5];
printf("%p\n",(ptr1+ptr2));
return 0;
}
Output
error: invalid operands to binary + (have 'int *' and 'int *')
Explanation
Error in the statement printf("%p\n",(ptr1+ptr2));
Addition of two pointers is not allowed.

int main()
{
int color=2;
switch(color)
{
case 0: printf("Black");
case 1: printf("Blue");
case 2: printf("Green");
case 3: printf("Aqua");
default: printf("Other");
}
return 0;
}

Output
GreenAquaOther
Explanation
There are no break statements, so all the statements after case 2 will be executed
including default statement.

int main()
{
if(0);
printf("Hello");
printf("Hi");
return 0;
}

Output
HelloHi
Explanation
There is a semicolon after the if statement, so this statement will be considered as separate statement; and
here printf("Hello"); will not be associated with the if statement. Both printf statements will be
executed.
#include <stdio.h>

int main()
{
int i;
for(i=65; i<(65+26);i++)
printf("%c ",i);
return 0;
}
Output
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Explanation
See the printf statement, here we are printing the value in character format (using "%c" format specifier)
and the value of i is 65 to 90, which are the ASCII codes of Uppercase alphabets from A to Z. Thus,
the output will be "A B C ... Z".

What will happen if we assigned a value to an array element whose size of subscript is greater than
the size of array in C programming?

A. A compilation error occurs.


B. The element will be set 0.
C. The program crashes at run time.
D. The array size increases automatically.

Output
C) The program crashes at run time.
Explanation
In C programming if we assign a value to array element whose size of subscript is greater than size of
subscript of array then it crashes at run time because array have fixed size for data management. We
cannot extend or reduce the size of array. Due to this drawback of array, linked list is formed. In linked
list we can increase or decrease the size. But now modern gcc compilers will take care of this kind of
errors.

int main()
{
int x,y;
int *ptr;
x=100;
ptr=&x;
y=*ptr;
printf("%d\n",y);
return 0;
}
Output
100
Explanation
Here, y is assigned the value stored at the pointee variable, which is pointer through ptr.

#include <stdio.h>
#include <string.h>
int main()
{
char str[20] = "ABCDEFGHIJK";
int s = strlen(str);

str[3] = '\0';
s = strlen(str);

printf("%d\n",s);
return 0;
}

Output
3

#include <stdio.h>
void main()
{
int a=10,b=2,x=0;
x=a+b*a+10/2*a;
printf("value is =%d",x);
}

Output: value is =80

#include <stdio.h>
void main()
{
int x;
x= (printf("AA")||printf("BB"));
printf("%d",x);
printf("\n");

x= (printf("AA")&&printf("BB"));
printf("%d",x);
}

Output: AA1
AABB1

#include <stdio.h>
int main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
return 0;
}

Output:
00131
inti=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;

++ Operator has precedence over &&, || operator,


thus it is evaluated first. So the expression turns to be
m= 0 && 0 && 1 || 3
i=0. j=0, k=1, l=3
Now && has precedence over || and both has associativity
left to right. Hence the expression part 0 && 0 is evaluated first, so on.
m=((0 && 0) && 1) || 3)
=((0 && 1) || 3)
= 1 || 3= 1
Thus value of
m=1
i=0
j=0
k=1
l=3

You might also like