Skip to main content

printf() and scanf() Functions

Printf() is a function to print strings to the display unit and

scanf() is to scan the input through the keyboard

the f in the printf and scanf are called as format. That is, printf() and scanf() will print and scan in some particular format, it is the duty of the developer to provide that formats.

For example

printf(“a=%d, b=%d”, a,b); //This function will print a=10, b=20 if the values of a and b are 10 and 20

scanf(“%f”, &a); //this will accept a float number as the specifier is %f which is for float, See the Ampersand symbol which to tell the compiler to store the variable into the address.

Here is the list of format codes or format specifiers

scanf() format codes

Code Meaning
%c read a single character
%d read a decimal integer
%f read a floating point value
%e read a floating point value (even in exponential format)
%g read a floating point value
%h read a decimal, heaxadecimal or octal integer
%o read an octal integer
%s read a string
%u read an unsigned integer
%x read a hexadecimal integer

h for short integers like %hd

l for long integers like "%ld

L for long double like %Lf

printf() format codes

Code Meaning
%c prints a single character
%d print a decimal integer
%e print a floating point value in exponential form
%f print a float point value
%g print a float point value either in f type or e type depending on the value
%i print a signed decimal integer
%o prints an octal integer
%s prints a string
%u print an unsigned integer
%x print a hexadecimal number


Backslash Character constants or Escape Characters

There are some characters inside the double quotes for printf() and scanf() which are escaped by the compiler and hence it will the next character after the escape characters

Character What it will do
\n go to the new line
\t horizontal tab
\a alert bell
\f form feed
\r carriage return
\” will print “
\’ will print ‘
\? will print ?
\\ will print \
\v vertical tab
\b backspace


Popular posts from this blog

Ant Colony algorithm using NS2

Ant Colony Algorithm in NS2 for Wired Networks. Please follow the video for full instructions  I am running in Ubuntu 20.04 There are multiple files in Ant Colony (we use a protocol called Antnet) which i have already included in my blog called  Lets go to my Blog. And Search for Ant Colony and Download the complete folder AntNet as shown in the video. Now Copy paste the files in their corresponding location: My Location if ns2 is  /home/pradeepkumar/ns-allinone-2.35/ns-2.35/ Inside this folder, the files have to be pasted in differnet sub folders...  Let me show you one by one... and cmu-trace.h have to be pasted to ~trace/ and drop-tail.h ---- ~queue/ to be replaced in the ns-2.35/ folder. ns-default.tcl, ns-lib.tcl and ns-packet.tcl to be pasted at  ~tcl/lib packet.h to be replaced at common/ folder. and priqueue.h to be pasted at queue/ antnet / folder have to be copied to ns-2.35/ folder (this is very imp

Installing NS-3.32 in Ubuntu 20.04

This is about installing ns version 3.32 in Ubuntu 20.04 LTS. #ns3 #ns3 .32 #networksimulation The commands used in the video are given here. $] sudo apt update $] sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-qmake qt5-default gnuplot-x11 wireshark Download the ns-allinone-3.32.tar.bz2 package from and copy it to /home/ folder See the full video for detailed instructions Extract it either in GUI or using command $] tar jxvf ns-allinone-3.32.tar.bz2 $] cd ns-allinone-3.32/ $] ./ --enable-examples --enable-tests The above command will take some time to install all the packages  You can see the output as shown below ns3 To check whether ns3 installed successfully, use the following commands. $] cd ns-3.32/ $] ./waf --run hello-simulator You should get the output as Hello Simulator $] ./waf --run first This is the example from the ns-3.32/exa

ns3 installation in Ubuntu 16.04

This post serves the installation instructions of ns3 in ubuntu 16.04 version. Some of my students are working in ns3, this post will benefit them in installing ns3. OS Used: Ubuntu 16.04.4 ns3 version: ns3 version 3.27 The same procedure will be applied for OS like Debian, Linux Mint. Fresh installation of Ubuntu 16.04 Let you try the fresh installation of ubuntu in your hard disk along with windows. Installation of ns3 dependencies ns3 needs so many dependencies, developmental libraries, drivers, etc. so install all those $] sudo apt update  $] sudo apt upgrade $] sudo apt-get install build-essential autoconf automake libxmu-dev python-pygoocanvas python-pygraphviz cvs mercurial bzr git cmake p7zip-full python-matplotlib python-tk python-dev python-kiwi python-gnome2 python-gnome2-desktop-dev python-rsvg qt4-dev-tools qt4-qmake qt4-qmake qt4-default gnuplot-x11 wireshark The above command make take some time to download, compile and install it, Be Patie