0

My question is"which value of 'a' and 'b' will give me output '1' not '0'?

C language's code is below here

#include<stdio.h>
int main()
{ float a=1.2,b=10.7;

printf("output is = %f",a&&b);
Return 0; }

Comments
  • 3
    Why you treating boolean as float?
  • 1
    A and B as int and any number

    And then in the string instead of %f use %d

    But i don't know why you ask that question?
  • 0
    @jonas-w
    please tell that what is difference Between %d and %f
    And what is use of it.
  • 1
    @Demolishun

    I started learning c programming yesterday so i did some foolish mistake.
  • 0
    @gugrfchijvf printf is for formatting text. I don't know C never learned it. But i think %f is for float and %d for signed ints
  • 0
    @jo@jonas-w

    If i replace %f with %d then i get output 1
  • 4
  • 3
    this is a joke account right?

    your code makes no sense, floating point numbers are not meant to be used with the logical AND (&&) operator. logical operators are for boolean logic, which only asks whether a statement is true or not

    Right now you are treating floating point numbers as if they are logical statements, asking "is 1.2 and 10.7 true?"

    which is like asking "is sandwich a yes?"

    C is able to "kinda" understand it, because in C even boolean values (true/false) are treated like numbers (1/0) or (on/off)... which means that all numbers that are not 0 (off) are actually treated as a 1 (on). There might be some magic cases where it could result in a 0, I don't recall, but that doesn't change the fact this behavior is 'undefined'... you can't know for certain the statement will evaluate into the same result on every system/cpu/compiler because it could be implementation specific...

    in short

    1.2 AND 10.7 == True AND True == True == 1
  • 0
  • 2
    not to even mention, that printf isn't capable of formatting a boolean value. check the formatting string reference: https://cplusplus.com/reference/...

    The closest you can do is %d or %u that will just print out "1" because that's what C tries to guess you wanted to see, since True is just a 1 in the register, and you asked it to format a boolean value as a number. Or even worse, you asked it to format a %f, which is a floating point number... there's quite a bit of difference of how computers handle floating points numbers and boolean values...

    hell, if I were to translate your code into human instructions, your code is something like:

    "Hey man, I have some decimal numbers, one is called 'a' and is 1.2 and the other is 'b' and it's 10.7. Could you please write down for me if 'a' and 'b'...? but as a decimal number, thanks!"

    if it reads like nonsense to you, good, it should, because it is nonsense for the computer...
  • 1
    @Hazarth You should be a writer for the movies:

    "Hey man, I have some decimal numbers, one is called 'a' and is 1.2 and the other is 'b' and it's 10.7. Could you please write down for me if 'a' and 'b'...? but as a decimal number, thanks!"

    Is basically anytime they try to "technical" in a movie or tv show.
  • 0
    @Hazarth

    Can you recommend any type of meterial that learn to me c language?
    Because i started to learn it yesterday
  • 1
    @gugrfchijvf I don't know about materials. Best way to learn for me personally is just starting a project and then using google.

    try making a "guess what number am I thinking" game, where a computer generates a random number between 0-100 and asks you for input. If you guess the right number the program will congratulate you and it will close, if you guess wrong it will tell you if you guessed too much or too little and allow you to guess again until you finally get it right.

    you should be able to do this using the functions printf (printing into console), scanf (reading user input), a 'while' loop (for repeated guesses), rand() for random numbers and some basic math including the '%' modulo function

    unless I forgot about something I think I pretty much gave you the entire list of keywords and functions to look up and build into the simple game I just described. once you can make that, you can move to more complex projects
  • 2
  • 0
    @Demolishun

    Any video tutorials?
  • 2
    Sir this is devrant
Add Comment