Home > OS >  use a string and output of function in c
use a string and output of function in c

Time:12-29

I know that is possible to initialize a variable with multiple strings:

static const char *str = "hello\n"
                         "world!";

But I want to have the string output of a function into the string like this:

static const char *str = "hello\n"
                         foo()
                         "world!";

It is possible with the #define directive btw

#define STRING "some string"

static const char *str = "hello\n"
                         STRING
                         "world!";

Thanks for your help :)

I couldn't find anything about it. Only the #define directive.

But even I tried to call the function from #define, and I'm only able to print the variable alone.

CodePudding user response:

Unfortunately, this can't be as simple as this because your first and last example use constant string that are known at compilation time, which enables the compiler to form one long single string just as if you had enclosed all of them inside a single pair of double quotes " ".

In C, strings are represented by pointers onto their first character, directly where they lie in memory, those string being terminated with a null code '\0' (which resolves into a byte set to zero : 0x00).

Since there is no dedicated, managed object to handle strings, you need to allocate some space somewhere, then filling it with strncpy() or strncat().

However, if your function directly returns a pointer toward a string that already exists somewhere, you may just ask printf to compose it for you:

printf("Hello %s world.", foo());

CodePudding user response:

That's impossible. What you call "initialize with multiple strings" isn't actually multiple strings. You're just building one large string literal (a compile-time constant explicitly written down) from multiple string literals.

Your preprocessor macro is just that: macro that gets replaced before the compiler even runs. Macros do not "exist" as code at runtime.

No. You need to use string concatenation. You'll have to call strlen on all strings you want to combine to get the individual lengths, add them up, allocate memory large enough for the sum 1 using malloc, then use strcat or strncpy to build the complete string.

C is not an intuitive, easy, safe or comfortable language when it comes to dealing with strings. Making mistakes when doing such string combinations and modifications is still today one of the, if not the most common source of security vulnerabilities. My private two cents here are: if you need to handle a lot of strings, use a programming language that has a proper string type, like rust, C , go, python...

CodePudding user response:

"hello\n" "world!" is not concatenating the string. It is only a syntactic sugar when you define a string literal.

All "parts" of this string literal have to be known compile time.

you can use #define as macro definitions are textually replaced by the preprocessor before the actual compilation starts

#define H "Hello"
#define N "\n"
#define W "world"

char *x = H N W;
char *y = H
          N
          W;

After the preprocessing the compiler will compile:

char *x = "Hello" "\n" "world";
char *y = "Hello"
          "\n"
          "world";

https://godbolt.org/z/rb713c3c7

CodePudding user response:

Like everyone has said, string literal is handled by preprocessor at compile time and you need a runtime call to allocate memory and fill the memory with the string returned from the foo function. Here is some code to examine furhter.

The function asprintf allocates the memory and fills the value using formatted string literal. More on asprintf

And always remember to free the string memory allocated by asprintf.

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

#define STRING "some string"
char* foo() { return "some string"; }

int main()
{
  static const char *str1 = "hello\n" "world!";
  static char *str2;
  static const char *str3 = "hello\n" STRING "world!";
  printf( "str1\n%s\n", str1 );
  if ( asprintf( &str2, "hello\n%sworld!", foo() ) >= 0 )
  {
    printf( "str2\n%s\n", str2 );
    free(str2);
  }
  printf( "str3\n%s\n", str3 );
}
  •  Tags:  
  • c
  • Related