Comparisons

A

Akhil

#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");


if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}

Output:
true
false
false
true

Please justify the outputs.
 
S

Sandeep

Akhil said:
#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");


if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}

Output:
true
false
false
true

Please justify the outputs.
I got this as a homework question during my college days ......
Hint : look for more about "float" on the web ......
 
C

Christian Bau

"Akhil said:
#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");


if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}

Output:
true
false
false
true

Please justify the outputs.

Describe as precisely as you can which values are stored in b, c, d and
e.
 
C

CBFalconer

Chris said:
Certainly.
true
false
false
true

There we are: right 'and justified.

Dagnabit. I was about to write a similar smart alec response,
mentioning format strings, and pointing out that the output was
already left justified. But you beat me to it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Akhil said:
#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");


if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}

Aside from your actual question, I'd like to make some more points
about your code.

You have a "\n" at the beginning of each string you print. It should
be at the end. If there's no newline at the end of your program's output,
it might not even appear, depending on the system.

Proper indentation makes code easier to read. So does judicious use
of whitespace.

"int main()" is acceptable, but "int main(void)" is preferred.

"//" comments are valid in C99, and are supported by many C compilers,
but they're not supported by the C89/C90 standard. They're also
discouraged in Usenet postings, since they can cause problems with
line wrapping.

Applying all these suggestions, your code becomes:

#include <stdio.h>

int main(void)
{
int a = 1;
float b = 1.0;
float c = 1.1;
double d = 1.1;
long double e = 1.1;

if (a == b) /* true */
printf("true\n");
else
printf("false\n");

if (c == d) /* false */
printf("true\n");
else
printf("false\n");

if (c == e) /* false */
printf("true\n");
else
printf("false\n");

if (d == e) /* true */
printf("true\n");
else
printf("false\n");

return 0;
}

(I also use braces ({ ... }) even for single statements in control
structures.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top