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,959 questions

51,901 answers

573 users

How to get the length of one dimensional int array (array[]) in C

2 Answers

0 votes
#include <stdio.h>
 
int main(void)
{
    int n[] = { 1, 2, 3, 4, 5 };
 
	printf("int size: %d\n", (int)sizeof(int));
	printf("Total size: %d\n", (int)sizeof(n));
    printf("n[0] size: %d\n", (int)sizeof(n[0]));
    printf("Array size: %d\n", (int)(sizeof(n)/sizeof(n[0])));
     
    return 0;
}
 
  
/*
    
run:
    
int size: 4
Total size: 20 // 4 x 5
n[0] size: 4
Array size: 5

*/

 



answered Jan 27, 2016 by avibootz
edited Jan 27, 2016 by avibootz
0 votes
#include <stdio.h>
 
int main(void)
{
    char arr[] = { 'a', 'b', 'c', 'd', 'e' };
 
	printf("char size: %d\n", (int)sizeof(char));
	printf("Total size: %d\n", (int)sizeof(arr));
    printf("arr[0] size: %d\n", (int)sizeof(arr[0]));
    printf("Array size: %d\n", (int)(sizeof(arr)/sizeof(arr[0])));
     
    return 0;
}
 
  
/*
    
run:
    
char size: 1
Total size: 5
arr[0] size: 1
Array size: 5

*/

 



answered Jan 27, 2016 by avibootz

Related questions

1 answer 143 views
1 answer 153 views
3 answers 259 views
3 answers 211 views
...