1

I am new to programming in C and I am making a C program to find the average and percentage of marks obtained by a student in three subjects using a single function. My code is:

#include <stdio.h>
int main()
{
    float aver, per, mark1, mark2, mark3;
    printf("Enter the marks of subject 1: ");
    scanf(" %f", &mark1);
    printf("Enter the marks of subject 2: ");
    scanf(" %f", &mark2);
    printf("Enter the marks of subject 3: ");
    scanf(" %f", &mark3);
    averper(mark1, mark2, mark3, &aver, &per);
    printf("The average of marks entered by you = %f\n", aver);
    printf("The percentage of marks entered by you = %f", per);
    return 0;
}
float averper(float a, float b, float c, float *d, float *e)
{
    float sum = a + b + c;
    *d = sum / 3;
    *e = (sum / 300) * 100;
}

The errors obtained are:

main.c: In function ‘main’:
main.c:11:2: warning: implicit declaration of function ‘averper’ [-Wimplicit-function-declaration]
  averper(mark1, mark2, mark3, &aver, &per);
  ^~~~~~~
main.c: At top level:
main.c:16:7: error: conflicting types for ‘averper’
 float averper(float a, float b, float c, float *d, float *e)
       ^~~~~~~
main.c:11:2: note: previous implicit declaration of ‘averper’ was here
  averper(mark1, mark2, mark3, &aver, &per);
  ^~~~~~~ 

Thanks

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 2
    Please turn off your CAPS LOCK. Online, all caps = shouting. You don't want to shout at the people you're asking for help. :-) *(I've removed them for you this time.)* – T.J. Crowder Jun 25 '20 at 07:34
  • 2
    You need to declare functions _before_ using them. – Lukas-T Jun 25 '20 at 07:35
  • the return is missing in `averper` – Ôrel Jun 25 '20 at 07:37
  • You can see here: https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function. Coming from C++, amazing that C just *implicitly* declares an undeclared function. (post was tagged C++ before) – Mikael H Jun 25 '20 at 07:40

1 Answers1

0

The defects in the program:

  1. Used the function before defining the function signature or directly function at the top of the code.

  2. The function doesn't returns anything and the if the program is designed to return, yet the value is nowhere used.


Try this approach:

#include <stdio.h>

typedef struct { // declaring a struct to return avg and per together
    float avg;
    float per;
} averS;

averS averper(float, float, float, float *, float *); // function signature

int main()
{
    float aver, per, mark1, mark2, mark3;

    printf("Enter the marks of subject 1: ");
    scanf(" %f", &mark1);

    printf("Enter the marks of subject 2: ");
    scanf(" %f", &mark2);

    printf("Enter the marks of subject 3: ");
    scanf(" %f", &mark3);

    averS s = averper(mark1, mark2, mark3, &aver, &per); // holding values

    printf("The average of marks entered by you = %f\n", s.avg);
    printf("The percentage of marks entered by you = %f", s.per);

    return 0;
}

averS averper(float a, float b, float c, float *d, float *e)
{
    averS as; // declaring a local structure for returning value purpose.

    float sum = a + b + c;

    as.avg = sum / 3;
    as.per = (sum / 300) * 100;

    return as; // returning the struct
}

Here we've used a struct which holds two values and return them together and then they're used in the main().


After the successful compilation, it'll output something like:

Enter the marks of subject 1: 10 // --- INPUT
Enter the marks of subject 2: 20
Enter the marks of subject 3: 30
The average of marks entered by you = 20.000000 // --- OUTPUT
The percentage of marks entered by you = 20.000000
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34