Use of ampersand & in c to save value in variables

3

In this code

printf("ingrese el numero a guardar");
scanf("%i",&a);

What is the function of the aspersand (&) when saving the value and what is its name?

    
asked by Victor Alvarado 02.04.2017 в 04:15
source

2 answers

2

Functions in C (most) start with lowercase.

The function of scanf(tipo, &var); must specify the type of data to be read and the variable. in the types can be %i -> Integer , %s-> cadena , %f -> float , %c -> caracter (the most common)

The other parameter that it receives is the variable but before it appears the & ampersand which is used to indicate a memory address of the variable where the data will be stored

  

Keeping in mind that this & is omitted when dealing with variables   of type string or a array of characters.

Why is it Skip?

When a variable of type entero , flotante o char is declared, it is assigned a memory address, as does C scanf to know in which direction it will keep the entered value? using the operator & (by reference) , for an array or a String, the variable will also have the address of the first element, it is no longer necessary to pass the & since it will know where it will begin to write the value or Valores

    
answered by 02.04.2017 / 05:14
source
1

Firstly, two clarifications:

  • the method is printf and not "Printf".
  • the method is scanf and not "Scanf".

The value you enter is actually stored in variable a , and in this case it must be an integer type, this is an example:

int a;
printf("ingrese el numero a guardar");
scanf("%i", &a);
printf("El valor del numero a guardar es: %i", a);

Using the C language when sending values to a function can be done in two ways:

  • Value
  • Reference

In this case the ampersand ( & ) means to indicate the address in memory of the variable, sending the value by reference.

    
answered by 02.04.2017 в 04:52