We can help you develop your capabilities until you achieve your dream

Digital marketing

Format Specifiers in C: Syntax and Examples

In C programming language, values can be type integer, floating-point, single character, or sequence of characters. We use format specifiers in C to display values of a variable of a different type. 

C contains different format specifiers used in printf() and scanf() functions; in this tutorial, we will go through a few important and most commonly used format specifiers in our C programs.

Want a Top Software Development Job? Start Here!

Full Stack Developer – MERN StackExplore Program

Want a Top Software Development Job? Start Here!

Why Do We Use C Format Specifiers?

Format specifiers in C are used to take inputs and print the output of a type. The symbol we use in every format specifier is %. Format specifiers tell the compiler about the type of data that must be given or input and the type of data that must be printed on the screen.

Now that you have a brief idea of format specifiers in C, we have a few lists of format specifiers moving ahead.  

The Most Commonly Used Format Specifiers in C

The Most commonly used format specifiers are given below:

Format Specifiers 

Type of Output

%d or %i

A decimal integer or signed integer 

%c

Signed character 

%f

Signed float

%e

A floating-point number

%s 

A string or sequence of character 

%lf

double

%Lf

Long double 

%o

%u

Octal integer 

Short unsigned integer

%ld 

Long decimal integer

%x

Hexadecimal integer

%p

Print memory address in the hexadecimal form

We will go through a few examples that will help you understand how to use format specifiers in printf() and scanf() functions for a better understanding.

Want a Top Software Development Job? Start Here!

Full Stack Developer – MERN StackExplore Program

Want a Top Software Development Job? Start Here!

%d (Decimal Integer) Format Specifier 

#include

int main()

{

    int a=50;

    printf(“The integer value of a is %d \n”,a);

    return 0;

}

Output:

Specifiers_In_C_1

%c (Character) Format Specifier

#include

int main()

{

    char s;

    printf(“Enter the character \n”);

    scanf(“%c”,&s);

    printf(“The character is: %c”,s);

    return 0;

}

Output:

Specifiers_In_C_2

%f (Floating Point) Format Specifier

#include

int main()

{

    float a=3;

    printf(“The floating point of a is %f \n”,a);

    return 0;

}

Output:

Specifiers_In_C_3

%e (Floating Pointer Number) Format Specifier

#include

int main()

{

    float a=12.5;

    printf(“The floating-point of a is %e \n”,a);

    return 0;

}

Output:

specifiers_In_C_4

%s (String) Format Specifier

#include

int main()

{

    char s[15]=”simplilearn”;

    printf(“The string value of s is %s \n”,s);

    return 0;

}

Output:

Specifiers_In_C_5.

%lf (Double) Format Specifier

#include

int main()

{

    double d=12.5;

    printf(“The double value of d is %lf \n”,d);

    return 0;

}

Output:

Specifiers_In_C_6.

Master Core Java 8 Concepts, Java Servlet & More!

Java Certification TrainingENROLL NOW

Master Core Java 8 Concepts, Java Servlet & More!

%o (octal integer) Format Specifier

#include

int main()

{

    int oct=11;

    printf(“The octal integer value of oct is %o \n”,oct);

    return 0;

}

Output:

Specifiers_In_C_7

%x (Hexadecimal Integer) Format Specifier

#include

int main()

{

    int h=14;

    printf(“The hexadecimal value of h is %x \n”,h);

    return 0;

}

Output:

Specifiers_In_C_8.

%p (Prints Memory Address) Format Specifier

To find the memory address that holds values of a variable, we use the %p format specifier, and it prints in hexadecimal form.

#include

int main()

{

    int sum=0;

    printf(“The memory address of sum is %p \n”,&sum);

    return 0;

}

output:

Specifiers_In_C_9

With that, you can conclude this tutorial on Format Specifiers in C

Next Steps

Data Structures in C” can be your next topic. So far, you have learned why we use format specifiers in C. The next fundamentals will be the data structures and the varieties in data structures used for different purposes.

If you are interested in building a career in software development, then feel free to explore Simplilearn’s Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages today? If yes, our Full Stack Developer – MERN Stack should be the right thing for your career. Explore the course and enroll soon.

Please let us know in the comment section below if you have any questions regarding the “Format specifiers in C ” tutorial. Our experts will get back to you at the earliest.


Source link

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى

Please turn off the ad blocker, as ads are the only source of our continuity

برجاء دعمنا عن طريق تعطيل إضافة Adblock