C Variables And Constants

In a C program, some values do not change and some changes with the execution of the program. A constant never changes throughout the program and the variable changes frequently, hence the name.

Advertisements

Types of Constants

Constants can be classified into different types such as numeric constants and character constants. The following figure depicts the types of constants available with C language.

Types of Constants in C language
Figure 1 – Types of Constants in C language

Numeric Constants and Character Constants

The numeric constants involve numbers such as integers and real numbers with decimal part – floating point number and double numbers are most popular types.

The character types are single character constant enclosed using single quotes and string constants are enclosed in double quotes. You can call string constant as a character array. For example,

/* Character constants */
'c'
'A'
/* String constants */
"Elephant"
"Oranges"

Named Constants

Some constant can be named in C programs. This is achieved with the help of C preprocessor commands. The C preprocessor will turn these constants in to values before execution of a program.

#define MAX 100

The command is #define and the name of the constant is MAX with a value of 100.

Rules to Construct Constants and Variables

There are rules to construct constants and variables.

Rules for Constants

You can use a constant directly into an expression in C programs. C programming language will allow only specific operation of a type constant.

For example, you can perform arithmetic operations on numbers, but not on characters.

Rules for Variables

There are important rules to remember before you create a variable. These rules are listed below.

Rule 1: Variable names always start with an underscore or a character.

Advertisements

Rule 2: No other special character allowed in variable names except an underscore.

Rule 3: No white space or a comma allowed in a variable name.

Rule 4: Variable name length depends on the compiler.

This is not a rule, but a best practice – always use meaningful variable names in your program.

Syntax for Constants and Variables

There is no specific syntax for a constant, you can use them directly in your programs.

For example,

A = 10 + 30;
printf ( "%d\n", 100 + 30);

Every variable must be declared and initialized before you use it in a C program. The syntax to declare a variable requires

  • the type of data for the variable
  • a user-defined name for the variable.

For example,

int number;
char grade;

For example,

number = 100;
grade = 'A';

You can initialize a variable at the time of declaration itself.

For example,

int number = 200;
char key = 'B';

Note:- Every statement in C programming language must end with a semi-colon;

Example Program

#include <stdio.h>;
int main() {
    int number;
    float sum;
/* Variable Declaration with data type */
    sum = 0.0;
    number = 1000;
/* Variable initialization for sum and number */
    printf(" %d\n", number);
/* Printing output to console */
    printf(" %d\n", 10.23 + 3.7);
    system("PAUSE");
    return 0;
}

Output

The output of the above example program is given below.

1000
-171798692

References

  • Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
  • Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.
  • Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.