C String

Strings is an combination of alphabets, letters characters and symbols. C programming language are implement string using an character array. That are stored in form of 1d character array, and '\0' are indicates end of string.

C string

There syntax as follows.

char variable_name[size];
Iteam overview
char keyword
variable_name Name of string
size size in form of number of character
Access specifier %s

We can define C strings like this.

char name[]="CODE";
char store[5]={'C','O','D','E','\0'};
char data[]={'C','O','D','E','\0'};

For example.

/*Example of c strings*/
#include <stdio.h>
int main(){
  //declare and initialize of string array elements
  char data[]="CODE";
  printf("%s",data);//print string
}
C string representation

Note that when need to print all element of character array, then use %s specifiers and provide array name to printf() function.

Output

CODE

Passing strings to function

In case passing string (char array) to another function, Compiler is set address of one array to another array. see this example.

/*Example of c strings*/
#include <stdio.h>
void display(char arr[]){
  //print data
  printf("%s",arr);
}
int main(){
  //declare and initialize of string array elements
  char data[]="Hello Programmer";
  display(data);//passing array
}
Passing String
Hello Programmer

Suppose that in case passing array are modified (change array element value). That changes is reflected to both array element. Because both array are use same memory location.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved