Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » C Language » Array in C programming | Character Array in C

  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड
    Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड Spiritual
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar
  • Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी
    Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी Biography
  • Ohms law Definition, Formula, Statement
    Ohms law Definition, Formula, Statement | ओम का नियम Science
  • Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म
    Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म Spiritual
  • Nelson Mandela Biography in Hindi | Nelson Mandela Day
    Nelson Mandela Biography in Hindi | Nelson Mandela Day Biography
  • Sunder Kand in Hindi | Hanuman Kand | हनुमान जी का सुंदरकांड
    Sunder Kand in Hindi | Hanuman Kand | हनुमान जी का सुंदरकांड Spiritual

Array in C programming | Character Array in C

Posted on November 19, 2021November 22, 2021 By GeekCer Education No Comments on Array in C programming | Character Array in C
Array in C programming | Character Array in C

A collection of similar types of objects stored in sequential memory locations under a same header or variable name is referred to as an array in C. Elements are the individual values in an array.

In simple words, what is an array in C? In C, an array is a collection of elements of the same type that are stored in memory in a sequential order.

Table of Contents

  • Advantages of using Array in C
  • Limitations of Array in C
  • In C, how do you declare arrays?
  • Array Types in C Programming
    • Single Dimensional Array or 1D Array
      • Syntax of 1D Array
      • Example of 1D Array
      • 1D Array Initialization
      • Example of 1D Array Initialization
      • Write a program of one dimensional Array
    • Two Dimensional Array or 2D Array
      • Syntax of 2D Array in C
      • Example of 2D Array
      • Write a program of two dimensional Array
  • Character Array in C
    • Syntax of Character Array in C
    • Example of Character Array in C
    • Character Array Initialization
    • Example of Character Array Initialization
    • Reading string from the keyboard and display on screen
  • getchar() function in C
  • putchar() function in C

Advantages of using Array in C

  • It is simple to visit all of the items or to move from one element to another.
  • Sorting, searching, reversing, and other data structure operations are also feasible.
  • Multiple names are more difficult to recall than a single name.

Limitations of Array in C

What are Array’s limitations? The following information must be provided to the compiler:

  • You can’t change the size of an array once it’s been constructed because it’s static.
  • At runtime, memory cannot be allocated. Array allocates memory at compile time.
  • Only elements of the same kind are stored in an array unused memory cannot be used while using an array.

In C, how do you declare arrays?

When declaring the array, you must include the following information.

  • The array’s type (int, float, char etc.)
  • Name of the Array
  • The array’s number of subscripts (Dimensioning the array, whether it is 1D or 2D)
  • You have to assign the the total number of memory locations.

Array Types in C Programming

  • Single Dimensional Array
  • Multiple Dimensional Array

Single Dimensional Array or 1D Array

A single dimensional array represents a single row or column of elements.

Syntax of 1D Array


storage_class data_type array_name[index]; 

Example of 1D Array


int intArray[10];
float salary[20];
char name[100];

1D Array Initialization


storage_class data_type array_name[index] = {element1, element2, element3... }; 

Example of 1D Array Initialization


int intArray[5] = {10, 20, 30, 40, 50};
// Representation in the memory
intArray[0]=10;
intArray[1]=20;
intArray[2]=30;
intArray[3]=40;
intArray[4]=50;

Write a program of one dimensional Array

                  #include <stdio.h>
int main() 
{
  int i;
  int a[5] = {10, 20, 30, 40, 50};
  printf("Elements of array: \n");
  for(i = 0; i < 5; i++)
  {
    printf("%d  ", a[i]);
  }
  return 0;
} 

Output:
Elements of array:
1  2  3  4  5

Two Dimensional Array or 2D Array

The two-dimensional array represents more than one row and column of elements.

Syntax of 2D Array in C


storage_class data_type array_name[rows][cols]; 

Example of 2D Array


int intArray[10][10];
float salary[20][3];

Note:
The number of elements in a two-dimensional array is determined by the array's size. There will be 3 * 3 = 9 elements in the arr[3][3] array.

Write a program of two dimensional Array

                  #include <stdio.h>
int main() 
{
  int a[10][10];
  int i, j, row = 3, col = 3;
  printf("Enter array elements: ");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      scanf("%d", &a[i][j]);
    }
  }
  printf("Output of array elements\n");
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
    {
      printf("%d  ", a[i][j]);
    }
    printf("\n");
  }
  return 0;
}

Output:
Enter array elements: 1
2
3
4
5
6
7
8
9
Output of array elements
1  2  3
4  5  6
7  8  9
Note:
The storage class is optional in the declaration, and it can be one of the variable's scopes, such as automatic, external, static, or register.

Character Array in C

Character arrays are used to declare alpha numeric characters in programming languages. This array can include both single characters and single integers.

Syntax of Character Array in C


storage_class char_data_type array_name[index]; 

Example of Character Array in C


char name[100];
char alphbets[26];

Character Array Initialization


storage_class char_data_type array_name[index] = {element1, element2, element3... }; 

Example of Character Array Initialization


char vowels[] = {'a', 'e', 'i', 'o', 'u'};
// Representation in the memory
vowels[0]='a';
vowels[1]='e';
vowels[2]='i';
vowels[3]='o';
vowels[4]='u';

char gender[2] = {'M', 'F', '\0'};
char site[7] = "GeekCer";

Reading string from the keyboard and display on screen

                  #include <stdio.h>
int main() 
{
  char site[7];
  printf("Enter website name: \n");
  scanf("%s", site);
  printf("Website name is %s", site);
  return 0;
} 

Output:
Enter website name:
GeekCer
Website name is GeekCer

getchar() function in C

To read a single character from the keyboard, use the getchar() method.

                  #include <stdio.h>
int main() 
{
  char ch;
  printf("Enter any character: ");
  ch = getchar();
  printf("Entered character is %c", ch);
  return 0;
} 

Output:
Enter any character:G
Entered character is G

putchar() function in C

To print a single character on the screen, use the putchar() function.

                  #include <stdio.h>
int main() 
{
  char ch;
  printf("Enter any character: ");
  ch = getchar();
  printf("Entered character is ");
  putchar(ch);
  return 0;
} 

Output:
Enter any character: G
Entered character is G

This page discusses the several types of arrays, including one-dimensional and two-dimensional arrays. We’ve also talked about the character array, which is the most significant topic for programmers.

Click here to visit this page which will provide you programs related to arrays, character arrays, and other topics.

If you have any questions or concerns, please email us at geekcer.code@gmail.com. Or, you can write us on the contact us page.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language Tags:2d array in c, How do you initialize an array in C?, how to declare array in c?, What are the types of arrays?, What is an array in C with example?, What is array explain with example?, What is array in C and its types?, What is array in programming language?, What is array syntax?, What is proper way to initialize array?

Post navigation

Previous Post: Statements in C Language | Types of statements in C Language
Next Post: String Array in C | What is an array of string | String Functions

More Related Articles

Storage classes in C with examples (auto, extern, static, register) Storage classes in C with examples (auto, extern, register, static) C Language
Programming with C Language Tutorial Programming with C Language Tutorial C Language
Constants and Operators in C Language Constants and Operators in C Language C Language
Structure in C program with example | Nested structure in C Structure in C program with example | Nested structure in C C Language
Pointers in C Programming, Definition, Types & Example Pointers in C Programming, Definition, Types & Example C Language
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • C Home
  • Constants and Operators
  • Statements in C Language
  • Array in C
  • String Array in C
  • Functions in C
  • Pointers in C
  • Structure in C
  • Storage Classes
  • File Handling in C
  • Top C Programs
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved