TOKENS
TOKENS
Just like we can't create a meaningful sentence without using words, we can't imagine a human
body without living cells similarly, we can't develop or construct a C program without using
tokens in C. Tokens in C language are the smallest elements or the building blocks used to
construct a C program.
A compiler breaks a C program into tokens and then proceeds ahead to the next stages used in the
compilation process.
1. Identifiers
2. Keywords
3. Constants
4. Operators
5. Special Characters
6. Strings
Keywords
Keywords in C language are the collection of pre-defined or reserved words. These are case-
sensitive and written in lower cases. Their meaning and functionality are already known to the
compiler.
We can't use these keywords as variable names or function names because by doing so, we are
trying to assign a new meaning to the keyword, which is not allowed in C language. There are a
total of 32 keywords supported by the C language:
Identifiers in C are short and informative names that uniquely identify variables or function
names.
These are user-defined words used for naming of functions, variables, structures, unions, arrays
etc. These can be composed of lowercase letters, uppercase letters, underscore or digits, but the
first character should be either an underscore or an alphabet.
Identifiers shouldn't begin with any numerical digit and hence, the first character must be
either an underscore or an alphabet.
Identifiers are case-sensitive and hence, both lowercase and uppercase letters are distinct.
The length of identifiers shouldn't be more than 31 characters.
Commas and blank spaces are not allowed while declaring an identifier.
Also, the most important rule is that we can't use keywords as identifiers because
keywords in C language are reserved words for special purposes only.
The above examples are following all the essential rules for defining identifiers i.e, they are not
started with a numerical digit, not a keyword and also, there is no blank space or any special
operator.
The above examples are not following all the essential rules for defining identifiers and hence,
are invalid identifiers.
**
Constants
Constants are the variables whose values are fixed and can not be modified during the execution
of a program once they are defined. They are also known as literals.
The constant variables in C can be initialized only once and their default value is zero. If we try
to re-initialize or re-define any constant variable, then we will get a compilation error.
const keyword Here, we are using the const keyword to declare a variable and assigning a
value to it that can not be modified later.
#define pre-processor Here, we are using #define pre-processor and constant ll will be an
alias-name for long keyword.
Constant Example
Integer constant 10, 20, 30 etc.
Floating-point constant 10.2, 20.5, 30.6 etc.
Octal constant 011, 022, 088 etc.
Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
Character constant 'x', 'y', 'z' etc.
String constant "Java", "C++", "Python" etc.
Strings in C
The strings in C always get represented in the form of an array of characters. We have a ‘\0′ null
character at the end of any string- thus, this null character represents the end of that string. In C
language, double quotes enclose the strings, while the characters get enclosed typically within
various single characters.
char x[9] = “chocolate’; // Here, the compiler allocates a total of 9 bytes to the ‘x’ array.
char x[] = ‘chocolate’; // Here, the compiler performs allocation of memory during the run time.
Strings in C are represented as an array of characters having null character '\0' at the end. This
null character '\0' denotes the end of the string. Also, these strings are always enclosed within
double quotes. The size of the string is basically the number of characters it contains and also, 1
byte memory space is always reserved for null character value.
Here using string[10] denotes that 10 bytes of memory space is allocated for holding the
string value.
When we declare char as “string []”, memory space will be allocated dynamically as per
the requirements during the execution of the program.
C Token – Operators
Operators:
Operators are symbols that provide instructions for the performance of various
mathematical and logical operations. Operators are tools that can alter data and
variable values.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment/Decrement Operators
5. Assignment Operator
6. Bitwise Operators
7. Conditional Operators
8. Special Operators
9. Hello World: Write a program that prints "Hello, World!" to the console.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
10. User Input and Output: Write a program that takes the user's name as input and
prints a greeting using printf.
#include <stdio.h>
int main() {
scanf("%s", name);
return 0;
}
11. Sum of Two Numbers: Write a program that takes two numbers as input and
prints their sum.
#include <stdio.h>
int main() {
return 0;
}
12. Area of a Rectangle: Write a program that takes the length and width of a
rectangle as input and calculates its area.
13. #include <stdio.h>
14.
17.
20.
22.
24.
25. return 0;
26. }
27. Celsius to Fahrenheit: Write a program that converts Celsius to Fahrenheit. Take
Celsius as input and print the equivalent Fahrenheit temperature.
#include <stdio.h>
int main() {
scanf("%f", &celsius);
return 0;
}
28. Swapping Two Numbers: Write a program that swaps the values of two
variables. Take two numbers as input and print their swapped values.
#include <stdio.h>
int main() {
// Swapping
temp = num1;
num1 = num2;
num2 = temp;
return 0;
}