'Auto adds ascii 127 at the beginning of array

please have a look at the following c program. The program automatically adds the ASCII 127 i.e delete character at the beginning i.e index 0 of the array exp[].

Everything works fine, the array exp[] is as expected until the validate() function is called. while calling validate(exp) the ascii 127 is added to the exp[0]. Thus the strlen(exp) is increased by 1 inside the validate() function.


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

#define EXPL 10 //expression lenght

int validate(char *src, long len)
{
    printf("%ld but long len is: %ld\n", strlen(src), len);
    if(isdigit(*src))
        printf("%d, %c is digit\n", *src, *src);
    else
        printf("%d, %d not not digit\n", *src, '\0');

    while (*src != '\0'){
        if( 
            isdigit(*src)
            || *src == '(' || *src == ')' 
            || *src == '.' || *src == '+' 
            || *src == '-' || *src == '/' || *src == '*' 
            )
            src++;
        else{
            printf("\nExpression Contains Illegal Characters.\n");
            return 0;
        }
    }
    return 1;
}

int main(int argc, char const *argv[])
{
    //parse and store the expression for calculation
    char exp[EXPL+1]; // +1 is for null terminator \0

    int count = 0;
    int i = 1, j = 1;
    
    // check whether argument are supplied
    if(argc < 2)
        return 0;
    // check if the expression is within the limit i.e EXPL
    while(j < argc){
        count += strlen(argv[j]);
        ++j;
        }
    if(count > EXPL){
        printf("Expression is too long;\nMax %d digits.\nGiven %d\n ", EXPL, count);
        return 0;
    }

    for(i = 1; i < argc; ++i)
        strcat(exp, argv[i]);
  
    // printf("%ld is len. %s -  is array. \nand %d is at index 0\n", strlen(exp), exp, exp[0]);
 
    validate(exp, strlen(exp));
    return 0;
}

But if you uncomment the line 57 i.e printf("%ld is len. %s - is array. \nand %d is at index 0\n", strlen(exp), exp, exp[0]); The 4th last line, the program works fine. Nothing is changed, function validate() receives the correct array and program works as intended. But again if you remove that line or comment it, the problem repeats.

Following is the output while the line 57 commented

$./test 1234 5  6
7 but long len is: 7
127, 0 not not digit

Expression Contains Illegal Characters.

Even the argument strlen(exp) of validate() function, i.e 3rd last line returns incorrect length of array.

Following is the output with 4th last line uncommented printf("%ld is len. %s - is array. \nand %d is at index 0\n", strlen(exp), exp, exp[0]);

$./test 1234 5  6
6 is len. 123456 -  is array. 
and 49 is at index 0
6 but long len is: 6
49, 1 is digit

This is the expected output and here the code works fine but I don't want that printf() line there



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source