Reputation: 2206
I need to check if my program is correctly inputting these characters in my strings, so how can I see a "raw" string with these characters not parsed but actually shown?
Upvotes: 0
Views: 179
Reputation: 11628
just print the string as a sequence of characters, something like:
str is your string:
char *ptr = str;
while(*ptr != 0)
{
print("%02X", *ptr);
ptr++;
}
Upvotes: 0
Reputation: 49393
One possible solution would be to printout the ASCII values of what's in the string, this works regardless of it being a fixed string or coming from stdin
:
char str[] = "A\tBC1\n";
int j;
for(j = 0; j<strlen(str); str++)
printf("%#x ", str[j]);
Output:
>>0x41 0x9 0x42 0x43 0x31 0xA
From stdin:
fgets(str, 5, stdin);
for(j = 0; j<strlen(str); j++)
printf("%#x ", str[j]);
Input/Output:
>> 1 2 <---- That's a tab between 1 and 2
>> 0x31 0x9 0x32 0xA
You can get fancy and escape the characters to show \t
(0x9) and \n
(0xA) instead of the raw values, but if you just want to verify that you're getting them, this should work (and be faster)
Upvotes: 2
Reputation: 25705
You can either escape the characters, or write your own special routine like this:
void print_raw(const char *ch)
{
char *d = ch;
while(*d){
switch(*d){
case '\n':
printf("\\n");
break;
case '\v':
printf("\\n");
break;
case '\r':
printf("\\r");
break;
case '\a':
printf("\\a");
break;
case '\?':
printf("\\?");
break;
case '\"':
printf("\"");
break;
case '\t':
printf("\\t");
break;
case '\b':
printf("\\b");
break;
case '\f':
printf("\\f");
break;
case '\\':
printf("\\");
break;
case '\'':
printf("\'");
break;
default:
putchar(d);
}
}
Upvotes: 1
Reputation: 409166
If you read input (from file or from user) the special escape codes are not parsed. It's only in string and character literals in the source that the compiler treats those specially.
Edit: Simple example program with input and output to show what I'm talking about.
#include <stdio.h>
#include <string.h>
int main(int ac, char *av[])
{
char input[32];
printf("Enter input: ");
fgets(input, sizeof(input), stdin);
/* Remove trailing newline */
if (input[strlen(input) - 1] == '\n')
input[strlen(input) - 1] = '\0';
printf("input is \"%s\"\n", input);
return 0;
}
Example run of the above program:
Enter input: foo\nbar\thello input is "foo\nbar\thello"
The function fgets
leaves the actual newline at the end of the string. However, the sequences \n
and \t
in the input does not get translated to newline or tab (respectively). That is because it's not the input or output functions that handles these special character sequences, but the compiler.
If you have those sequences inside a string or character literal in the source, then the compiler recognizes those and changes them to a proper newline, tab or whatever it is you wrote. However, as the compiler doesn't know anything about input read from a file or from a user, these sequences are not translated.
Edit 2: In case you wonder about how to show literal special characters in a string, then please see this program:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void print_raw_string(const char *str)
{
while (*str != '\0')
{
if (isprint(*str))
fputc(*str, stdout);
else
{
switch (*str)
{
/* First check for known special sequences */
case '\0':
printf("\\0");
break;
case '\a':
printf("\\a");
break;
case '\b':
printf("\\b");
break;
case '\t':
printf("\\t");
break;
case '\n':
printf("\\n");
break;
case '\v':
printf("\\v");
break;
case '\f':
printf("\\f");
break;
case '\r':
printf("\\r");
break;
default:
/* None of the above, print it out as a hex escape sequence */
printf("\\x%02x", *str);
break;
}
}
str++;
}
}
int main(int ac, char *av[])
{
char input[32];
printf("Enter input: ");
fgets(input, sizeof(input), stdin);
printf("Input is: ");
print_raw_string(input);
printf("\n");
return 0;
}
When running the program:
Enter input: foo bar Input is: foo\tbar\n
Upvotes: 5
Reputation: 20392
Printf doesn't do anything special with \n or such. Those character sequences are interpreted by the compiler. Try this:
$ cat foo.c
#include <stdio.h>
#include <string.h>
int
main(int argc, char **argv)
{
if (argc != 2)
return 1;
printf("%d\n", (int)strlen(argv[1]));
printf("%d\n", (int)strlen("\n"));
printf("[%s]\n", argv[1]);
return 0;
}
$ cc -Wall -O2 -o foo foo.c && ./foo '\n'
2
1
[\n]
$
Notice that I escape \n when calling foo because the shell interprets \ (differently) if it's not escaped.
Upvotes: 0