Variables & Constants

Thanks for Visiting here

Print Friendly and PDF

Variables 

A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.


It is a way to represent memory location through symbol so that it can be easily identified.


Let's see the syntax to declare a variable:type variable_list;  
The example of declaring the variable is given below:
» int a;  
» int _ab;  
» int a30;  

Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:

» int a=10,b=20;//declaring 2 variable of integer type  
» float f=20.8;  
» char c='A';  


Rules for defining variables

→ A variable can have alphabets, digits, and underscore.
→ A variable name can start with the alphabet, and underscore only. It can't start with a digit.
→ No whitespace is allowed within the variable name.
→ A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Valid variable names:

» int a;  
» int _ab;  
» int a30;  

Invalid variable names:

» int 2;  
» int a b;  
» int long;  

Types of Variables in C

There are many types of variables in c:
» local variable
» global variable
» static variable
» automatic variable
» external variable

1. Local Variable

A variable that is declared inside the function or block is called a local variable.
It must be declared at the start of the block.

void function1(){  
int x=10;//local variable  
}  

You must have to initialize the local variable before it is used.

2. Global Variable

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions.
It must be declared at the start of the block.

int value=20;//global variable  
void function1(){  
int x=10;//local variable  
}  

3. Static Variable

A variable that is declared with the static keyword is called static variable.
It retains its value between multiple function calls.

void function1(){  
int x=10;//local variable  
static int y=10;//static variable  
x=x+1;  
y=y+1;  
printf("%d,%d",x,y);  
}  

If you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.


4. Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

void main(){  
int x=10;//local variable (also automatic)  
auto int y=20;//automatic variable  
}  

5.External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.

myfile.h

extern int x=10;//external variable (also global)  

 


Constants

A constant is a value or variable that can't be changed in the program, for example: 10, 20, 'a', 3.4, "c programming" etc.
There are different types of constants in C programming.

List of Constants in C

ConstantExample
Decimal Constant10, 20, 450 etc.
Real or Floating-point Constant10.3, 20.2, 450.6 etc.
Octal Constant021, 033, 046 etc.
Hexadecimal Constant0x2a, 0x7b, 0xaa etc.
Character Constant'a', 'b', 'x' etc.
String Constant"c", "c program", "c in codesikhe" etc.

Character Constants

Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable of char type.

A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

There are certain characters in C that represent special meaning when preceded by a backslash for example, newline (\n) or tab (\t).

Following is the example to show a few escape sequence characters −


#include <stdio.h>

int main()
 {
   printf("Hello\tWorld\n\n");

   return 0;
}


When the above code is compiled and executed, it produces the following result −

Hello   World

String Literals

String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
You can break a long line into multiple lines using string literals and separating them using white spaces.

Here are some examples of string literals. All the three forms are identical strings.

"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"

Defining Constants

There are two simple ways in C to define constants −

→ Using #define preprocessor.
→ Using const keyword.

The #define Preprocessor

Given below is the form to use #define preprocessor to define a constant −

#define identifier value

The following example explains it in detail −
#include <stdio.h>

#define LENGTH 10  
#define WIDTH  5
#define NEWLINE '\n'

int main() {
   int area;  
 
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);

   return 0;
}

The const Keyword

You can use const prefix to declare constants with a specific type as follows −

const type variable = value;

The following example explains it in detail −

#include <stdio.h>
int main() {
   const int  LENGTH = 10;
   const int  WIDTH = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);
   return 0;
}
When the above code is compiled and executed, it produces the following result −

value of area : 50

Note that it is a good programming practice to define constants in CAPITALS.

Comments

Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types of comments in the C language.

  1. Single Line Comments
  2. Multi-Line Comments

⪼ Single Line Comments

Single line comments are represented by double slash \\. Let's see an example of a single line comment in C.

#include<stdio.h>    
int main(){    
    //printing information    
    printf("Hello C");    
return 0;
 }   
Output:

Hello C
Even you can place the comment after the statement. For example:


printf("Hello C");//printing information  

 

⪼ Mult Line Comments

Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many lines of code, but it can't be nested. Syntax:

/*  
code
to be commented
*/  

Let's see an example of a multi-Line comment in C.

#include<stdio.h>    
int main(){    
    /*printing information  
      Multi-Line Comment*/  
    printf("Hello C");    
return 0;  
}    

Output:
Hello C

Escape Sequences

Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc.

In order to use these characters, escape sequences are used.

Escape Sequences
Escape SequencesCharacter
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null character

For example: \n is used for a newline. The backslash \ causes escape from the normal way the characters are handled by the compiler.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad