Variables in C - AsgarTech

 Variables in C - AsgarTech


In C programming, there are several data types that can be used to declare variables, including:

int: used to declare integer variables, which can hold whole numbers. For example: int age = 25;

float: used to declare floating-point variables, which can hold decimal numbers with single precision. For example: float height = 5.7;

double: used to declare floating-point variables, which can hold decimal numbers with double precision. For example: double weight = 68.5;

char: used to declare character variables, which can hold single characters. For example: char grade = 'A';

bool: used to declare Boolean variables, which can hold either true or false. For example: bool isStudent = true;

short: used to declare short integer variables, which can hold smaller whole numbers than int. For example: short year = 2023;

long: used to declare long integer variables, which can hold larger whole numbers than int. For example: long population = 8000000;

unsigned: used to declare unsigned integer variables, which can hold only non-negative numbers. For example: unsigned int count = 100;

long double: used to declare floating-point variables, which can hold decimal numbers with greater precision than double. For example: long double pi = 3.14159265359;

Here's an example of how to declare and initialize a variable in C:

int age = 25; // declare and initialize an integer variable named "age" with the value 25

float height; // declare a floating-point variable named "height"

height = 5.7; // assign the value 5.7 to the variable "height"

int x = 10, y = 20, z = 30; // declare and initialize three integer variables "x", "y", and "z" with the values 10, 20, and 30 respectively



Thanks

No comments: