Variables in C Language

When we want to store any information(data) on our computer/laptop, we store it in the computer’s memory space. Instead of remembering the complex address of that memory space where we have stored our data, our operating system provides us with an option to create folders, name them, so that it becomes easier for us to find it and access it. Similarly, in C language, when we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it. The naming of an address is known as variable. Variable is the name of memory location. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer can choose a meaningful variable name. Example : average, height, age, total etc.

Datatype of Variable

A variable in C language must be given a type, which defines what type of data the variable will hold. It can be:

  • Char: Can hold/store a character in it.
  • Int: Used to hold an integer.
  • Float: Used to hold a float value.
  • Double: Used to hold a double value.
  • Void

Difference b/w variable declaration and definition

Variable declaration refers to the part where a variable is first declared or introduced before its first use. Variable definition is the part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.

Is it possible to have separate declaration and definition? It is possible in case of extern variables and functions. See question 1 of this for more details.

RULES FOR NAMING C VARIABLE

1. Variable name must begin with letter or underscore.
2. Variables are case sensitive .
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscore.
5. sum, height, _value are some examples for variable name.

THERE ARE THREE TYPES OF VARIABLES IN C PROGRAM THEY ARE :
1.Local variable
2.Global variable
3.Environment variable

Local Variable In C

  • The scope of local variables will be within the function only.
  • These variables are declared within the function and can’t be accessed outside the function.
  • In the below example, m and n variables are having scope within the main function only. These are not visible to test function.
  • Like wise, a and b variables are having scope within the test function only. These are not visible to main function.

Global Variable In C

  • The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.
  • This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.

Environment Variable In C

  • The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.
  • This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.

Keywords

Keywords are specific reserved words in C each of which has a specific feature associated with it. Almost all of the words which help us use the functionality of the C language are included in the list of keywords. So you can imagine that the list of keywords is not going to be a small one!

There are a total of 32 keywords in C:

   
                            auto       break    case     char     const     continue
                            default    do       double   else     enum      extern
                            float      for      goto     if       int       long
                            register   return   short    signed   sizeof    static
                            struct     switch   typedef  union    unsigned  void
                            volatile   while

Const

const can be used to declare constant variables. Constant variables are variables which, when initialized, can’t change their value. Or in other words, the value assigned to them cannot be modified further down in the program.

Syntax: const data_type var_name = var_value;

Note: Constant variables must be initialized during their declaration. const keyword is also used with pointers. Please refer the const qualifier in C for understanding the same.

Extern

extern simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can me made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.

Syntax: extern data_type var_name = var_value;

Static & Void & Typedef 

static keyword is used to declare static variables, which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere within that file as their scope is local to the file. By default, they are assigned the value 0 by the compiler.

void is a special data type. But what makes it so special? void, as it literally means, is an empty data type. It means it has nothing or it holds no value. For example, when it is used as the return data type for a function it simply represents that the function returns no value. Similarly, when its added to a function heading, it represents that the function takes no arguments. Note: void also has a significant use with pointers. We will See void pointer in C for understanding the same.

typedef is used to give a new name to an already existing or even a custom data type (like a structure). It comes in very handy at times, for example in a case when the name of the structure defined by you is very long or you just need a short-hand notation of a per-existing data type.

C Standard Library Functions

you’ll learn about the standard library functions in C. More specifically, what are they, different library functions in C and how to use them in your program. This Is only for Your Knowledge No one ask any questions on this Topic .. C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of the functions are present in their respective header files, and must be included in your program to access them.

Advantages of using C library functions

  1. Since, the functions are “standard library” functions, a dedicated group of developers constantly make them better. In the process, they are able to create the most efficient code optimized for maximum performance.
  2. Since, the functions are “standard library” functions, a dedicated group of developers constantly make them better. In the process, they are able to create the most efficient code optimized for maximum performance.
  3. Since the general functions like printing to a screen, calculating the square root, and many more are already written. You shouldn’t worry about creating them once again. It saves valuable time and your code may not always be the most efficient.

Lets See an Example to find Squere Root :

  1. #include 
  2. #include 
  3. int main()
  4. {
  5. float num, root;
  6. printf("Enter a number: ");
  7. scanf("%f", &num);
  8. // Computes the square root of num and stores in root.
  9. root = sqrt(num);
  10. printf("Square root of %.2f = %.2f", num, root);
  11. return 0;

}

In this way we can use pre built libraries but This is not good Practice for Beginner .
So try to implement same above code without using any library Wright your own logic