What is the Correction Required?

R

ranjeet.gupta

Dear All,

I have line of code

fprintf(Fp_write, "%x", *Data++);

Data is char *,

The problem in the above line of code

its says that data is char but you print in the file with binary mode
i.e "%x", this x is unsigned int.... so when we print in the file
we get the extra field as the ffffff as the prefix on most of the
data.

I can understand this problem as I am trying to take the char pointer
value but forcing the compiler to print in the unsigned int format
i.e (%x)... But I dont know how to fix ? as i only want to print
the char data into the binary.

any idea and pointer to this

Thanks In Advance.
Ranjeet
 
E

Emmanuel Delahaye

fprintf(Fp_write, "%x", *Data++);

It's not recommended to use a unary operator in a parameter. The result
is implementation-dependent, and the semantic is unclear...

Prefer

fprintf (Fp_write, "%x", *Data);
Data++;
Data is char *,

If so, there is a type problem. "%x" is expecting an unsigned int. Note
also that a char could be signed (it's implementation-dependent). To
have it right, I suggest :

fprintf (Fp_write, "%x", (unsigned) (unsigned char) *Data);
Data++;

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
R

ranjeet.gupta

Emmanuel said:
It's not recommended to use a unary operator in a parameter. The result
is implementation-dependent, and the semantic is unclear...

Prefer

fprintf (Fp_write, "%x", *Data);
Data++;


If so, there is a type problem. "%x" is expecting an unsigned int. Note
also that a char could be signed (it's implementation-dependent). To
have it right, I suggest :

fprintf (Fp_write, "%x", (unsigned) (unsigned char) *Data);
Data++;

Thanks Emmanuel you cleared my issue, Thanks
Ranjeet
 
S

Spiro Trikaliotis

Hello Emmanuel,

Emmanuel Delahaye said:
It's not recommended to use a unary operator in a parameter. The result
is implementation-dependent, and the semantic is unclear...

Prefer
fprintf (Fp_write, "%x", *Data);
Data++;

Can you please elaborate where the problem is in this construction of
the OP? I do not see any problem in it as long as fprintf() is not a
makro. As Data is not used more than once in this line, it seems
well-defined to me.

Regards,
Spiro.
 
R

Richard Heathfield

Spiro said:
Hello Emmanuel,



Can you please elaborate where the problem is in this construction of
the OP? I do not see any problem in it as long as fprintf() is not a
makro. As Data is not used more than once in this line, it seems
well-defined to me.

*Data++ is indeed well-defined, and a well-known idiom. I think Emmanuel was
concerned that the OP was not using "good style", although I think on this
occasion there wasn't anything wrong with it. We are, after all, supposed
to be writing C, not BASIC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top