473,403 Members | 2,359 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,403 software developers and data experts.

sprintf problems

I am trying to build variables for a function using sprintf. However
they don't seem to be proper
char strings since submiting literals seems to work fine. Any advice
to get me rolling?

sprintf( i1, "%s %s \0",v1,v2 );
sprintf( i1, "%s %s \0",v3,v4 );
printf("answer %s\n",add(cTypeCurr,i1,i2));
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG♥ èG♥
---0.000000 3 i 4 i

the function:

char *add(char *ctype, char *i, char *j)
{
double x,y,z,a;
static char retval[10]="retval";
printf("---%s %s %s\n",retval,i,j); <--output line
x=atof(i);
y=atof(j);
z=x+y;
sprintf( retval, "%f",z );
return retval;
}

May 21 '07 #1
6 2640
On 20 May 2007 23:43:15 -0700, merrittr <me******@gmail.comwrote in
comp.lang.c:
I am trying to build variables for a function using sprintf. However
they don't seem to be proper
char strings since submiting literals seems to work fine. Any advice
to get me rolling?

sprintf( i1, "%s %s \0",v1,v2 );
What is the definition of 'i1', and what are the contents of 'v1' and
'v2'? Are v1 and v2 strings, or are they numeric values?
sprintf( i1, "%s %s \0",v3,v4 );
Do you realize that this sprintf() just overwrites the value of the
last sprintf()?
printf("answer %s\n",add(cTypeCurr,i1,i2));
What is 'cTypeCurr'?
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG? èG?
---0.000000 3 i 4 i

the function:

