Logical Operator

Oct 15, 2019 at 12:15pm
Guys, if the variable is declared, then how is it possible to initialize it? from declaration to initialization? like how to change it and put the input process?




#include <stdio.h>
void main ()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
}

Oct 15, 2019 at 12:22pm
if the variable is declared, then how is it possible to initialize it?

and why would that not be possible?
you can initialize variable after declaration as many times as you want, unless its declared as const

and you can intialize the variable at the same time when declaring it, which is what you are doing in your example.

like how to change it and put the input process?

what do you mean by "put the input process"?
Last edited on Oct 15, 2019 at 12:26pm
Oct 15, 2019 at 12:28pm
you can initialize variable after declaration as many times as you want
C++ terminology can be confusing, but let's be a bit careful at least. You can only initialize something once (perhaps being "default initialized" if you're not setting it to anything explicitly). After that, you're assigning to it.

That being said, I'm not actually sure what OP is asking, so what you said might make more sense for him :)
Last edited on Oct 15, 2019 at 12:31pm
Oct 15, 2019 at 12:31pm
@Ganado, heh yes you're right, initializing is one thing, assigning is not the same thing.
Oct 15, 2019 at 12:36pm
I mean for clear like this,

if i change the code, what would be the input or the scanf process? what would I put? after the printf? like for example you're putting printf("Enter Number:"); something like this.
scanf("%d",&n1);

#include <stdio.h>
void main ()
{
int a ,b, c, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
}
Oct 15, 2019 at 12:43pm
Assuming this is C, then yes, you can tell the user to do something with printf, and then receive input via scanf.
Last edited on Oct 15, 2019 at 12:45pm
Oct 15, 2019 at 12:46pm
You might want to consider using std::cin for C++ code instead of scanf which is C function.

in either case std::cin or scanf should be called after variable declaration to read user input into varaibles.
Oct 15, 2019 at 2:57pm
void main is a bad habit. Strict language settings will not like it, but most compilers will take it when in relaxed/extended language mode. There isn't anything to gain from it, and its 1 more letter, so just type int instead and forget this exists.

Oct 21, 2019 at 4:45pm
ok dude, thanks and to all of you <3
Topic archived. No new replies allowed.