Javatpoint Logo
Javatpoint Logo

Control String in C Language

Introduction:

The C language is a high-level programming language widely used for developing Operating Systems, System Software, Embedded Systems, and other applications. One of the essential features of C language is the ability to manipulate Strings. In this article, we will discuss the Control String in C language.

What is a Control String?

A Control String is a String that contains control characters, which are non-printable characters that control the formatting of the output. Control characters are represented by escape sequences that begin with the backslash (\) character, followed by a letter or symbol representing the 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

These Control Characters are used in the printf() and scanf() functions to format the output or input. The printf() function is taken in consideration to print the formatted output to the console, while the scanf() function is taken in consideration to read the formatted input from the console.

printf() Function:

The printf() function is taken in consideration to print the formatted output to the console. The Control String in the printf() function is a string that contains placeholders, which are replaced by the corresponding values at runtime.

The syntax of the printf() function is:

The Control String in the printf() function is a string that contains placeholders, which are replaced by the corresponding values at runtime. The placeholders begin with the percent (%) symbol, followed by a letter or symbol that represents the type of value to be printed.

The most commonly used placeholders in the printf() function are:

%d - decimal integer

%f - floating-point number

%c - character

%s - string

%p - pointer

C Code:

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 taken in consideration to read the formatted input from the console. The Control String in the scanf() function is a string that contains placeholders, which are replaced by the corresponding values at runtime.

The syntax of the scanf() function is:

The Control String in the scanf() function is a string that contains placeholders, which are replaced by the corresponding values at runtime. The placeholders begin with the percent (%) symbol, followed by a letter or symbol that represents the type of value to be read.

The most commonly used placeholders in the scanf() function are:

%d - decimal integer

%f - floating-point number

%c - character

%s - string

Control Strings for Formatting:

Control Strings can be used to format output data in various ways. For example, the %c format specifier can be used to print a single character, while the %s format specifier can be used to print a string. The %f format specifier can be used to print floating-point numbers, with control over the number of decimal places to display.

Control Strings can also be used to specify the width and precision of output data. For example, consider the following code snippet:

C Code:

In this example, the control string "%.2f" specifies that the printf() function should print the floating-point number with two decimal places. The output of this code snippet will be "3.14".

Control Strings for Reading Input:

Control Strings can also be used to read input data in various formats. The scanf() function is commonly used for this purpose, and it uses control strings to specify the format of the input data to be read.

For example, consider the following code snippet:

C Code:

In this example, the control string "%s %d" specifies that the scanf() function should read two values from the input stream: a string and an integer. The values are stored in the variables name and age, respectively. The %s format specifier reads a string of characters, while the %d format specifier reads an integer value.

Conclusion:

In conclusion, Control Strings are an important aspect of string manipulation in C programming. They are used to format or read input data in a specific way, with control over the data type, width, and precision. Format specifiers are used to specify the data type of the variable that needs to be formatted or read. In contrast, modifiers can be used to further specify the formatting or reading of data. The printf() and scanf() functions are generally used with control strings to manipulate strings in C programming.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA