Home > database >  How to add text to a variable in C?
How to add text to a variable in C?

Time:03-07

I'm learning C at the moment and I run into some problem, hard to understand what is going on with variables and adding text to variable in C.

I know C is not treating the strings and character as in other programming language. If I understand it correctly from my book: I have to define a variable before I can use it, that's ok for me. So, If I do this code, where I wish to print the variable text_1 it is failing:

#include <stdio.h>

int main()
{
    char text_1[];
    text_1[] = "Testing";
    printf("Test 1 is: %s", text_1);
return(0);
}

But if I do this way, this is working:

#include <stdio.h>

int main()
{
    char text_1[] = "Testing";
    printf("Test 1 is: %s", text_1);
return(0);
}

In some other programming language I can do this way: Dim a as string

a="Testing"
print("Testing 1 is:", a) --> or similar option to print out the variable 'a'.

What is the correct way to do this in C?

Thank you.

CodePudding user response:

Oops... C language is a rather low level language if you compare it to Java, Python or Ruby, or even Basic or JavaScript:

  • it has no notion of text string but only uses null terminated character arrays in its standard library
  • arrays by themselves are not first class citizens: except at initialization time, the language can only process arrays elements and not the full array

Long story made short, an array has a size that is defined only once (at definition time) and will never change during the life time of the array. char text_1[] = "Testing"; is an idiomatic initialization: the size of the array is set to the number of characters of the literal string 1 for the terminating null, so here 8. After that text_1 will be able to contain other strings of at most 7 characters 1 terminating null if you copy the relevant character for example with strcpy.

To go back to your code, char text_1[]; declares an incomplete array with a declared size of 0 bytes. That means that you cannot use it. The 2 correct ways would be:

char text_1[] = "Testing";   // idiomatic initialization of a 8 characters array

or

char text_1[8];              // definition of an uninitialized char array of size 8
strcpy(text_1, "Testing");   // copy a string into the array

Not really sexy, but C language is like that...

CodePudding user response:

Ok, for conclusion, what is the correct way to learn how to set up a variable with string in a way I can print it to the screen or use it for compare with other variables who are also holding strings?

I like this way but I'm not sure is it correct to use:

char * text_1;  // btw. what is this `*` meaning here?
text_1 = "Some text...";
printf("Variable text is: %s\n",text_1);

but this way is also acceptable by me:

char text_1[n];  // where n is a number of max char 
strcpy(text_1, "Some text...");
printf("Variable text is: %s\n",text_1);

CodePudding user response:

text_1[] is an array and therefore won't output correctly just adding it to a printf statement. One simple way you could do it (but just a more understandable way as your just starting out) would be to have a loop that concatenates the characters in the char array into a string and then output the string

  •  Tags:  
  • c
  • Related