#include<stdio.h>
int main()
{
printf("He %c llo",65);
}
Output: He A llo
#include<stdio.h>
int main()
{
printf("He %c llo",13);
}
Output: llo. It doesnt print He.
I can understand that 65 is ascii value for A and hence A is printed in first case but why llo in second case.
Thanks
ASCII 13 is carriage return, which on some systems simply moves the cursor to the beginning of the line you were just on.
Further characters then wipe out the earlier text.
Man ascii:
Oct Dec Hex Char
015 13 0D CR '\r'
Character 13 is carriage return so it prints He then returns to the beginning of the line and prints the remining llo.
It's just being rendered strangely due to the nature of a carriage return*. You can see the characters that are output, by piping to another tool such as xxd:
$ gcc b.c && ./a.out | xxd
0000000: 4865 200d 206c 6c6f He . llo
$ gcc c.c && ./a.out | xxd
0000000: 4865 2041 206c 6c6f He A llo
* See Wikipedia:
On printers, teletypes, and computer terminals that were not capable of displaying graphics, the carriage return was used without moving to the next line to allow characters to be placed on top of existing characters to produce character graphics, underlines, and crossed out text.
Related
I'm trying to print non-printable characters in C files, but I can't print the given character because I don't know its meaning. I noticed this when I compared my program with cat.
cat -nt file.txt:
134 is 127 7f
cat -n file.txt:
134 ^? is 127 7f
In the Caret Notation (which cat uses when -v or -t is specified) ^? represents the DEL character (Unicode U+007F, ASCII encoding 127 decimal, 0x7f in hex).
I have a .echo_colors file containing some variables for colors in the following format:
export red="\033[0;31m"
this works fine with echo -e, but i want to use this environment variables on a C code. I'm getting the variable via getenv and printing with printf:
#include <stdlib.h>
#include <stdio.h>
int main(){
char* color = getenv("red");
printf("%s", color);
printf("THIS SHOULD BE IN RED\n");
return 0;
}
with this program, i get
\033[0;31mTHIS SHOULD BE IN RED
The string is just being printed and not interpreted as a color code. printf("\033[0;31m") works and prints output in red as i want to. Any ideas for what to do to correct this problem?
Bash doesn't interpret \033 as "ESC" by default, as evident from hex-dumping the variable, but as "backslash, zero, three, three":
bash-3.2$ export red="\033[0;31m"
bash-3.2$ echo $red | xxd
00000000: 5c30 3333 5b30 3b33 316d 0a \033[0;31m.
You'll need to use a different Bash syntax to export the variable to have it interpret the escape sequence (instead of echo -e doing it):
export red=$'\033[0;31m'
i.e.
bash-3.2$ export red=$'\033[0;31m'
bash-3.2$ echo $red | xxd
00000000: 1b5b 303b 3331 6d0a .[0;31m.
Use ^[ (Control-left-bracket) in your shell script to key in the export command, as in
$ export red="<ctrl-[>[0;31m"
in your shell script, the ctrl-[ is actually the escape character (as typed from the terminal, it is possible that you need to escape it with Ctrl-V so it is not interpreted as an editing character, in that case, put a Ctrl-V in front of the escape char)
If you do
$ echo "<ctrl-[>[31mTHIS SHOULD BE IN RED"
THIS SHOULD BE IN RED (in red letters)
$ _
you will see the effect.
I am recoding printf and the behavior I get from my printf is slightly different from the real printf one because I work with strings (which are malloc) and real printf works with write functions.
When doing :
./a.out | cat -e
of :
#include <unistd.h>
int main()
{
printf(%c, 0);
return (0);
}
It is written ^# in the terminal
Whereas my function react the same without the pipe (cat -e) but with the pipe still prints nothing. Can you tell me how I can get my function write the "^#" when a | cat -e is used on it?
You are printing the character ascii value 0. It is a null value. When you are simply executing with ./a.out | cat or ./a.out it does not print anything. Because by default cat will not print non printable characters. But when you are using with cat -e, it is for printing the non-printable characters.
printf("%c",0);
From the man page of cat,
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
I am testing a C program in the windows terminal. I mocked up a quick example of the section I am having issues with. The example is as follows:
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main() {
char var[6];
scanf("%s", var);
int i=0;
while(var[i] != '\0') {
printf("%x ", var[i]);
i++;
}
return 0;
}
When I use a string with "normal" characters such as "dd" the output is as expected "61 61" (hex 61 is the letter "d"). When I try to input special characters such as í (0xA1 or U+00ED) I get the following output:
$ ./a.exe
í
ffffffc3 ffffffad
The UTF-8 codepage at http://www.utf8-chartable.de/ shows that the backwards 'i' is in fact 0xc3ad. How can I copy and paste this character as 0xA1, as I really want to input 0xA1 into the terminal, not 0xc3ad? I am copy and pasting this from "charmap". I even tried saving a text file in ANSI with the character and copying and pasting but I still get 0xc3ad. Please assist me.
EDIT: Running the same on a mac also gives me c3ad.
For reference, I'm copying this example (nearly) letter for letter from page 18 in The C Programming Language, Second Edition.
#include <stdio.h>
/*count characters in input, 2nd version*/
main(){
double n;
for (n = 0; getchar() != EOF; ++n)
;
printf("%.0f\n", n); /*this never prints*/
}
I wasn't sure if it was my version of gcc (I'm a noob):
% gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
Because
for (n = 0; getchar() != EOF; ++n)
printf("%.0f",n); /*returns 0123456...n*/
I tried printing "foo" instead of value n, just to see. It still isn't printing.
What am I missing here? I don't like moving ahead while ignoring little problems like these.
=====================================
EDIT
So the end result should be:
% gcc ./counter.c -o ./counter
% ./counter
foo
3
Right now, this is the output from the first snippet:
% ./counter
foo
0123
And the second one:
% ./counter
foo
^C
%
If you are on an OSX or linux box, you need to type Ctrl+D on its own line to generate an EOF character. On Windows, Ctrl+Z on its own line. Don't type Ctrl+Z on a unix box because that will just send your proces to the background.
You are typing Ctrl+C which is break, and will send a SIGTERM to your program.
On my mac, I get:
$ ./foo
hello
6D
Or if you don't want to signal the EOF condition, use echo and a pipe:
$ echo "hello" | ./foo
6
Note that the EOF has to be on its own line. a ^D is printed, and then 6 overwrites the ^, so it looks like the output is 6D.
Of course, in the two above examples, the characters being counted are h e l l o \n. If you don't want a newline, do this:
$ echo -n "hello" | ./foo
5
You shouldn't have that semi-colon after the for. That means the only thing in your loop is an empty statement.
Actually, David is correct. The empty statement is the only thing in the loop, but that's fine. It will count the number of characters. Then, outside the loop, it will print the total.
This demo shows that it works as intended (6-character input results in printing 6). However, I would use braces, or keep the indentation as in the original for clarity.