Tuesday, November 07, 2017

Tokens of C (Continue..), Keywords in C, Identifiers, Constants and Variables, String

2 Keywords in C
1.      Keywords are those words whose meaning is already defined by Compiler
2.      Cannot be used as Variable Name
3.      There are 32 Keywords in C
4.      C Keywords are also called as “Reserved words”.
5.      All keywords are in lower case.

auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
If
int
long
register
return
short
signed
sizeof
Static
struct
switch
typedef
union
unsigned
void
volatile
While





These keywords include all those keywords, which are declared by ANSI (American National Standard Institute) C. This institute has published the standards to be used in C programming language.

3 Identifiers
¾     Identifiers are the names that are given to various program elements such as variables, symbolic constants and functions. Variable or function identifier is called a symbolic constant name.
¾     While making these identifiers we need to follow some rules. These rules are stated below:
1.      Identifier can consist of alphabets, digits and a special symbol i.e. underscore ‘_’.
2.      An identifier cannot start with digit. It can start either with an alphabet or underscore.
3.      The underscore character (‘_’) is considered as letter.
4.      Both upper-case letter and lower-case letter characters are allowed. However, they're not interchangeable.
5.      No identifier may be keyword.
6.      No special characters, such as semicolon, period, blank space, slash or comma are permitted.
7.      Earlier; there was a limit of the length of the identifier to be 32 characters, but now this limit is removed. Hence, an identifier can be as long as required and minimum of one character.
Examples:

Legal Identifiers
Illegal Identifiers
int _a1;
int :e;
float b_11;
float for;
char ch,ch1;
float 9PI;
char abs_34;
char 7g;







4. Constants and Variables
A) Constants
¾     Constant in C means the content whose value does not change at the time of execution of a program.
¾     C Constants are also like normal variables. But, only difference is, their values cannot be modified by the program once they are defined.
¾     Constants refer to fixed values. They are also called as literals.
¾     Constants may be belonging to any of the data type.
Types of C constant:
1.      Integer constants
2.      Real or Floating point constants
3.      Octal & Hexadecimal constants
4.      Character constants
5.      String constants
6.      Backslash character constants

Constant type
Data type
Example

Integer constants
int
53, 762, -478 etc. 
unsigned int
5000u, 1000U etc.
long int
483,647
long long int
2,147,483,680
Real or Floating point constants
float
10.456789
doule
600.123456789
Octal constant
int
013   /* starts with 0  */
Hexadecimal constant
int
0×90 /* starts with 0x */
character constants
 char
‘A’   ,   ‘B’,     ‘C’
string constants
char
“ABCD”   ,   “Hai”













1. Integer Constants
¾     An integer constant must have at least one digit.
¾     It must not have a decimal point.
¾     It can either be positive or negative.
¾     No commas or blanks are allowed within an integer constant.
¾     If no sign precedes an integer constant, it is assumed to be positive.
¾     The allowable range for integer constants is -32768 to 32767.
2. Real or Floating point constants
¾     A real constant must have at least one digit.
¾     It must have a decimal point.
¾     It could be either positive or negative.
¾     If no sign precedes an integer constant, it is assumed to be positive.
¾     No commas or blanks are allowed within a real constant.
3. Character and string constants
¾     A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
¾     The maximum length of a character constant is 1 character.
¾     String constants are enclosed within double quotes.
4. Backslash Character Constants
¾     There are some characters which have special meaning in C language.
¾     They should be preceded by backslash symbol to make use of special function of them.
¾     Each escape sequence has unique American Standard Code for Information Interchange (ASCII) value.
¾     Each and Every combination starts with back slash ‘\’
¾     They are non-printable characters.
¾     Given below is the list of special characters and their purpose.
We can define constants in a C program in the following ways.

¾     By “const” keyword
¾     By “#define” preprocessor directive


Escape sequence
Character represented
Escape sequence
Character represented
\a
\\
\b
\'
\f
\"
\n
Newline (Line Feed);
\?
\r
\nnn
The character whose numerical value is given by nnn interpreted as an octal number
\t
\xhh
The character whose numerical value is given by hh interpreted as a hexadecimal number
\v

B) Variables in C
            A variable is a name of 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 variable is given below:

int a;
float b;
char c;

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

int a=14,b=75;// Declaring two variables a and b are of integer type
float f=2.81; // Declaring f variable of float type
char ch=’b’; // Declaring ch variable of caharacter type

NOTE: characters are initialized using single quotes.
There are many types of variables in c:
1.      local variable
2.      global variable
3.      static variable
4.      automatic variable
5.      external variable
1) Local Variable
            A variable that is declared inside the function or block is called local variable. It must be declared at the start of the block or function.
Program: WAP to use of local variable.
#include<stdio.h>
#include<conio.h>
void main()
{
    int a=10; // local variable
    printf(“The value of a local variable is %d”, a);
    getch();
}

Output:
The value of a local variable is 10

2) Global Variable
            A variable that is declared outside the function or block is called 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 or function.
Program: WAP to use of Global variable.

#include<stdio.h>
#include<conio.h>
int a=10;// Global variable
void main()
{
    float b; // Local variable
    printf(“The value of a global variable is %d”, a);
    getch();
}

Output:
The value of a global variable is 10

3) Static Variable
            A variable that is declared with static keyword is called static variable. It retains its value between multiple function calls.
Program : WAP to use of static variable.

#include<stdio.h>
#include<conio.h>
void main()
{
    int a=10; // Local variable
    static int b=10; //Static variable
    a=a+1;
    b=b+1;
    printf(“The value of a local variable is %d”, a);
    printf(“The value of a static variable is %d”, b);
    getch();
}

Output:
Output (Execution first time)

The value of a local variable is 11
The value of a static variable is 11

Output (Execution second time)
The value of a local variable is 11
The value of a static variable is 12

Output (Execution third time)
The value of a local variable is 11
The value of a static variable is 13

If you call this function many times, local variable will print the same value for each function call e.g., 11, 11, 11 and so on. But 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 is declared inside the block, are automatic variables by default. By we can explicitly declare automatic variable using auto keyword.
Program : WAP to use of automatic variable.
#include<stdio.h>
#include<conio.h>
void main()
    auto  int a=10; // Automatic  variable
    printf(“The value of an automatic variable is %d”, a);
    getch();
}

Output:
The value of an automatic variable is 10

5) External Variable
We can share a variable in multiple C source files by using external variable. To declare a external variable, you need to use extern keyword.
Program: WAP to use of external variable.
myfile.h
extern int x=10;//external variable (also global) 

Program1.c
#include “myfile.h”
#include<stdio.h>
#include<conio.h>
void main()
    printf(“The value of an external variable is %d”, x);
    getch();
}
Output
The value of an external variable is 10

5 String
¾     String constants are sequence of characters enclosed within double quotes.
¾     For example. “Hello”, “abc”, “hello911″, etc.
¾     Every sting constant is automatically terminated with a special character ‘’ called the null character ‘\0’ which represents the end of the string.
¾     Thus, the size of the string is the total number of characters plus one for the null character.
Post a Comment

C language Introduction

The C Language is developed for creating system applications that direct interacts to the hardware devices such as drivers, kernels etc. C...