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
?