C Programming for Beginners : Length of an Array

Suraj Das
4 min readNov 11, 2021

--

Photo by Alina Grubnyak on Unsplash

Contents

Ternary Operator

Sizes

Length of an Array

Ternary Operator

It helps us write the if-else statement in just one line ✌️

Traditional if-else Statement Syntax

if-else Statement with Ternary Operator

Example

Let’s just say we want to compare two numbers and print the number which is the greater one.

Now, we could do the same thing within just one line like this :

Or if you wanted to print it rather than returning it, write like this :

This looks so cool 😃

Let’s now implement this into an actual program.

5

Sizes

Each of the data types consumes a particular set of memory. For example, the int data has a size of 4 bytes of memory.

We can print the size of each of each of the data types by using the sizeof() function.

4

There is also a specific range of values for each data type.

(This can be vary for different compilers)

Details are listed below in the program in form of comments.

Length of an Array

We know that char type takes 1 byte of memory.

1

Let’s try a string array.

error: array size missing in 'a'

It gave us an error saying the array size is missing.

Let’s set the size this time.

5

So we get to choose the size of an array.

Let’s store a string of 3 letters within this array and check whether it prints 3 of 5.

5

It stays at 5 😏

Lets store another string. But this time we won’t specify the size within those brackets.

4

It gives 4. But it should give 3 right ?
Don’t worry. We can use this feature ✌️

Moral of the story : The size of array depends on the number within those squared brackets or the value stored within it.

So the size is done. Now what about the length ?

The length of the above the above is 4(cuz there are three letters). And the size is 4 too.

For a string array, the length is same as the array size. But can be different for other types of arrays. For example an integer array.

But wait what’s an integer array ?

Like a string array is a set of chars; an int array is a set of integers.
The set is created with curly brackets.

Note : Do not forget to specify the data type of the array as int.

We can see the length of this array is 3 as it has three numbers.

But is the size same ?

No. The size is 12 because each integers holds 4 bytes and there are 3 integers.
So 4 times 3 is 12.

12

In case of string array, the size was the same the length because the string is a set of chars. And a char takes 1 byte of memory.

So how can we print the length of the integer array ?

Simple! We divide the size of array (12) with the size of first element (4). So we get three.

The size of the first element of the array is 4 cuz it is an integer type.

This is how we can get access to the elements of the array.

Printing length of the array :

Length is 3

Hope you enjoyed this blog.

Jeez! That was worth exploring 😃

Have any doubt ?
DM me on Instagram

Peace ✌️

Index of C Programming lessons :

  1. Getting Started within 5 minutes
  2. Learning Fundamentals Made Easy
  3. Circumference and Area of Circle
  4. scanf() vs fgets()
  5. Hypotenuse Calculator
  6. Learn by Building a Calculator
  7. Functions
  8. More on Functions
  9. Length of an Array
  10. String Functions
  11. The While Loop

--

--