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

Note: We can’t read a string value with spaces, we can use either gets() or fgets() in the C programming language.

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’.

Similar Reads

C String Declaration Syntax

Declaring a string in C is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string....

C String Initialization

A string in C can be initialized in different ways. We will explain this with the help of an example. Below are the examples to declare a string with the name str and initialize it with “GeeksforGeeks”....

C String Example

C // C program to illustrate strings   #include #include   int main() {     // declare and initialize string     char str[] = "Geeks";       // print string     printf("%s\n", str);       int length = 0;     length = strlen(str);       // displaying the length of string     printf("Length of string str is %d", length);       return 0; }...

Read a String Input From the User

...

How to Read a String Separated by Whitespaces in C?

The following example demonstrates how to take string input using scanf() function in C...

C String Length

...

Passing Strings to Function

...

Strings and Pointers in C

We can use multiple methods to read a string separated by spaces in C. The two of the common ones are:...

Standard C Library – String.h  Functions

...