473,770 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About the typecasting to the LHS

Can this be done
float a;
(int) a = getc();

Oct 4 '07 #1
27 2792
In article <11************ *********@o80g2 000hse.googlegr oups.com>,
pa********@hotm ail.com <pa********@hot mail.comwrote:
>Can this be done
float a;
(int) a = getc();
No, but you could do

(int) (a = getc());

In C, assignments are expressions, so this would do the assignment
first, convert the resulting double to int, and then (because there
is nothing else on the line) throw away the int.

What were you hoping (int) a = getc() to mean? On a number of
platforms, float arithmetic (e.g., silently converting the int returned
by getc() into a float for the assignment) takes place only in
special purpose floating-point registers which cannot be manipulated
as integers.
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth
Oct 4 '07 #2
"pa********@hot mail.com" wrote:
>
Can this be done
float a;
(int) a = getc();
No. (Check your manual and/or this group's archives for a description
of "l-value".)

Well, that and getc() needs a FILE* as an argument. Perhaps you meant
getchar()?

You could use:

a = (float)getchar( );

However, the cast to float is unnecessary.

What, exactly, are you _really_ trying to do?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Oct 4 '07 #3
pa********@hotm ail.com wrote:
Can this be done
float a;
(int) a = getc();
No. What exactly are you trying to do? Since getc returns an int, assigning
it into floating type and trying to cast it to an int again seems pretty
pointless to me.

Oct 4 '07 #4
Kenneth Brody <ke******@spamc op.netwrites:
"pa********@hot mail.com" wrote:
>Can this be done
float a;
(int) a = getc();

No. (Check your manual and/or this group's archives for a description
of "l-value".)
"lvalue", without the hyphen, is likely to be a better search term.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 4 '07 #5
In data Thu, 04 Oct 2007 07:38:18 -0700, parag_paul@ scrisse:
>Can this be done
float a;
(int) a = getc();
float a;
a=getchar();
i think it is be done
Oct 5 '07 #6
I am not trying anything.
just trying to understand the scope of typecasting on the left side.
Since I saw some code similar to that.

I was confused initially.
But do you see any potential for the above construct.

@(all who are talking about lvalue)

I am a bit familiar with the concept myself. Just trying to get an
insight from the others on the casting on the left.
Oct 11 '07 #7
pa********@hotm ail.com said:
I am not trying anything.
just trying to understand the scope of typecasting on the left side.
A cast doesn't of itself yield a modifiable lvalue, so the result of a cast
expression cannot of itself receive an assignment.
Since I saw some code similar to that.
Then it wasn't, strictly speaking, C code.
I was confused initially.
But do you see any potential for the above construct.
Er, no, none at all. Do you? What do you think such a construct would mean?
C doesn't offer a meaning for it, so what meaning do you think it should
have?
@(all who are talking about lvalue)

I am a bit familiar with the concept myself. Just trying to get an
insight from the others on the casting on the left.
I think you need to consider more carefully the responses that you've
received so far. They're telling you something important, but you don't
seem to be getting it. Where a is a float, the expression (int)a = getc()
is simply meaningless. It's not legal C. Not even if we replace getc with
getchar.

Let's see what the compiler makes of it:

me@here:~/scratchcat foo.c
#include <stdio.h>

int main(void)
{
float FloatingPointOb ject;
(int)FloatingPo intObject = getchar();
printf("%f\n", FloatingPointOb ject);
return 0;
}

me@here:~/scratchmake
gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-ffloat-store -O2 -g -pg -c -o foo.o foo.c
foo.c: In function `main':
foo.c:6: warning: ANSI C forbids use of cast expressions as lvalues
foo.c:6: warning: value computed is not used

You see? You're just not supposed to do that.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 11 '07 #8
pa********@hotm ail.com wrote:
I am not trying anything.
just trying to understand the scope of typecasting on the left side.
Since I saw some code similar to that.

I was confused initially.
But do you see any potential for the above construct.

@(all who are talking about lvalue)

I am a bit familiar with the concept myself. Just trying to get an
insight from the others on the casting on the left.
Remember this: The cast operator is an *operator*. Like
other operators, it takes operands (one, for a cast) and yields
a value. A *value*, not an assignable object.

Given `double x;', all these are nonsense in the same way:

(int)x = 42;
-x = 42;
tan(x) = 42;
&x = malloc(sizeof x);

Clearer now?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 11 '07 #9
In article <Kq************ *************** ***@comcast.com >,
Eric Sosman <es*****@ieee-dot-org.invalidwrot e:
Remember this: The cast operator is an *operator*. Like
other operators, it takes operands (one, for a cast) and yields
a value. A *value*, not an assignable object.
Several operators yield assignable values, * for example, so remembering
that a cast is an operator doesn't necessarily help.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Oct 11 '07 #10

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

Similar topics

2
4357
by: sarmin kho | last post by:
Hi Pythoners, When it is an integer number, what is the range of the integer number and long integer number?? do we have typecasting of 8bits or 16bits signed and unsigned number in python? the python script i m working on would need to typecast the number it reads from a hardware. this number would then be typecasted according to its type before further processing. the typecasting is in form of signed 8bits and 16bits number. how do i...
3
8186
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to understand what is typecasting in C++. Say I have a Base class and a Derived class, I have a pointer to an object to each. Base *ba = new Base; Derived *de = new Derived;
2
4180
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin code*/
63
3417
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is 0.00000 and no 10.0000. Can anybody tell me why it is like that and why typecasting i not done in this case?
11
4424
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple assignment. int b = some_value;
3
1646
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have deduced that the "S" can be replaced with (String __gc *) and the code will compile and run just fine. But what I can't find is what exactly Microsoft calls these macros (I have also seen "L" used the same way) and where they are all documented. I...
7
2187
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
12
4479
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an integer as a pointer, but the pointer is 8 bytes while the integer is only 4 bytes. Here's an example function:
8
3629
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs says "is evaluated as a void expression, its value or designator is discarded." If I do the following: #include <stdio.h>
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.