ErrorException Message: WP_Translation_Controller::load_file(): Argument #2 ($textdomain) must be of type string, null given, called in /home/u695865543/domains/ on line 838 What Are C Variables? - Techow99

What Are C Variables?

Variables are containers that store data values like numbers and letters. In C, there are different kinds of variables (defined by different keywords), for example:

  • int – Used to declare variables that store whole numbers (integers) such as 12, 123, 3456.
  • float – Used to declare variables that store single-precision floating-point numbers such as 19.99 or -19.99
  • char – Used to declare variables that store individual characters, such as ‘B’ or ‘a’. Char values are surrounded by single quotes

C Variable Syntax

data type, variable name = value; // this is use for single variable
datatype variable name1, variable name2; // this is used to define multiple variable 

Another brief:

  • data type: Type of data that a variable can store.
  • variable name: Name of the variable given by the user.
  • value: value assigned to the variable by the user.

For Example:

int var;    // That’s called an integer variable 
char a;     // That’s called a character variable
float fff;  // That’s called a float variables

Keep in mind that C is a strongly typed language so all the variable types must be specified before using them.

Declaring (Creating) Variables in C

To create a variable in C, you need to specify its data type followed by the variable name. Here’s an example:

For Example: 

#include <stdio.h>
int main() {
    // Integer variable
    int age; // Declare a variable
    age = 25; // Assign a value to the variable
    // Floating-point variable
    float temperature;
    temperature = 98.6;
    // Character variable
    char grade;
    grade = 'A';
    // Display the values of the variables
    printf("Age: %d\n", age);
    printf("Temperature: %.2f\n", temperature);
    printf("Grade: %c\n", grade);
    return 0;

In this example, we create three variables of different types: age (integer), temperature (floating-point), and grade (character). Each variable is declared with its respective data type and then assigned a value.

After creating the variables and assigning values, we use printf() to display the values of the variables. %d is used as the format specifier for the integer variable, %f for the floating-point variable (with %.2f to display the temperature with 2 decimal places), and %c for the character variable.

When you run this program, it will output:

Age: 25
Temperature: 98.60
Grade: A


Format Specifiers

Format specifiers in C are special placeholders used in format strings within functions like printf() and scanf(). They indicate the expected data type of the corresponding argument to be printed or scanned.

In the context of printf(), format specifiers start with a percent sign % followed by a character that represents the data type to be formatted. Some commonly used format specifiers in printf() are:

%d or %i: Used for printing integer values.

int num = 10;
printf("The number is: %d\n", num);  // Output: The number is: 10

%f: Used for printing floating-point values.

float num = 3.14;
printf("The value of pi is: %f\n", num);  // Output: The value of pi is: 3.140000

%lf: Used for printing double values.

double num = 2.71828;
printf("The value of e is: %lf\n", num);  // Output: The value of e is: 2.718280

%c: Used for printing single characters.

char letter = 'A';
printf("The letter is: %c\n", letter);  // Output: The letter is: A

%s: Used for printing strings (character arrays).

char name[] = "John";
printf("My name is: %s\n", name);  // Output: My name is: John

%p: Used for printing memory addresses (pointers).

int num = 42;
int *ptr = &num;
printf("The memory address of num is: %p\n", (void *)ptr);  
// Output: The memory address of num is: 0x7ffd8cc74134

These are just a few examples of format specifiers in C. There are more format specifiers available for different data types and formatting options. The choice of the format specifier depends on the type of data you want to print or scan and the desired formatting.

Another type

In C, to combine text and a variable in a printf() function, you can use a comma to separate them inside the function call. 

For Example: 

#include <stdio.h>
int main() {
    int num = 42;
    printf("The value of num is: %d\n", num);
    return 0;

In this example, the text “The value of num is: ” is combined with the variable num inside the printf() function. The comma is used to separate the text and the variable. The format specifier %d is used to specify the type of the variable (int in this case), and the corresponding variable num is passed as an argument after the format string.

When you run this program, it will print the combined text and the value of the variable num:

The value of num is: 42

To print different types in a single printf() function, you can use multiple format specifiers and provide corresponding variables or values as arguments. 

For Example: 

#include <stdio.h>
int main() {
    int age = 25;
    float height = 1.75;
    char grade = 'A';
    printf("Age: %d, Height: %.2f, Grade: %c\n", age, height, grade);

    return 0;

When you run this program, it will print the values of age, height, and grade as specified:

Age: 25, Height: 1.75, Grade: A

Change Variable Values

To change the values of variables in C, you can simply assign new values to them using the assignment operator (=). 

For Example: 

#include <stdio.h>
int main() {
    int num = 5;  // Declare and initialize num with the value 5
    printf("Initial value of num: %d\n", num);
    num = 10;  // Change the value of num to 10
    printf("Updated value of num: %d\n", num);
    return 0;

In this example, the variable num is initially assigned the value 5. Then, the value of num is changed to 10 using the assignment statement num = 10; Finally, the updated value of num is printed using printf.

When assigning a new value to a variable, you don’t need to redeclare the variable; you can simply assign a new value to it using the assignment operator (=).

Note: If you assign a new value to an existing variable, it will overwrite the previous value:

Also: you can assign the value of one variable to another variable in C. This is known as a variable assignment or variable copying. 

For Example: 

#include <stdio.h>
int main() {
    int num1 = 5;  // Declare and initialize num1 with the value 5
    int num2;     // Declare num2
    num2 = num1;  // Assign the value of num1 to num2
    printf("num1: %d\n", num1);
    printf("num2: %d\n", num2);
    return 0;

In this example, the value of num1 is assigned to num2 using the assignment statement num2 = num1;. After the assignment, both num1 and num2 will have the same value of 5.

Assigning the value of one variable to another is a common operation in C and is useful for various scenarios, such as storing a calculated result in a different variable or passing the value of one variable to a function using another variable.

Add Variables Together

To add variables together in C, you can use the addition operator (+). 

For Example: 

#include <stdio.h>
int main() {
    int num1 = 5;
    int num2 = 3;
    int sum;
    sum = num1 + num2;
    printf("The sum of %d and %d is %d\n", num1, num2, sum);
    return 0;

In this example, num1 and num2 are two variables containing integer values. The addition operation num1 + num2 adds the values of num1 and num2 together, and the result is stored in the variable sum using the assignment operator (=).

After performing the addition, the value of the sum will be 8. The printf function is then used to display the result.

You can add variables of various data types together using the appropriate operator. For example, if you have two floating-point variables, you can use the addition operator (+) to add them together in a similar manner:

For Example: 

float num1 = 3.14;
float num2 = 2.71;
float sum;
sum = num1 + num2;
printf("The sum of %.2f and %.2f is %.2f\n", num1, num2, sum);

In this case, the result will be a floating-point sum with two decimal places.

Declare Multiple Variables

In C, you can declare multiple variables of the same type in a single declaration statement by separating them with commas. Here’s an example:

For Example: 

#include <stdio.h>
int main() {
int num1 = 5, num2 = 10, num3 = 15;
float float1 = 2.5, float2 = 3.7, float3 = 5.6;
printf("%d\n", num1+num2+num3);
printf("%.2f", float1+float2+float3);
return 0;



You can also assign the same value to multiple variables of the same type:

For Example: 

#include <stdio.h>
int main() {
int num1, num2, num3;
num1 = num2 = num3 = 50;
printf("%d", num1 + num2 + num2);
return 0;



C Variable Names

In C, variables are identified by unique names called identifiers. Identifiers are used to name variables, functions, and other entities in your code. It is indeed recommended to use descriptive names for your identifiers to create more understandable and maintainable code.

Using meaningful and descriptive names for your identifiers helps to improve code readability, making it easier for you and other developers to understand the purpose and functionality of variables and functions. Descriptive names can convey the intent and purpose of the identifier, making the code self-explanatory.

Here are some examples of using descriptive names for variables:

int studentAge = 25; 
float studentFee = 75.25;
char employeeName[50] = "Ajay"; 
int numberOfStudents = 80; 
double averageTemperature = 30.00;

In the examples above, the variable names are descriptive and provide information about the data they represent. This makes it easier for someone reading the code to understand the purpose and meaning of each variable.

By using descriptive names for your identifiers, you make your code more maintainable, as it becomes easier to understand and modify when needed. It is a good practice to choose meaningful and descriptive names that reflect the purpose and context of the entities they represent in your code.

What Rules for Naming Variables in C?

While naming variables in C, you need to follow specific rules. Here are the rules for naming variables in C:

  • A variable name must only contain alphabets, digits, and underscore.
  • A variable name must start with an alphabet or an underscore only. It cannot start with a digit.
  • No whitespace is allowed within the variable name.
  • A variable name must not be any reserved word or keyword.

Read More: What Is The Difference Between An Algorithm And A Program?

Tim R
Tim R
This is Tim, your friendly neighborhood tech geek. With a passion for all things geeky, I'm here to share the latest tech scoop and unravel the mysteries of the digital world. From gadgets to innovations, I've got you covered with my insightful and down-to-earth articles. So buckle up and get ready to embark on an exciting journey through the ever-evolving realm of technology!

Similar Articles



Please enter your comment!
Please enter your name here

Follow us

Most Popular