The %s means, "insert the first argument, a string, right here." The %d indicates that the second argument (an integer) should be placed there. There are different %-codes for different variable types, as well as options to limit the length of the variables and whatnot. Control Character.
"%s" expects a pointer to a null-terminated string ( char* ). "%c" expects a character ( int ).
%s and string
We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character.
In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.
%c means character, range is 0x00 to 0xff (Unsigned) that is 1 byte, a memory location can hold 1 byte of data, %c will print data from one memory location only, so termination character is not required.
%d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value.
This is implemented for fetching values from the address of a variable having an unsigned decimal integer stored in memory. An unsigned Integer means the variable can hold only a positive value. This format specifier is used within the printf() function for printing the unsigned integer variables.
For type long double, the correct format specifier is %Lf. For input using the scanf family of functions, the floating-point format specifiers are %f, %lf, and %Lf. These require pointers to objects of type float, double, and long double, respectively.
%p is a format specifier in C Programming language, that is used to work with pointers while writing a code in C.
%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.
%3d can be broken down as follows: % means "Print a variable here" 3 means "use at least 3 spaces to display, padding as needed" d means "The variable will be an integer"
\t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line. \a (Audible bell) – A beep is generated indicating the execution of the program to alert the user. \r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
Example 1: C++ printf()
In this program, we have used the printf() function to print the integer num and the C-string my_name . printf("num = %d \n", num); printf("My name is %s", my_name); Here, %d is replaced by the num variable in the output.
%d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int .
%d is a signed integer, while %u is an unsigned integer. Pointers (when treated as numbers) are usually non-negative. If you actually want to display a pointer, use the %p format specifier.
Let's see an example of taking and displaying a float , a double and a char value. So, you can see here that %d is used for integers, %f for floats and %c for characters. As simple as that!
The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.
getch() method pauses the Output Console until a key is pressed. It does not use any buffer to store the input character. The entered character is immediately returned without waiting for the enter key. The entered character does not show up on the console.
return 0: A return 0 means that the program will execute successfully and did what it was intended to do.
The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.
Logical operators in C are used to combine multiple conditions/constraints. Logical Operators returns either 0 or 1, it depends on the expression result true or false. In C programming for decision-making, we use logical operators. We have 3 major logical operators in the C language: Logical AND (&&)
"%10d" means to pad with spaces on the left, as needed, so decimal output is at least 10 characters.