🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C Data Types & Constants

C Data Types & Constants

Data Types in C

As explained previously, a variable in C must be a specified data type, and you must use a format specifier inside the printf function to display the value present in the variable. The data type specifies the size and type of information the variable will store.

Data Types
Data Type Size Description Format Specifier
int 2 or 4 bytes Stores whole numbers, without decimals %d or %i
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits %f
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits %lf
char 1 byte Stores a single character/letter/number, or ASCII values %c
Sub Data Types
short int 2 bytes Stores whole numbers, without decimals %sd
long int 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits %ld
unsigned short int 2 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits %usd
unsigned long int 4 bytes Stores a single character/letter/number, or ASCII values %uld

Here’s how we make use of the data type of a variable to print it.

        
#include <stdio.h>

int main() {
    // Creating variables having different data types
    int integer = 26;
    float floating = 39.32;
    char character = 'A';

    // Printing variables with the help of their respective format specifiers
    printf("%d\n", integer);
    printf("%f\n", floating);
    printf("%c\n", character);
}
        
    

Output:

        
26
39.320000
A
        
    

C Constants

A. When you don't want the variables you declare to get modified intentionally or mistakenly in the later part of your program by you or others, you use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only).

B. You should always declare the variable as constant when you have values that are unlikely to change, like any mathematical constant as PI.

C. When you declare a constant variable, it must be assigned a value.

Here’s an example of how we declare a constant.

        
#include <stdio.h>

int main() {
    const int MOD = 10000007;
}
        
    

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.