How to define a static const using previously defined constants in C? -


this isn't quite answered static const vs #define or #defining constants in c++.

in when did lot of programming in c, ansi c brand new. used #define define constants. i've been made aware no longer best practice (https://codereview.stackexchange.com/questions/123848/verifying-e-mail-address-in-c/123856#123856).

using #define can use previous constants define current constant, 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 program (below) have attempted use static const rather #define, constant miniumum_email_length doesn't compile when try use static const using previous constants. there way use defined constants in static const type declaration?

const_testemail.c:12:5: error: initializer element 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 email address less minimum lenght %d\n", miniumum_email_length);         emailaddressisgood = bad_email_address;     }      atsignlocation = strchr(emailaddress, '@'); /* first instance of @ */     if (!atsignlocation)     {         printf("there no @ in email address\n");         emailaddressisgood = bad_email_address;         return emailaddressisgood;     }      if (atsignlocation == emailaddress) { /* @ first character? */         printf("there no user name in email address, @ first character\n");         emailaddressisgood = bad_email_address;     }      pos2 = strrchr(emailaddress, '@'); /* find other @ */     if ((pos2) && (atsignlocation != pos2)) {         printf("there more 1 @ in email address\n");         emailaddressisgood = bad_email_address;     }      pos2 = strrchr(emailaddress, '.'); /* last instance of '.' */     if (atsignlocation > pos2) /* . before @ ? */     {            printf("there no root domain in email address\n");         emailaddressisgood = bad_email_address;     }      pos2++;     rootdomainlength = lengthofemailaddress - ((int)(pos2 - emailaddress));     /* if root domain less length 2 */     if (rootdomainlength < minimum_root_domain_length)     {         printf("the root domain length (%d) less minimum length required (%d) in 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 not in proper format, please re-enter email address\n");         }     }     strcpy(emailaddress, tempemail); }  main() {     char emailaddress[email_char_array_size];      printf("please enter email address\n");     getandvalidateemailaddress(emailaddress);     printf("the email address entered %s\n", emailaddress); } 

c not have symbolic constants other enum-constants (which of type int). #define macro , part of preprocessor. textual replacement before actual c language compilation.

const qualified variables semantically still variables. qualifier guarantee programmer not change value. compiler may rely on guarantee. breaking contract invokes undefined behaviour, not enforced run-time environment.

static initialisers , array indexes @ file-level require constant expression, error. briefly, constant expression must yield constant value @ compile-time, cannot use variables.

note array parameter in function has similar problem. here can use variable length array. however, either can use empty length ([]) or pass length preceeding parameter explicitly; global variables don't work:

int isemailaddressproper(size_t len, const char emailaddress[len]) 

c++ different language similar syntax/grammar, , quite different semantics same grammar. cannot apply knowledge on language other.


Comments