Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

475
Visualizações
How to add text to a variable in C?

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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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...

over 4 years ago · Santiago Trujillo Relatório

0

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);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda