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.

39,950 questions

51,892 answers

573 users

How to declare and initialize 1D (one dimensional) int array in C

3 Answers

0 votes
#include <stdio.h>
 
#define SIZE 3
#define LEN 5

void print(int arr[], int len);
 
int main(void)
{
    int n1[] = {1, 2, 3}; 
    int n2[4] = {1, 2, 3};
    int n3[3] = {0};
    int n4[SIZE] = {1, 2, 3};
    int n5[6] = {[4] = 13, [0] = 1, 2, 3, 4};
    int n6[LEN] = { 1, 3, 5, 7, 9, [LEN - 4] = 2, 4, 6, 8, 0};
    int n7[15] = { 1, 3, 5, 7, 9, [LEN - 3] = 2, 4, 6, 8, 0};
    
    print(n1, sizeof n1 / sizeof(int));
    print(n2, sizeof n2 / sizeof(int));
    print(n3, sizeof n3 / sizeof(int));
    print(n4, sizeof n4 / sizeof(int));
    print(n5, sizeof n5 / sizeof(int));
    print(n6, sizeof n6 / sizeof(int));
    print(n7, sizeof n7 / sizeof(int));
    
    return 0;
}

void print(int arr[], int len)
{
    for (int i = 0; i < len; i++)
        printf("%d ", arr[i]);
        
    printf("\n");
}

 
/*
run:

1 2 3
1 2 3 0
0 0 0
1 2 3
1 2 3 4 13 0
1 2 4 6 8
1 3 2 4 6 8 0 0 0 0 0 0 0 0 0

*/

 



answered Aug 24, 2016 by avibootz
0 votes
#include <stdio.h>
  
void print(int arr[], int len);
  
int main(void)
{
    int n = 1;
    int arr[3] = {n++, n++};
     
    print(arr, sizeof arr / sizeof(int));
     
    return 0;
}
 
void print(int arr[], int len)
{
    for (int i = 0; i < len; i++)
        printf("%d ", arr[i]);
         
    printf("\n");
}
 
  
/*
run:
 
1 2 0
 
*/

 



answered Aug 24, 2016 by avibootz
0 votes
#include <stdio.h>
  
void print(int arr[], int len);
  
int main(void)
{
    int arr[3] = {};
     
    print(arr, sizeof arr / sizeof(int));
     
    return 0;
}
 
void print(int arr[], int len)
{
    for (int i = 0; i < len; i++)
        printf("%d ", arr[i]);
         
    printf("\n");
}
 
  
/*
run:
 
0 0 0

*/

 



answered Aug 24, 2016 by avibootz
...