C Language Array Declarations. Skill UP
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

One Dimensional (1D) Array Declaration in C Language

📑 On this page:
  • Introduction to 1D Array Declaration
  • Syntax of Array Declaration
  • Rules for Array Declaration
  • Examples of Array Declarations
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • How to declare one dimensional (1D) arrays in C language
  • The syntax for declaring integer, float, character, and double arrays
  • Important rules to follow when declaring arrays
  • Memory allocation and indexing concepts
  • Real-world examples of array declarations

Introduction to 1D Array Declaration

In this tutorial section, we will learn how to declare 1D arrays in C Language. Let's start to learn how to declare different types of arrays in C Language:

  • a. Integer Array — stores integer values
  • b. Float Array — stores floating-point numbers
  • c. Character Array — stores characters or strings
  • d. Double Array — stores double-precision numbers

Syntax of Array Declaration

data_type   array_name[array_size];

Rules for Array Declaration

We need to keep in mind the following rules while declaring arrays:

  1. While declaring an array, they must have a data type (int, float, char, double, etc.), variable name, and subscript [].
  2. An array variable must be declared before being used in a program.
  3. The size of the array is represented by the subscript [].
  4. If the size of an array is declared as 10, then programmers can store 10 elements.
  5. Each array element is stored in a separate memory location.
  6. The index of an array in C language always starts from 0.

📝 Note: If an array variable is declared as int n[10], then it ranges from 0 to 9.

Examples: Array Declaration in C Programming

Example:   int N[10];

The above declaration defines an array called N of size 10. This means N can store 10 integer values in contiguous memory locations. All elements in the array are of the same data type (int). The elements occupy consecutive memory locations, forming an ordered set of elements. Each element can be identified by its position or index in the array, also referred to as the subscript. The first element is at index position 0, and the nth element can be accessed at the (n-1)th index position.

The name of the array, N, represents the address of the first element (i.e., &N[0]) in the array.

1D array declaration diagram in C language

Declaring Single or One-Dimensional Arrays in C Programming

The following examples demonstrate how to declare 1D arrays of type char, float, and int in C programming.

In C, an array must be declared like any other variable before it is used in the program.

Syntax of Array Declaration:

data_type array_name[size];

i. Character Array Declaration

char a[5];

Here, char is the data type, a is the name of the array, and 5 is its size. The name a is followed by a pair of square brackets [] containing the array size. This array can hold 5 characters. The semicolon ; ends the statement.

ii. Float Array Declaration

float salary[5];

float is the data type, salary is the array name, and the size is 5. The array salary can store 5 floating-point numbers, e.g., 4569.98, 4567.90, etc.

iii. Integer Array Declaration

int srno[5];

int is the data type, srno is the name of the array, and the size is 5. This means the srno array can store 5 integer elements.

iv. Double Array Declaration

double balance[5];

Here, double is the data type and balance is the array name. This array can hold up to 5 double-precision numbers.

Array Declaration example c language

More Examples of Array Declarations in C Programming

1. Array of Pointers to Characters

extern char *name[];

The declaration above defines an array of pointers to char. Each element of the array name points to a character or a string.

2. Array of Structures

struct {
    float salary, bonus;
} employee[10];

This declares an array of structures. The array employee[10] has 10 elements, and each element is a structure containing two members: salary and bonus.

Example Program: Character Array Declaration and Initialization

/* Character array Declaration and initialization program */

#include <stdio.h>
#include <string.h>

int main()
{
    char vowels[] = {'a','e','i','o','u'};
    int c = puts(vowels);     // print array
    if(c > 0){
        printf("\nVowels printed successfully");
    }
    printf("\nLength of character array is = %d", strlen(vowels));
    return(0);
}

Output:

aeiou
Vowels printed successfully
Length of character array is = 5

Explanation:

  • The puts() function is used to print a character string or array to the output. Its prototype is available in the string.h header file.
  • The strlen() function is also present in string.h and is used to find the length of a string.

Common Questions About Declaring 1D Arrays in C

1. What does declaring a 1D array in C mean?

Declaring a 1D array in C means reserving a fixed block of memory that can store multiple values of the same data type. For example, when you write int n[10];, the compiler creates space for 10 integer values.

2. Why do we need to specify the array size during declaration?

The array size tells the compiler how many elements the array can store. Based on this size, memory is allocated. For example, float salary[5]; can store exactly five float values.

3. Why does array indexing start from 0 in C?

In C, array indexing starts from 0 because the first element is stored at the starting memory location of the array. So an array of size 5 has indexes from 0 to 4.

4. Can a 1D array store different data types?

No, a one dimensional array can store only one type of data. For example, an integer array stores only integers, and a character array stores only characters.

5. What happens if we access an index outside the array size?

Accessing an index outside the declared size may lead to incorrect output or runtime errors. That is why it is important to stay within valid index limits.

💡 Tip: Always ensure that you declare your array with the correct size and never access elements beyond the declared range.

📖 Related Tutorials

  • 1D Array in C
  • Initialize 1D Array in C
  • Accessing Array Elements in C
  • Read and Display 1D Arrays

Previous Topic: -->> 1D Array in C   ||   Next topic: -->> Initialize 1D Array in C


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.