Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to initialize struct in C

3 Answers

0 votes
#include <stdio.h>

typedef struct {
    int n;
    char ch;
    int iarr[5];
    char carr[7];
} data;

int main() {
    data d = { 4, 'a', {6, 8, 2, 9, 3}, "c pro" };
    
    printf("%d\n", d.n);
    printf("%c\n", d.ch);
   
    for (int i = 0; i < 5; i++) {
         printf("%d ", d.iarr[i]);
    }
    
    printf("\n%s\n", d.carr);
}



/*
run:

4
a
6 8 2 9 3 
c pro

*/
    

 



answered Dec 5, 2020 by avibootz
0 votes
#include <stdio.h>
  
typedef struct {
    int age;
    float salary;
} record;
  
int main(int argc, char **argv) 
{ 
    record st = {47, 12300};
  
    printf("age = %d\n", st.age);
    printf("salary = %.2f\n", st.salary);
      
    return 0;
}
  
  
  
/*
run:
  
age = 47
salary = 12300.00
  
*/

 



answered Apr 17, 2024 by avibootz
edited Apr 17, 2024 by avibootz
0 votes
#include <stdio.h>
 
struct user
{
    char name[30];
    int age;
} vip = {"tom", 51};
 
int main(int argc, char **argv) 
{ 
    printf("vip name: %s\n", vip.name);
    printf("vip age: %d\n", vip.age);
     
    return 0;
}


 
/*
run:
 
vip name: tom
vip age: 51
 
*/

 



answered Apr 17, 2024 by avibootz

Related questions

1 answer 108 views
1 answer 112 views
1 answer 93 views
1 answer 124 views
...