一个简单c程序的演练

一个简单c程序的演练,c,walkthrough,C,Walkthrough,我有一个问题要解决,并在程序运行时显示输出。有一件事我不明白,f是如何被发现为4的,甚至是如何被发现的。我知道正确的答案是 7猎鹰3 9 RK 4 _ 我只是不知道他们是怎么发现f值是4的,一旦我有了它,我就可以把剩下的做得很好了 #include <stdio.h> #include <string.h> void falcon(int f); char a[20]; int main() { int i, j; a[3] = 'G';

我有一个问题要解决,并在程序运行时显示输出。有一件事我不明白,f是如何被发现为4的,甚至是如何被发现的。我知道正确的答案是

7猎鹰3

9 RK 4

_
我只是不知道他们是怎么发现f值是4的,一旦我有了它,我就可以把剩下的做得很好了

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

void falcon(int f); 
char a[20]; 

int main() { 
    int i, j; 
    a[3] = 'G'; 
    a[1] = 'K'; 
    i = 3 + 2 * 3; 
    j = 4; 
    a[2] = 'Y'; 
    falcon(j); 
    printf("%d %s %d\n", i, a, j); 
} 

void falcon(int f) { 
    int j; 
    j = 11 % f; 
    printf("%d falcon %d\n", f+3, j); 
    a[2] = '\0'; 
    a[0] = 'R'; 
}
#包括
#包括
虚空猎鹰(INTF);
chara[20];
int main(){
int i,j;
a[3]=‘G’;
a[1]=“K”;
i=3+2*3;
j=4;
a[2]=“Y”;
猎鹰(j);
printf(“%d%s%d\n”,i,a,j);
} 
虚空猎鹰(INTF){
int j;
j=11%f;
printf(“%d猎鹰%d\n”,f+3,j);
a[2]='\0';
a[0]=“R”;
}

让我们一起浏览一下这个程序(去掉一些不相关的部分)

#包括
#包括
虚空猎鹰(INTF);
chara[20];
int main(){
int i,j;
j=4;
猎鹰(j);//换言之,猎鹰(4)。现在,让我们下到
//falcon函数,其中第一个参数为4。
printf(“%d%s%d\n”,i,a,j);
} 
void falcon(int f){//除了这里我们看到在这个函数中,
//第一个参数由“f”引用,
//正如我们看到的,是4。
int j;
j=11%f;//这里,j等于11除以
//f是4。
printf(“%d猎鹰%d\n”,f+3,j);
}

现在您明白了为什么代码中变量名不应该有
i
s和
j
s,除了可能的循环之外

不管怎样

chara[20]表示
a
是一个全局声明的字符数组

int main()
{ 
   int i, j; // declares two local stack variables, i and j
   a[3] = 'G'; // sets 4th location in 'a'(remember, arrays start at 0) to 'G'{useless}
   a[1] = 'K'; // sets 2nd location in 'a' array to 'K'
   i = 3 + 2 * 3; // i is now 9 {remember, multiplication before addition}
   j = 4;    // j is now 4
   a[2] = 'Y';  // a[2] is now 'Y'
   falcon(j);  // call to falcon, with argument 4, explained next
   printf("%d %s %d\n", i, a, j);  // prints "9 RK 4"
   //return 0; -- this should be added as part of 'good' practices
} 



 void falcon(int f) 
 { 
     // from main(), the value of 'f' is 4
     int j;  // declares a local variable called 'j'
     j = 11 % f;  // j = 11 % 4 = 3 
     printf("%d falcon %d\n", f+3, j); // prints 7 falcon 3
     a[2] = '\0'; // a[2] contains null terminating character, overwrites 'Y'. 
     a[0] = 'R'; // sets a[0] to 'R'. At this moment, printf("%s",a); must yield "RK"
 }

在调用
falcon
时,它们作为
f
参数传递的是什么?嗯。。这将是
j=4然后<代码>猎鹰(j)
函数中的
j
main()
中的
j
无关(除了后者的值,后者在计算前者时用作模运算符的除数)。也许这就是你迷路的地方?我也不明白他是C编程新手,很明显。这就是困惑。伙计们,我们应该否决这个吗?这个问题并没有冒犯作者,显示出一定程度的误解,排除了谷歌搜索的可能性,因为它太基本,太具体到他的问题。他需要一个这样的网站来回答这个问题。问题是,代码写得很差,而且包含很多噪音。OP很难找出所有噪音中的“方式”。
int main()
{ 
   int i, j; // declares two local stack variables, i and j
   a[3] = 'G'; // sets 4th location in 'a'(remember, arrays start at 0) to 'G'{useless}
   a[1] = 'K'; // sets 2nd location in 'a' array to 'K'
   i = 3 + 2 * 3; // i is now 9 {remember, multiplication before addition}
   j = 4;    // j is now 4
   a[2] = 'Y';  // a[2] is now 'Y'
   falcon(j);  // call to falcon, with argument 4, explained next
   printf("%d %s %d\n", i, a, j);  // prints "9 RK 4"
   //return 0; -- this should be added as part of 'good' practices
} 



 void falcon(int f) 
 { 
     // from main(), the value of 'f' is 4
     int j;  // declares a local variable called 'j'
     j = 11 % f;  // j = 11 % 4 = 3 
     printf("%d falcon %d\n", f+3, j); // prints 7 falcon 3
     a[2] = '\0'; // a[2] contains null terminating character, overwrites 'Y'. 
     a[0] = 'R'; // sets a[0] to 'R'. At this moment, printf("%s",a); must yield "RK"
 }