Search code examples
cpascals-triangle

error while generating pascal triangle


This is my code to generate Pascal's triangle in C language.

#include<stdio.h>
#include<conio.h>

void main()
{
    int i, n, c;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        for (c = 0; c <= (n - i - 2); c++)
            printf(" ");
        for (c = 0; c <= i; c++)
            printf("%ld", factorial(i) / (factorial(c)*factorial(i - c)));
        printf("\n");
    }
    getche();
}

long factorial(int n)
{
    int c;
    long res = 1;
    for (c = 1; c <= n; c++)
        res = res*c;
    return(res);
}

On compilation it shows two errors:

  • conflicting types for 'factorial'

  • previous implicit declaration of 'factorial' was here

What is my mistake here?


Solution

  • conflicting types for 'factorial'
    previous implicit declaration of 'factorial' was here

    Both errors refers to one thing: The function factorial shall be declared before used.
    Simply move the definition before main or write a declaration for it before main.

    I'd not write a detailed explanation for you as there're already some, e.g. What is the difference between a definition and a declaration?