Xb74Dkjb
Xb74Dkjb

Reputation: 990

It always prompts "code will never be executed"

Here is my C++ code:

#include <stdio.h>

int main ()
{
    int n, ip1, ip2, ip3, ip4, port;
    unsigned ip;
    char str [80];
    printf ("Enter IPv4 address: ");
    scanf ("%79s",str);

    n = sscanf(str, "%3d.%3d.%3d.%3d:%5d", &ip1, &ip2, &ip3, &ip4, &port);
    printf ("Content length is %d\r\n", n);
    if (n != 4 || n != 5) {
        ip = 0;
        port = 161;
    }
    else if (n==4) {
        port = 161;
    }
    ip =(ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;
    printf ("ip addr is %d , port is %d \r\n", ip, port);
}

But xcode alway prompts me:

else if (n==4) 

will never be executed, and indeed happened. How the conditions should be?

Upvotes: 0

Views: 3646

Answers (2)

Didac Perez Parera
Didac Perez Parera

Reputation: 3834

Please, note that (n != 4 || n != 5) = true

Upvotes: 3

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32717

You probably want if (n != 4 && n != 5), because if you use || then n will either not be 4 or not be 5.

Upvotes: 2

Related Questions