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 player=40;
Rules for naming a variable
- A variable name can have only letters (both uppercase and lowercase letters), digits and underscore.
- The first letter of a variable should be either a letter or an underscore.
- There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.
Literals : Literals are data used for representing fixed values. They can be used directly in the code.
For example: 1, 2.5, 'c' etc.
1. Integers : An integer is a numeric literal (associated with numbers) without any fractional or exponential part.
There are three types of integer literals in C programming:
- decimal (base 10)
- octal (base 8)
- hexadecimal (base 16)
For example:
Decimal : 0, -9, 23 etc.
Octal : 021, 077, 045 etc.
Hexadecimal : 0x7f, 0x3a, 0x542 etc.
In C programming, octal starts with a 0, and hexadecimal starts with a 0x.
2. Floating-point Literals : A floating-point literal is a numeric literal that has either a fractional form or an exponent form.
For example: -2.0, 0.00034,-0.33E-6 etc.
3. Characters : A character literal is created by enclosing a single character inside single quotation marks.
For example: ’a’, ’c’, ‘s’, ‘]’ etc.
4. 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 |
Character |
|
\b |
Backspace |
|
\f |
Form
feed |
|
\n |
Newline |
|
\r |
Return |
|
\t |
Horizontal
tab |
|
\v |
Vertical
tab |
|
\\ |
Backslash |
|
\' |
Single
quotation mark |
|
\" |
Double
quotation mark |
|
\? |
Question
mark |
|
\0 |
Null
character |
For example: \n is used for a newline. The backslash \ causes escape from the normal way the characters are handled by the compiler.
5. String Literals : A string literal is a sequence of characters enclosed in double-quote marks.
For example :
"program" // string constant
"earth is round\n" // print string with a newline
Constants : If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant.
For example: const double PI=3.14;
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.
We can break a long line into multiple lines using string literals and separating them using white spaces.
For example :
"hello,world"
"hello, \
world"
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.
Syntax: #define identifier value
Example: #define PI 3.14
The const Keyword : we can use const prefix to declare constants with a specific type as follows
Syntax:
const type variable=value;
Example: const int MAX=10;
No comments:
Post a Comment