diff --git a/src/variable.cpp b/src/variable.cpp
index 2991f8c78a5784c37a7b0fcb11a4c89d76f71140..d77bc0e696c2414cb31202aa03ac3bdb055446cb 100644
--- a/src/variable.cpp
+++ b/src/variable.cpp
@@ -4589,18 +4589,19 @@ char *Variable::find_next_comma(char *str)
 
 
 /* ----------------------------------------------------------------------
-   debug routine for printing formula tree recursively
+   helper routine for printing variable name with error message
 ------------------------------------------------------------------------- */
 
 void Variable::print_var_error(const char *srcfile, int lineno,
                                const char *errmsg, int ivar)
 {
-  const char *varname = (const char*)"(unknown)";
-  if ((ivar >= 0) && (ivar < nvar)) varname = names[ivar];
+  if ((ivar >= 0) && (ivar < nvar)) {
+    char msg[128];
 
-  char msg[128];
-  sprintf(msg,"Variable %s: %s",varname,errmsg);
-  error->all(srcfile,lineno,msg);}
+    sprintf(msg,"Variable %s: %s",names[ivar],errmsg);
+    error->all(srcfile,lineno,msg);
+  } else error->all(srcfile,lineno,errmsg);
+}
 
 /* ----------------------------------------------------------------------
    debug routine for printing formula tree recursively