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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,111 questions

40,781 answers

573 users

How to declare and initialize an array in C

5 Answers

0 votes
#include <stdio.h>

int main(void) 
{
    int array[5] = {0};

    size_t size = sizeof(array) / sizeof(array[0]);
    
    for (int i = 0; i < size; i++)
        printf("%3d", array[i]);
    
    return 0;
}
   
/*
run:

  0  0  0  0  0

*/

 





answered Aug 9, 2017 by avibootz
0 votes
#include <stdio.h>

int main(void) 
{
    int array[5] = {1, 2, 3, 4, 5};

    size_t size = sizeof(array) / sizeof(array[0]);
    
    for (int i = 0; i < size; i++)
        printf("%3d", array[i]);
    
    return 0;
}
   
/*
run:

  1  2  3  4  5

*/

 





answered Aug 9, 2017 by avibootz
0 votes
#include <stdio.h>

int main(void) 
{
    int array[5] = {[2] = 98, [4] = 13, [1] = 37};

    size_t size = sizeof(array) / sizeof(array[0]);
    
    for (int i = 0; i < size; i++)
        printf("%3d", array[i]);
    
    return 0;
}
   
/*
run:

  0 37 98  0 13

*/

 





answered Aug 9, 2017 by avibootz
0 votes
#include <stdio.h>

int main(void) 
{
    int array[] = {[2] = 98, [5] = 13, [1] = 37};

    size_t size = sizeof(array) / sizeof(array[0]);
    
    for (int i = 0; i < size; i++)
        printf("%3d", array[i]);
    
    return 0;
}
   
/*
run:

  0 37 98  0  0 13

*/

 





answered Aug 9, 2017 by avibootz
0 votes
#include <stdio.h>

int main(void) 
{
    int array[] = {1, 2, 3, 98};

    size_t size = sizeof(array) / sizeof(array[0]);
    
    for (int i = 0; i < size; i++)
        printf("%3d", array[i]);
    
    return 0;
}
   
/*
run:

  1  2  3 98

*/

 





answered Aug 9, 2017 by avibootz

Related questions

1 answer 80 views
1 answer 86 views
4 answers 134 views
...