Format Specifiers in C Programming

In C programming there are number of data types and format specifiers is used to defines the type of data to be printed.

Whether to print output or to take input in both case we required format specifiers. Format specifiers are also called as format string.

list of format specifiers used in C language.

Format specifier Description Supported data types
%c Character char
unsigned char
%d Signed Integer short
unsigned short
int
long
%e or %E Scientific notation of float values float
double
%f Floating point float
%g or %G Similar as %e or %E float
double
%hi Signed Integer(Short) short
%hu Unsigned Integer(Short) unsigned short
%i Signed Integer short
unsigned short
int
long
%l or %ld or %li Signed Integer long
%lf Floating point double
%Lf Floating point long double
%lu Unsigned integer unsigned int
unsigned long
%s String char *
%u Unsigned Integer unsigned int
unsigned long

How to print integer character float and double using format string

Print integer : we can print integer value by using %d. for example:

Result

Here whatever written within double code “  ” print as it is.  %d is used to tell compiler print integer value. And value of  “ i  (integer value) ” will be print.

Print multiple integer : suppose we want to print two integer value then we need to write two times  %d. for example:

Output

Here whatever written within double code “  ” print as it is.  %d%d is used to tell compiler print two integer value. And value of  “ i & j  (integer value) ” will be print.

Print Character and float value : suppose we want to print two character and float value then we need two format string  %c  and %f  respectively. for example:

Here whatever written within double code “  ” print as it is.  %d%d is used to tell compiler print character and float value. And value of  “ c & f  ” will be print.

How to take input from user : scanf()

scanf() function is used to read character, string, numeric data from keyboard. The scanf() function allows you to accept input from keyboard.

Take  integer input from user : we can take input integer value by user using %d format string. for example:

above line will read an integer value that the user enters on the keyboard. %d is used to tell compiler to read integers value from keyboard and store in a integer variable i.

Take  multiple integer input from user: we can take two integer value as s input  by user using  two times %d  format string. for example:

above line will read two integer value that the user enters on the keyboard. Two times %d is used to tell compiler to read two integers value from keyboard and store in a integer variable i & j

Note: similarly to take char, float value from user %c & %f formatted string used in a scanf() function.

Variables in C Programming

Variables is a name given to memory location. 

            int x =10;                       

                                                                    

here, x is a integer variable.                                                     

Integer variable x is a name given to memory location and where we stored integer value10.

Initialize multiple variable : we can also initialize multipleby comma separated list.

int  a,b,c;         

Here, we have initialized three integer variable a,b & c. variable  ‘a’  stores 10, variable ‘b’ stores 20 and variable ‘c’ stores 30.

Initialize multiple variable : we can also initialize multipleby comma separated list.

A Simple C Program

A simple C program basically consists of the following parts −

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Basic structure of C program

#include

 Above shown the basic structure of C program. For every program we have to write above mention basic structure.

How to write or print

We can write or print in C language by using printf() function. printf() function is a predefined function. for example: code to write “hello” is as follows:

printtf(“hello”);

output:   hello

Note: any thing written within double code in a printf() function will be write.

A simple C program to print “Hello”

Description

  • First line  #include <stdio.h> is a preprocessor, which instruct to C compiler to include stdio.h file. stdio.h (slandered input output ) is used for printf function.
  • int main() is the main function from where the program execution begins.
  •  /*………..*/  is a “comments” in the program. This comments will be ignored by the compiler during execution of program
  •  printf() is a predifned function in C which is used to print message.
  •  line return 0; terminates the main() function and returns the value 0.return 0 tells the compiler that everything is ok and the execution of main() function ends. Because 0 is the code for successful execution, while values greater than 0 (integers) is an indication that something went wrong. 

 

Compile and Execute C Program in Unix

How to save and compile the source code and run it:

  • Write the above-mentioned code on a text editor.
  • Save the file as hello.c
  • Open a command prompt and go to the directory where you have saved the file.
  • To compile your code: type gcc hello.c and press enter.
  • If there are no errors in your code, the command prompt will take you to the next line and generate a.out executable file.
  • To execute your program, type  a.out  and press enter.
  • Output “Hello” will print on the screen.

COMPILATION

OUTPUT

GCC installation on linux unix

In a Linux or UNIX, to check whether GCC is installed on your system  enter the following command in the command line −

If GCC compiler installed on your system, then it should print a message as follows −

If GCC is not installed, then you will have to install it yourself

On ubantu to install GCC write the following command in terminal

History of C Programming

C is computer programming language developed in 1972 by Dennis M. Ritchie.

C programming language developed in  Bell Laboratories to develop the UNIX operating system.

C is a  high-level language which can be easily read and written by any one.

C is a case-sensitive programming language. Thus, “HELLO” and “Hello” two different identifiers in C.