×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Arguments for printf() that formats a long datatype

In this tutorial, we will learn about the arguments used with printf() function for a long datatype.
Submitted by Shubh Pachori, on July 10, 2022

In C programming language there are many long datatypes like signed long int or long int, unsigned long int, signed long long int or long long int, unsigned long long int, and long double as well. These data types are used to store much bigger values than in normal data types.

Brief description of long datatypes

  1. signed long int or long int: The size of these datatypes is 4 bytes and its range is (-231) to (231 -1). The format specifier is used for long int are %ld and %li.
  2. unsigned long int: The size of these datatypes is also 4 bytes and its range is 0 to (232 -1). The format specifier is used for unsigned long int is %lu.
  3. signed long long int or long long int: The size of these datatypes is 8 bytes. The format specifier is used for long long int are %lld and %lli.
  4. unsigned long long int: The size of these datatypes is 8 bytes. The format specifier is used for unsigned long long int is %llu.
  5. long double: The size of these datatypes is 10 bytes. The format specifier is used for unsigned long double is %Lf.

The argument for printf() function that formats a long variable depends on the datatypes which you're referring to like long int, long long int, or long double. There is a different argument used in every different datatype.

Example of Arguments for printf() that formats a long datatype

C language code for understanding of the arguments in printf() function

#include <stdio.h>

int main()
{
    long int li; //declaring a long int variable
    unsigned long int uli; //declaring an unsigned long int variable
    long long int lli; //declaring a long long int variable
    unsigned long long int ulli; //declaring an unsigned long long int variable
    long double ld; //declaring a long double variable

    printf("Enter long int: ");
    scanf("%ld", &li); //argument in printf for long int(%ld)

    printf("Enter unsigned Long Int: ");
    scanf("%lu", &uli); //argument in printf for unsigned long int(%lu)

    printf("Enter long long int: ");
    scanf("%lld", &lli); //argument in printf for long long int(%lld)

    printf("Enter unsigned long long int: ");
    scanf("%llu", &ulli); //argument in printf for unsigned long long int(%llu)

    printf("Enter long double: ");
    scanf("%Lf", &ld); //argument in printf for long double(%Lf)

    printf("\nlong int: %ld", li);
    printf("\nunsigned long int: %lu", uli);
    printf("\nLong long int: %lld", lli);
    printf("\nunsigned long long int: %llu", ulli);
    printf("\nlong double: %Lf", ld);

    return 0;
}

Output:

Enter long int: 12345
Enter unsigned Long Int: 1234567
Enter long long int: 123456789
Enter unsigned long long int: 1234567890987
Enter long double: 123456.678

long int: 12345
unsigned long int: 1234567
Long long int: 123456789
unsigned long long int: 1234567890987
long double: 123456.678000



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.