Strings in C language

Strings in ‘c’ are defined as a series of characters. The difference between a string and a string is that the string ends with a special character “”.

Strings in c declaration: declaring a string is as simple as declaring a dimensional array. Below is the basic syntax for declaring a string.

char name [size];

In the syntax above str_name is any name given to the string variable and the size is used to define the length of the string, ie the number of characters the string will store. Please note that there is an additional end character, which is the Null character (” 0″) used to indicate the end of the string that differs the strings from normal arrays.

Initializing a string: A string can be initialized in different ways. We will explain this with an example. Below is an example to declare a string named as str and initialize with “ProJugaadu”.

1. char str[] = "ProJugaadu";
2. char str[50] = "ProJugaadu";
3. char str[] = {'P', 'r', ​​'o', ​​'J', 'u','g','a','a','d','u',''};
4. char str[11] = {'P', 'r', ​​'o', ​​'J', 'u','g','a','a','d','u',''};

Below is a representation of the memory of a string of “Jugaadu”.

Let’s now look at a test program to clearly understand the declaration and initialization of strings in C and also how to print a string.

// C program to illustrate strings 
  
#include<stdio.h> 
  
int main() 
{    
    // declare and initialize string 
    char str[] = "Jugaadu"; 
      
    // print string 
    printf("%s",str); 
      
    return 0; 
} 

Output:

Jugaadu


In the program above we can see that the strings can be printed using normal printf instructions, just like we print any other variable. Unlike arrays, it is not necessary to print a strings, character by character. The C language does not provide a built-in data type for strings, but has an access specifier “% s” that can be used to print and read strings directly.

Below is an example program to read a string from the user:

// C program to read strings 
  
#include<stdio.h> 
  
int main() 
{    
    // declaring string 
    char str[50]; 
      
    // reading string 
    scanf("%s",str); 
      
    // print string 
    printf("%s",str); 
  
    return 0; 
} 

In the program above you can see that the string can also be read using a single scanf statement. You may also be wondering why I didn’t use the “&” sign with the string name “str” ​​in the scanf statement! To understand this, you will need to remember your knowledge about scanf. We know that the ‘&’ sign is used to provide the address of the scanf () function variable to store the value read in memory. Because str [] is an array of characters, so using str without braces “[” and “]” will provide the base address of this string. This is why we did not use “&” in this case, because we already provide the base address of the scan string.

Passing strings to work: Since strings are arrays of characters, so we can pass strings to work the same way, we pass an array to a function. Below is an example program to do this:

// 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[] = "ProJugaadu"; 
      
    // print string by passing string 
    // to a different function 
    printStr(str); 
      
    return 0; 
} 

output:


The string is: Projugaadu

More related C program here

http://projugaadu.com/category/topics/c-language/

/*54745756836*/

Leave a Comment