M
Michael Mair said:Rod said:[snip!]Martin Ambuhl said:(e-mail address removed) wrote:
Martin, _fix_ your quoting. You merged your reply with to Santosh's
unquoted statements. You're missing Santosh's message header.
Martin Ambuhl's post is a direct answer to the OP as can
be seen from the message headers. santosh was never in
between here. You probably mixed that up with another
subthread.
Keith said:santosh said:Hi,
I needed help in converting a character to the correspoding
hexadecimal values, like the following example,
ASCII value : ABC
Hex Code Value : %41%42%43...
whats the logic of conversion...
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char arr[3] = { 'A', 'B', 'C' };
short cnt;
for(cnt = 0; cnt < 3; cnt++)
printf("%%%x ", arr[cnt]);
fflush(stdout);
return 0;
}
You don't use anything from <stdlib.h>.
That prints the hexadecimal values rather than converting them, but
the original problem statement wasn't very clear so it's probably ok.
(Converting to a string would require some moderately complex memory
management.)
For characters with values less than 16, you print a single digit,
e.g., "%f" rather than "%f". Again, the problem statement wasn't
clear on this point.
You print a spaces between the characters, which is inconsistent with
the example.
Why do you use type short for the array index? It typically saves
only an insigificant amount of data space, and the resulting code
could be larger and slower on many systems. Just use int.
Okay.
The "%x" format expects an unsigned int; you're giving it a char.
It's likely to work anyway, but it could cause problems -- and proving
that it does what you want is a lot more work than just fixing the
code. This is one of those rare cases where a cast is actually
appropriate.
3 is a magic number (not a huge deal in a snippet like this).
The output isn't terminated by a new-line, so it's not guaranteed to
appear even with the fflush(stdout) (and the standard is unclear on
just what can go wrong).
#include <stdio.h>
int main(void)
{
char arr[3] = { 'A', 'B', 'C' };
const int arr_len = sizeof(arr) / sizeof(arr[0]);
int i;
for (i = 0; i < arr_len; i++) {
printf("%%%02x", (unsigned int)arr);
}
putchar('\n');
return 0;
}
santosh said:Keith Thompson wrote: [...]The output isn't terminated by a new-line, so it's not guaranteed to
appear even with the fflush(stdout) (and the standard is unclear on
just what can go wrong).
I was thus far under the impression that fflush(stdout) was equivalent,
(in terms of writing buffered output), to a newline character. If
fflush(stdout) is not guaranteed to do it's job, then why define it,
atleast for stdout?
Thanks for this improvement.
Jordan Abel said:for(int i=0;i<strlen(s);i++)
char myChar = s;
That re-evaluates strlen(s) on each iteration, making the loop
O(N**2) rather than O(N).
While any decent compiler ...., you're right. How about i=0;s;i++?
Sure (though I'd write "s != '\0'"), or use a pointer, or compute
strlen() outside the loop. (The latter does a single unnecessary
traversal of the string, which isn't nearly as bad as doing N
unnecessary traversals.)
Many do even without full c99 support, though, mainly as a consequence
of also being C++ compilers. The same applies to //comments, though
those are bad in code to be posted on this group for other reasons.
Many != All. Using C99-specific feature, even ones that are widely
implemented, limits the portability of your code. If you're willing
to accept that, that's fine, but you should be aware of it, and you
should know how to avoid the problem if you need to (in this case, by
declaring the variable separately).
potato, potato.
Would you rather call printf and sprintf "potatoes"?
More like potatoe, potato, Mr Vice President.
Jordan Abel said:"potato, potato" is pronounced as /p@teIto p@tAto/ i.e. "ay" and "ah"
sounds for the 'a'.
And I was joking, of course there's a different. There are also
differences between functions and subroutines, but we call them all
'functions', even the ones that aren't.
Keith said:Yeah, I got the joke. The point of the joke is that there's no
practical difference between "potayto" and "potahto". There's a very
real difference between "function" and "program" (and the C standard
doesn't even use the word "subroutine".)
What exactly was your point?
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.