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

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.