Formatting functions in C Language Formatting means display data in different format, within given set of columns, show specified set of decimal and align the data to left or right along with zero fill options. Format Specifiers Meaning %d or i Decimal format (signed integer) %o Octal format %x or X Hexadecimal format %u Unsigned Integer %c Character format [Single Character Only] %s String format %f Fixed Float %e or E Exponent Float or Scientific Float %g or G General Float %lf %ld double and long double long integer %p Pointer Address %% Print % sign C provides various functions to format the data under stdio.h header file printf() cprintf() sprintf()
fprintf() C provides pre-defined set of codes to display data in different format, align the data and define the decimal points in the output along with zero fill option. Syntax is % flag width.decimals format The flag can be + (default) - 0 to pad with leading zeros # to pad 0 for octal and 0x for hexa decimal numbers
Difference between %d and %i %d and %i are same in case of printf() since they consider base 10 decimal output by default. But while getting input using scanf() they are different. %d always consider base 10 number while %i automatically detects the base, based on 0 for Octal, 0x or 0X for Hexadecimal. %d cannot understand 0x or 0X prefix while %i can understand it.
How we can prove that double takes 8 bytes of memory without using sizeof()? Answer: Using %p or %u format Specifiers to view the address of variables
How to print %d is used for decimal input and output?
How to define columns and decimal points while displaying some output? Standard Output Screen has 25 rows and 80 columns per row. We can divide the 80 columns to display different fields in different columns. By default all data is right aligned. Use hyphen (-) to make it left align.
Decimal point can also be used to extract some characters from a string starting from first position. Example
What is mean by zero fill? While printing the output we can automatically fill zeros in the left side of the number to avoid to write or print data in left side.
What is mean by formatting in scanf() function? We can also format data while getting input For Example WAP to input some date in ddmmyyyy format Now we need to break first 2 digits as day number, next 2 digits as month number and next 4 digits as year.
What is suppression character? Why and how to use it? Special character in C which can be used to avoid (suppress) a character input. It is written as %*c Example WAP to input date of birth in any of format like dd-mm-yyyy dd/mm/yyyy dd:mm:yyyy dd mm yyyy etc.
Here we need to suppress 3 rd character and 6 th character What is mean by cprintf()? The function cprintf() is used to print in color Use textcolor() function to define the text color and textbackground() to define the background color. These functions are provided in conio.h header file. All color names are constants and written in capital. We can use the color name or its value. Example WAP to print "C is a beautiful language in Yellow color on Green background with blinking text
What is sprintf()? The function sprintf() is used to print formatted data into a string and use that date later on. Syntax sprintf(char buffer[],char *format, argumentlist) Example WAP to input two numbers and store the product of those two numbers into a string and print that string on the monitor in Green color. The function sprintf() is best suitable to return some formatted result from a function. Example
WAP having a function which take a year and returns a message as given year is leap year or given year is not a leap year. What is fprintf()? The function fprintf() is used to write some contents to the file. Syntax fprintf(file *fp, format", argumentlist) Example WAP to input two numbers and save product of those numbers into a file.
How to get multiline input using scanf()? You can get the multiline input by defining the terminating character other than \n while getting input with scanf() function Example
With best wishes Prof.(Dr.) B P Sharma ABES Engineering College, Ghaziabad +91 98108 49501