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

330
Vistas
Understanding sizeof struct in C program

I have defined in my C program a struct as shown below:

typedef struct {
    long time_of_arrival;           // Number of minutes after the salon opening when the customer arrived
    char customer_name[21];         // Upto 20 characters long in all caps
    char stylist_name[21];          // Preferred stylist name, upto 20 characters long or NONE, in all caps
    long loyalty_points;            // The loyalty points customer holds
    long requested_haircut_time;    // The time corresponding to the haircut customer requested
    long loyalty_points_earned;     // The loyalty points customer earned
} customer;

I wish to create an array of structures of size per the number of customers as supplied by the user via command line argument.

To determine the amount of memory a single instance of the said structure would occupy in memory, I ran the following snippet of code:

int main(int argc, char const *argv[]) {
    printf("%ld\n", sizeof(customer));

    return 0;
}

which printed the following output:

80

However, in my limited understanding, the size should be 74 bytes, i.e. 8 bytes for each long variable and 21 x 2 bytes for the two character arrays.

Upon further debugging, (i.e. by creating a standalone array of 21 characters and checking its size using sizeof operator, which got reported as 21 bytes), I concluded that for some reason, the size of character array under the structure was getting rounded up to the multiple of 8 bytes.

I need help in understanding what phenomenon is responsible for this behaviour and what is it that I am missing here?

Note: I am building the C program using Apple clang version 13.0.0 (clang-1300.0.29.3) running on macOS 12.0.1 (21A559).

over 4 years ago · Santiago Trujillo
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