puts

Why are all my puts returning =>nil?

泪湿孤枕 提交于 2020-01-02 01:54:13
问题 I know this may seem like a really simple question, but it really bothers me that my puts keep generating "=> nil" and I scoured for an answer but could not find one. Thanks. puts 'blink ' *4 blink blink blink blink => nil 回答1: Because that is the return value of puts : puts(obj, ...) → nil Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each

How do the puts and gets functions work?

末鹿安然 提交于 2019-12-28 03:11:13
问题 main() { char name[20]; printf("enter your name\n"); scanf("%s",name); printf("%s",name); gets(name); puts(name); } input: Sampad Saha Output Sampad Saha Here puts only uses the input taken from gets() . as, if I omit this printf() the output would be Saha So here puts does not print anything given through scanf() . main() { char color[20]; printf("enter your name\n"); scanf("%s",color); puts(color); } But here puts() uses the input taken from scanf() also. 回答1: The problem here is, for an

How to print a string with embedded nulls so that “(null)” is substituted for '\0'

99封情书 提交于 2019-12-20 02:58:14
问题 I have a string I composed using memcpy() that (when expanded) looks like this: char* str = "AAAA\x00\x00\x00...\x11\x11\x11\x11\x00\x00..."; I would like to print every character in the string, and if the character is null, print out "(null)" as a substitute for '\0'. If I use a function like puts() or printf() it will just end at the first null and print out AAAA So how can I get it to print out the actual word "(null)" without it interpreting it as the end of the string? 回答1: You have to

Difference between puts() and printf() in C while using sleep()

主宰稳场 提交于 2019-12-08 18:50:48
问题 I was wondering the difference between puts() and printf() functions while using sleep() function. Here is my code(In C language): printf("hello, world"); sleep(1); printf("Good, bye!"); After compiling and running the program, it seems that it will sleep first and then print "hello, worldGood, bye!" However, if using puts() instead of printf(), it will print "hello, world" then sleep, and finally print "Good, bye". puts("hello, world"); sleep(1); puts("Good, bye!); 回答1: This is because of

How to print a string with embedded nulls so that “(null)” is substituted for '\\0'

ぃ、小莉子 提交于 2019-12-02 00:40:25
I have a string I composed using memcpy() that (when expanded) looks like this: char* str = "AAAA\x00\x00\x00...\x11\x11\x11\x11\x00\x00..."; I would like to print every character in the string, and if the character is null, print out "(null)" as a substitute for '\0'. If I use a function like puts() or printf() it will just end at the first null and print out AAAA So how can I get it to print out the actual word "(null)" without it interpreting it as the end of the string? You have to do that mapping yourself. If you want to, that is. In C, strings are null-terminated. So, if you use a

Why is printf with a single argument (without conversion specifiers) deprecated?

主宰稳场 提交于 2019-11-27 17:39:13
In a book that I'm reading, it's written that printf with a single argument (without conversion specifiers) is deprecated. It recommends to substitute printf("Hello World!"); with puts("Hello World!"); or printf("%s", "Hello World!"); Can someone tell me why printf("Hello World!"); is wrong? It is written in the book that it contains vulnerabilities. What are these vulnerabilities? Jabberwocky printf("Hello World!"); is IMHO not vulnerable but consider this: const char *str; ... printf(str); If str happens to point to a string containing %s format specifiers, your program will exhibit

How do the puts and gets functions work?

末鹿安然 提交于 2019-11-27 09:38:32
main() { char name[20]; printf("enter your name\n"); scanf("%s",name); printf("%s",name); gets(name); puts(name); } input: Sampad Saha Output Sampad Saha Here puts only uses the input taken from gets() . as, if I omit this printf() the output would be Saha So here puts does not print anything given through scanf() . main() { char color[20]; printf("enter your name\n"); scanf("%s",color); puts(color); } But here puts() uses the input taken from scanf() also. Sourav Ghosh The problem here is, for an input like abc XYZ the code scanf("%s",name); reads only the "abc" part and the "XYZ" is left in

Why is printf with a single argument (without conversion specifiers) deprecated?

牧云@^-^@ 提交于 2019-11-26 19:06:48
问题 In a book that I'm reading, it's written that printf with a single argument (without conversion specifiers) is deprecated. It recommends to substitute printf("Hello World!"); with puts("Hello World!"); or printf("%s", "Hello World!"); Can someone tell me why printf("Hello World!"); is wrong? It is written in the book that it contains vulnerabilities. What are these vulnerabilities? 回答1: printf("Hello World!"); is IMHO not vulnerable but consider this: const char *str; ... printf(str); If str