Parse Error: Simple C Program

canton picture canton · Apr 29, 2012 · Viewed 8.7k times · Source

I have the following simple code for a class and am getting a parse error for each line starting c = c ... in the if statements. Additionally I'm getting the following errors:

Warning: type mismatch with previous implicit declaration. Previous implicit declaration of isUpperCase in function isUpperCase: parse error before '=' token. And a similar error for isLowerCase.

Does anyone have any insight?

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

#define LOWERCASE_START = 97
#define LOWERCASE_END = 122
#define UPPERCASE_START = 65
#define UPPERCASE_END = 90
#define ALPHABET_LENGTH = 26

void simpleEncryption(char s[]){
    int i;
    for (i=0; i < strlen(s); i++){
        char c = s[i];
        if (isUpperCase(c) == 1){
            c = c - UPPERCASE_START + 1;
            c = c % ALPHABET_LENGTH;
            c = c + UPPERCASE_START;
        } else if (isLowerCase(c) == 1){
            c = c - LOWERCASE_START + 1;
            c = c % ALPHABET_LENGTH;
            c = c + LOWERCASE_START;
        }
        s[i]=c;
    }
}

int isUpperCase(char c) {
    if (c >= UPPERCASE_START && c <= UPPERCASE_END) {
        return 1;   
    } else {
        return 0;
    }
}

int isLowerCase(char c) {
    if (c >= LOWERCASE_START && c <= LOWERCASE_END) {
        return 1;   
    } else {
        return 0;
    }
}

Answer

mu is too short picture mu is too short · Apr 29, 2012

Your macro definitions shouldn't contain equals signs, you just want this:

#define LOWERCASE_START 97
#define LOWERCASE_END 122
#define UPPERCASE_START 65
#define UPPERCASE_END 90
#define ALPHABET_LENGTH 26

The preprocessor is rather simple minded and will blindly substitute = 97 into your C to produce broken things like this:

c = c - = 97 + 1;

if you have the = in your #defines.