0

Here is my code to count the number of characters of a given string (i.e use of strlen() function):

#include <stdio.h>
#include <string.h>

#define N 30

int main() {
    char input[N];
    gets(input);
    int j = strlen(input);
    printf("using library fnc strlen=%d", j);
    int i = 0, sum = 0;
    for (i = 0; input[i] != '\0'; i++) {
        sum = sum + i;
    }
    printf("\rusing loop %d", sum);
}

On Code::Blocks (gcc compiler), the result shows like this:

abcdef78
using loop 4y fnc strlen=8
Process returned 0 (0x0)   execution time : 5.816 s
Press any key to continue.

But the online compiler of Programiz shows like this:

/tmp/rzNX0Zm3SF.o
abcdef78
using library fnc strlen=8
using loop 8

But after I have changed the code \r by \n, like this:

#include <stdio.h>
#include <string.h>

#define N 30

int main() {
    char input[N];
    gets(input);
    int j = strlen(input);
    printf("using library fnc strlen=%d", j);
    int i = 0, sum = 0;
    for (i = 0; input[i] != '\0'; i++) {
        sum = sum + 1;
    }
    printf("\nusing loop %d", sum);
}

the code works fine everywhere (Code::Blocks too) what up with this \r and \n?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • `\r` is "carriage return" (to the left), `\n` is "newline" (next line). Don't use `printf("\rusing loop %d", sum);` or `printf("\nusing loop %d", sum);`. Use `printf("using loop %d\n", sum);`. It's like doing the dishes: you should wash them after you have eaten, not before. Note that `gets()` is obsolete and is no longer part of the standard C library. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Weather Vane Sep 07 '22 at 07:25
  • 4
    Whoever told you to use `gets` has 25+ years outdated knowledge and should not be trusted as a source of learning C. – Lundin Sep 07 '22 at 07:32
  • 1
    Does this answer your question? [Difference between \n and \r?](https://stackoverflow.com/questions/1761051/difference-between-n-and-r) – abdo Salm Sep 07 '22 at 09:05
  • 1
    basically... `'\r'`: continue writing at the beginning of **current** line; `'\n'`: continue writing at the beginning of **next** line. – pmg Sep 07 '22 at 10:26
  • 1
    @pmg Originally, `\n` meant "continue writing on the **next** line **at the same column you're already at**". Hence the pairing if the `\r` **carriage return** with `\n` **line feed**: "return to the beginning of the line, then advance (the paper being written on) to the next line". Now get off of my lawn. :-D – Andrew Henle Sep 07 '22 at 12:47

2 Answers2

5

Your first code has

for(i=0;input[i]!='\0';i++) {
   sum = sum + i;
}

and your second code has

for(i=0;input[i]!='\0';i++) {
   sum = sum + 1;
}

i.e. for a five-character string, the first computes 0 + 1 + 2 + 3 + 4, while the second computes 1 + 1 + 1 + 1 + 1.

Beyond that, \r just returns the cursor to the start of the line, while \n advances to the next line.

IOW, printing foo\rbar would only show bar because you're printing foo, rewinding to the start of the line and overwriting it with bar.

AKX
  • 152,115
  • 15
  • 115
  • 172
3
  • \r (carriage return) returns the cursor to the first symbol on the line. This is actually something computers inherited from typewriters, the carriage being the mechanical part that moved as you typed.

  • \n (line feed) moves to the next line which will also implicitly involve a carriage return. Also, on a lot of common systems, \n "flushes" the stdout buffer, meaning that the symbols that your program has prepared to print will actually get printed.

For these reasons you should make it a habit of ending your printf lines with \n. It moves on to the next line but also likely ensures that the text gets printed in the order you expect.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • @EricPostpischil Corrected choice of words, thanks. I wasn't sure if this was a CR+LF vs LF thing or actually specified by C. – Lundin Sep 07 '22 at 10:24