473,386 Members | 1,621 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,386 software developers and data experts.

casting

i want convert int to char
Feb 2 '14 #1
5 1328
stdq
94 64KB
Hi! Can you post your code? Thanks!
Feb 2 '14 #2
Write a program that reads in three floating-point numbers and sorts them.
For example
4
9
2.5
Use only conditional statement
2.5
4
9
this example want convert int to float or not
Feb 3 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Be careful here.

You cannot convert an int to a char. An int can be 32 or even 64 bits while a char is typically 8 bits. You cannot stuff 32 bits into 8. The compiler gives a warning here about possible loss of data. You can do a cast but all that does is shut off the warning. 32 bits still cannot be packed into 8.

BTW: is was just this situation in the guidance software of a French Ariadne satellite missile that caused it to dump into the ocean for a multi-million dollar loss.

You can convert little things into big things. float to double is OK but double to float is not.

char to short is OK. short to int is OK. int to long is OK. But not the other way around because of possible data loss.

int to float is bad news. Only 6 significant figures in a float leading to loss of data again.

float to int is never OK. double to int is never OK. All the decimals get chopped off leading to loss of data. The compiler warns about this and you have to do cast to shut the warning off.

float to double is OK but double to float is not. double is big while float is small. Loss of data again.
Feb 3 '14 #4
donbock
2,426 Expert 2GB
Why would you want to "convert int to float" in that sorting example?

Are you asking how to convert a string ("45", "-34.92", etc) into a number?
Feb 3 '14 #5
Nepomuk
3,112 Expert 2GB
Could it be, that you're trying to create a string from an integer value by any chance? (So something like int value = 42 to char* answer = "42"?) If so, you can safely convert ints to chars digit by digit and then save that to a string.

@weaknessforcats: You are bringing out the big guns with that reply, aren't you? :-D
Feb 3 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.