-1

I want to change the value of a macro during the run of the program, for that I've wrote this example :

#include <stdio.h>
#define MAX  (65535 *0.5)

int main ( ){
float amp = 0.0;
float temp = 0.0 ;
temp    = MAX ;
char c;

while (1){
  printf(" MAX value %.2d.%.3d \n ", (short)temp,(short)(temp*1000));
  scanf("%c",&c);
  if( c =='x') {
    #undef MAX
    #define MAX  (65535 +amp);
    amp+= 0.1;
    temp = MAX;
  }
}
return 0 ; }

I've got two problems : 1. the printf doessn't show the values as hoped, for instand 19.211, it always -32768.-32768 2. I don't see any change of the value of the macro.

any idea what I'm doing wrong here ?

Engine
  • 5,360
  • 18
  • 84
  • 162
  • Did you expect the value of the macro to change when you enter `'x'`? – dhke Mar 25 '15 at 13:25
  • acutaly yes I'Ve read this post http://stackoverflow.com/questions/9274500/redefining-or-changing-macro-value and was hopping it wil do it – Engine Mar 25 '15 at 13:26
  • Macros aren't variables. They are macros. If you need a variable, use a variable instead. – Lundin Mar 25 '15 at 13:43

4 Answers4

3

You can only define macros at compile time. The c preprocessor replaces every occurrance of a macro before actual compilation happens, with gcc you can see what code was generated after preprocessing by using the -E switch, if you try it experimenting with different macros, you may get to understand the preprocessor a little better, read the link to understand more.

This

if( c =='x') {
  #undef MAX
  #define MAX  (65535 +amp);
  amp+= 0.1;
  temp = MAX;
}

does not do what you think.

If you execute the preprocessor on the source code, then what will actually happen is that the snippet above will be compiled as

if( c =='x') {
  amp+= 0.1;
  temp = MAX;
}

so as you can see, it doesn't do what you think.

Also, this is not related to the macro redefinition issue, but your code has a bug that can make it enter an infinite loop, this

scanf("%c",&c);

will keep scanning the '\n' that is left in the stdin after pressing Enter/Return, so you need to explicitly ignore that character by adding a white space before the specifier like this

scanf(" %c", &c);
/*     ^ white space goes here */

Note: another answer has suggested that you are using the incorrect data type and suggested a solution, you should take a look at it, since in fact it seems your program has an integer overflow issue.

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

Macros are resolved during compilation, so during program execution there is no such thing like MAX - each occurence was already replaced by its value (65535).

However, if you need to define local constant (local in terms of translation unit), why not use static variable?

static unsigned int MAX = 65535;

And then:

if (c == 'x')
{
    MAX = 65535 + amp;
   //...
}
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
1

Let's run your file through the C preprocessor (CPP) manually. The result is

int main ( ) {
    float amp = 0.0;
    float temp = 0.0 ;
    temp = (65535 *0.5) ;
    char c;

    while (1) {
        printf(" MAX value %.2d.%.3d \n ", (short)temp,(short)(temp*1000));
        scanf("%c",&c);
        if( c =='x') {


            amp+= 0.1;
            temp = (65535 +amp);;
        }
    }
    return 0 ;
}

Macros are evaluated before the compiler even sees your source code. You cannot change the value of a macro based on a decision taken at runtime.

Why not instead use a variable float max = ... and change that one's value depending on user input?

dhke
  • 15,008
  • 2
  • 39
  • 56
1

You are using too short data type and printing the fractional part incorrectly. Consider doing this:

double i;
printf(" MAX value %.2d.%.3d \n ", (int)temp,(int)(modf(temp,&i)*1000));

...instead of this:

printf(" MAX value %.2d.%.3d \n ", (short)temp,(short)(temp*1000));

Be sure to #include <math.h> if using modf.

Your redefinition of the MAX macro looks strange to me, though. Macros are defined at compile time, not at run time, so most of the time you don't want to redefine macros.

Note also that

scanf("%c",&c);

will read the newline as a separate character, so you want to do instead:

scanf(" %c",&c);

...to consume whitespace. Do consider checking also for the EOF (end-of-file) condition; currently you don't do that.

juhist
  • 4,210
  • 16
  • 33