C++ Bit Format Flags

The C++ bit format flags changes the characteristics of .The flags hold the current setting for the . We can turn the flags on or off using built-in function such as or . Changing the behavior of the object is useful in displaying output only after bit level manipulation.

Advertisements

Here is the list of flags available in C++ .

Flag NameDescription
skipwsskip whitespace while input
rightleft justify the output
internalpad after sign or base
decdecimal base
hexhexadecimal base
octoctal base
showbasedisplay base for hex and oct
showpointshows all decimal point for float
uppercaseshows hex in uppercase
showposdisplay a + sign for positive numbers
scientificdisplay float numbers with E
fixedfloating point notation
unitbufflush all streams after cin
stdioflush stdout,stderr after cin

setf() function

Advertisements

The function sets the bit format flags. It takes the flag name as the argument. The syntax to use this function is given below.

setf(ios::<flag_name>);

You can use bitwise or operator to set more than one field.

setf(ios::<field1>| ios::<field2> | ios::<field3>);

unsetf() function

The function unsets the bit format flag for object. The general syntax to use this function is given below.

unsetf(ios::<flag_name>);

Example #1 : skipws

Your site doesn’t include support for the CodeMirror Blocks block. You can try installing the block, convert it to a Custom HTML block, or remove it entirely.

Install CodeMirror Blocks
Keep as HTML
//using skipws function
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    //Variable declaration
    
    char animal_name[100];
       
    //Reading values
    
    cout << "Enter an animal name:";
    cin >> skipws;
    cin >> animal_name;
    
    //printing output 
    
    cout << "Number = " << animal_name << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

Enter an animal name:bengal tiger
Number = bengal

Example #2 : right, left, internal and adjustfield

/* Demo of adjustfield, left,right and internal bit format */
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    //variable declaration
    int number1, number2, number3;
    
    //variable initialization
    
    number1 = 100;
    number2 = 230;
    number3 = 436;
    
    //using adjustfield and other bit formats
    
    cout.setf(ios::showpos);
    cout.setf(ios::left,ios::adjustfield);
    cout.width(10);
    cout << number1 << endl;
    
    cout.setf(ios::right,ios::adjustfield);
    cout.width(10);
    cout << number2 << endl;
    
    cout.setf(ios::internal,ios::adjustfield);
    cout.width(10);
    cout << number3 << endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

+100
      +230
+      436 

Example #3 : dec, hex, oct, and showbase

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
    //Variable declaration
    
    int amount;
    
    //Variable initialization
    
    amount = 564;
    
    //Converting base using showbase, hex, oct and dec
    
    cout.setf(ios::showbase);
    cout.setf(ios::hex,ios::basefield);
    cout << "Hexadecimal =" << amount << endl;
    
    cout.setf(ios::oct,ios::basefield);
    cout << "Octal =" << amount << endl;
    
    cout.setf(ios::dec,ios::basefield);
    cout << "Decimal =" << amount << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

Hexadecimal =0x234
Octal =01064
Decimal =564

Example #4 : scientific, fixed, showpoint and floatfield

/* Demo of all floating point format flags - 
floatpoint, showpoint, scientific, fixed 
*/
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ 
    //variable declaration
    
    float pi;
    float radius;
    float area;
    
    //variable initializtiom
    
    pi = 3.14159;
    radius = 2.23;
    area = 0.0;
    
    area = pi * (radius * radius);
    
    // printing result using bit format flags
   
    cout.setf(ios::showpoint);
    cout.precision(3);
    cout.setf(ios::scientific,ios::floatfield);
    cout << "Pi=" << pi << endl;
    cout << "Radius=" << radius << endl;
    cout << "Area=" << area << endl;
    
    cout << endl;
    cout << endl;
    cout.setf(ios::fixed,ios::floatfield);
    cout << "Pi(fixed)=" << pi << endl;
    cout << "Radius(fixed)=" << radius << endl;
    cout << "Area(fixed)=" << area << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

Pi=3.142e+000
Radius=2.230e+000
Area=1.562e+001
Pi(fixed)=3.142
Radius(fixed)=2.230
Area(fixed)=15.623
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version