DEV Community

Cover image for Variables in C Programming
Sujith V S
Sujith V S

Posted on • Updated on

Variables in C Programming

A variable is a name given to a memory location inside our computer where we can store data.

Variable declaration
int age;
In C programming, a variable declaration is a statement that informs the compiler about the name and type of a variable without allocating any memory for it. It essentially tells the compiler that a variable with a specific name and data type will be used in the program. Variable declarations are necessary before you can use a variable in your program.

Variable Initialisation
age = 22
Variable initialisation in C refers to the process of assigning an initial value to a variable when it is declared.

We can write variable declaration and initialisation in single line: int age = 22;

Changing values in variable

#include <stdio.h>

int main(){
    int age = 25;
    printf("Age: %d", age);

    age = 31;
    printf("\nNew AGE: %d", age); 

    return 0;
}
Enter fullscreen mode Exit fullscreen mode
  • %d is format specifier. It is a special character sequence used in the printf and scanf functions (and related functions) to specify the type and format of the data that will be input or output.

  • \n is used for new line.

Assigning one variable to another variable

#include <stdio.h>

int main(){

    int firstNumber = 33;
    printf("firstNumber = %d", firstNumber);

    int secondNumber = firstNumber;
    printf("\nsecondNumber = %d", secondNumber);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Declaring two variable in a single line

#include <stdio.h>

int main(){

    int variable1, variable2=25;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Variable naming convention

  • Use camel case variable like myAge, firstName, lightSpeed. Cannot:
    • Create variable names with space in between. int first number
    • Start variable names with numbers. int 1number
    • Use keywords as variables. int if

Top comments (4)

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Dennis Ritchie (the creator of C) disagrees with the use of CamelCase. It is not the common naming convention in C.

You neglected to mention that:

  • _ counts as a letter.
  • External names that begin with _ are reserved for use by the implementation.
  • Any names that begin with a _ followed by either a capital letter or _ are also reserved for use by the implementation.

The full list is here.

Your example of declaring two variables in the same line makes it seem like both are initialized to 25 when that is not true.

Collapse
 
sujithvsuresh profile image
Sujith V S • Edited

Thanks for your valuable comment. But what is the problem with declaring two variables in a single line. I have done that in many occasions for declaring and initialising same type of values.

Collapse
 
pauljlucas profile image
Paul J. Lucas

Strictly speaking, nothing. But if you read what I actually said, your example:

int variable1, variable2 = 25;
Enter fullscreen mode Exit fullscreen mode

looks to someone not familiar with C like that might assign 25 to variable2 and also to variable1 when in reality variable1 is uninitialized.

Thread Thread
 
sujithvsuresh profile image
Sujith V S

Yeah okay.