c - C 中未调用的函数

标签 c function

我的函数 prefixSumsuffixSum 没有在 main 函数中被调用?我尝试用谷歌搜索但找不到任何东西。出了什么问题?

#include <stdio.h>
int A[1000];
int N;
int prefixSum(int i){
    int sum,j;
    for(j=0;j<=i;j++){
        sum=sum+A[j];
    }
    return sum;
    }

int suffixSum(int i){
    int sum,j;
    for(j=i;j<N;j++){
        sum=sum+A[j];
    }
    return sum;
}

int main(void) {
    int T,p,s,index,temp,t,k;
    int total=99999;
    scanf("%d",&T);
    for(t=0;t<T;t++){
        scanf("%d",&N);
        for(k=0;k<N;k++){
            p=prefixSum(k);
            s=suffixSum(k);
            temp=p+s;
            if(temp<total){
                index=k;
            }
        }
        printf("%d",index);
    }
    return 0;
}

最佳答案

以下建议代码:

  1. 展示如何检查调用 scanf() 时的错误
  2. 包含对代码中问题的注释
  3. 突出显示可能/将会发生未定义行为的几个领域
  4. 展示了一种限制变量范围的方法
  5. 正确初始化局部变量
  6. 记录了包含每个头文件的原因
  7. 显示水平间距的适当使用
  8. 结合了这样一个公理:每行只有一个语句,并且每个语句(最多)一个变量声明。
  9. 显示含义:通过一个空行分隔代码块(for、if、else、while、do...while、switch、case、default)
  10. 显示含义:用 2 或 3 个空行分隔函数(保持一致)。

该代码不会提示用户,因此用户将看到一个带有闪烁光标的空白屏幕,并且不知道下一步该做什么。 在建议的代码中,我没有纠正这个问题;但是,您应该这样做。

为了进一步简化可读性和理解,变量名称和参数名称应指示“内容”或“用法”(或更好,两者都指示)。

现在是代码,进行了一些更正:

#include <stdio.h>   // scanf(), printf(), perror()
#include <stdlib.h>  // exit(), EXIT_FAILURE 

// NOTE: the array A[] and N are in file global space, so are initialized to all 0's 
int A[ 1000 ];
int N;


int prefixSum( int i )
{
    //int sum;    // this is on the stack, so not initialized to any specific value
    int sum = 0;

    //int j;
    for( int j=0; j<=i; j++ )  // limit scope of variables to where they are used
    {
        // this line has no effect because all values in A[] are 0
        // accessing the content of an uninitialized variable 'sum' is undefined behavior
        sum = sum + A[j]; // can also be written as: 'sum += A[j];'
    }

    return sum;
} // end function: prefixSum


int suffixSum( int i )
{
    //int sum;    // this is on the stack, so not initialized to any specific value
    int sum = 0;

    //int j;
    for( int j=i; j<N; j++ )  // limit scope of variables to where they are used
    {
        // this line has no effect because all values in A[] are 0
        // accessing the content of an uninitialized variable 'sum' is undefined behavior
        sum = sum + A[j]; // can also be written as: 'sum += A[j];'
    }
    return sum;
} // end function: suffixSum


int main( void )
{
    // follow the axiom:
    // only one statement per line
    // and (at most) one variable declaration per statement.
    int T;
    int p;
    int s;
    int index;
    int temp;

    //int t;  -- keep variable declarations tightly knit to where they are used
    //int k;

    int total=99999;

    // always check the returned value
    // from any of the 'scanf()' family of functions
    // to assure the operation was successful
    // scanf("%d",&T);
    if( 1 != scanf( "%d", &T ) )
    {
        perror( "scanf failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    for(int t=0; t<T; t++ )
    {
        // what happpens when the user enters a number greater than 999?
        // then would be accessing beyond end of array 'A[]',
        // which is undefined behavior
        scanf( "%d", &N );  // -- also needs error checking

        for( int k=0; k<N; k++ )
        {
            p = prefixSum( k );
            s = suffixSum( k );
            temp = p+s;

            if( temp < total )
            {
                index = k;
            }
        }

        // the output stream 'stdout' is buffered.
        // to have immediate display on the terminal
        // the last item in the format string needs to be a 'newline' sequence
        //printf("%d",index);
        printf( "%d\n", index );
    }
    return 0;
} // end function: main

关于c - C 中未调用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46003653/

相关文章:

arrays - Int 变量从 void 返回后神秘地改变了值

arrays - TypeScript 函数数组

function - Haskell调用函数onload

javascript - 函数未定义,不知道为什么

php - 为什么在PHP中使用@function(禁用每个函数的错误报告)是不好的做法?

php - 将换行符转换为 <li> 标签

c - 这两种内存分配方式有什么区别呢?

c - 是否可以在条件运算符中使用 continue? c语言中

c - 为什么 printf ("-%d",025);打印-21? (C)

c - C 链表的问题