C Programming

How to Declare Variables in C

“A variable is simply a name that is assigned to a storage space so it will be easy for users to access or read in the program. The size, layout of a variable’s memory, and the range of values or set of different operations that can be implemented on the variable are all recognized by the type of variable, and each variable is unique in C programming language. It is understandable that the variable declared in a program can be edited anywhere in the code. You can assign the variable names as any number, letter, or character. Because C is a case-sensitive language, so the uppercase and lowercase characters are separate.

C programming language allows us to define various data types such as Integer, float, char, string, etc., which we will learn with various examples.”

Example # 01

In this example, we will see how to initialize an integer value in the C programming language.

Variables that are initialized without any value, like in line 3, are undefined. Also, the values that are only initialized with NULL. In a declaration, variables can be initialized (given a base value). A constant expression is mentioned after the equal sign when initializing; as you can see in the above image in the 4th line, a variable “a” is initialized with the value of 10 as an integer.

In the 5th line, the print command is called to display the string “value of a:” and the value stored in the variable “a,” as shown below.

Another data type in C is a float which is a value in floating points with one precision.

In the second line variable, “a” is declared of type float without any value, which means it is undefined, and the compiler will set any garbage value as its base value. In the next line, a decimal value of “10.58” is assigned to the variable “a.” In the 5th line, the print command is written to display the value stored in the variable “a,” as shown below.

In the above image, line three, “int a, b, c,” means that the compiler needs to create integer variables with the names a, b, and c, respectively. The variables in the above statement are defined.

The next line assigns the value “10” to variable a, and the next line assigns the value “20” to variable b. The sixth line assigns the arithmetic sum of the values of variables a and b to the third variable c.

In the 7th line, the print command is written to display the string “value of c:” with the integer value stored in c.

Now we will explore another type of variable, which is an integer array. The syntax to declare an integer array is int <variable name>[size] = {elements} as shown in line 4 below. In the next line, for loop is run with a print command to display all the values in the array line by line.

Example # 02

In this example, we will learn how to declare char arrays with strings, as the C language does not support string data types.

Here in line 6, the data type is char, and empty brackets [] indicate the size of the char array is undefined. To the right side of the ‘=‘ string is created, “Hello.” The size of the string is 6, with 5 letters and a null character at the end (\0), which is not visible, to indicate the end of the string. This string is stored in the variable “a,” which is of char type. In the next line, the print function is called to display the string, and the output is shown below.

In this example, we have included the size of char as 50, and the string value is assigned to the variable “a.” As the string’s size is less than the size defined, the whole string is displayed as shown below.

In the second line, “ABC” is assigned the value 20 as global, which means it will remain constant throughout the program. A new variable “s” of char data type is created with an undefined base value as there is no ‘=‘ after it. Here we have used the fgets function, which allows the user to use the fgets() method to enter certain characters followed by the enter key. If you want to make the array a string, then you need to append the null character.

You can use the space-separated string in a program using the fgets() function. The use of the fgets() function is to return a string. What makes it different from the gets() function is that the fgets() function ensures that no more characters than the maximum size are read. After reading the input, it stores it in the variable “s.” The puts() function is used here to display the string input. This function prints the value passed into it while adding a new line at the end, so we would not need “/n” to go to the next line.

Example # 03

In these examples, we will discuss another way to declare variables with the method of “extern.” External variables can also be referred to as global variables. The functions can change the values of global variables. The term “extern” is used to declare and define external variables.

These variables are only declared, not defined. In the following example, 3 extern variables are declared before the function. Inside the function, they are assigned different values where c is the arithmetic sum of variables “a” and “b”, which is proved at the output terminal.

Conclusion

In this article, we have learned that the declaration of variables is valuable when multiple files are in use, and you need to specify your variable in any of the files which will be accessible and usable when the application is linked. You can declare a variable more than one time in the C program, but it only can be defined once in a function, file, or piece of code in your program.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.