Getting Started With C

Declaration of Variables

In C, variable names are identifiers that you use to name variables. Here are the rules of construction for variable names in C:

  1. Syntax:
  2. Variable names must begin with a letter (a-z, A-Z) or an underscore (_). After the first character, variable names can also contain digits (0-9).
                int my_variable;

  3. Length:
  4. The length of a variable name can vary, but only the first 31 characters are significant. However, it's good practice to keep variable names meaningful and not too long for readability.
     int a_very_long_variable_name; // valid, but not recommended

  5. Case Sensitivity:
  6. C is case-sensitive, so uppercase and lowercase letters are considered different.
    int my_variable;
                int My_Variable; // different variable from my_variable

  7. Keywords:
  8. Variable names cannot be the same as C keywords (reserved words).
                int if; // invalid, 'if' is a keyword
                

  9. Variable names cannot contain spaces or special characters (except underscore _).
    int my variable; // invalid, contains a space
                    int my_variable!; // invalid, contains a special character (!)
                

  10. Names:
  11. Certain names may be reserved by the implementation, so it's best to avoid using them.
    int int; // invalid, 'int' is a restricted name

  12. Global vs. Local Scope:
  13. Variable names declared in different scopes (e.g., global vs. local to a function) can have the same name without conflict.
                                int global_variable;
                             
                                void my_function() {
                                    int local_variable;
                                    // Both 'global_variable' and 'local_variable' are valid names
                                }

Resources :

Featured
Special title treatment

With supporting text below as a natural lead-in to additional content.

Q1. What is the output of this program?

Program.c


Q2. Which of the following is not a valid declaration in C?
                            1. short int x;
                           2. signed short x; 
                           3. short x; 
                           4. unsigned short x;