c - 功能:变量替换

标签 c function

我在使用下面的代码时遇到了一些问题;我知道这对你们更有经验的编码员来说是基本的东西,但这是我第一次尝试用 C 编写代码,所以请多多包涵。

#include <stdio.h>

int main()
{
    printf("This program, or function, substitutes one given number for another given number, and then places the former with the latter number. ");

    float y1 = 10;  /* Assign value to variable 1*/
    float y2 = 20;  /* Assign value to variable 2*/
    float y3 = 30;  /* Assign value to variable 3*/
    float y4 = 40;  /* Assign value to variable 4*/

    void sub( float *, float *, float *, float * );                 /*declare function prototype*/

    printf("\nThe numbers' given values before the aforementioned function:  Number 1 = %f , Number 2 = %f\n , Number 3 = &f , Number 4 = %f", y1, y2, y3, y4 );
    sub( &y1, &y2, &y3, &y4 );   /*This is where the function, to move the numbers, is called */
    printf("\nThe numbers' given values after the aforementioned function:   Number 1 = %f , Number 2 = %f\n , Number 3 = &f , Number 4 = %f", y1, y2, y3, y4 );
}

void sub(float *z1, float *z2, float *n3, float *n4)
{
    int z;
    int n;
    z = *z1;
    *z1 = *z2;
    *z2 = z;
    n = *n3;
    *n3 = *n4;
    *n4 = n;
}

输出是:

This program, or function, substitutes one given number for another given number, and then places the former with the latter number. 

The numbers' given values before the aforementioned function:  Number 1 = 10.000000 , Number 2 = 20.000000  , Number 3 = &f , Number 4 =
30.000000

The numbers' given values after the aforementioned function:   Number 1 = 20.000000 , Number 2 = 10.000000  , Number 3 = &f , Number 4 =
40.000000

我的问题是:如何更改代码以便“数字 3”像其余变量一样替换自身。

最佳答案

您只是打错了字:将 nf 更改为 %f for

数字 3 = & f

关于c - 功能:变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16330457/

相关文章:

c - 确定使用 fork 运行的进程数量

c - 如何强制两个进程在同一个CPU上运行?

c++ - 就常量性和引用性而言,函数应该如何接受 *lambda* 参数?

javascript - 为什么我的 javascript 函数不抛出错误?

Python乘法表,与字符串分开

javascript - 如何继续 'if' 语句来比较多个函数返回值作为参数?

c - C语言中的if-else条件

c - 在 C 中将 char append 到 char*?

C: 警告:数组初始值设定项中的元素过多; ‘xxx' 接近初始化;期望 ‘char *’ ,但类型为 ‘int’

r - 在函数内调用 glmulti 时未找到错误对象