C Programming/Tokens: Difference between revisions
No edit summary |
|||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== | == Tokens == | ||
C Supports Six Types of Tokens: | C Supports Six Types of Tokens: | ||
# [[ | # [[#Identifiers |Identifiers ]] | ||
# [[ | # [[#Keywords|Keywords]] | ||
# [[ | # [[#Constants |Constants ]] | ||
# Strings | # [[#Strings |Strings ]] | ||
# Operators | # [[#Operators |Operators ]] | ||
# Special Symbols | # Special Symbols | ||
Line 45: | Line 45: | ||
=== Constant === | === Constant === | ||
A constant is an identifier whose value cannot be altered in a program. | A constant is an identifier whose value cannot be altered in a program. Constants in C are the fixed values that are used in a program, and its value remains the same during the entire execution of the program. | ||
# Constants are also called literals. | |||
# Constants can be any of the data types. | |||
# Best practice to define constants using only upper-case. | |||
=== Strings === | |||
Strings are actually one-dimensional array of characters terminated by a <code>null</code> character <code>'\0'</code>. Thus a null-terminated string contains the characters that comprise the string followed by a <code>null</code>. | |||
<syntaxhighlight lang="c"> | |||
//main.c | |||
#include <stdio.h> | |||
int main() | |||
{ | |||
//char name[] = "World"; | |||
char name[6] = {'W', 'o', 'r', 'l', 'd', '\0'}; | |||
printf("Hello %s!\n", name); | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
=== Operators === | |||
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators: | |||
# Arithmetic Operators | |||
# Relational Operators | |||
# Logical Operators | |||
# Bitwise Operators | |||
# Assignment Operators | |||
# Misc Operators | |||
== Differentiate == | == Differentiate == |
Latest revision as of 03:14, 5 February 2018
Tokens
C Supports Six Types of Tokens:
- Identifiers
- Keywords
- Constants
- Strings
- Operators
- Special Symbols
Identifiers
Identifiers are names for entities in a C program, such as variables, arrays, functions, structures, unions and labels. An identifier can be composed only of uppercase, lowercase letters, underscore and digits, but should start only with an alphabet or an underscore.
Rules for Naming Identifiers:
- An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters & digits) and underscore( _ ) symbol.
- Identifier names must be unique
- The first character must be an alphabet or underscore.
- Cannot be keyword as identifiers.
- Should not be of length more than 31 characters.
- Must not contain white spaces.
- Identifiers are case-sensitive.
Kinds of identifiers:
- Internal
- External
Keywords
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
const | float | short | unsigned |
continue | for | signed | void |
default | goto | sizeof | volatile |
do | if | static | while |
Constant
A constant is an identifier whose value cannot be altered in a program. Constants in C are the fixed values that are used in a program, and its value remains the same during the entire execution of the program.
- Constants are also called literals.
- Constants can be any of the data types.
- Best practice to define constants using only upper-case.
Strings
Strings are actually one-dimensional array of characters terminated by a null
character '\0'
. Thus a null-terminated string contains the characters that comprise the string followed by a null
.
//main.c
#include <stdio.h>
int main()
{
//char name[] = "World";
char name[6] = {'W', 'o', 'r', 'l', 'd', '\0'};
printf("Hello %s!\n", name);
return 0;
}
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
Differentiate
Keywords vs Identifiers
Keyword | Identifier |
---|---|
Predefined-word | User-defined word |
Must be written in lowercase only | Can written in lowercase and uppercase |
Has fixed meaning | Must be meaningful in the program |
Whose meaning has already been explained to the C compiler | Whose meaning not explained to the C compiler |
Combination of alphabetic characters | Combination of alphanumeric characters |
Used only for it intended purpose | Used for required purpose |
Underscore character is not considered as a letter | Underscore character is considered as a letter |