I have a loop that has to go from j to 0 (inclusively). My
j
size_t
#include<stdio.h>
#include<conio.h>
#define SIZE 100
int main(){
char str[SIZE];
size_t i=0;
size_t j;
puts("Enter any string");
scanf("%s",str);
while(str[i]!='\0'){
i++;
}
for(j=i-1;j>=0;j--){
printf("%c",str[j]);
}
getch();
return 0;
}
size_t
is an unsigned integer and it's never going to be less than 0
. So
the condition in for
loop is always true:
for(j=i;j>=0;j--)
You can modify the condition to (a bit ugly though):
for(j=i; j-- > 0;){
...
}
Note that in your condition, you are printing the \0
null byte too, which is a non-printable character. (since the j
starts with a value equal to the string length). The above condition takes care of that too.
Also:
strlen()
instead of looping over it yourself.scanf()
if input reading wad successful.