Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
How to check for empty string in PHP ?
Next article icon

How to Check if Empty Array in C?

Last Updated : 09 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite: Array in C

The array is a type of Data-structure that can store homogeneous data-type elements. The size of the array is fixed.

Syntax:

int arr[N];

here, the data type of the array is an integer, the name of an array is arr, and the size of the array is N.

Example:

C
// C Program to declare an // array and check it's size #include <stdio.h>  int main() {     int arr[5] = { 1, 2, 3, 4, 5 };      printf("Size of the array: %d bytes\n", sizeof(arr));      return 0; } 

Output
Size of the array: 20 bytes

In the above program, we have created an array with some elements in it. This is the most common way of creating an array. Then, we used the sizeof() operator to print the size of the array. To know more about the sizeof() operator refer to sizeof() in C. 

The size of a data type varies from machine to machine. On our machine, the int data type consists of 4 bytes and the above array has 5 elements inside it, so the total size of the array is 5 x 4 = 20 bytes.

Now, we will create an array by specifying only its size. We will not insert any elements into it. Look at the below program:

Example:

C
// C Program to check // above condition #include <stdio.h>  int main() {     int arr[5] = {};      printf("Size of the array: %d bytes\n", sizeof(arr));      return 0; } 

Output
Size of the array: 20 bytes

The above output says that the size of the array is 20 bytes.

Reason: The reason is that we have mentioned the size of the array. So, the compiler automatically creates an array that can hold 5 values, despite the fact that we haven't declared any values yet. In this case, some pre-determined values are put into the array to fill its positions. That's why the output shows that the array has a size of 20 bytes. It's just the fact that we haven't inserted any values yet.

Empty Array:

An array can be said only when the size of the array is 0. And it is only possible during initialization. So, let's check how to check when an array is empty.

Example:

C
// C Program to check array is // empty or not #include <stdio.h>  int main() {     // Method1 to declare empty array     int arr1[] = {};      // Method2 to declare empty array     int arr2[0];      printf("Size of the array1: %d bytes\n", sizeof(arr1));     printf("Size of the array2: %d bytes\n", sizeof(arr2));      return 0; } 

Output
Size of the array1: 0 bytes Size of the array2: 0 bytes

Method 1: Here, we have declared an array but haven't specified its size or inserted any elements into it. This means that the compiler doesn't know either the size of the array or its elements. So, it can't use any of them to make the array have a particular size. As a result, the array hasn't got any space to contain elements, which is why the above output shows that the size of the array is 0 bytes.

Method 2: As the size of the array is already 0 so no memory is allocated to it. So, it is also empty


Next Article
How to check for empty string in PHP ?

S

sriparnxnw7
Improve
Article Tags :
  • Technical Scripter
  • C Language
  • Technical Scripter 2022
  • C-Arrays

Similar Reads

  • How to Empty a Char Array in C?
    Prerequisite: Char Array in C Char arrays are used for storing multiple character elements in a continuous memory allocated. In a few questions, we have the necessity to empty the char array but You can’t clear a char array by setting the individual members to some value indicating that they don’t c
    4 min read
  • How to check for empty string in PHP ?
    In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not. here we have some common approaches Table of Content Using empty() func
    4 min read
  • Check if an array is empty or not in JavaScript
    These are the following ways to check whether the given array is empty or not: 1. The length Property - mostly usedThe length property can be used to get the length of the given array if it returns 0 then the length of the array is 0 means the given array is empty else the array have the elements. [
    1 min read
  • Check if an Array is Empty or not in TypeScript
    In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs
    2 min read
  • How to check if a value exists in an Array in Ruby?
    In this article, we will discuss how to check if a value exists in an array in ruby. We can check if a value exists in an array through different methods ranging from using Array#member? method and Array#include? method to Array#any? method Table of Content Check if a value exists in an array using
    3 min read
  • How to check if an Array contains a value or not?
    There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b
    3 min read
  • How to Check Whether a Variable is Empty in PHP?
    Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article,
    5 min read
  • How to check an element is exists in array or not in PHP ?
    An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
    2 min read
  • How to Remove Empty String from Array in Ruby?
    In this article, we will learn how to remove empty strings from an array in Ruby. We can remove empty strings from an array in Ruby using various methods. Table of Content Remove empty string from array using rejectRemove empty string from array using selectRemove empty string from array using map a
    2 min read
  • How to declare a Two Dimensional Array of pointers in C?
    A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences