Control String In C Language

What is a Control String?

A Control String is a sequence of characters that includes control characters, which are characters that are not displayed and are used to format the output. These control characters are denoted by escape sequences starting with a backslash (\), followed by a letter or symbol that signifies the specific control character.

The most commonly used Control Characters in C language are:

  • \n - newline
  • \t - tab
  • \r - carriage return
  • \a - alert or bell
  • \b - backspace
  • \f - form feed
  • \v - vertical tab

Control characters play a vital role in formatting the input and output of the printf and scanf functions. In the context of printf, these characters help structure and display formatted output on the console. Conversely, when working with scanf, control characters are essential for interpreting and capturing formatted input from the console.

printf Function:

The printf function is utilized to display formatted output on the console. The Control String within the printf function consists of placeholders that get substituted with the respective values during program execution.

The syntax of the printf function is:

Example

printf("control string", argument list);

The Control String within the printf function is a character string that includes placeholders. These placeholders are substituted with the relevant values during program execution. They typically start with the percent (%) symbol, succeeded by a character or symbol denoting the data type intended for output.

The placeholders frequently utilized in the printf function include:

%d - decimal integer

%f - floating-point number

%c - character

%s - string

%p - pointer

C Code:

Example

#include<stdio.h>
int main()
{
int a = 10;
float b = 3.14;
char c = 'A';
char str[] = "Hello, World!";

printf("The value of a is %d\n", a);
printf("The value of b is %f\n", b);
printf("The character is %c\n", c);
printf("The string is %s\n", str);
printf("The address of a is %p\n", &a);

return 0;
}

Output

Output

The value of a is 10
The value of b is 3.140000
The character is A
The string is Hello, World!
The address of a is 0x7ffd1c998d0c

scanf Function:

The scanf function is utilized to receive formatted input from the console. The Control String within the scanf function comprises placeholders that get substituted with respective values during execution.

The syntax of the scanf function is:

Example

scanf("control string", argument list);

The Control String within the scanf function serves as a string containing substitution points that are filled with actual values during program execution. These substitution points commence with the percent (%) symbol, succeeded by a character or symbol denoting the data type to be input.

The placeholders frequently utilized in the scanf function include:

%d - decimal integer

%f - floating-point number

%c - character

%s - string

Control Strings for Formatting:

Control Strings are valuable tools for formatting output data in diverse manners. For instance, the %c format specifier allows printing a solitary character, whereas the %s format specifier enables printing a string. Moreover, the %f format specifier facilitates the printing of floating-point numbers, granting control over the precision of decimal places to exhibit.

Control Strings are also employed to define the width and precision of output information. To illustrate, take into account the subsequent code excerpt:

C Code:

Example

double num = 3.14159;
printf("%.2f", num);

In this instance, the format specifier " %.2f " directs the printf function to display the float value with precision up to two decimal points. The resultant value from executing this code snippet will be " 3.14 ".

Control Strings for Reading Input:

Control Strings are also capable of reading input data in different formats. The scanf function is frequently employed for this task, utilizing control strings to indicate the desired format of the input data for reading.

For example, consider the following code snippet:

C Code:

Example

char name[20];
int age;
scanf("%s %d", name, &age);

In this instance, the format string "%s %d" directs the scanf function to retrieve two inputs from the input stream: a string and a whole number. These inputs are then saved in the variables name and age, in that order. The %s specifier is designed to capture a sequence of characters, whereas the %d specifier is used to capture an integer value.

Conclusion:

In summary, Control Strings play a crucial role in manipulating strings within C programming. They serve to structure or interpret input data in a particular manner, enabling control over aspects such as data type, width, and precision. Format specifiers are employed to define the data type of the variable requiring formatting or reading. On the other hand, modifiers can provide additional instructions for refining the formatting or reading process. Typically, the printf and scanf functions are utilized alongside control strings to manage string operations in C programming.

Input Required

This code uses input(). Please provide values below: