Passing Strings to Function
As strings are character arrays, we can pass strings to functions in the same way we pass an array to a function. Below is a sample program to do this:
C
// C program to illustrate how to // pass string to functions #include <stdio.h> void printStr( char str[]) { printf ( "String is : %s" , str); } int main() { // declare and initialize string char str[] = "w3wiki" ; // print string by passing string // to a different function printStr(str); return 0; } |
Output:
String is : w3wiki
Strings in C
A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.