[英]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:现在我有以下问题:
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.