Printing characters in a while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Printing characters in a while loop

I've been trying to get a grasp of pointers but I'm not understanding one part when i try and print out characters seperately. It works, but i have no idea why adding brackets fixes my issue. I am trying to print out each string in a seperate line one character at a time. Line 16 of my code shows a while loop printing out each character of the pointer array. Why do i need the curly brackets? Without them, the '\n' character gets teated as if its in the while loop without any indentation or enclosing brackets. I threw an empty pair on there for laughs and it worked. Can anybody shed some light on why it acts like that? https://code.sololearn.com/clfS8YSqkXjs/?ref=app

19th Sep 2020, 10:19 PM
Slick
Slick - avatar
6 Answers
+ 2
No. See don't have identation counting.. You can put any empty spaces in between 2 statements.. Other than python, most other languages treats semicolon as end of statement, and curved braces as blocks of Code..
19th Sep 2020, 10:28 PM
Jayakrishna 🇮🇳
+ 2
Without braces, next statement is treated as belong loop.. You know for only single statement we don't need braces..
19th Sep 2020, 10:24 PM
Jayakrishna 🇮🇳
+ 2
Considering the use of `putchar` return value as the while-loop condition, you can simply put a semicolon at the end of while-loop definition to tell the compiler that the loop doesn't have a loop body. while(putchar(*(*(fruit+x)+y++))); * Additional info for those who wonders why putchar is used in while-loop condition: The `putchar` function returns the given character argument (promoted as int) on success, except for EOF, in which case `ferror` is set. When the string terminator was given as argument (dereferenced from the char pointer), zero is returned - effectively stops the loop. http://www.cplusplus.com/reference/cstdio/putchar/
20th Sep 2020, 1:08 AM
Ipang
0
Right, Jayakrishna🇮🇳 but even so does the indentation not matter?
19th Sep 2020, 10:25 PM
Slick
Slick - avatar
0
Nice, i got it. Thanks
19th Sep 2020, 10:29 PM
Slick
Slick - avatar
0
You're welcome..Slick
19th Sep 2020, 10:29 PM
Jayakrishna 🇮🇳