-1

I am a beginner in C programming.

I was writing a simple program to calculate average.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
    int n, s = 0, num, i;
    float avg;
    printf("Enter value of total no\n");
    scanf("%d", &n);
    for (i=1; i<=n; i++)
    {
        void pri(int i){
            switch(i){
                case 1:
                    printf("st");
                    break;

                case 2:
                    printf("nd");
                    break;

                case 3:
                    printf("rd");
                    break;

                default:
                    printf("th");
                    break;
            }
        }
        printf("Enter %d pri(i) number\n", i);
        scanf("%d", &num);
        s += num;
    }
    avg = s / n;
    printf("The average is %f",avg);
    return 0;
}

But pri(i) is not working as I expected. But later I found another way to do this, so here is the second version of this code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
    int n, s = 0, num, i;
    float avg;
    printf("Enter value of total no\n");
    scanf("%d",&n);
    for (i=1; i<=n; i++)
    {
        void pri(int i){
            switch(i){
                case 1:
                    printf("enter 1st number\n");
                    break;

                case 2:
                    printf("enter 2nd number\n");
                    break;

                case 3:
                    printf("enter 3rd number\n");
                    break;

                default:
                    printf("enter %dth number\n",i);
                    break;
            }
        }
        pri(i);
        scanf("%d", &num);
        s += num;
    }
    avg = s / n;
    printf("the average is %f",avg);
    return 0;
}

I want to get the results of this second piece of code from the first version.

Can I call functions in printf which are defined somewhere in program?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mssirvi
  • 147
  • 3
  • 14

1 Answers1

2

You cannot tell printf() to call another function in the middle of its execution. printf() expects to receive a formatting string and arguments to replace parts of this string. What you're trying to do (embed a function call in the formatting string) is not possible for a number of reasons.

What you can do is return the string instead of printing it and use it as argument.

const char *pri(int i) {
    switch(i) {
    case 1:
        return "st";
    case 2:
        return "nd";
    case 3:
        return "rd";
    default:
        return "th";
    }
}

printf("enter %d%s number\n", i, pri(i));

C doesn't support nested functions (function defined inside another function). Your code works because your compiler adds support for such functions as an extension. In general, you should probably avoid nesting functions.

martinkunev
  • 1,364
  • 18
  • 39
  • *"You cannot call a function in the middle of another function"*??? I guess I've been programming wrong all these years. ;) What you really mean is you cannot **DECLARE** a function inside another function. – abelenky May 27 '16 at 17:39
  • @abelenky I did mean "call". Maybe I could have expressed myself more clearly. I changed the sentence. – martinkunev May 27 '16 at 17:44
  • 1
    Nice! (Note: needs works for -21, -3, -2, -1 ,21, 31, ... ) – chux - Reinstate Monica May 27 '16 at 18:33
  • 1
    @abelenky the OP wrote this: `printf("enter %d pri(i) number\n",i);` hoping that printf, when interpreting the format string, would call pri(i) and substitute the resultant string into the format string – FredK May 27 '16 at 21:34
  • You cannot call a function from within a ***string literal***, which is what the poster tried to do. You certainly can call a function from within another function. (obviously: `printf("output %s\n", GetOutput(input));`) – abelenky May 29 '16 at 02:28
  • @abelenky In your example you pass the return value of a function as an argument to another function. You don't call it from within the other function. – martinkunev May 29 '16 at 11:37
  • @melpomene What is your point? I have long changed my wording to clarify what I wanted to say. Either you haven't read my answer, you don't understand it or you are looking for something to cavil at. Which one is it? – martinkunev Jun 11 '16 at 12:25
  • @martinkunev None of the above. I read and agree with your answer. The reasoning/wording in your comments, not so much. My point is that abelenky's example does (unintentionally) demonstrate calling a function from within another function. – melpomene Jun 11 '16 at 12:45