How to initialize a structure in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

typedef struct {
  char str[16];
  int n;
  float f;
} STExample;

int main() {
  STExample a = { "abc", 9098, 3.14f }; 
  printf("%s %d %f\n", a.str, a.n, a.f);

  STExample b = { "", 8472, 8.912f }; 
  strcpy(b.str, "xyz");
  printf("%s %d %f\n", b.str, b.n, b.f);

  return 0;
}



/*
run:

abc 9098 3.140000
xyz 8472 8.912000

*/

 



answered Jun 7, 2024 by avibootz

Related questions

2 answers 220 views
220 views asked Jun 9, 2015 by avibootz
2 answers 155 views
1 answer 166 views
1 answer 134 views
1 answer 131 views
131 views asked Aug 23, 2021 by avibootz
1 answer 151 views
151 views asked Aug 22, 2021 by avibootz
...