Home > Back-end >  #include<stdio.h> int main() { int a=10,b=4,c=2; b != !a; c =! !a; printf("b = %d\t c =
#include<stdio.h> int main() { int a=10,b=4,c=2; b != !a; c =! !a; printf("b = %d\t c =

Time:11-15

#include<stdio.h>
int main()
{
    int a=10,b=4,c=2;
    b != !a;
    c =! !a;
    printf("b = %d\t c = %d",b,c);
}


I need output of this question explain me the outcome??

CodePudding user response:

When several valid operators are written together, C decides what operators that the code is using by utilizing something called the "maximal munch rule". Which essentially means that during parsing, it will always left-to-right grab the largest bite of operator goo which could be used to form a valid operator. For example i would be treated as (i ) since is a valid operator, but is not.

In case of != then that's a valid operator (not equal), so that's what we get instead of ! and =.

In case of =! then that's not a valid operator in itself, but there are two separate, valid operators = (assignment) and ! (logical NOT).

So this code is the same as:

(b) != (!a);  // b not equal to (not a)
(c) = (!!a);  // assign (not not a) to c

Note that the former of these is a no-op, it's a comparison using != where the result isn't stored anywhere.

What the individual operators do from there, is left as an exercise to study.

CodePudding user response:

So, it looks like you don't know anything about C, well, I would try to explain everything that this code does:

#include <stdio.h> This is standart directive, that allows you to include different types of files into your code, in this case you are including stdio.h which is header file of standart library, that allows you to use IO functions (in your case it is printf). It is being processed by preprocessor, before compilation, you can read about it yourself.

int main() This is global function, that is beign called, when you start your programm. Basically, you cant write anything without this function (I mean, you can, but you wouldn't be able to use it, unless you are writing some kind of a library)

int a = 10, b = 4, c = 2; Here you are declaring and simoltaniously initializing your 3 variables a, b, c, with integer data type, what you show by typing int brefore variable names. Vareiables are needed in order to hold some data, that you can use later, and as C has datatypes, you need to specify what kind of data you whant to hold in those variables. (you can read about it here)

b != !a; This line is actually doesn't do anything for you, because != is logical operator, that returns true if variables you are comparing ar not equal, and as you don't save result of this operation, it basically disappears to nowhere. (It would be probably deleted by compiler during optimization)

c =! !a; This line looks very simmilar to the previous one, but actually it does something completely differen, as you might see, exclamation mark here is on the other side of the equality, so it is actually c = !!a;, wich means, that you are assigning !!a to c. So, let's look at !!a, what does those exclamation marks do? well, they are called negation operators, and they return opposite of its operand. But a is equal to 10, how it can be negated? Well, variables in C can be casted in different datatypes, in this case a is being converted from integer into boolean, so it's value changes from 10 to 1 or true. But as it is being negated 2 times, negation canceles and true is being assigned to 'c' with yet another type casting, now back to int from bool, and as true is represented by 1, 1 is what actually is assigned to c.

printf("b = %d\t c = %d", b, c); Here you just output your variables, to be specific: b and c. So, you can see %d here, what it does, is it just read the next argument of this fucntion and repleces itself as signed integer in decimal represenation.

So, after execution of this programm you get this output: b = 4 c = 1

Also you can see here, that \t is being replced by tab.

CodePudding user response:

You have to look maximum munch rule of c/cpp languages

https://shafik.github.io/c /maximal munch/2020/12/28/maximal_munch_and_cpp.html

you can find information above link. According the rule the codes will compiled like this;

b != !a;
c = !!a;

!a -> will 0 because it is a logical not and "a" is a non zero value

!0 -> will 1

  •  Tags:  
  • c
  • Related