• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Variables

Contents

  • 1 Declaration of Variable
  • 2 Printing the value of Variable
  • 3 Variable Example
  • 4 Where to declare the variables?
  • 5 Declaring multiple variables in a single line
  • 6 Changing the value in a variable
  • 7 Variable Naming Conventions

Variables allow us to store information, perform calculations and operations on them. They are the named memory locations to store information temporarily. Before storing value in a variable, you need to declare it first using a data type.

Declaration of Variable

int age = 21;

Here, we are declaring an integer variable age using a data type called int and initializing it with value 21. Since we declared this variable with int, it now only allows integer values in this variable.

It is a best practice to initialize a variable as soon as you declare, as non-initialized variables contain undefined value, and if you accidentally use those non-initialized variable, your program may not behave as expected.

Printing the value of Variable

To print the value of the variable we can use the printf function which we saw in our previous section.

printf("Your age is %d", age);

As we want to print the value of the variable, we are passing the age variable as additional argument to printf function. The first argument is the text that gets printed on the screen.

If you observe we’ve written %d at the end of the text. We call it a format specifier. Using this format specifier, we are telling the program to print the variable value as a decimal value on the screen.

Variable Example

#include <stdio.h>

void main() {
    int age = 21;
    printf("Your age is %d", age);
}

Output:

Your age is 21

Where to declare the variables?

The new C compilers allow you to declare the variables wherever you need them, which was not the case with older compilers. Old C compilers require declaring all variables right after you open the curly bracket of a function. Below code works perfectly fine with new C compilers.

#include <stdio.h>

void main() {
    int age = 21;
    printf("Your age is %d", age);

    int yearOfBirth = 2000;
    printf("Year of Birth is %d", yearOfBirth);
}

Declaring multiple variables in a single line

You can declare multiple variables in a single line and initialize them at the same time as follows:

#include <stdio.h>

void main()
{
    int a = 10, b = 20;

    printf("a = %d, b = %d", a, b);
}

But it is a good practice to declare each variable in a new line for better readability.

Changing the value in a variable

Value in a variable can be changed and that is the reason why they are called variables.

#include <stdio.h>

void main()
{
    int n1 = 10, n2 = 20, sum = 0;
    sum = n1 + n2;
    printf("%d + %d = %d", n1, n2, sum);
}

In this program, sum is initialized as 0 when it was declared, but later we are modifying its value to the sum of both n1 and n2.

10 + 20 = 30

Variable Naming Conventions

  • First character of a variable must be an alphabet or underscore
  • Variable declaration doesn’t allow special characters and blanks other than underscore
  • We cannot use C language keywords as a variable name. Keywords are the reserved words that define the semantics of the language.

Below are the valid variable names:

int a123 = 10;
int _123 = 20;
int emp_id = 101;

Below are some invalid variable names:

int int = 10;
int void = 10;
int 123 = 10;
int priceIn$ = 10;

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

  • C Tutorial
    • First C Program
    • Variables
    • Data Types
    • Arithmetic Operators
    • Relational Operators
    • if else statement

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in