Sponsored Content
Top Forums UNIX for Dummies Questions & Answers convert from an integer to a string Post 1781 by larryprg on Thursday 29th of March 2001 12:18:26 PM
Old 03-29-2001
Glad you caught it

I was just copying example code, deleting stuff that
wasn't necessary -- EXCEPT for the initialization of j,
which could easily be catastophic.

The code was from VC++:

/* SPRINTF.C: This program uses sprintf to format various
* data and place them in the string named buffer.
*/

#include <stdio.h>

void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;

/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );

printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}

 

10 More Discussions You Might Find Interesting

1. Programming

Integer to String

Which function should I use to convert an Integer to a String or Char format ? Thanx (2 Replies)
Discussion started by: psilva
2 Replies

2. UNIX for Dummies Questions & Answers

integer to string

Hi all, is there an easy way to convert integer to string in bash? I have numbers like 1, 2, ..., 112, ... and I would like to get 001 002 003 004 ... Thank you, Sarah (4 Replies)
Discussion started by: f_o_555
4 Replies

3. Shell Programming and Scripting

how to convert string to an integer and how to do calculations like add.,sub.,mult. on it

How to convert string into an integer or number For example : % set tim = `date` % echo $tim Tue Feb 22 16:25:08 IST 2011 here How to increment time by 10 hrs like 16+10 , here 16 is a string in date cmd. .. how to convert 16 to an integer and added to a another nimber ? Thanks... (3 Replies)
Discussion started by: sbhamidi
3 Replies

4. Shell Programming and Scripting

Read Variable From Fille And Convert it to Integer

I read 3 variables from from Inputfile.txt the third one "startnumber" is a number when i compare it with 9 ($startnumber -le 9) it give's me a "unary operator expected", i know that -le is for number comparison. What i need is to convert $startnumber to integer (i have try to do it with expr but... (8 Replies)
Discussion started by: marios
8 Replies

5. Shell Programming and Scripting

Convert to Integer

Hi fellows!! i'm doing something which is not working out for me properly which i don't understand why nowdate=`date +%s` echo $nowdate now the problem how to convert a date which is stored in a variable mydate="22/Oct/2011" mydate=`date -d '$mydate' +%s` it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies

6. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

7. Shell Programming and Scripting

[Solved] need to convert decimal to integer

Using below command awk 'NR==FNR{A=$1;next} {sum+=($2*A)}END{OFMT="%20f";print int(sum)}' Market.txt Product.txt answer:351770174.00000 how to convert this to 351770174. when i try with below command i am getting different result. awk 'NR==FNR{A=$1;next}... (3 Replies)
Discussion started by: katakamvivek
3 Replies

8. Shell Programming and Scripting

How to convert string into integer in shell scripting?

Hi All, sessionid_remote=$(echo "select odb_sessionid from sysopendb where odb_dbname='syscdr';" | sudo -u cucluster ssh ucbu-aricent-vm93 "source /opt/cisco/connection/lib/connection.profile; $INFORMIXDIR/bin/dbaccess sysmaster@ciscounity") for sid in $sessionid_remote;do if * ]];... (2 Replies)
Discussion started by: deeptis
2 Replies

9. Shell Programming and Scripting

How to read from file and convert from string to integer?

Hi all, I am trying to write a script to be able to Run top command Pick the PIDs that are taking more than 90% of CPU time Get more details like what is the script name and location for that PID Script should run until I manually kill it by ctrl + C I have come up with following script... (3 Replies)
Discussion started by: pat_pramod
3 Replies

10. Shell Programming and Scripting

Convert string number to a integer

I have data as below "ROWS merge process complete. thousand rows changed" I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000. Any help or pointer. Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: dsravanam
6 Replies
ECVT(3P)						     POSIX Programmer's Manual							  ECVT(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
ecvt, fcvt, gcvt - convert a floating-point number to a string (LEGACY) SYNOPSIS
#include <stdlib.h> char *ecvt(double value, int ndigit, int *restrict decpt, int *restrict sign); char *fcvt(double value, int ndigit, int *restrict decpt, int *restrict sign); char *gcvt(double value, int ndigit, char *buf); DESCRIPTION
The ecvt(), fcvt(), and gcvt() functions shall convert floating-point numbers to null-terminated strings. The ecvt() function shall convert value to a null-terminated string of ndigit digits (where ndigit is reduced to an unspecified limit determined by the precision of a double) and return a pointer to the string. The high-order digit shall be non-zero, unless the value is 0. The low-order digit shall be rounded in an implementation-defined manner. The position of the radix character relative to the beginning of the string shall be stored in the integer pointed to by decpt (negative means to the left of the returned digits). If value is zero, it is unspecified whether the integer pointed to by decpt would be 0 or 1. The radix character shall not be included in the returned string. If the sign of the result is negative, the integer pointed to by sign shall be non-zero; otherwise, it shall be 0. If the converted value is out of range or is not representable, the contents of the returned string are unspecified. The fcvt() function shall be equivalent to ecvt(), except that ndigit specifies the number of digits desired after the radix character. The total number of digits in the result string is restricted to an unspecified limit as determined by the precision of a double. The gcvt() function shall convert value to a null-terminated string (similar to that of the %g conversion specification format of printf()) in the array pointed to by buf and shall return buf. It shall produce ndigit significant digits (limited to an unspecified value determined by the precision of a double) in the %f conversion specification format of printf() if possible, or the %e conversion specification format of printf() (scientific notation) otherwise. A minus sign shall be included in the returned string if value is less than 0. A radix charac- ter shall be included in the returned string if value is not a whole number. Trailing zeros shall be suppressed where value is not a whole number. The radix character is determined by the current locale. If setlocale() has not been called successfully, the default locale, POSIX, is used. The default locale specifies a period ( '.' ) as the radix character. The LC_NUMERIC category determines the value of the radix character within the current locale. These functions need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe. RETURN VALUE
The ecvt() and fcvt() functions shall return a pointer to a null-terminated string of digits. The gcvt() function shall return buf. The return values from ecvt() and fcvt() may point to static data which may be overwritten by subsequent calls to these functions. ERRORS
No errors are defined. The following sections are informative. EXAMPLES
None. APPLICATION USAGE
The sprintf() function is preferred over this function. RATIONALE
None. FUTURE DIRECTIONS
These functions may be withdrawn in a future version. SEE ALSO
printf(), setlocale(), the Base Definitions volume of IEEE Std 1003.1-2001, <stdlib.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 ECVT(3P)
All times are GMT -4. The time now is 04:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy