LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2003, 01:55 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
How to understand the declaration?


How to understand the declaration, int c1=i%3==0; ?

Why
Code:
int i;
cin>>i;
cout<<c1=i%3==0;
is right while
Code:
cout<<1%3==0;
is wrong?

Thank you.

Last edited by Xiangbuilder; 09-13-2003 at 01:56 AM.
 
Old 09-13-2003, 02:18 AM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Code:
#include<iostream>
using namespace std;
main()
{
        int i;
        cout<<(15%3==0);
}
The output is 1 as required.

If we don't use the brackets then the value of 15%3 will be outputted.

If I haven't understood ur question plz be more specific

Last edited by SaTaN; 09-13-2003 at 02:21 AM.
 
Old 09-13-2003, 02:46 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
I guess c1=i%3 means give the value of i%3 to the variable c1, if add "==0" at the end, what is the meaning of the clause, c1=i%3==0 ?
Another question:
Code:
 #include<iostream>
using namespace std;
main()
{
        int i;
        cout<<1%3==0;
}
why the output is 1 instead of 0?
 
Old 09-13-2003, 02:57 AM   #4
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
The reason the output given is the ans for (1%3) .
The cout takes it that way due to I suppose operator precedence.
If you want entire thing i.e , 1%3==0 , you must enclose them in
brackets i.e , (1%3==0)

The meaning of this clause is (1%3==0) is it checks if the value
of 1%3 is equal to 0 and returns (0 for false, 1 for true)

Ex:- Suppose we say (14%3==0)
14%3 is 2 and it is not equal to 0
So , output will be "0" [False]

Last edited by SaTaN; 09-13-2003 at 03:03 AM.
 
Old 09-13-2003, 03:11 AM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
I am poor in both c++ and English.

Now I want to know why the output of cout<<1%3==0; is 1 instead of some error information?
What is the computer's thinking about the clause, cout<<1%3==0;?
Does the computer consider cout<<1%3==0; as cout<<1;, and ignore %3==0?

Last edited by Xiangbuilder; 09-13-2003 at 03:16 AM.
 
Old 09-13-2003, 03:21 AM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
No , the computer will think that
cout<<14%3==0 is same as

((cout<<14%3)==0);
So, the output will be 2.
 
Old 09-13-2003, 04:08 AM   #7
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
I feel 14%3 is equal to the integer 2,
does (cout<<14%3) is equal to the integer 2?
does the type of (cout<<i) is same as the type of i?

Why the output of ((cout<<14%3)==0); is 2?
Does the computer only compute (cout<<14%3), while ignore (2==0)?
Why thc computer don't think ((cout<<14%3)==0) in this process:
check the value of (cout<<14%3), if the value of (cout<<14%3) ia equal to 0, then the result of ((cout<<14%3)==0) is 1 (right), otherwise, is 0 (false)?

Another question:
Why cout<<1%3==0; can output 1, while I can not successfully compile cout<<1%3==0<<endl;?

Last edited by Xiangbuilder; 09-13-2003 at 04:09 AM.
 
Old 09-13-2003, 04:36 AM   #8
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
cout <<14%3 will print value 2 .
The compiler isn't ignoring (2==0)

cout<<((cout<<14%3)==0);

This will first give output 2 and then 0 [since 2==0 is false]
 
Old 09-13-2003, 04:49 AM   #9
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you for your patience.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
declaration does not declare anything tristanm Programming 5 10-24-2005 04:00 PM
C Global Declaration Aju Programming 3 09-29-2005 04:21 AM
char * declaration in c; alaios Programming 19 09-14-2005 09:50 PM
the c declaration kapsikum Programming 3 04-06-2005 02:12 AM
Perl global declaration? mikeshn Programming 1 08-16-2002 06:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration