LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-16-2006, 09:49 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
How can I cast an int to a double (C)?


How can I cast an int, stored in a variable, to a double?

I am trying to do this:
Code:
double d;
d = (double)strlen(str) \ (double)cols
Its obviously not correct, but that is how I would do it in java. Can anyone help?
 
Old 04-16-2006, 10:32 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
First, why do you say it's not working? Is it a compile error? What's the compiler's error message? Is it a runtime error? How do you know it's handling it incorrectly? Are you displaying the value after the operation and it's not what you expect?

Need more clarity on what the problem is.

What you've given is not actually doing division. You use a backslash ( \ ) as opposed to a forward slash ( / ) in the statement. A forward slash is used for division. A backslash is used to escape a character.

Second, what you have should cast the variable correctly. If not, I would suggest multiplying the return values by 1.0. Such as this:
Code:
d = (1.0 * strlen(str)) / (1.0 * cols);
Technically, you only need to multiply one operand by 1.0, but it doesn't hurt to do both.

Regardless, the typecasting you used originally should work.

Last edited by Dark_Helmet; 04-16-2006 at 10:33 PM.
 
Old 04-16-2006, 10:53 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Casting an int w/ (double) will do just fine. So, if i were an int, then (double)i would cast i to a double. No need to worry.
 
Old 04-16-2006, 11:37 PM   #4
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
*Edit*
Nevermind this post, I got it figured out, thank you all.

Last edited by smoothdogg00; 04-16-2006 at 11:45 PM.
 
  


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
cast from sting to int in Visual C++ Diederick Programming 3 11-29-2005 08:27 AM
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 12:22 PM
type cast from int to QString spoody_goon Programming 2 05-24-2005 06:14 AM
what does a cast in c++ actually do? markhod Programming 2 12-23-2004 09:01 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM

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

All times are GMT -5. The time now is 02:15 AM.

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