Function #define

1

In a code that uses #define RULE 90, before the function main() , Is there any way that the value of the variable, in this example the 90, is requested when the program is executed, instead of having to enter it in the code, every time you want to change it?

Thank you.

    
asked by Vacendak 17.04.2018 в 10:06
source

5 answers

4

#define is not used to declare variables but macros or constants to be processed by the precompiler and can not, therefore, see their modified values at runtime. To do what you ask you have to use variables:

int main()
{
  int variable;                    // <<--- Variable
  printf("Introduce un numero: ");
  scanf("%d",&variable);           // <<--- El usuario modifica su valor
}
    
answered by 17.04.2018 в 10:09
1

If what you want is that when you call the program to run with a certain value equal what is best for you is to pass that value when invoking the program

for example: Program 10

Then you can capture that value within your code

int main(int argc, char *argv[]) 

The first argument of the argc function indicates the total number of parameters. The second argv argument allows access to the values of the parameters passed to the program. (always the first parameter, ie argv [0] contains the name of the program)

so argv [1] will contain the value 10

    
answered by 17.04.2018 в 10:55
1

I think you do not understand well the concept of what a #define is, the precompilation directive #define is used to create macros, in this case it replaces a word in your code with another code portion, suppose that your program looks like this:

#include <stdio.h>
#define VALOR 90

int main(){
    printf("%d\n",VALOR);
    return 0;
}

In the precompiler step, your main function will look like this:

int main(){
    printf("%d\n",90);
    return 0;
}

Then VALUE is not a variable, nor a constant, but it is a harded number in your program, there is no way to change it without recompiling since that 90 becomes part of the code of your program.
What you have to do as you decide, is declare a variable (in this case in global scope, since the define affects the same scope and is marked in the tags of the question)

#include <stdio.h>

int valor;

int main(){
    printf("Por favor introduzca un valor: ");
    scanf("%d",&valor);
    printf("%d\n",valor);
    return 0;
}
    
answered by 09.05.2018 в 20:06
0

I have attached an example code, to be reviewed.

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

#define REGLA 10

int main(int argc, char **argv){
        if (argc<2){
                printf("Uso:\nredefine.exe <numero>\n");
                exit(1);
        }

        int numero=0;
        numero=atoi(argv[1]);

        printf("Valor REGLA  :%d\n",REGLA);
        printf("Valor numero :%d\n",numero);

        #ifdef REGLA
                #undef REGLA
                #define REGLA numero
        #endif

        printf("Valor REGLA Redef :%d\n",REGLA);
}
    
answered by 09.05.2018 в 19:50
-1

you can use #undef and then again #def , after the main

#ifdef REGLA
   #undef  REGLA
   #define REGLA   <variable o nuevo valor>
#endif
    
answered by 08.05.2018 в 20:09