Strings in C Language with Examples

Strings in C Language with Examples

In this article, I am going to discuss the Strings in C Language with Examples. Please read our previous articles, where we give a brief introduction to Strings in C Language. As part of this article, you will learn the following pointers in detail with examples.

  1. What are Strings in C?
  2. Why do we need strings?
  3. Declaration and Initialization of String
  4. Memory representation of String
  5. Multiple Examples to understand string in C.
  6. What do u mean by formatted and unformatted functions?
  7. Stringing Operator (#)
  8. Token Paste Operator (##)
Strings in C Language:

Character array or group of characters or collection of characters are called strings. In implementation when we are manipulating multiple characters, then recommended going for strings. Within the ‘ ‘ any content is called character constant, within the “ “ any content is called string constant. Character constant always returns an integer value i.e. ASCII value of a character. String constant always returns the base address of a string. When we are working with a string constant, always ends with nul(‘\0’). The representation of the null character is nul(‘\0’) and the ASCII value is 0.

Syntax: char str[size];

Note: Null is a global constant value which is defined in <stdio.h>. Null is a macro which is having the replacement data as 0 or (void*)0.

Example: int x=NULL;
                 int *ptr=NULL;
nul(‘\0’) is an ASCII character data which is having an ASCII value of 0.

Declaration of String in C Language:

C does not support string data type that’s why it allows us to represent the string as character arrays.

Syntax: char string_name[size];
Example: char book[10];

A null character (\0) is assigned to the string automatically when the compiler assigns a string to a character array. So, the size of the array becomes the maximum number of arrays plus 1.

Initialization of String:

We can initialize a String in different ways.

  1. char str[] = “Cprogramming”;
  2. char str[50] = “Cprogramming”;
  3. char str[] = {‘C’,’p’,’r’,’o’,’g’,’r’,’a’,’m’,’m’,’i’,’n’,’g’,’\0′};
  4. char str[14] = {‘C’,’p’,’r’,’o’,’g’,’r’,’a’,’m’,’m’,’i’,’n’,’g’,’\0′};
Memory representation of String:

Memory representation of String in C

Program:
#include<stdio.h>   
int main() 
{    
    // declare and initialize string 
    char str[] = "Strings";   
    // print string 
    printf("%s",str); 
    return 0; 
}

Output: Strings

Properties of Strings in C Language:
  1. In the declaration of string, the size must be an unsigned integer constant whose value is greater than zero only.
  2. In the initialization of the string, specific characters are not initialized then remaining all elements are automatically initialized with nul(\0).
  3. In the initialization of the string, it is not possible to initialize more than the size of string elements.
  4. In the initialization of the string, if we are assigning a numeric value, then according to ASCII value, corresponding data will be stored.
  5. In the initialization of the string, specifying the size is optional, in this case, how many characters are initialized and how many variables are created.
  6. When we are working with strings, always recommended to initialize the data in double-quotes only.
  7. When we are working with a string constant, always it ends with ‘\0’ (null) character that’s why one extra byte memory is required but if we are working with a character array then it doesn’t require one extra byte memory.
  8. When we are working with character operations recommended going for %c format specifier.
  9. When we are working with string operations recommended going for %s format specifier.
  10. When we are working with %s format specifier then we are required to pass an address of a string, from given address up to null, entire content will print on the console.
  11. When the null character has occurred in the middle of the string, then we are not able to print complete data because the null character indicates termination of the string.
What do u mean by formatted and unformatted functions?

The functions which will work with the help of format specifiers are called formatted functions. A formatted function can be applied to any data type. For example: printf(), scanf(), fprintf(), fscanf(), sprint(), etc.

The functions which does not require any format specifier and need to be applied for specific data type only are called unformatted function. For example: puts(), gets(), fputs(), cgets(), getch(), etc.

puts():

It is a predefined unformatted function, which is declared in stdio.h. By using this function, we can print string data on the console. Puts() function required 1 argument of type char* and returns an integer value. When we are working with the puts function, automatically it prints a newline character after printing string data.

Syntax: int puts(char*str);

Program:
#include<stdio.h>
int main() 
{    
    char str[] = "Strings";  
    puts(str);
    printf("%s",str);
    return 0; 
}
Output:

Strings in C Language with Examples

Program:
#include<stdio.h>
#include<conio.h>
int main() 
{    
    char str[10];  
    printf("Enter a string: ");
    scanf("%s", str);
    printf("input string:%s", str);
    getch();
    return 0; 
}
Output:

Strings in C Language with Examples

By using scanf function, we can’t read the string data properly when we have multiple works because, in scanf function space, tab and newline characters are treated like separators so when the separator is present, it is replaced with \0 character. In sacnf function, when we are using %[^\n]s format specifier, then it indicates that read the string data up to newline character occurrence.

gets():

It is a predefined unformatted function that is declared in stdio.h. By using this function we can read the string data properly, even when we are having multiple words. gets() function requires one argument of type (char*) & returns (char*) only. In the gets() function only a newline character is treated as a separator.

Syntax: char*gets(char*str);

Program:
#include<stdio.h>
#include<conio.h>
int main() 
{    
    char str[10];  
    printf("Enter a string: ");
    gets(str);
    printf("input string:%s", str);
    getch();
    return 0; 
}
Output:

Why we need strings in C?

Program:
#include<stdio.h>
#include<conio.h>
int main() 
{    
    char s1[10]="hello"; 
    char s2[10]="welcome";
    puts(s1);
    puts(s2);
    s2=s1;
    puts(s1);
    puts(s2);
    getch();
    return 0; 
}
Output:

blank

Any kind of string manipulations, we can’t perform directly by using operators. In implementation when we are required to perform any kind of string operations then recommended to go for any string handling functions or go for user-defined function logic.

Stringing Operator (#):

This operator is introduced in the NCC version. By using this operator, we can convert the text in the form of string i.e. replacement in ” “. Following is an example.

#include<stdio.h>
#define ABC(xy) printf(#xy "=%d",xy);
int main()
{
 int a,b;
 a=10;
 b=20;
 ABC(a+b);
 return 0;
}

Output: a+b=30

Token Paste Operator (##):

NCC programming language supports this operator. By using this operator, we can concatenate multiple tokens. Following is an example.

#include<stdio.h>
#define ABC(x,y) printf("%d",x##y);
void main()
{
 int var12=120;
 ABC(var,12);
 return 0;
}

Output: 120

In the next article, I am going to discuss String Predefined Functions in C Language with Examples. Here, in this article, I try to explain Strings in C Language with Examples. I hope you enjoy this Strings in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this Strings in C Language with Examples article

Leave a Reply

Your email address will not be published. Required fields are marked *