Click here to Skip to main content
15,888,008 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
#include <stdio.h>

int main()
{
    int i=10,j=20,k=0;
    
    if(i=40)
      
      printf("One");
    if(j=50)
      
      printf("Two");
    if(k=60)
      
      printf("Three");
    if(k=0)
      
      printf("This is the end...");
    printf("%d %d %d\n",i,j,k);
}


What I have tried:

Why the result is onetwothree40 50 0 and not onetwothreethis is the end...40 50 0 ??
Posted
Updated 11-Oct-16 22:09pm
v3
Comments
OriginalGriff 12-Oct-16 2:56am    
Unfortunately, you have become the victim of a bug in the system, and your code is not readable. The admins are working on the problem, but it's a recalcitrant devil...
Basically, all your code after the first less than symbol has been assumed to be an HTML tag and removed, so we can't read anything after the "#include".
Please, edit your question (using the "Improve question" widget) and paste it again - this time using the "encode" or "code block" options in the paste pop-up on the right of your screen.
Suvendu Shekhar Giri 12-Oct-16 3:16am    
try updating the question as sugested by @OriginalGriff
Stemm33 12-Oct-16 3:18am    
I think i did
Suvendu Shekhar Giri 12-Oct-16 3:27am    
Yes. It's perfect now.
Garth J Lancaster 12-Oct-16 3:32am    
firstly, how do you compare values in 'c' vs assign values to variables .. ie, what is the difference between '=' and '==' and which do you think you should be using

That's because = is an assignment operator.
You need to use == for comparision.
Try-
C#
int main()
{
    int i=10,j=20,k=0;
    
    if(i==40)
      
      printf("One");
    if(j==50)
      
      printf("Two");
    if(k==60)
      
      printf("Three");
    if(k==0)      
      printf("This is the end...");

    printf("%d %d %d\n",i,j,k);
}


Hope, it helps :)
 
Share this answer
 
v2
Comments
Stemm33 12-Oct-16 3:47am    
I agree that = is an assignment operator,but how the three printf commands appear at the result and not the printf("This is the end...") command?that's my actual question.
Suvendu Shekhar Giri 12-Oct-16 4:00am    
Just replace -
if(k=0)
 printf("This is the end...");

with
if(k=1)
  printf("This is the end...");


and see the changes in the result.

Clue: 0=false
CPallini 12-Oct-16 4:01am    
5.
Stemm33 12-Oct-16 4:04am    
I get it!Thank you very much!
As suggested by Suvendu Shekhar Giri, you are using the assignment operator instead of the test for equality. Now in C programming language, the construct
C
i=40

is an expression, whoose evaluation is 40. So writing

C
if (i=40)

ends up in
C
if (40)

That is a true condition in the if (any number but 0 is assumed to be true)

That's the reason you're getting such unexpected output.
 
Share this answer
 
Comments
Suvendu Shekhar Giri 12-Oct-16 4:02am    
Indeed, an useful explaination. That's what OP is looking for.
My 5!
CPallini 12-Oct-16 4:06am    
Thank you.
Stemm33 12-Oct-16 4:05am    
Now its more understood!Thank you!
CPallini 12-Oct-16 4:06am    
You are welcome.
Quote:
Why the result is onetwothree40 50 0 and not onetwothreethis is the end...40 50 0 ?
Because in C, any integer is true but the value 0 which is false.
Be careful, while assinging a value in a if condition is legal, it is often unwanted and a common error of newbies.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
Comments
Stemm33 12-Oct-16 4:19am    
You give me great help.Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900