This isn't quite answered by static const vs #define or #defining constants in C++.
In when I did a lot of programming in C, ANSI C was brand new. We mostly used #define to define constants. I've been made aware that this is no longer a best practice (https://codereview.stackexchange.com/questions/123848/verifying-e-mail-address-in-c/123856#123856).
Using #define I can use previous constants to define the current constant, an exampe is
#define EMAIL_CHAR_ARRAY_SIZE 40
#define GOOD_EMAIL_ADDRESS 1
#define BAD_EMAIL_ADDRESS 0
#define MIMIMUM_USER_NAME_LENGTH 1
#define AT_SIGN_LENGTH 1
#define DOT_LENGTH 1
#define MINIMUM_DOMAIN_LENGTH 1
#define MINIMUM_ROOT_DOMAIN_LENGTH 2
#define MINIUMUM_EMAIL_LENGTH MIMIMUM_USER_NAME_LENGTH + AT_SIGN_LENGTH + MINIMUM_DOMAIN_LENGTH + MINIMUM_ROOT_DOMAIN_LENGTH
I have a program (below) where I have attempted to use static const rather than #define, however the constant MINIUMUM_EMAIL_LENGTH doesn't compile when I try to use static const using the previous constants. Is there a way to use previously defined constants in a static const TYPE declaration?
const_testemail.c:12:5: error: initializer element is not constant
static const int MINIUMUM_EMAIL_LENGTH = (MIMIMUM_USER_NAME_LENGTH + AT_SIGN_LENGTH + MINIMUM_DOMAIN_LENGTH + MINIMUM_ROOT_DOMAIN_LENGTH);
#include <stdio.h>
#include <string.h>
static const int EMAIL_CHAR_ARRAY_SIZE = 40;
static const int GOOD_EMAIL_ADDRESS = 1;
static const int BAD_EMAIL_ADDRESS = 0;
static const int MIMIMUM_USER_NAME_LENGTH = 1;
static const int AT_SIGN_LENGTH = 1;
static const int DOT_LENGTH = 1;
static const int MINIMUM_DOMAIN_LENGTH = 1;
static const int MINIMUM_ROOT_DOMAIN_LENGTH = 2;
/* Doesn't compile
* static const int MINIUMUM_EMAIL_LENGTH = MIMIMUM_USER_NAME_LENGTH + AT_SIGN_LENGTH + MINIMUM_DOMAIN_LENGTH + MINIMUM_ROOT_DOMAIN_LENGTH;
* */
#define MINIUMUM_EMAIL_LENGTH (MIMIMUM_USER_NAME_LENGTH + AT_SIGN_LENGTH + MINIMUM_DOMAIN_LENGTH + MINIMUM_ROOT_DOMAIN_LENGTH)
int isEmailAddressProper(const char EmailAddress[EMAIL_CHAR_ARRAY_SIZE])
{
int EmailAddressIsGood = GOOD_EMAIL_ADDRESS;
int LengthOfEmailAddress;
char *AtSignLocation, *pos2;
int rootDomainLength;
LengthOfEmailAddress = strlen(EmailAddress);
if (LengthOfEmailAddress < MINIUMUM_EMAIL_LENGTH)
{
printf("The length of the email address is less than the minimum lenght %d\n", MINIUMUM_EMAIL_LENGTH);
EmailAddressIsGood = BAD_EMAIL_ADDRESS;
}
AtSignLocation = strchr(EmailAddress, '@'); /* get the first instance of @ */
if (!AtSignLocation)
{
printf("There is no @ in the email address\n");
EmailAddressIsGood = BAD_EMAIL_ADDRESS;
return EmailAddressIsGood;
}
if (AtSignLocation == EmailAddress) { /* Is @ the first character? */
printf("There is no user name in the email address, @ is the first character\n");
EmailAddressIsGood = BAD_EMAIL_ADDRESS;
}
pos2 = strrchr(EmailAddress, '@'); /* find any other @ */
if ((pos2) && (AtSignLocation != pos2)) {
printf("There is more than 1 @ in the email address\n");
EmailAddressIsGood = BAD_EMAIL_ADDRESS;
}
pos2 = strrchr(EmailAddress, '.'); /* get the last instance of '.' */
if (AtSignLocation > pos2) /* is . before @ ? */
{ printf("There is no root domain in the email address\n");
EmailAddressIsGood = BAD_EMAIL_ADDRESS;
}
pos2++;
rootDomainLength = LengthOfEmailAddress - ((int)(pos2 - EmailAddress));
/* if root domain less than length 2 */
if (rootDomainLength < MINIMUM_ROOT_DOMAIN_LENGTH)
{
printf("The root domain length (%d) is less than the minimum length required (%d) in the email address\n", rootDomainLength, MINIMUM_ROOT_DOMAIN_LENGTH);
EmailAddressIsGood = BAD_EMAIL_ADDRESS;
}
return EmailAddressIsGood;
}
void GetAndValidateEmailAddress(char EmailAddress[EMAIL_CHAR_ARRAY_SIZE])
{
int EmailAddressIsGood = BAD_EMAIL_ADDRESS;
char TempEmail[EMAIL_CHAR_ARRAY_SIZE];
while (!EmailAddressIsGood)
{
scanf("%39s", TempEmail);
EmailAddressIsGood = isEmailAddressProper(TempEmail);
if (!EmailAddressIsGood) {
printf("The email address is not in the proper format, please re-enter the email address\n");
}
}
strcpy(EmailAddress, TempEmail);
}
main()
{
char EmailAddress[EMAIL_CHAR_ARRAY_SIZE];
printf("Please enter the email address\n");
GetAndValidateEmailAddress(EmailAddress);
printf("The email address you entered is %s\n", EmailAddress);
}