C Program Code Basics - Part IV

Submitted by Karthikeyan on

1.     Derived Types:
Arrays
Functions

Pointers

 

2.     Data Structures

Linked lists

Stacks

Queues

Trees

 

3.     Declaration of one-dimensional array
            type variable-name[size]

 

e.g.  char name[10]

 

4.     When declaring character arrays, we must allow one extra element space for the null terminator.

 

5.     Declaration of one-dimensional array
            type array_name[row_size][column_size]

 

e.g.  int table[2][3] =      {

{0,0,0},

{1,1,1}

};

           

 

6.     Multi-dimensional arrays

type array_name[s1][s2][s3]……[sm];

 

7.     The allocation of memory at compile time is known as static memory allocation and the arrays that receive static memory allocation are called static arrays.

8.     The process of allocating memory at run-time is known as dynamic memory allocation and the arrays created at run time are called dynamic arrays.

9.     Dynamic arrays are created using pointer variables and memory management functions malloc, calloc, and realloc.

10.                       Character string must end with a terminating null character. Since, a string is not a data type in C, but it is considered a data structure stored in an array. The string is a variable-length structure and is stored in a fixed-length array. The array size is not always represents the string size, so the last element of the array need not represent the end of the string. So we need some way to determine the end of the string. This is also known as “end of string” marker.

e.g. char city[10] = “NEW DELHI”

char city[10] = {‘N’,’E’,’W’,’ ‘, ‘D’,’E’,’L’,’H’,’I’,’\0’};

 

11.                       Reading string (Single word, i.e.,  terminated with white space)

e.g. char name[10];

scanf(“%s”, name);

 

12.                       Reading a line of text

char line[80];

scanf(“%[^\n]”, line);

printf(“%s”, line);

13.                       Getchar can also be used to read input

char line[50], character;

int ca = 0;

do{

character = getchar();

line[ca] = character;

c++;

}

 

 

14.                       More convenient method of reading a string of text containing whitespaces is gets (library function available in stdio.h)

gets(str);

char line[80];

gets(line);

printf(“%s”,line)

 

 

15.                       Copying strings

for(i=0; string2[i] != ‘\0’; i++)

            string1[i] = string2[i];

string1[i] = ‘\0’;

 

16.                       Using putchar

e.g. char ch = ‘a’;

       putchar(ch);

 

char name[6] = “PARIS”;

for (i=0, i<5; i++)

            putchar(name[i]);

putchar(‘\n’);

 

 

17.                       Using puts

puts(str);

 

 

18.                       Printing ASCII value

e.g.      x = ‘a’;

            printf(“%d\n”,x);

 

19.                       Testing upper case & lowercase

ch >= ‘A’ && ch <= ‘Z’

 

20.                       Converting string of digits into their integer values(include : <std.lib.h>

x = atoi(string);

                        char number = “2013”;

                        int year = atoi(number);

 

21.                       Concatenation of strings (without using library function)

            Each character has to be copied to new character array which holds enough space using for loop.

 

22.                       String – handling library functions.

strcat()

Concatenation:

strcat(string1, string2);

string2 is added to string1, string1 must be large enough to hold string2 together.

Nesting also possible:

strcat(strcat(string1, string2),string3);

Result will be stroed in string1.

 

strcat(string1, string2, n)

-         Concatenates left most n characters of str2 to str1.

strcmp()

Compare:

strcmp(string1,string2);

This function compares the given to string and has the value 0 if matches, else return a integer value that represents the difference of the ascii value of the given strings.

 

strcmp(str1, str2, n);

-         Compares left most n characters of str1 with str2.

strcpy()

Copy:

strcpy(string1, string2);

value of string2 assigned to string1.

e.g.

strcpy(city1, city2)

strcpy(city, “CHENNAI”);

 

strcpy(str1, str2, n);

n – integer value

copies first n characters from str2 to str1.

e.g. strcpy(s1, s2, 5);

strlen()

Find length of string:

n = strlen(string);

n – integer variable

strstr()

To locate sub-string:

strstr(s1,s2);

It searches the string s1 to see whether the s2 is contained in s1. If yes, returns the position of the first occurrence of the sub-string. Otherwise, it returns a NULL pointer.

strchr()

To locate a charater:

strchr(s1,’c’)

It searches the string s1 for the existence of the character ‘c’, if exists returns first occurrence of the character, else returns null pointer.