Q:

printf() examples/variations in C

0

printf() examples/variations in C

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

1) Print normal text

printf ("Hello world");

Output

Hello world

2) Print text in new line

printf ("Hello world\nHow are you?");

Output

Hello world
How are you?

3) Print double quote

To print double quote, we use ".

printf("Hello "World", How are you?\n");

Output

Hello "World", How are you?

4) Print percentage sign (%)

To print percentage sign/ character, we use %%.

printf ("Hey I got 84.20%% in my final exams\n");

Output

Hey I got 84.20% in my final exams

5) Print octal value

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);

Output

Hello
Length: 6

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
C program to calculate profit or loss... >>
<< printf() statement within another printf() stateme...