char *add(char *ctype, char *i, char *j)
{
double x,y,z,a;
static char retval[10]="retval";
printf("---%s %s %s\n",retval,i,j); <--output line
x=atof(i);
y=atof(j);
z=x+y;
sprintf( retval, "%f",z );
return retval;
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
May 21 '07 #2
merrittr wrote, On 21/05/07 07:43:
I am trying to build variables for a function using sprintf. However
they don't seem to be proper
char strings since submiting literals seems to work fine. Any advice
to get me rolling?

sprintf( i1, "%s %s \0",v1,v2 );
<snip>

You probably have something wrong before this line is executed. However,
since you have not provided a *complete* compilable example showing the
problem it is anybodies guess.

One thing I do know is that you need to reread the chapter in your text
book that introduces string literals, since you do not need the explicit \0.
--
Flash Gordon
May 21 '07 #3
the v1,v2,v3,v4 vars ar built like this:

cVal=getchar();
while(cVal != '\n')
{
v1[iCharCount++] = tolower(cVal);
cVal=getchar();
}
v1[iCharCount++]='\0';
iCharCount=0;

typically vi v2 v3 v4 are "5 i" or "4.5 d"

cTypeCurr = "i" or "d" I don't think I will need this variable it is a
remnant of something I was trying.
whoops the second i1 should have been i2

I added the \0 after I was getting these results , sort of as a long
shot
to see if my string termination wasn't working. So sprintf null
terminates eh?
sprintf( i1, "%s %s \0",v1,v2 );
sprintf( i2, "%s %s \0",v3,v4 );
printf("answer %s\n",add(cTypeCurr,i1,i2));
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG♥ èG♥
---0.000000 3 i 4 i

May 21 '07 #4
On 21 May, 08:43, merrittr <merri...@gmail.comwrote:

leave some context in your posts
the v1,v2,v3,v4 vars ar built like this:

Â* Â* cVal=getchar();
Â* Â* Â*while(cVal != '\n')
Â* Â* Â*{
Â* Â* Â* v1[iCharCount++] = tolower(cVal);
Â* Â* Â* cVal=getchar();
Â* Â* Â*}
Â* Â* Â*v1[iCharCount++]='\0';
Â* Â* Â*iCharCount=0;

typically vi v2 v3 v4 are "5 i" or "4.5 d"

cTypeCurr = "i" or "d" I don't think I will need this variable it is a
remnant of something I was trying.

whoops the second i1 should have been i2

I added the \0 after I was getting these results , sort of as a long
shot
to see if my string termination wasn't working. So sprintf null
terminates eh?

sprintf( i1, "%s %s \0",v1,v2 );
sprintf( i2, "%s %s \0",v3,v4 );
printf("answer %s\n",add(cTypeCurr,i1,i2));
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG♥ èG♥
---0.000000 3 i 4 i
so why don't you POST A COMPLETE COMPILABLE EXAMPLE? As you
have been asked to (twice). Don't put in things like
"typically" and "this was left over from one I did before".

For instance this is complete and compilable. It is also
problably wrong because you made me guess a lot

****************
#include <stdio.h>
#include <stdlib.h>

char *add (char *ctype, char *i, char *j)
{
double x,y,z;
static char retval[10] = "retval";

printf ("---%s %s %s\n", retval, i, j);
x = atof(i);
y = atof(j);
z = x + y;
sprintf (retval, "%f", z);

return retval;
}

int main (void)
{
char i1 [100];
char i2 [100];
char *r1;
char *r2;
char *v1 = "5 i";
char *v2 = "4.5 d";
char *v3 = "5 i"; /* 4 variables and you give me two
examples... */
char *v4 = "4.5 d";

sprintf (i1, "%s %s", v1, v2);
sprintf (i2, "%s %s", v3, v4);
r1 = add("h",i1,i2); /* no idea what cTypeCur is */
printf ("answer %s\n", r1);
r2 = add("i","3 i","4 i");
printf ("answer %s\n", r2);

return 0;
}

*************
the output looks like this:-

G:\Debug>mer.exe
---retval 5 i 4.5 d 5 i 4.5 d
answer 10.000000
---10.000000 3 i 4 i
answer 7.000000

which is different from yours so I guessed wrong.
--
Nick Keighley

"Remember, the TAB key is your friend!!"
Computer Training at Large Insurance Company

May 21 '07 #5
Nick Keighley wrote, On 21/05/07 12:55:
On 21 May, 08:43, merrittr <merri...@gmail.comwrote:

leave some context in your posts
Because of this

<snip>
>typically vi v2 v3 v4 are "5 i" or "4.5 d"
<snip>
so why don't you POST A COMPLETE COMPILABLE EXAMPLE? As you
have been asked to (twice). Don't put in things like
"typically" and "this was left over from one I did before".
and this which Nick pointed out, Merrittr has lost the opportunity of me
trying to help, and others may feel the same. If s/he can't be bothered
to make it easy for those helping the chances people being helpful
rapidly decrease.

I may or may not change my mind if s/he takes note and posts sensibly.
--
Flash Gordon
May 21 '07 #6
On May 21, 8:43 am, merrittr <merri...@gmail.comwrote:
sprintf( i1, "%s %s \0",v1,v2 );
sprintf( i2, "%s %s \0",v3,v4 );
printf("answer %s\n",add(cTypeCurr,i1,i2));
printf("answer %s\n",add("i","3 i","4 i"));

here is the output I get:

---retval èG♥ èG♥
---0.000000 3 i 4 i
Guess 1: You didn't allocate enough (any?) memory for i1 and i2.
Guess 2: Bug in the function add you tell us nothing about results in
the garbage.

May 21 '07 #7

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

Similar topics

6
by: Venkat Venkataraju | last post by:
Hi All I'm having problems printing a string with a % char. Here is the code line = sprintf("<table width=100% id=%s>\n", $table_id); I'm getting an not enough parameter error. The % for the...
13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
15
by: Earth | last post by:
Hi all, I am new to c and trying the sprintf function. I have written a testing program to try the sprintf fuction and expect the output is 1.234. However, the output shows nothing. Am I...
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
26
by: steve | last post by:
Well I've been working all morning and have finally found the source of my "bus error (signal 10)" errors. The source is odd. The error occurs in any function where I make the function call: ...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
4
by: MimiMi | last post by:
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly! I wouldn't be surprised if my problems have something to do with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
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...
0
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...
0
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...
0
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,...

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.