GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

printf in C

  • printf is used to print the values of variables or display messages or both on the output screen.
  • printf is formatted output function.
  • Since it is formatted output function, we need to use format-specifiers. (Discussed in scanf)
  • Prototype:
int printf("format-specifier", argument_list);
  • printf gives the answer as number of characters printed on the screen.

You may watch the video posted at the end of this page.

Example 1:

int b = 10;

variable in c

To print the value of ‘a’ on the screen.

we write,

printf("%d", b);
  • Whenever we get format-specifier we wait, search for end of double quotations and comma and take the value of corresponding variable.
  • Since we are printing the value of int, the format-specifier here used is %d. 
  • Replace %d with the first value after double quotations and comma that is b.
  • Therefore, it prints the value of b (10) on the output screen.

Note: We don’t write &b, because we don’t want to print the address of variable. We would want to print the value of b.


 

Example 2:

double d = 1.1;

variable in c

To print the value of ‘d’ on the screen.

we write,

printf("%lf",d);
  • Whenever we get format-specifier we wait, search for end of double quotations and comma and take the value of corresponding variable.
  • Since we are printing the value of double, the format-specifier here used is %lf. 
  • Replace %lf with the first value after double quotations and comma that is d.
  • Therefore, it prints the value of d (1.1) on the output screen.

 

Example 3:

char ch = ‘A’;

variable in c

To print the value of ‘ch’ on the screen.

we write,

printf("%c", ch);
  • Whenever we get format-specifier we wait, search for end of double quotations and comma and take the value of corresponding variable.
  • Since we are printing the value of char, the format-specifier here used is %c.
  • Replace %c with the first value after double quotations and comma that is ch.
  • Therefore, it prints the value of ch (A) on the output screen.

If we write printf as,

printf("%d", ch);
  • Here we want to print the value of ch as integer, since we have used the format-specifier as %d.
  • Therefore, the compiler will try to convert ch which is char to int. This is possible since characters are stored as ASCII value internally.
  • Hence, the output is ASCII value of , which is 65.

printing multiple variables

Example 1:

int a = 10, b = 20;

We want to print the values of both ‘a’ and ‘b’ in single printf.

printf("%d%d",a,b);
  • We have written two %d’s since we want to print two variables (a and b)
  • Whenever we get format-specifier we wait, search for end of double quotations and comma and take the value of corresponding variable.
  • In printf as we get the first format-specifier %d, we wait search for end of double quotations and comma and take the value of first variable, that is a. So 10 is printed on the output screen.
  • As we get the second format-specifier %d, we wait search for end of double quotations and comma and take the value of second variable, that is b. So 2is printed on the output screen.

The output is,

1020

The output looks as it is one thousand and twenty. To print 10 and 20 with a space, we would require giving the space in the printf as well.

printf("%d %d",a,b);

Now the output looks like,

10 20

 

Example 2:

int a = 25;

double b = 9.24;

char c = ‘D’;

To print the values of all the variables in single printf, 

printf("%d %lf %c", a , b ,c);
  • The first-format specifier replaces the value of the first variable, second with second and third with third. (1-1 correspondence)
  • Therefore, the output is:
25 9.24 D

 

  • Whatever is written in double quotations in printf is printed as it is, except for format-specifiers and escape sequences.

Example:

printf("Welcome to geteducate.org");

Output: 

Welcome to geteducate.org

Suppose we want to print a new line in between the message or give a tab space , then we use escape sequences for these.


 

Escape sequence

The escape sequence takes the usual meaning of the character away. The following are the commonly used escape sequences.

Escape Sequence Meaning

\n

new line

\t

tab

\r

Carriage return

\b

back-space

\a

alarm beep

\\

prints \

%%

prints %

\"

prints "

Examples with escape sequence

Example 1:

printf("Welcome to \n geteducate.org");

Output:

It prints Welcome to in 1st line and since we have got \n, a new line character is added and geteducate.org is printed on the second line.

Welcome to
geteducate.org_
(The underscore at the end says,
the cursor is pointing here)

Example 2:

printf("Welcome to \t geteducate.org");

Output:

It prints Welcome to and we get \t , it gives a tab space (4/8 white spaces) and then prints geteducate.org 

Welcome to    geteducate.org

Example 3:

printf("ABCD\b");

Output:

prints ABCD on the screen and then we see \b, meaning give a backspace. The cursor at the end points to D.

ABCD

Example 4:

printf("ABCD\bE");

Output:

prints ABCD on the screen and then we see \b, meaning give a backspace. The cursor points to D and we have one more character ‘E’, it prints from where the cursor is pointing and D is replaced by E.

ABCE_

 

printing double quotes in printf

If we write,

printf(""ABCD"");

The above printf results in an error. The printf is written between the double quotations and as we get the second double quotation [printf(” “], printf thinks its the end, and we have some more characters after that.

To make print think that the second quote is not the end, we need to take its usual meaning away and that we do using escape sequence.

printf("\"ABCD\"");

Output:

"ABCD"

 

printing %

double per = 87.36;

Suppose, if we want to print 87.36% on the screen, we would write

printf("%lf%%",per);

Output:

87.36%

 

Example 1:

int a = 27;

printf("a = %d", a);
  • Whatever is written in double quotations in printf is printed as it is, except for format-specifiers and escape sequences.
  • Whenever we get format-specifier we wait, search for end of double quotations and comma and take the value of corresponding variable.

Output is:

a = 27
printf("%d a = ", a);

Output:
27 a =
printf("%d\n = a", a);
27
= a
printf("A = %d\n", a);
Output:
A = 27
printf("a = %d\n", A);
Error: Since the variable A is undeclared. Variable names are case-sensitive in C programming.

 

Using expressions in printf

  • We can write expressions or constants in place of variable names in printf.
printf("%d", 25);
Output:
12.568

It replaces the integer value for %d which is 25.

Output:
25

 

printf("%lf",3.142*2*2);
Output: 12.568

 

int x = 1, y = 2;
printf("x = %d\ny = %d\n",y,x);

Output:
x = 2
y = 1

The return value of printf

int y = printf("Hello World");

prints Hello world on the screen. Since 11 characters are printed on screen, the value of y will be 11. (‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’)

Note: Even space is a character

int y = printf("Hello\n");

prints Hello followed by a new line character.

The value of y = 6 (‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\n’)

Scroll to Top