简体   繁体   English

在 C 的 for 循环中使用 strcmp 和 printf

[英]Using strcmp and printf in for loop in C

I have a question which I like to demonstrate with a piece of code.我有一个问题,我想用一段代码来演示。 Having the following code:具有以下代码:

#include <stdio.h>
#include <string.h>

int main()
{
    char *fruit[] = {
        "apricot"
    ,   "banana"
    ,   "pineapple"
    ,   "apple"
    ,   "persimmon"
    ,   "pear"
    ,   "blueberry"
    };
    char *temp;
    int a,b,x,y;

    for (a=0;a<6;a++)
            for(b=a+1;b<7;b++)
                y=strcmp(*(fruit+a),*(fruit+b));
                printf("strcompare value is %x\n",y);
                if(strcmp(*(fruit+a),*(fruit+b)) > 0)
                {
                    temp = *(fruit+a);
                    *(fruit+a) = *(fruit+b);
                    *(fruit+b) = temp;
                }

    for (x=0;x<7;x++)
        puts(fruit[x]);

    return (0);
}

上面代码的输出

As you can see, the printf output is only executes ones.如您所见,printf output 仅执行。 And the list of fruit is not sorted.并且水果列表没有排序。 When I make a slight change to the code, exclude the use of variable y, as in the below code block:当我对代码稍作改动时,排除变量 y 的使用,如下面的代码块所示:

#include <stdio.h>
#include <string.h>

int main()
{
    char *fruit[] = {
        "apricot"
    ,   "banana"
    ,   "pineapple"
    ,   "apple"
    ,   "persimmon"
    ,   "pear"
    ,   "blueberry"
    };
    char *temp;
    int a,b,x,y;

    for (a=0;a<6;a++)
            for(b=a+1;b<7;b++)
                //y=strcmp(*(fruit+a),*(fruit+b));
                printf("strcompare value is %x\n",strcmp(*(fruit+a),*(fruit+b)));
                if(strcmp(*(fruit+a),*(fruit+b)) > 0)
                {
                    temp = *(fruit+a);
                    *(fruit+a) = *(fruit+b);
                    *(fruit+b) = temp;
                }

    for (x=0;x<7;x++)
        puts(fruit[x]);

    return (0);
}

Now we see this output:现在我们看到这个 output:

第二次尝试

The printf is repeated but the sorting is not correct yet. printf 重复但排序还不正确。 Removing the printf command as in the code below:删除 printf 命令,如下面的代码所示:

#include <stdio.h>
#include <string.h>

int main()
{
    char *fruit[] = {
        "apricot"
    ,   "banana"
    ,   "pineapple"
    ,   "apple"
    ,   "persimmon"
    ,   "pear"
    ,   "blueberry"
    };
    char *temp;
    int a,b,x,y;

    for (a=0;a<6;a++)
            for(b=a+1;b<7;b++)
                //y=strcmp(*(fruit+a),*(fruit+b));
                //printf("strcompare value is %x\n",strcmp(*(fruit+a),*(fruit+b)));
                if(strcmp(*(fruit+a),*(fruit+b)) > 0)
                {
                    temp = *(fruit+a);
                    *(fruit+a) = *(fruit+b);
                    *(fruit+b) = temp;
                }

    for (x=0;x<7;x++)
        puts(fruit[x]);

    return (0);
}

This code is giving the requested output:此代码给出了请求的 output: 第三次也是最后一次尝试

Now i have the following questions:现在我有以下问题:

  1. why is the printf command in the first attemp not repeated and the sorting not happening?为什么第一次尝试中的 printf 命令没有重复并且排序没有发生?
  2. what is wrong using a interger var to accept the strcmp outcome and use that var afterwards?使用整数 var 接受 strcmp 结果并在之后使用该 var 有什么问题?
  3. why does leaving out the printf function has a positive effect on the outcome?为什么省略 printf function 会对结果产生积极影响?

If one loop contains more than 1 statement, you need to surround the statements by curly braces to create a compound statement:如果一个循环包含超过 1 个语句,则需要用花括号将语句括起来以创建复合语句:

for (a=0;a<6;a++) 
{
    for(b=a+1;b<7;b++)
    {
        //y=strcmp(*(fruit+a),*(fruit+b));
        printf("strcompare value is %x\n",strcmp(*(fruit+a),*(fruit+b)));

        if(strcmp(*(fruit+a),*(fruit+b)) > 0)
        {
            temp = *(fruit+a);
            *(fruit+a) = *(fruit+b);
            *(fruit+b) = temp;
        }
    }
}

Else if you do:否则,如果你这样做:

for (a=0;a<6;a++) 
    for(b=a+1;b<7;b++)
        //y=strcmp(*(fruit+a),*(fruit+b));
        printf("strcompare value is %x\n",strcmp(*(fruit+a),*(fruit+b)));

        if(strcmp(*(fruit+a),*(fruit+b)) > 0)
        {
            temp = *(fruit+a);
            *(fruit+a) = *(fruit+b);
            *(fruit+b) = temp;
        }

the if statement of: if语句:

 if(strcmp(*(fruit+a),*(fruit+b)) > 0)
 {
     temp = *(fruit+a);
     *(fruit+a) = *(fruit+b);
     *(fruit+b) = temp;
 }

is executed after the nested loops, but not inside the inner one, which is equivalent to:在嵌套循环之后执行,但不在内部循环内部执行,相当于:

for (a=0;a<6;a++) 
{
    for(b=a+1;b<7;b++)
    {
        //y=strcmp(*(fruit+a),*(fruit+b));
        printf("strcompare value is %x\n",strcmp(*(fruit+a),*(fruit+b)));
    }
}

if(strcmp(*(fruit+a),*(fruit+b)) > 0)
{
     temp = *(fruit+a);
     *(fruit+a) = *(fruit+b);
     *(fruit+b) = temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM