Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Using Character Arrays to Store Strings in C programming

August 25, 2013 Posted by: GPA Views: 683 0 comments

Problem

Using Character Arrays to Store Strings and write simple application

Solution

In this article we discuss how to to use character arrays for storing strings. A character array can be initialized using a string literal. For example: 

char mystring[] = "my string";

and you can initialize with individual character constants in an initializer list:

char mystring1[] = { 'm', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0' }; 

 

Example:

   

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
   // initialized using a string literal
   char mystring[] = "my string";
   // initialize with individual character constants in an initializer list:
   char mystring1[] = { 'm', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0' };
   //show result
   printf("%s",mystring);
   //using loop show mystring1 array
   printf("\n");
   int i;
   for(i=0;mystring1[i] != '\0'; i++){
         printf("%c",mystring1[i]);
   }
   printf("\n");
   system("PAUSE");
   return 0;
}

Output

Using Character Arrays to Store Strings in C

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe