Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

472
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda