Escape Sequence | Formatted Printing | Interview Questions

Escape sequence

Escape sequence are some non-printable character that shows the effect while printing the output. The usage and example of escape sequence are given below.

These are the combination of two characters backslash(\) and one more character but treated as a single character occupy only one byte as other characters.

Escape Sequence Character Meaning Usage
\a Bell alert Used to make a sound.
\b backspace Used to move the cursor to the one character back.
\n newline Used to change the line
\r Carriage Return Used to move the cursor to the beginning of the current line.
\" Double Quotes Used to print the double quotes
\' Single Quotes Used to print the single quotes
\f Form feed Used to change the page
\t tab Used to print the tab

Program to demonstrate the use of escape sequence

int main()
  printf("Hello\"Alex \"\n");
  printf("Hello \'Clark\'\n");
  return 0; 


Hello   World
Hello"Alex "
Hello 'Clark'

More Controlled Printing in C

We use the printf() function for printing till now. But print() function have many variation as given below.

Integer formatted printing


Syntax Explained

In the above syntax w stands for white spaces. For print, the given integer variables value, w spaces are reserved and the number is print by right aligned on the given spaces. If the w is less the number, then the specified width is ignored. Read the following example to understand the fact.

int a=512;

Syntax for printing floating point numbers


Syntax Explained

In the above example w stands for the total width reserved for the printing including decimal places and p specifies how many digits(precision) are allowed after decimal point.Read the following example to understand the fact.By default 6 digit precision is allowed for float printing.

float f=5.6789;
printf("%10f",f);//__5.678900, two blank spaces
printf("%-10.2f",f);//5.68______, round off the decimal places if truncation is performed.

Type conversion using formatted printing

If you want to print the decimal number into octal, hexadecimal then this task can be done directly by using the format specifier explained in the following example.

printf("%o",25);//31, octal equivalent of decimal 25.
printf("%x",25);//19, hexadecimal equivalent of decimal 25.

If an integer constant is prefixed with zero(0) then constant is called octal constant or if prefixed with (zero-x)0x then constant is called hexadecimal constant.
printf("%d",031);//print 25,031 is an octal integer and 25 is decimal equivalent of oct-31
printf("%d",0x19);//print 25, 0x19 is hexadecimal constant and 25 is decimal equivalent of hex-19.

Note: %i can be also used for the decimal integer printing.

Some Conditional operators tips

int i=5;

//output: print "Hello"

In the above example, if you think that output should be "Bye" so you are absolutely wrong. The reason is within the if i=3 initialize the "i" with 3. It doesn't compare "i" with 3. So within the if "i" becomes 3 and statement is converted if(i)=>if(3). Because any non-zero value is considered as true so print "Hello".

int i=5;

This program snippet will complain about the compile-time error. The is generally known as misplaced else.

In the above program snippet the if contains multiple statements but no brackets are used to enclose multiple statements in a group.

The reason for the error is what else is a counterpart of the if so it must appear just after the if. but if no brackets are used to make the if block so "if ends with the first statement written after it and then one more statement occurs and then else appears which break the direct link of else with it.

The solution of the above problem is that just make a block of the statement written after the if so else will appear just after the if block.

int i=5;

This program snippet will complain the compile-time error because there is a semicolon after the if and if ends with a semicolon and then one more statement is written without enclosing them in a block. This problem is much similar to the above problem. Here semicolon(;) is also a statement called null statement.

int i=5;

//output: HelloOut

The semicolon just after the else is perfectly alright. This semicolon ends the else with null statement means if a condition is false the nothing happens. In the above case, it is optional to write the else statement.


Popular posts from this blog

Program to calculate telephone bill

Arrays in C Language

String in golang

Sum of two numbers in go lang

Pointers in C Language

Decision Making Statements

Say Hello world in Go lang

Decision Making in Go lang

Installing go lang

Program to convert time from seconds to hours, minutes and seconds