Doubts of logic in C

Asked

Viewed 69 times

0

I’m solving a small exercise in C that should print the predecessor and successor of a number on screen. The predecessor prints correctly, but the successor always shows 108. regardless of the number typed. I imagine it’s kind of garbage. How do you fix this?

#include <stdio.h>
#include<stdlib.h>
int main ()
{
    int numero,antecessor,sucessor;
    printf("\n Escreva um numero inteiro: ");
    scanf("%d",&numero);
    antecessor=numero-1;
    sucessor+numero+1;
    printf("=============================================\n\n");
    printf("\n O antecessor do numero %d e %d\n\n", numero, antecessor);    
    printf("=============================================\n\n");
    printf("\n O sucessor do numero %d e %d\n\n", numero, sucessor);
    printf("=============================================\n\n");

system("pause");    
return(0);
}

IN TIME: I apologize to everyone. The mistake had nothing to do with garbage but with wrong typing on my part. Only after posting did I see that the second number (successor) was getting the value assignment wrong.

Instead of me writing:

successor=number+1

writ:

successor number+1

  • 1

    successor = number + 1 and not successor + number + 1

  • Because it is pmargeff... Only after posting the doubt is that I saw that it was a typo! Thank you for the reply!

1 answer

1

    antecessor=numero-1;
    sucessor+numero+1;             /* esta linha nao faz nada */
    printf("=============================================\n\n");

Take a good look at the second line of the above section :)

  • Thank you for the answer dear pmg ! As you can see, it was beginner’s question level ZERO, of these that still misses attribution of value!! But thank you for your attention.

Browser other questions tagged

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