"Segmentation fault(core dumped)" error in C language if-Else program

Asked

Viewed 27 times

-3

Greetings!

I’m a beginner in programming and found this problem in a code: Segmentation fault (core dumped), after entering the first variable.

That is the code:

#include <stdio.h>  
#include <math.h>

/*Elabore um algoritmo que permita a entrada de dois valores (x e y) e escreva como resultado o maior entre eles, usando apenas duas variaveis. */

int main()  
{  
     int x, y;  
    //inserindo e processando os numeros  
        printf("Digite um valor para x:\n", &x);  
        scanf("%d", x);  
        printf("Digite um valor para y:\n", &y);  
        scanf("%d", y);  
    //condicoes para aparecer o maior  
        if(x > y){  
            printf("%d\n", x);  
                }else{  
                    if (x == y)  
                        printf("OS NUMEROS SAO IGUAIS A %d",x);  
                            else  
                                printf("%d",y);  
                }  
} 

That’s what comes up in relation to what’s wrong, but I couldn’t understand.

[lista03questao01.c 2021-08-21 23:33:48.604]  
,,lista03questao01.c: In function ‘main’:  
lista03questao01.c:11:16: warning: too many arguments for format [-Wformat-extra-args]  
   11 |         printf("Digite um valor para x:\n", &x);  
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lista03questao01.c:12:17: warning: format ‘%d’ expects argument of type ‘int *’, but  
 argument 2 has type ‘int’ [-Wformat=]
   12 |         scanf("%d", x);
      |                ~^   ~
      |                 |   |
      |                 |   int
      |                 int *
lista03questao01.c:13:16: warning: too many arguments for format [-Wformat-extra-args]
   13 |         printf("Digite um valor para y:\n", &y);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lista03questao01.c:14:17: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]  
   14 |         scanf("%d", y);         
      |                 ~^   ~
      |                  |   |
      |                  |   int
      |                  int *

Sorry if the doubt is very basic, I tried to search for answers to other questions, but found only for other cases and could not adapt to mine. Thank you very much to anyone who can help!

1 answer

0


Your code has some problems:

  • First problem: printf("Digite um valor para x:\n", &x);
    The symbol & serves, in that case, to pass a memory address, would have no problem pass a memory address as argument to a function printf but the main problem in this case is that the function does not know what to do with this argument, because it does not have %d inside the string of printf so it can be used, then the compiler displays the first and third warnings.
  • Second problem: scanf("%d", x);
    The scanf yes you need a memory address to change the value of the variable, in which case the compiler displays the second and fourth warnings.

Solution

#include <stdio.h>

int main()  
{
    int x, y;
    printf("Digite um valor para x:\n");
    scanf("%d", &x);
    printf("Digite um valor para y:\n");
    scanf("%d", &y);

    if(x > y)
        printf("%d\n", x);
    else
    {
        if (x == y)
            printf("OS NUMEROS SAO IGUAIS A %d\n", x);
        else
            printf("%d\n", y);
    }
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.