Featured Post

Tuesday, June 28, 2016

printf() and back slash characters


how to print something on screen using C printf() function?

consider a simple code given below-

main()  /* program begins here */
{
  printf("hello this is my first program");
}

if we execute this code we get output on screen as

output : 
hello this is my first program

In c if we want to print something on screen we have to use printf() function which is available in standard library function stdio.h

whatever we give in printf() function in between two inverted comas that is printed same(except some special symbols).

main()
{
  printf("hai Programmers");
}

output: 
hai Programmers

remember that every printf() function in C must be terminated by a semicolon symbol(;).

Now my task is i want to give a tab space in between two fields hi and programmers

main()
{
   printf("hi\tprogrammers");/
}

output:
hi    programmers.

\t has a special meaning in c which gives one tab space when it is encountered by a C compiler 

now my task is i want to print hi in one line and programmers in second line

main()
{
  printf("hi");
printf("\n programmers");
}
output:
  hi
  programmers

\n has a special meaning in c which moves cursor to next line and prints next word or line in next line  when it is encountered by a C compiler 



you may get doubt why i cant print two words in two lines using two printf() statements without using \n symbol. let me explain what happens
main()
{
 printf("hi");
printf("programmers");
}

output:
hiprogrammers

what happens here is after printing first word hi then cursor is at location immediately after hi. so when second printf() function is executed then second word programmers printed immediately after hi.

note:   \t is used for allocating one tab space between two words.
           \n is used to move the cursor next line and prints other lines.
           \a is used to give a beep sound  
            let us compare two things for example in word document and C editor
            

                         action                                   command or key                                  command or key

                                                                      (MSWORD)                                            (C output )
                         to move to new line              press enter key                                             \n
                        to give one tab space             press Tab key                                               \t

lets close our session with this program

main()
{
  printf("hi");
  printf("\n hello\t programmers");
}

output

hi
hello    programmers


Any queries or comments?
(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.
back slash characters,printf statement.

No comments:

Post a Comment