Home > Mobile >  Why does defining a function in C shows a weird error of putting ')' before a variable
Why does defining a function in C shows a weird error of putting ')' before a variable

Time:10-20

Wen I try to compile my file stack-zero.c with the command gcc stack-zero.c -o stack0 it show this as error stack-zero.c:19:17: error: expected ‘)’ before ‘LEVELNAME’

The code is as follows

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

#define BANNER \
  "Welcome to " LEVELNAME ", brought to you by https://exploit.education"

char *gets(char *);

int main(int argc, char **argv) {
  struct {
    char buffer[64];
    volatile int changeme;
  } locals;

  printf("%s\n", BANNER);

  locals.changeme = 0;
  gets(locals.buffer);

  if (locals.changeme != 0) {
    puts("Well done, the 'changeme' variable has been changed!");
  } else {
    puts(
        "Uh oh, 'changeme' has not yet been changed. Would you like to try "
        "again?");
  }

  exit(0);
}

CodePudding user response:

LEVELNAME is not defined. Define it and the problem is gone. Adding #define LEVELNAME "Ultimate level" before BANNER would compile and yield output as expected.

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

#define LEVELNAME "Ultimate level"
#define BANNER \
  "Welcome to " LEVELNAME ", brought to you by https://exploit.education"

char *gets(char *);

int main(int argc, char **argv) {
  struct {
    char buffer[64];
    volatile int changeme;
  } locals;

  printf("%s\n", BANNER);

  locals.changeme = 0;
  gets(locals.buffer);

  if (locals.changeme != 0) {
    puts("Well done, the 'changeme' variable has been changed!");
  } else {
    puts(
        "Uh oh, 'changeme' has not yet been changed. Would you like to try "
        "again?");
  }

  exit(0);
}

CodePudding user response:

In C programming language, the character " is considered to be a special character, as it defines the opening and closing of a string.

Try using your BANNER definition like this:

#define BANNER \
  "Welcome to \" LEVELNAME \", brought to you by https://exploit.education"
  •  Tags:  
  • c
  • Related