Variables and Constants



Variables

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:

int playerScore = 95;

Here, playerScore is a variable of integer type. The variable is holding integer 95 in above program.

The value of an variable can be changed, hence the name 'variable'.



Rules for writing variable name in C :

  1. A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.
  2. The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with a system name and may cause errot.
  3. There is no rule on how long a variable can be. However, the first 31 characters of a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.

In C programming, you have to declare a variable before you can use. It is a common practice in C programming to declare all the variables at the beginning of the program.





Constants/Literals

A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy" etc.

As mentioned, an identifier also can be defined as a constant.

const double PI = 3.14;

Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.



Integer constants

A integer constant is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:/p>

  • Decimal constant(base 10)
  • Octal constant(base 8)
  • Hexadecimal constant(base 16)

For example:
Decimal constants: 0, -9, 22 etc Octal constants: 021, 077, 033 etc Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.


Floating-point constants

A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:

-2.0
0.0000234
-0.22E-5
Note :

E-5 = 10-5



Character constants

A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F'



Escape Sequences

Sometimes, it is necessary to use characters which 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 sequence is used.

For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.

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


String constants

String constants are the constants which are enclosed in a pair of double-quote marks. For example:

"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline

Enumeration constants

Keyword enum is used to define enumeration types. For example:

enum color {yellow, green, black, white};

Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively.


Keywords and Identifiers


Character set

Character set is a set of alphabets, letters and some special characters that are valid in C language.


Alphabets

Uppercase: A B C ................................... X Y Z

Lowercase: a b c ...................................... x y z


Digits

0 1 2 3 4 5 6 7 8 9


Special Characters


Special Characters in C Programming
,<>._
();$:
%[]#?
'&{}"
^!*/|
-\~+ 

White space Characters :

blank space, new line, horizontal tab, carriage return and form feed





Keywords

Keywords are predefined, reserved words used in programming that have special meaning. Keywords are part of the syntax and they cannot be used as an identifier. For example:

int money;

Here, int is a keyword that indicates 'money' is a variable of type integer.



As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.


Keywords in C Language
autodoubleintstruct
breakelselongswitch
caseenumregister typedef
charexternreturnunion
continueforsignedvoid
doifstatic while
defaultgotosizeofvolatile
constfloatshortunsigned

Along with these keywords, C supports other numerous keywords depending upon the compiler.






Identifiers

Identifiers are the names you can give to entities such as variables, functions, structures etc

Identifier names must be unique. They are created to give unique name to a C entity to identify it during the execution of a program. For example:


int money;
double accountBalance;

Here, money and accountBalance are identifiers.

Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.


Rules for writing an identifier

  1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscore only.
  2. The first letter of an identifier should be either a letter or an underscore. However, it is discouraged to start an identifier name with an underscore. It is because identifier that starts with an underscore can conflict with system names.
    In such cases, compiler will complain about it. Some system names that start with underscore are _fileno, _iob, _wfopen etc.
  3. There is no rule on the length of an identifier. However, the first 31 characters of identifiers are discriminated by the compiler. So, the first 31 letters of two identifiers in a program should be different.

Good Programming Practice

You can choose any name for an identifier. However, if the programmer choose meaningful name for an identifier, it will be easy to understand and work on.



Write a program to print "Hello World"

Program : -


#include<stdio.h>            //Heder file
#include<conio.h>           //Heder file
void min()                        // Main function
{
    clrscr();                       // clear screen function
    printf("Hello World");  // Print Function
    getch();                      // getch is hold our output
}


Output :-

Hello World





C program to create singly link-list

Programs:-


#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
     int data;
     struct node *next;
}*start=NULL;


void creat()
{
     char ch;
     do
     {
     struct node *new_node,*current;

     new_node=(struct node *)malloc(sizeof(struct node));
     
     printf("nEnter the data : ");
     scanf("%d",&new_node->data);
     new_node->next=NULL;

     if(start==NULL)
     {
          start=new_node;
          current=new_node;
      }
     else
     {
          current->next=new_node;
          current=new_node;
     }
     
      printf("Do you want to creat another : ");
      ch=getche();
      }while(ch!='n');
}
void display()
{
     struct node *new_node;
      printf("The Linked List : n");
      new_node=start;
      while(new_node!=NULL)
     {
          printf("%d--->",new_node->data);
           new_node=new_node->next;
       }
       printf("NULL");
}

void main()
{
     create();
     display();
}


Output : -


Enter the data : 10
Do you want to creat another :  y
Enter the data : 20
Do you want to creat another : y

Enter the data : 30
Do you want to creat another : n

The Linked List :
10--->20--->30--->NULL


C program to find the frequency of characters in a string

Program : - 

#include<stdio.h>
#include<conio.h>
void main()
{
       char c[1000],ch;
       int i,count=0;
       printf("Enter a string: ");
       gets(c);
       printf("Enter a character to find frequency: ");
       scanf("%c",&ch);
       for(i=0;c[i]!='\0';++i)
       {
               if(ch==c[i])
               ++count;
       }
       printf("Frequency of %c = %d", ch, count);
       getch();
}

 

Output : -

Enter a string: This website is awesome.
Enter a character to find frequency: e
Frequency of e = 4