C Literals

By | February 14, 2023

In the C programming language, a literal is a value that appears directly in the source code. There are several types of literals in C, including:

  1. Integer literals –
    An integer literal is a whole number without a fractional component, such as 42 or -17.

    int a = 42;
    long b = -17;
    unsigned int c = 123456;
    
  2. Floating-point literals –
    A floating-point literal is a number with a fractional component, such as 3.14 or -0.5.

    float d = 3.14;
    double e = -0.5;
    long double f = 0.000001;
    
  3. Character literals –
    A character literal is a single character surrounded by single quotes, such as ‘A’ or ‘$’.

    char g = 'A';
    char h = '$';
    
  4. String literals –
    A string literal is a sequence of characters surrounded by double quotes, such as “hello” or “Good morning!”.

    char *i = "hello";
    char *j = "Good morning!";
    
  5. Boolean literals –
    A boolean literal is either the value true or false.

    _Bool k = true;
    _Bool l = false;
    

Each type of literal has a specific type in C and must be used in the appropriate context. For example, an integer literal can be used as an argument to a function that takes an int, while a string literal can be used as an argument to a function that takes a char * or a string. Literals play an important role in C programming, as they provide a way to represent constant values in your code.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.