¿Es posible establecer const usando una entrada de usuario?

When programming in C, is it possible to set a const with a value of a user-input? If so, how?

preguntado el 08 de noviembre de 11 a las 09:11

No. If it was it wouldn't be a const would it. Perhaps if you describe the problem you are trying to solve, rather than the (impossible) solution you are trying to implement. -

6 Respuestas

¿Por qué no?

void some_function(int user_input)
{
    const int const_user_input = user_input;
    ...
    return;
}

int main (void)
{
    int user_input;
    scanf("%d", &user_input);
    some_function(user_input);
    return 0;
}

respondido 08 nov., 11:13

Thanks! that's exactly what I was looking for. - winuall

you can have that even more directly than in Dadam's answer. (Normally I would have put just in a comment, but it is easier to put that in code directly.)

int get_user_input(void)
{
    int user_input;
    scanf("%d", &user_input);
    return user_input;
}

int main(void)
{
    int const user_input = get_user_input();
    ...
    return 0;
}

respondido 08 nov., 11:16

sí tu puedes.

#include <stdio.h>
int main()
{
   printf("enter your number : ");
   const int i = scanf("%d",&i)*i;
   printf("%d",i);
}

let me explain how this code works. first you should know the point that scanf() función devuelve un integer value equal to the no of items it read from the user.

por ejemplo:

1) scanf("%d",&a); this statement returns value 1 since it read only one item.

2) scanf("%d %d",&a,&b); this statement returns value 2 since it read two integers a y b.

similarly when we assigned scanf("%d",&i)*i a i it gives the value one multiplied by the value of i(which we given as input). therefore you get the same value of i.

Respondido 04 Jul 18, 11:07

Linker usually locate global const in read-only space (like code space) and therefore it cannot be changed later on

See comments on local const

respondido 08 nov., 11:17

Not always true: A local variable can be const, and initialized by a run-time value, at least in C99 and many C89 variants. - Johan Bezem

@JohanBezem OP never mentioned scope, so it's best to err on the side of caution and say no. - moshbear

In addition to the other answers (which all say no), you could do some ugly things like

static const int notsoconst = 3;
scanf("%d", ((int*) &notsoconst));

But this could compile, but it probably would crash at runtime (and is comportamiento indefinido in the C language specification), because notsoconst would be put in a read-only segment (at least with GCC on Linux).

Even if it is doable, I don't recommend coding this way. And even if your implementation does not put constants in some read-only segment, the compiler is allowed to expect const to never change (as specified in the language standard) and is allowed to optimize with this assumption.

Respondido el 11 de junio de 15 a las 12:06

It's definitely undefined behavior (6.7.3/5: "If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.") - Steve Jessop

A const variable is C is technically read only. So one can't set it from user-input

respondido 08 nov., 11:13

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.