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?
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?
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 aarray
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
Firstly, two clarifications:
printf
and not "Printf". 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:
In this case the ampersand ( &
) means to indicate the address in memory of the variable, sending the value by reference.