Many times we talk about undefined, unspecified and implementation-defined behavior in c
. However, what is the difference between these concepts?
I am trying to make a C console application collect (via keyboard) special Spanish characters like accents, 'ñ', etc in a scanf
or in gets
.
I have managed to display these characters correctly (stored in a variable or, directly, from the printf
) thanks to the locale.h
. I show an example:
#include <stdio.h>
// Añade paquete de idiomas
#include <locale.h>
int main(void)
{
// Declaración de variables
char cadena[254];
// Establecer el idioma a español
setlocale(LC_ALL, "spanish");
// Mostrar correctamente los caracteres especiales del español
printf("¡Éxito!. Se muestran los caracteres especiales del español.");
printf("Introduce un string con caracteres especiales: ");
gets(cadena);
printf("El string con caracteres especiales que has introducido es: %s", cadena);
return 0;
}
but I still couldn't get them to be picked up correctly with the functions mentioned above.
Does anyone know how to do it?
Thank you
EDIT 1:
Doing tests I have observed that:
setlocale(LC_ALL, "spanish");
It shows the Spanish characters correctly, but it doesn't pick them up from the keyboard.setlocale(LC_ALL, "es_ES");
It collects the Spanish characters correctly from the keyboard, but it does not display them correctly.
I'm starting a youtube course on C.
And they show that to print the values of the variables it is necessary to use this operator %
.
example:
int suma, a, b;
a=2;
b=3;
suma=a+b;
printf("El valor de la suma es %i", suma);
In case of integer %i
, float %f
and char%c
I would like to know what other uses this operator has and what attributes it has depending on the value, because I saw that I could delimit the displayed decimals using %.1f
or%.2f
Since in C/C++ floating-point literals without a suffix default to type double , then assigning such a literal to a float performs an implicit conversion from double to float .
float n = 3.14;
if(n == 3.14f)
puts("Igual");
This does not print the message instead if we add the suffix f to 3.14 in the declaration of n to prevent the conversion if printed. The question is, is there a loss of precision when the conversion is carried out?
Many times one is programming in C , and comes across the segmentation fault message , what does it mean and why does it occur?