paperAgamer
paperAgamer

Reputation: 1

segmentation fault at value assignment in C

Hello I am learning C and tried to write code that will compare 3 numbers and tell me which one is the biggest.

#include <stdio.h>
int max(int a, int b){
    if(a > b){
        return a;    
    }
    return b;
}
int main()
{
    int a, b, c;
    printf("Please provide the first number --> ");
    scanf("%d", &a);
    printf("Please provide the seceond number --> ");
    scanf("%d", &b);
    printf("Please provide the third number --> ");
    scanf("%d", &c);
    int big = max(max(a, b), c);
    printf("The biggest number is --> ");
    printf(big);
    printf("\n");
    return 0;
}

I wrote it on a website called onlinegdb.com but after I input the 3 numbers I get a segmentation fault error after trying to find what was wrong it showed me that there seems to be a problem with inserting the result of max into big

Upvotes: 0

Views: 47

Answers (2)

Praveer Kumar
Praveer Kumar

Reputation: 1018

I think it might be due to this line of code

 printf(big);

try using:

 printf("%d", big);

basically you forgot to put the format specifier.

Upvotes: 4

Musa
Musa

Reputation: 97717

The first argument to printf is the format string followed by any values

printf("%d\n",big);

Upvotes: 1

Related Questions