- 2

explain this program step by step

#include <stdio.h> #include <string.h> void print_reverse(char *s) { size_t len = strlen(s); //storing size of things char *t = s + len - 1; //coudnt understant this line the most,bcz s is string, len is variable printf("%c\n", *t); while (t >= s) { printf("%c", *t); t = t - 1; } puts(""); } int main() { char search_for[80]; printf("what to print in reverse "); //scanf("%s", search_for); fgets(search_for, 80, stdin); print_reverse(search_for); return 0; }

11th Oct 2019, 7:43 AM
Reiner
Reiner - avatar
8 ответов
+ 7
Here, I've analyzed your code 🧐😁: https://code.sololearn.com/cZw6jldSD1Gl/?ref=app
11th Oct 2019, 8:55 AM
voja
voja - avatar
+ 5
Thanks a lot Aaron Eberhardt! 🙂
11th Oct 2019, 9:03 AM
voja
voja - avatar
+ 5
reiner A Actually, I wasn't quite right. Something was off in my attempt to explain your code. Now I figured it out, it was fgets function that didn't fit in. This function stores new line, I didn't know that until now (I was using scanf much more frequently..) I fixed it. Also I wrote a code about this: https://code.sololearn.com/ct7COWkOnLLS/?ref=app My apologies for not realizing it sooner. Here's the result I found on stackoverflow: https://stackoverflow.com/questions/16314468/strlen-returns-the-string-count-with-the-null-terminator
12th Oct 2019, 3:56 AM
voja
voja - avatar
+ 5
1. Yes, scanf doesn't store \n 2. It's like unsigned int type. You can use int in your example. 3. Yes, you can do that. Just don't be surprised by the output because the next line is: printf("%c\n", *t) and *t = last character but not \n. 4. the difference is just \n new line. 5. I would like to write a code about differences between these functions, but I really don't want to rush this time, when writing explanations in C you have to know every detail (just like in your example). So, can't really guarantee that to you. The important thing is that you know how to take data from user and how to print it, these functions have little differences but their purpose is the same.
12th Oct 2019, 4:27 PM
voja
voja - avatar
+ 4
This program is certainly a bit confusing. The line char *t = s + len - 1; creates a pointer to the last character of the string. s is the pointer to the first char so the memory location at s + len - 1 is the last char. You could also say s[0] is the first char and s[len - 1] is the last char. It is exactly the same just written differently. In the loop it prints the char at t and moves t down a single memory location to the next chars until t is the same memory location as s.
11th Oct 2019, 8:54 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 3
voja wow, awesome work! Respect dude!!
11th Oct 2019, 8:59 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
thanks voja for explaining much deeper 🤗 why i added two more printif which is not needed is, to know what the pointer holding?? i checked each line by adding printf.
12th Oct 2019, 12:54 AM
Reiner
Reiner - avatar
+ 1
@voja 1.so for scanf it will not store \n right 2. do we really need size_t instead of int. in what case we should use size_t 3.then we can modify this like " t = s + len - 2;" bcz we dont want \n to be printed first right 4.i tested with scanf too in the program, just now i got to know the difference of scanf and fgets 5. can you give one example program using printf,scanf,fgets,gets,puts,fputs...im confused with these
12th Oct 2019, 10:06 AM
Reiner
Reiner - avatar