473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

operator has no effect

#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
~m;
printf("%x\n",m);
return 0;
}
gives me :
$ gcc -Wall -g -o test test.c
test.c: In function `main':
test.c:4: warning: statement with no effect
$ ./test
fdff
whereas this works out properly :

#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
printf("%x\n",~m);
return 0;
}

$gcc -Wall -g -o test test.c
$ ./test
ffff0200
Why ???

Nov 14 '06 #1
5 4250
#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
~m;
printf("%x\n",m);
return 0;

Try this,

m=~m;

Nov 14 '06 #2
I mean than why did the second case work ??

sam_...@yahoo.co.in wrote:
#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
~m;
printf("%x\n",m);
return 0;


Try this,

m=~m;
Nov 14 '06 #3
onkar wrote:

Please don't top post.
sam_...@yahoo.co.in wrote:
>>>#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
~m;
printf("%x\n",m);
return 0;


Try this,

m=~m;

I mean than why did the second case work ??
Because you were passing ~m to printf. ~m on its own doesn't change m,
it just evaluates the expression and discards the result.

--
Ian Collins.
Nov 14 '06 #4
On 13 Nov 2006 22:14:50 -0800, "onkar" <on*******@gmail.comwrote:
>#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
~m;
You complement m and do nothing with result. As complementing an
unsigned int has no effects, then the operator has no effect.
printf("%x\n",m);
return 0;
}
gives me :
<...>
>test.c:4: warning: statement with no effect
<...>
>
whereas this works out properly :

#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
printf("%x\n",~m);
You now complement m and use its result to pass it to printf, thus the
operator serves some clear purpose and does have effect.
return 0;
}
Regrads,

Zara
Nov 14 '06 #5
onkar said:
#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
~m;
This is an expression, and C likes to evaluate expressions. In this case,
the expression ~m is evaluated, and you do nothing with the result. m is
not changed.
printf("%x\n",m);
return 0;
}
gives me :
$ gcc -Wall -g -o test test.c
test.c: In function `main':
test.c:4: warning: statement with no effect
Right.
$ ./test
fdff
whereas this works out properly :

#include<stdio.h>
int main(void){
unsigned int m=0xfdff;
printf("%x\n",~m);
Here, you use the ~m expression as an argument to printf, so its value /is/
used, so it does have an effect on the program.

But m's value is unchanged. Add this line to your program:

printf("And the value of m is still %x\n", m);

If you want to change m's value, you will want to use some kind of
assignment statement.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ··········· sylvain | last post by:
hello, what does the operator |= mean? I saw it in a script : $output = "png"; $style = BCS_ALIGN_CENTER; $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
5
by: exits funnel | last post by:
Hello, I'm confused by the use of the Scope resolution operator on the indicated lines in the following code (which was copied from Thinking in C++ by Bruce Eckel). Removing them seems to have...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
1
by: DaKoadMunky | last post by:
Is it fair to say that in the statement T* t = new T; that construction of T is a side-effect and that it need occur only before the next sequence point? If that is the case, what about...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
6
by: Taras_96 | last post by:
Hi all, Jesse Liberty writes: "Note that the String class provides the operator+. The designer of the Employee class has blocked access to the operator+ being called on Employee objects by...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.