Data Types And Constant In C

Posted on October 22, 2023 by Vishesh Namdev
Python C C++ Javascript Java
C Programming Language

Data types in C define the type of data a variable can hold and the operations that can be performed on that data. C provides several fundamental data types, including:

1. int : Used for integer values, e.g., 22, -18.

2. float : Used for single-precision floating-point numbers, e.g., 2.18

3. double : Used for double-precision floating-point numbers, e.g., 1.9987845623

4. char : Used for individual characters, e.g., 'V', '22'.

5. Bool : Used for Boolean values, typically 0 (false) or 1 (true)

C also allows for the creation of user-defined data types through structs and enums, and it supports modifiers like long, short, and unsigned to further specify data type characteristics. Data types are crucial for memory allocation, arithmetic operations, and ensuring data integrity in C programs.

int age = 22 ; Copy Code
float number = 2.18 ;
char grade = 'A' ;
double pi = 3.14159265359 ;
_Bool is_true ;

Basic Data types in C:

Here's a table summarizing commonly used data types in C programming for quick reference:

Basic Data types in C

These data types are essential for defining the type of data a variable can store in C and for performing various operations in your C programs.

Example of Basic Data types:

#include < stdio.h > Copy Code
int main ()
{
//Now we create some variables of different Data types
int age = 18 ;
float num = 1.88 ;
char alpha = 'A' ;
double large_num = 8.2563149578 ;
// Now we print Value of every Data type
printf ( "%d " , age );
printf ( "%f " , num );
printf ( "%c " , alpha );
printf ( "%lf " , large_num );
return 0;
}

So, that's the code that lets you print any value using any data types

Constants

In C, a constant is a value that cannot be changed or modified during the execution of a program. Constants are used to represent fixed values, such as numbers, characters, or strings.

Here's the syntax to define a Constant in our C program

const data_type variable_name = any_fix_value ;

Types of Constant in C:

1. Integer Constant: These represent whole numbers and can be either positive or negative. For example:

const int myIntegerconstant = 22 ;

2. Floating-Point Constants: These represent decimal numbers. For example:

const float myfloatconstant = 22.18 ;

3. Character Constants: These represent individual characters and are enclosed in single quotes. For example:

const char myCharconstant = "V" ;

4. String Constants: These represent sequences of characters and are enclosed in double quotes. For example:

const char myStringconstant = "The Royal Coding" ;

5. Symbolic Constants: These are often defined using the #define preprocessor directive and are used to give a name to a constant value. For example:

#define PI 3.14159265359
const float myfloatconstant = 22.18 ;