C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Arrays

What is Array

Arrays are one of a kind of data structure because arrays defines the way of arranging the data, which allows us to manipulated the data in interesting ways.Array is a collection of data, which is very similar to a matrix but a difference is that array can hold only data of similar datatypes i.e) We could have an array of intergers or an array of characters or an array of floating point. It reduces the programmers task.

C Arrays
3 x 3
1  1  1
1  1  1
1  1  1
3 x 3
0.3  0.2  0.1
0.2  0.3  0.2
0.1  0.1  0.3
3 x 3
a  b  c
d  e  f
g  h  i

Syntax - Array

Syntax
data_type arrayname[index];
  • An array variable can be declared as any standard data type.
  • The index value is size of an array.
  • An array index always starts from 0.

Valid Array declaration

Here we go through some valid array declaration.

  • int arr[5]    = {1, 2, 3, 4, 5};
  • char arr[5] = {'a', 'b', 'c', 'd', 'e'};
  • char arr[5] = {'a', 98, 'c', 100, 'e'};
  • float arr[3] = {1.0, 2.5, 3.9};

Invalid Array declaration

Here we go through some invalid array declaration.

  • char arr[5] = {a, b, c, d, e};
  • int arr[5] = { '1', '2', '3', '4', '5'}
  • char arr[2] = {"Invalid", "array"} ;

Why Array

Array is most useful when programmers need to declare multiple variable of same type to store similar data(e.g. marks of 20 students). For example, if a user wish to store marks of 5 students, then he might have to declare 5 different variables to store each student's mark. This scenario will be tough, if the user has to store 10 or more students marks. So, c provides the alternative one named array.

C program - Without Using Arrays

Let's workout a program to demonstrate Arrays in C.

noarray.c
#include <stdio.h>
int main()
{
int s1, s2, s3, s4, s5;
printf ("Enter students marks details ");
printf ("\ns1 = ");
scanf ("%d", &s1);
printf ("\ns2 = ");
scanf ("%d", &s2);
printf ("\ns3 = ");
scanf ("%d", &s3);
printf ("\ns4 = ");
scanf ("%d", &s4);
printf ("\ns5 = ");
scanf ("%d", &s5);
printf ("\n---Students marks details---\n ");
printf ("s1 = %d\n", s1);
printf ("s2 = %d\n", s2);
printf ("s3 = %d\n", s3);
printf ("s4 = %d\n", s4);
printf ("s5 = %d\n", s5);
return 0;
}
  • Enter students marks details
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95
  • ---Students marks details---
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95

Note:

The above program reads and prints the marks of all the 5 students by declaring 5 variables to hold each student's mark. So, the program uses many statements.

C program - Using Arrays

Let's workout a program to demonstrate Arrays in C.

array.c
#include <stdio.h>
int main()
{
int s[5], i;
printf ("Enter students marks details ");
for(i = 0; i < 5; i++)
{
printf ("\ns%d = ", i + 1);
scanf ("%d",&s[i]);
}
printf ("\n---Students marks details--- ");
for(i = 0; i < 5; i++)
{
printf ("\ns%d = %d ", i + 1, s[i]);
}
return 0;
}
  • Enter students marks details
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95
  • ---Students marks details---
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95

Note:

In the above program the 's' array name with subscript of 5. The same output with less number of statements in a program even we have to store 1000 students marks.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report