Error in code, C

2

I've created an extremely simple code just to test Code::Blocks , but no matter what I do, it always returns the following error:

collect2.exe: error: ld returned 1 exit status

I still do not understand what I'm missing. the code goes here:

#include <stdio.h>
#include <stdlib.h>

int main(){
   int A;
   printf("Digite um valor: ");
   scanf("%d", &A);
   printf("O valor digitado foi: ", &A);
   return 0;
}
    
asked by anonymous 21.07.2014 / 01:27

4 answers

3

The program error is in the following line:

   printf("O valor digitado foi: ", &A);

Correct:

   printf("O valor digitado foi: %d", A);

Note that to refer to a variable and display it on the screen, you need to use '% d' within the phrase and, after the comma, you do NOT need to use the character '&' (this is only necessary for referencing in 'scanf').

    
26.10.2015 / 12:53
1

You have an error related to the printf line, it should be written like this:

printf("O valor digitado foi: %d\n", A);

The variable A in scanf is passed as a reference so that it can change it, whereas in printf is only informed so that it can display the contents of it.

    
21.07.2014 / 02:29
1
collect2.exe: error: ld returned 1 exit status
                     ^^

ld is the "linker": the part of the compiler that 'blends' your code with existing code (for scanf() , for example).

By taking out &A used in printf() , which should only be a , your program has no error. This error does not prevent compilation, but the results will be strange.

Check the configuration of your compiler, especially the part of the linker, paths to libraries, etc.

    
21.07.2014 / 10:16
1

Things to consider:

  • No input check: Nothing guarantees that an "int" will be typed in stdin
  • Scanf missing: Nothing guarantees that the entire received will be "sufficient" to house the same in a data type "int"
  • Lack of spacing: It is interesting that all messages in the console have at least one line break (\ n)
  • Missing formatting: Every parameter passed to printf needs to have a specifier according to the variable type (% d,% p,% i ....)
  • Incorrect representation of value stored by A: Use & A in printf will pass the "memory address" that the value of A resides:
  • (Address) printf("Valor: %p\n",&A)

    (Value) printf("Valor: %d\n",A)

    If this is a specific problem in the build process, you can pass a "-v" to figure out what it is.

        
    24.07.2014 / 17:12