Technical Training
C TutorialsTable of Contents
C Programming - Data Types : Part 1
C Programming - Data Types : Part 1 - Page 2
C Programming - Data Types : Part 1 - Page 3
C Programming - Data Types : Part 1 - Page 4C Programming - Data Types : Part 1 Page - 4
C Programming - Data Types : Part 1
Derived types
Derived types are types constructed from the ones we've seen so far in this tutorial. There are three main derived types: arrays, structures and unions.
Arrays
You have probably seen arrays in other programming languages. Arrays are one or more instances of one type. You declare arrays with a type name followed by variable name and then the size of the array in brackets:
- char name[50];
- long long num_array[256];
- float m[2][4];
The first two lines declare name as an array of 50 characters and num_array as an array of 256 long longs. The third line declares m as a 2-dimensional array of floats, like a matrix with 2 rows and 4 columns.
In C you cannot assign to arrays except when they are first declared or if they are members of a struct. So copying an array like this:
- int arr1[4] = { 1, 2, 3, 4 };
- int arr2[4];
- arr2 = arr1;
will not work. There are ways around this, like using a for loop to copy each element of the array, or using memcpy().
Array elements are accessed using the [] operator, and elements are numbered starting at 0. It is also possible to declare variable sized arrays (if they meet certain conditions). Let's see some examples of using arrays:
- #include <stdio.h>
- #include <stdlib.h>
- void main()
- {
- int numarr[ 4 ] = { 12, 92, 54, 890 };
- int sz;
- char buff[64];
- float m[2][4] = { { 1.1, 1.2, 1.3, 1.4 }, { 2.1, 2.2, 2.3, 2.4 }};
- printf( "numarr's 3rd element is %d\n", numarr[2] );
- printf( "m's 2nd row's 4th column value is %f\n", m[1][3] );
- printf("Enter size of variable sized array: ");
- fgets( buff, 64, stdin );
- sz = atoi(buff);
- int arr2[ sz ];
- int i;
- for( i = 0; i < sz; ++i )
- arr2[ i ] = i+1;
- for( i = 0; i < sz; ++i )
- printf("arr2[%d] = %d\n", i, arr2[ i ]);
- }
The program's output follows:
numarr's 3rd element is 54 m's 2nd row's 4th column value is 2.400000 Enter size of variable sized array: 5 arr2[0] = 1 arr2[1] = 2 arr2[2] = 3 arr2[3] = 4 arr2[4] = 5
On lines 6 and 9 are examples of initializing an array when you declare it. The initial values are inside the { } symbols separated by commas. On line 9, each of the two rows of m is initialized in two separate { } initializers. Lines 11-12 show that to access element N of an array you use index N-1 since the first element has index 0. The third element of numarr is accessed with index [2] and the second row and fourth column of m is accessed with index [1][3].
Line 18 declares a variable sized array. All the other arrays in this program are constant size - their sizes never change and can be calculated at compile time. The arr2 array's size depends on the number the user enters when the program is run. On this run the user enters 5 so it is 5 elements long. Lines 20-21 initialize the array so that the element at index 0 gets the number 1, element at 1 gets the number 2, and so on. Lines 23-24 print out the elements.
Arrays will be discussed further in another tutorial.
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







