P
P
prosg2014-11-29 14:06:30
Programming
prosg, 2014-11-29 14:06:30

How does double equal sign affect strings in C++?

Hello! I want to know why the printf command does not print a line if there is no second '=' sign in the if statement (if(str[i]='\0')), isn't this a normal equals sign? give a link where you can read about the characters
char str[10]="Hi!";
intlen=0;
for(int i=0;i<10;i=i+1)
{
if(str[i]=='\0')
break;
len=len+1;
}
printf("Length of string %s is %d\n",str,len);
system("pause");
getchar();
return 0;

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladislav Kilin, 2014-11-29
@Quilin

The simple equal sign in C-like languages ​​is the assignment operator. Typically, it returns the result of an assignment. The double equal sign is a comparison operator and it returns bool.
In fact, the conditional statement requires the parenthesized expression after the if keyword to return exactly bool, so a simple assignment won't work in this case. That's why we need a double equals sign.
In your particular code, it is obvious that you are familiar with the assignment operator (len = len + 1), but in this case it is sufficient to use the increment operator (len++).

A
Armenian Radio, 2014-11-29
@gbg

Everything is bad here:
with

  • Attachment to the magic constant 10 and an extra counter. You can measure the length of a string like this:
    size_t len=0;
    for(;str[len];len++);

    after which len will contain the desired length of the string.
  • If protection against strings of excessive length is needed:
    #define MAX_LEN (9000)
    ...
    size_t len=0;
    for(;(str[len])&&(len<MAX_LEN);len++);

  • V
    vreitech, 2014-11-29
    @fzfx

    if(str[i]='\0')
    assigning the number 0 to a variable, and then checking and executing if this variable has a value other than 0. therefore, it is never executed, respectively, break does not occur (all 10 iterations pass), and the entire line is clogged to the heap with the line ending character, becoming empty , as a result, printf does not output anything.

    R
    Rsa97, 2014-11-29
    @Rsa97

    I understand that you are just starting to learn C ++, but some things are better to fix right away than to relearn later.
    1. You set the array explicitly, and if you change the string to a longer one and forget to change the length of the array, then at best the compiler will give an error, at worst the string will overwrite other data or be cut off by the length of the array, losing the trailing '\0'.
    2. In the loop, you immediately set a limit on the length of the string (i<10), if you forget to change it, you will get a length of 10 characters on a longer string.
    3. It is not necessary to compare with zero (although there is no error in this). C++ has implicit type casting, for logical operations 0 and NULL are treated as FALSE, everything else as TRUE, so if (x == 0) is equivalent to if (x). The choice of option depends on the programmer himself or the style adopted in the development team.
    4. A little trick - a comparison like (0 == x) will avoid an error due to inattention, when instead of (x == 0) it turns out to be (x = 0).
    5. Master post- and pre-increment/decrement. The x++ and x-- operations increment and decrement a number by 1 (except for pointers), the result is the value of x before the change. The ++x and --x operations also increase and decrease the number by 1, but their result is the value of x afterchanges. For pointers, this operation shifts the value to the next/previous element (that is, for int *p, p++ is equivalent to p = p+sizeof(int)).

    char *str = "Привет!";
    int len = -1;
    while (str[++len]);
    printf("Длина строки '%s' равна %d\n", str, len);

    Didn't find what you were looking for?

    Ask your question

    Ask a Question

    731 491 924 answers to any question