strings 1

//strings are not a data type in c language
//string- array of characters terminated by null character('\0')
//we can create a character array in the following ways:
//1. char name[]="harish" here compiler will automaticaly take null character
//2. char name[]={'h','a','r','i','s','h','/0'} here we have to put null character('\0')
#include <stdio.h>
void printstr(char str[]) //function to print string
{
    int i=0;
    while(str[i]!='\0')
    {
        printf("%c",str[i]);
        i++;
    }
    printf("\n");
}
int main()
{
    char str1[]={'h','a','r','i','s','h','\0'}; // 1st way to giving value a string
    printstr(str1);
    char str2[]="shiva"; // 2nd way to giving value a string
    printstr(str2);
    return 0;
}

Comments

Popular posts from this blog

Steps taken by a compiler to execute a C program

Tokens in C

Variables and Data Types in C