What is the usage of printf function in c program
C language is a widely used programming language, and the printf function is one of the most commonly used functions in C language. It is used to output text and variable values to the screen. In this article, we will detail the usage of the printf function and some common formatting options.
First, let's take a look at the basic syntax of the printf function:
int printf(const char *format, ...);
The printf function accepts a format string as the first parameter, which specifies the format of the output. In the format string, you can insert placeholders that specify the type and format of the variable to be output. Placeholders begin with a percent sign (%) and are followed by one or more characters that specify the type and format of the variable.
The following are some common placeholders and their usage:
- %d: used to output integers.
- %f: used to output floating point numbers.
- %c: used to output characters.
- %s: used to output string.
In addition to these basic placeholders, the printf function also supports some formatting options for specifying the format of the output. Here are some common formatting options:
- d: Specifies the output field width to be 10 characters. If the output integer is not wide enough, spaces will be padded on the left.
- %-10d: Specifies that the output field width is 10 characters. If the output integer is not wide enough, spaces will be padded on the right.
- %.2f: Specify two digits after the decimal point of the output floating point number.
- .2f: Specifies that the output field width is 10 characters, and two decimal places are retained for floating point numbers.
The following are some examples demonstrating the usage of the printf function:
int main() { int num = 10; float pi = 3.14159; char letter = 'A'; char name[] = "John"; printf("The number is %d\n", num); printf("The value of pi is %.2f\n", pi); printf("The letter is %c\n", letter); printf("The name is %s\n", name); return 0; }
Running the above code will output the following results:
The number is 10 The value of pi is 3.14 The letter is A The name is John
In the above example, we use Different placeholders and formatting options are used to output different types of variables. Note that the order of placeholders in the format string must match the order of variables.
In addition, the printf function also supports some other escape characters for outputting special characters, such as newline (\n), tab (\t), etc. These escape characters can be used directly in the format string.
To sum up, the printf function is an important function in C language for outputting text and variable values. By using different placeholders and formatting options, we can flexibly control the format of the output. I hope this article can help you better understand and use the printf function .
The above is the detailed content of What is the usage of printf function in c program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Given below is a C language algorithm to convert Roman numerals to decimal numbers: Algorithm Step 1 - Start Step 2 - Read Roman numerals at runtime Step 3 - Length: = strlen(roman) Step 4 - For i=0 to Length-1 Step 4.1-switch(roman[i]) Step 4.1.1-case'm': &nbs

Lexicographic string comparison means that strings are compared in dictionary order. For example, if there are two strings 'apple' and 'appeal', the first string will come last because the first three characters of 'app' are the same. Then for the first string the character is 'l' and in the second string the fourth character is 'e'. Since 'e' is shorter than 'l', it will come first if we sort lexicographically. Strings are compared lexicographically before being arranged. In this article, we will see different techniques for lexicographically comparing two strings using C++. Using the compare() function in C++ strings The C++string object has a compare()

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

Linked lists use dynamic memory allocation, i.e. they grow and shrink accordingly. They are defined as collections of nodes. Here, a node has two parts, data and links. The representation of data, links and linked lists is as follows - Types of linked lists There are four types of linked lists, as follows: - Single linked list/Singly linked list Double/Doubly linked list Circular single linked list Circular double linked list We use the recursive method to find the length of the linked list The logic is -intlength(node *temp){ if(temp==NULL) returnl; else{&n

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

A map is a special type of container in C++ in which each element is a pair of two values, namely a key value and a map value. The key value is used to index each item, and the mapped value is the value associated with the key. Regardless of whether the mapped value is unique, the key is always unique. To print map elements in C++ we have to use iterator. An element in a set of items is indicated by an iterator object. Iterators are primarily used with arrays and other types of containers (such as vectors), and they have a specific set of operations that can be used to identify specific elements within a specific range. Iterators can be incremented or decremented to reference different elements present in a range or container. The iterator points to the memory location of a specific element in the range. Printing a map in C++ using iterators First, let's look at how to define

Strncmp is a predefined library function, present in the string.h file, which is used to compare two strings and display which string is larger. strcmp function (string comparison) This function compares two strings. It returns the ASCII difference of the first non-matching character in the two strings. Syntax intstrcmp(string1,string2); If the difference is equal to zero, then string1=string2. If the difference is positive, string1>string2. If the difference is negative, string1<string2. Example strncmp function This function is used to compare the first n characters of two strings. syntax strn

Using strings or characters is sometimes very useful when solving some logic programming problems. A string is a collection of characters, which is a 1-byte data type used to hold symbols in ASCII values. Symbols can be English letters, numbers, or special characters. In this article, we will learn how to check if a character is an English letter or a letter of the alphabet using C++. Checking the isalpha() function To check if a number is a letter, we can use the isalpha() function in the ctype.h header file. This takes a character as input and returns true if it is an alphabet, false otherwise. Let us look at the following C++ implementation to understand the usage of this function. The Chinese translation of Example is: show