-2

I'm trying to append a string, but whenever it gets to the strcat() function the program exits without printing the string.

I've been running this code from within Visual Studio as well as from its own .exe file and get the same result. Can anybody tell me what I'm doing wrong?

main()
{
    char str[100], slash = 'H';

    gets(str);

    printf("\n\n%s\n\n%i\n\n", str, strlen(str));

    strcat(str, slash);

    printf("\n%s", str);
}

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 6
    [Never use `gets`](https://stackoverflow.com/q/1694036/10077)!! – Fred Larson May 25 '22 at 15:49
  • 3
    Your [compiler can tell you](https://godbolt.org/z/sYfvqcrT8) what you are doing wrong. Perhaps you have told your compiler not to show you warnings? – Drew Dormann May 25 '22 at 15:49
  • 7
    `slash` has the wrong type. Characters are not strings. – Ian Abbott May 25 '22 at 15:50
  • 1
    `strlen(str)` returns a `size_t` which [must be printed using `%zu`](https://stackoverflow.com/q/940087/995714). Printing with the wrong format specifier invokes UB – phuclv May 25 '22 at 16:03

2 Answers2

1

char * strcat ( char * destination, const char * source ); this is a declaration of strcat(), so the second type should be a pointer to c-string, but you are passing a single character.

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

int main()
{
    char str[100], slash[] = "H";

    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';

    printf("\n\n%s\n\n%i\n\n", str, strlen(str));

    strcat(str, slash);

    printf("\n%s", str);

    return 0;
}

Just convert slash to be c-string instead of a single character.

Zakk
  • 1,935
  • 1
  • 6
  • 17
DimitrijeCiric
  • 446
  • 1
  • 10
  • 1
    You are a true hero, thank you so much. I had tried using 'strncat()' instead of 'strcat()' to just insert a character, but that didn't work. Your solution is working perfectly! Thank you so much again! – snakepliskin May 25 '22 at 16:21
1

The function gets is unsafe and is not supported by the C Standard. Instead you standard C function fgets.

The function strcat is declared the following way

char * strcat(char * restrict s1, const char * restrict s2);

That is it expects two arguments of the pointer type char * that point to string literal. However the variable slash is declared as having the type char

char str[100], slash = 'H';

Instead you should write the declaration the following way

char str[100], *slash = "H";

Now the variable slash has the pointer type char * and points to the string literal "H".

Pay attention to that you should guarantee that the array str has a space to accommodate the string literal.

Also to output the returned value of the function strlen you have to use the conversion specifier zu instead of i or d

printf("\n\n%s\n\n%zu\n\n", str, strlen(str));

Also bear in main that according to the C Standard the function main without parameters shall be declared like

int main( void )

Actually if you need to append only one character to a string then it can be done without the function strcat the following way

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

int main( void )
{
    char str[100], slash = 'H';

    fgets( str, sizeof( str ), stdin );

    size_t n = strcspn( str, "\n" );

    if ( n != sizeof( str ) - 1 )
    {
        str[n] = slash;
        str[++n] = '\0';
    }

    printf( "\n%s\n", str );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335