As we know that, printf() is used to print the text and value on the output device, here some of the examples that we wrote to use the printf() in a better way or for an advance programming.
To print octal value, we use %o (It's alphabet o in lowercase)
int num = 255;
printf("num in octal format: %o\n", num);
Output
num in octal format: 377
6) Print hexadecimal value (with lowercase alphabets)
To print hexadecimal value, we use %x (its alphabet 'x' in lowercase) - as we know that a hexadecimal value contains digits from 0 to 9 and alphabets A to F, "%x" prints the alphabets in lowercase.
int num = 255;
printf ("num in hexadecimal format(lowercase) : %x\n", num);
Output
num in hexadecimal format(lowercase) : ff
7) Print hexadecimal value (with uppercase alphabets)
To print hexadecimal value, we use %X (it's alphabet X in uppercase) - as we know that a hexadecimal value contains digits from o to 9 and alphabets A to F, "%X" prints the alphabets in uppercase.
int num = 255;
printf ("num in hexadecimal format(uppercase) : %X\n", num);
Output
num in hexadecimal format(uppercase) : FF
8) Print long string using \ (slash)
If there is a long string, that you want to print with a single printf() with two or more lines, we can use slash (\).
printf ("Hello world, how are you?\
I love C programing language.\n");
Output
Hello world, how are you? I love C programing language.
9) Print backslash (\)
To print backslash (\), we use double backslash (\\).
printf ("The file is store at c:\\files\\word_files\n");
Output
The file is store at c:\files\word_files
10) To get total number of printed characters
We can also get the total number of printed character using printf(), printf() returns the total number of printed character, that we can store in a variable and print.
int len = 0;
len = printf ("Hello\n");
printf ("Length: %d\n", len);
1) Print normal text
Output
2) Print text in new line
Output
3) Print double quote
To print double quote, we use ".
Output
4) Print percentage sign (%)
To print percentage sign/ character, we use %%.
Output
5) Print octal value
To print octal value, we use %o (It's alphabet o in lowercase)
Output
6) Print hexadecimal value (with lowercase alphabets)
To print hexadecimal value, we use %x (its alphabet 'x' in lowercase) - as we know that a hexadecimal value contains digits from 0 to 9 and alphabets A to F, "%x" prints the alphabets in lowercase.
Output
7) Print hexadecimal value (with uppercase alphabets)
To print hexadecimal value, we use %X (it's alphabet X in uppercase) - as we know that a hexadecimal value contains digits from o to 9 and alphabets A to F, "%X" prints the alphabets in uppercase.
Output
8) Print long string using \ (slash)
If there is a long string, that you want to print with a single printf() with two or more lines, we can use slash (\).
Output
9) Print backslash (\)
To print backslash (\), we use double backslash (\\).
Output
10) To get total number of printed characters
We can also get the total number of printed character using printf(), printf() returns the total number of printed character, that we can store in a variable and print.
Output
need an explanation for this answer? contact us directly to get an explanation for this answer