1

Following is the program I encountered

#include <stdio.h>       

void sp_to_dash(const char *str);
int main(void)
{
    sp_to_dash("this is a test");
    return 0;
}
void sp_to_dash(const char *str)
{
    while(*str) //beginning of while loop
    {               
        if(*str== ' ')printf("%c", '-');
        else printf("%c", *str);
        str++;
    }
}

At the beginning of while loop we can see that while(*str) is initiated without any condition, which means it is not mentioned that when the *str should stop on null or '\0'. The code is working fine, however according to me it should be like this while(*str != null) or while(*str != '\0'). Please explain this one to me.

2
  • Saying that the code should be while(*str != null){...} clearly indicates that don't have clear the difference between empty strings, null pointers and the NUL ASCII character '\0' (and BTW the null pointer is NULL, not null). It's a disease somewhat common among C newbies, but you should fix that as soon as possible. Commented Jul 31, 2016 at 5:45
  • i for one am glad you posted this because the below answer helped me too. most c++ tutorials only show examples of explicit test statements and i was encountering while (variable) {} syntaxes and wondering how that works. Commented Sep 4, 2021 at 2:46

3 Answers 3

2

It is same as writing this -

while (*str != '\0')

'\0' is of type int and is equivalent to 0 and will not effect execution of loop.

Note- Don't compare string with == and there is no operator !==.

This will cause error if you use this (comparing char with string literal) -

while (*str != "\0")  
Sign up to request clarification or add additional context in comments.

3 Comments

Ther is no string in the code that is being compared with == operator
@Dr.Haimovitz OP modified his code , this is result of that . Now he has removed !== too .
@Dr.Haimovitz No need to apologise, it is OP's mistake :)
2

The while loop needs a value as a condition in order to work. When you insert a condition like while (*str == '3'), your condition checks if *str actually is '3' and if so it is interpreted as 1 (you can think of this as True if you like) and if not, it interprets it as 0 (or False).

This is why you can write while (0) or while (1).

Now, your condition is the value of *str so, during the condition check, you will check the value of *str to see if it is 0 or not (every other value (-1, 7, 32333.34 ...) considered as True) str is a const char * so we need to translate char value into int values (using ASCII). The char that has the value 0 is '\0'.

1 Comment

thank you for explaining the possibilities of the condition check, very helpful to my noob self
0

It is exectly the same condition as (*str!='\0') because the ascii value of \0 is 0 so the loop will stop when *str will be \0, means at the end of the string,

*any other value except 0 is a true condition for itself (like *str) and will cause the while loop to continue e,g: while(5){} will be an endless loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.