Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
C Array Declaration 
 

To declare an array in C, you have to specify the name of the data type and the number of elements inside the square brackets.

 

C Array Declaration

                         

In this section, you will learn how to declare an array in C.

To declare an array in C, you have to specify the name of the data type and the number of elements inside the square brackets.

Syntax for creating an array
    data_type [int size] varname;
    If the the size is not defined a default size of the array is taken by the compiler which is sufficient for most purposes.

        int arr[5];
In the above declaration, the arr is the array of integer type. The values inside this array arr or any array can be accessed with the array indexes. 

Syntax for accessing array value
   
         array_name[2];
With the above syntax value an array at index 2 will be accessed.

Similarly, you can declare the array of float type and char type. At the time of allocating values to arrays, the index reference always starts from 0 and counts till the n -1 th  index. Here n is the size of the array.

In the example an array arr is created that has capacity of 5 elements to store from index 0 to 4. The maximum size of the above array is 5. The values of this array can be stored and accessed in the following way:

arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

Now for printing the array values, printf()  function is used. 

<stdio.h>- This is the header file which includes standard input/output functions to be used in your program. It is necessary to include a header file as it has some inbuilt functions like printf(), scanf() etc. The angle brackets are used to indicate system include files.

<conio.h>- Its a non standard header file which is used for clrscr(), getch() functions.

void main() - This is the main function which should be coded first. Compiler will look for the program stuff to compile inside this function first. main function has default return type as integer therefore in the program code an integer return statement needs to coded at the end of the program. But after writing void its return type becomes void. This means method will not return any value.

clrscr()- It is used to clear the console screen. Use of this function is optional.

getch()- This function reads each character. It holds the console screen and program output on it. Without this output on the screen will generate in flash and vanishes.

Here is the code:

ARRAYDEC.C

#include <stdio.h>
#include <conio.h>
int arr[5];
void main() {
  arr[0]=10;
  arr[1]=20;
  arr[2]=30;
  arr[3]=40;
  arr[4]=50;
  clrscr();
  printf(
      "At index 0,value is:%d,"
      "\n At index 1,value is:%d,"
      "\n At index 2,value is:%d,"
      "\n At index 3,value is:%d,"
      "\n At index 4,value is:%d\n",
      arr[0], arr[1], arr[2], arr[3], arr[4]);
  getch();
}

Output will be displayed as:

ARRAYDEC.EXE

Download Source Code:

 

                         

» View all related tutorials
Related Tags: c string io variable rmi ria this length example to ini learn exam ear e il it section can li

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.