char another_dt[21];
EXEC SQL VAR another_dt IS STRING(21);
<b>
EXEC SQL BEGIN DECLARE SECTION;
varchar dt[21];
EXEC SQL END DECLARE SECTION;
</b>
strcpy(user,"XXXXX@SIDMY.WORLD");
strcpy(passwd,"XXXXX");
EXEC SQL WHENEVER SQLERROR DO print_error_msg();
EXEC SQL WHENEVER SQLWARNING DO print_warning_msg();
EXEC SQL CONNECT :user IDENTIFIED BY :passwd;
memset(dt.arr,' ',21);
EXEC SQL SELECT TO_CHAR(sysdate,'DD/MON/YYYY HH24:MI:SS') into :dt from dual;
EXEC SQL SELECT TO_CHAR(sysdate,'DD/MON/YYYY HH24:MI:SS') into :another_dt from dual;
dt.len = strlen(dt.arr);
printf("Date is :%sn",dt.arr);
printf("Another Date is :%sn",another_dt);
You can see that after pre-compilation phase, the varchar declaration is transformed to structure like this. Everytime you have to reset the memory ( or null terminate) using memset.. as there is a possbility of garbage everytime.. Better to use dataype equivalencing ( see use of variable another_dt )
/* varchar dt[21]; */
struct { unsigned short len; char arr[21]; } dt;