简体   繁体   中英

Remove the space from infront of my C String

I have some code that outputs like this:

void exec_prompt(char * usr_name, char * host_name)
{
    printf(" %s::%s\n", usr_name, host_name);
    return;
}

But the print out doesnt look as expected:

 geisst::ALPHA-DT2

There is that space at the front of the string.

The usr_name variable is passed from the main function and is returned from the getenv() function. The host_name variable is passed from the main function with the use of the following function:

char * returnHost()
{
    char hostname[1024];
    hostname[1023] = '\0';
    gethostname(hostname, 1023);

    return hostname;
}

Maybe the getenv() function adds a space?

Any help or advice is appreciated and please be nice :P

GeissT

The reason is that your format has a space: " %s::%s\\n"

Just change it to:

printf("%s::%s\n", usr_name, host_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM