Variables in C

In C programming language, a variable is a named location in memory that is used to store a value. Variables can be defined using a specific data type, such as int, char, float, or double.

To define a variable in C, you need to provide the following information:

1. Data type: The data type specifies the type of value that the variable can hold, such as int, char, float, or double.

2. Variable name: The variable name is used to refer to the variable in the program.

3. Initial value (optional): You can optionally initialize a variable to a specific value when you define it.

Here’s an example of how to define and use variables in C:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
int age = 25; // Define an int variable named age and initialize it to 25
char grade = 'A'; // Define a char variable named grade and initialize it to 'A'
float pi = 3.14; // Define a float variable named pi and initialize it to 3.14
double salary = 50000; // Define a double variable named salary and initialize it to 50000
printf("I am %d years old\n", age); // Print the value of age
printf("My grade is %c\n", grade); // Print the value of grade
printf("The value of pi is %.2f\n", pi); // Print the value of pi with 2 decimal places
printf("My salary is $%.2f\n", salary); // Print the value of salary with 2 decimal places
return 0;
}
</stdio.h>
#include <stdio.h> int main() { int age = 25; // Define an int variable named age and initialize it to 25 char grade = 'A'; // Define a char variable named grade and initialize it to 'A' float pi = 3.14; // Define a float variable named pi and initialize it to 3.14 double salary = 50000; // Define a double variable named salary and initialize it to 50000 printf("I am %d years old\n", age); // Print the value of age printf("My grade is %c\n", grade); // Print the value of grade printf("The value of pi is %.2f\n", pi); // Print the value of pi with 2 decimal places printf("My salary is $%.2f\n", salary); // Print the value of salary with 2 decimal places return 0; } </stdio.h>
#include 

int main() {
    int age = 25;          // Define an int variable named age and initialize it to 25
    char grade = 'A';      // Define a char variable named grade and initialize it to 'A'
    float pi = 3.14;       // Define a float variable named pi and initialize it to 3.14
    double salary = 50000; // Define a double variable named salary and initialize it to 50000

    printf("I am %d years old\n", age);         // Print the value of age
    printf("My grade is %c\n", grade);          // Print the value of grade
    printf("The value of pi is %.2f\n", pi);    // Print the value of pi with 2 decimal places
    printf("My salary is $%.2f\n", salary);     // Print the value of salary with 2 decimal places

    return 0;
}

In this example, we define four variables of different data types and initialize them to specific values. We then use the printf function to print the values of the variables to the console.