返回两个值的C函数
我必须用C语言完成以下作业 编写一个函数,要求用户输入两个正整数,读取这两个数字,比如a和b,并不断询问,直到用户输入两个这样的数字。函数将两个数字返回到调用它的位置 我有点困惑。如何要求用户从函数中输入两个值?不能只从main()函数执行此操作吗?到目前为止,我有以下功能代码。它工作得很好,但我当然需要在外部函数中使用它返回两个值的C函数,c,function,C,Function,我必须用C语言完成以下作业 编写一个函数,要求用户输入两个正整数,读取这两个数字,比如a和b,并不断询问,直到用户输入两个这样的数字。函数将两个数字返回到调用它的位置 我有点困惑。如何要求用户从函数中输入两个值?不能只从main()函数执行此操作吗?到目前为止,我有以下功能代码。它工作得很好,但我当然需要在外部函数中使用它 #include <stdio.h> int main() { int a(2); // initialize just as some positive n
#include <stdio.h>
int main() {
int a(2); // initialize just as some positive number so as not to set off negative alert.
int b(2);
printf("Enter two positive numbers: \nFirst: ");
do {
if (a <= 0 || b <= 0) { // negative alert
printf("Woops. Those are negative. Try again. \nFirst: ");
}
scanf(" %d", &a);
printf("Second: ");
scanf(" %d", &b);
printf("\n");
} while (a <= 0 || b <= 0);
return(0);
}
#包括
int main(){
int a(2);//初始化为正数,以免引发负面警报。
INTB(2);
printf(“输入两个正数:\n第一:”);
做{
如果(amain
的唯一特殊之处是它是应用程序的入口点。您可以在任何时候调用任何您想调用的内容1。一旦指令指针命中入口点中的第一条指令,从那时起,它就只是一个操作码流。您拥有“函数”这一事实没有什么特别之处除了跳跃,你也可以把它们都放进去
将代码强加于另一个方法只会在传递和返回信息方面产生差异:
/* this signature will change if you need to pass/return information */
void work()
{
int a = 2; /* did you really mean C++? */
int b = 2;
printf("Enter two positive numbers: \nFirst: ");
do {
if (a <= 0 || b <= 0) { /* negative alert */
printf("Woops. Those are negative. Try again. \nFirst: ");
}
scanf(" %d", &a);
printf("Second: ");
scanf(" %d", &b);
printf("\n");
} while (a <= 0 || b <= 0);
}
1.对于“whatever”和“where”的合理定义,c
和c++
中的函数(oop中的方法)和c++
(实际上在我所知道的所有其他编程语言中)只能返回一个值。请使用包含这两个值的结构,并从函数返回它
#include<stdio.h>
#include<stdlib.h>
typedef struct two_ints {
int a, b;
} two_ints_t;
two_ints_t read_two_ints();
two_ints_t read_two_ints() {
two_ints_t two_ints;
two_ints.a = 0;
two_ints.b = 0;
char tmp[32] = "";
printf("Enter two positive numbers: \nFirst: ");
do {
scanf(" %s", tmp);
two_ints.a = atoi(tmp);
printf("Second: ");
scanf(" %s", tmp);
two_ints.b = atoi(tmp);
printf("\n");
if (two_ints.a <= 0 || two_ints.b <= 0) { // negative alert
printf("Woops. Those are negative. Try again. \nFirst: ");
}
} while (two_ints.a <= 0 || two_ints.b <= 0);
return two_ints;
}
int main() {
two_ints_t two_ints = read_two_ints();
printf("a=%i, b=%i\n", two_ints.a, two_ints.b);
return 0;
}
#包括
#包括
typedef结构两个整数{
INTA,b;
}两个输入;
两个整数不能读取两个整数();
两个整数不能读两个整数(){
两点两点;
两个整数a=0;
两个整数b=0;
字符tmp[32]=“”;
printf(“输入两个正数:\n第一:”);
做{
scanf(“%s”,tmp);
两个整数a=atoi(tmp);
printf(“第二:”);
scanf(“%s”,tmp);
两个整数b=atoi(tmp);
printf(“\n”);
if(two_int.a没有人提到的一个技巧是,从函数返回多个值的另一种方法是将指针作为参数传递。执行此操作的常见函数是scanf:
int x,y;
scanf("%d %d", &x, &y);
你可以把这个代码看成是ScNF ReTurnff两个值,并把它们分配给x和y.< /p>这里混合C+C++。看起来,函数是一个函数。没有任何理由你不能在任何函数中这样做。为什么你不能把代码放在不同的函数中?我不确定我理解这个问题…<代码> int A(2)是不是C++?是的,我意识到了。我已经教过C和C++了,所以有时候我知道混合。任务要求我“把两个数字都返回到它被调用的地方。”void函数不返回任何内容吗?我不一定要为您完成赋值,但您是正确的。目前我不返回任何内容。如果您愿意,您当然可以更改work
的签名来执行此操作。您似乎走在正确的轨道上,因为您意识到void
没有做什么你想要。哈哈,我没想到你会帮我做作业。:)谢谢你的帮助!嗯。有趣的是你的函数声明可能是int-myfunc(int*a,int*b){…}
。你的调用可能看起来像if(!myfunc(&I,&j)){…fail…}或者{…success…}
int x,y;
scanf("%d %d", &x, &y);