delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/03/29/23:21:04

From: mrscreen AT hotmail DOT com (Heliophobe)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: converting variables
Date: Mon, 29 Mar 1999 20:11:07 -0800
Organization: SOL
Lines: 37
Message-ID: <mrscreen-2903992011070001@mty2-131.dip.mbay.net>
References: <7dk0c8$v0e$1 AT nnrp1 DOT dejanews DOT com>
NNTP-Posting-Host: 206.55.224.131
NNTP-Posting-Date: Tue, 30 Mar 1999 04:11:29 GMT
X-Trace: 922767089.334.88 6BUII4S.ME083CE37C usenet1.supernews.com
X-Complaints-To: newsabuse AT remarQ DOT com
X-Newsreader: MT-NewsWatcher 2.3.5
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In article <7dk0c8$v0e$1 AT nnrp1 DOT dejanews DOT com>, ryan_brown AT my-dejanews DOT com wrote:

>How can I convert values from a variable of one type to another variable of a
>different type?  I need to convert a float variable over to a char variable. 
>In pascal there were functions for this in c I can't find anything.  I have
>heard of casting but I can't get it to work and there isn't any documentation
>about it anywhere.  Someone also told me that you can't cast between float
>and char. Does anyone know of a funciton or anything that can be used for
>this? Thanks in advance.

When you refer to a 'Char variable' do you mean a single one byte 'char',
or a string [of chars]?
 
  Mixing variables and constants of any data type is usually permissable
without any need for casting, you can just assign them to eachother or use
them both in expressions without problem.
  
  If you means strings, however, then there are libc functions for that: 
 
  for string to numeric variable:
 
  int i;
  float f;
 
  i=atoi("344");     //A[scii] to I[nteger] 
  f=atof("32.2");    //A[scii] to F[loat]

  for  numeric to string:
 
  char buffer[100];
 
  buffer=itoa(i);   //I[nteger] to A[scii]
  buffer=ftoa(f);   //F[loat] to A[scii]

 
  There are some other functions with can be a little more versitile, like
strtol, which can handle different number systems other than decimal.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019