Please enable JavaScript.
Coggle requires JavaScript to display documents.
Tokens in C language, Tokens are the smallest units of meaningful data in…
Tokens in C language
Strings
are sequences of characters terminated by a null character \0. They are essentially arrays of characters that represent text data. C does not have a built-in string data type.
Declaration and Initialization
Using Character Arrays
char str = {'H','e','l','l','o','\0'};
char str[] = "Hello"; (automatic null terminator)
Using Pointers
char
str = "Hello";
Points to a string literal stored in read-only memory, modifying is not allowed.*
Allocation
Static
Fixed size in stack memory.
Example: char name;
Dynamic
Allocated at runtime.
Example: char str = (char)malloc(30);
Types and Implementations
Character Array
Array in stack/heap
Mutable
String Literal (Pointer)
Points to read-only memory
Immutable
Array of Pointers
Multiple strings (list)
Immutable
String Handling Functions (from <string.h>)
Copy
:
strcpy(), strncpy()
Concatenate
:
strcat(), strncat()
Compare
:
strcmp(), strncmp()
Length
:
strlen()
Search
:
strchr(), strrchr(), strstr()
Tokenize
:
strtok()
Misc
:
sprintf(), memcpy(), memset()
Input & Output
Input Methods
scanf("%s", str);
reads input till whitespace
fgets(str, size, stdin);
reads an entire line safely (Best practice)
gets(str);
unsafe, may cause buffer overflows
Output Methods
printf("%s", str);
prints string
puts(str);
prints string and adds newline
Operators
are symbols that perform specific operations on operands (variables or values). They instruct the compiler to perform mathematical, logical, or other computations.
Functionality / Purpose
Arithmetic Operators
Perform mathematical operations.
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
Unary Plus (+)
Unary Minus (-)
Increment (++)
Decrement (--)
Relational (Comparison) Operators
Compare values, yield true/false.
Equal to (==)
Not equal to (!=)
Greater than (>)
Less than (<)
Greater than or equal to (>=)
Less than or equal to (<=)
Logical Operators
Combine multiple conditions/expressions.
Logical AND (&&)
Logical OR (||)
Logical NOT (!)
Bitwise Operators
Operate on individual bits.
Bitwise AND (&)
Bitwise OR (|)
Bitwise XOR (^)
Bitwise NOT (~)
Left Shift (<<)
Right Shift (>>)
Assignment Operators
Assign values to variables.
Simple Assignment (=)
Add and Assign (+=)
Subtract and Assign (-=)
Multiply and Assign (*=)
Divide and Assign (/=)
Modulus and Assign (%=)
Bitwise AND and Assign (&=)
Bitwise OR and Assign (|=)
Bitwise XOR and Assign (^=)
Left Shift and Assign (<<=)
Right Shift and Assign (>>=)
Conditional (Ternary) Operator
Shorthand for if-else; chooses value based on condition.
(? :)
Miscellaneous/Special Operators
Special/unique tasks.
Sizeof (sizeof)
returns size in bytes
Address-of (&)
returns memory address
Indirection/Dereference (*)
accesses value at pointer
Comma (,)
separates expressions
Member selection (.)
accesses structure member
Pointer to member selection (->)
accesses structure member via pointer
Number of Operands
Binary
operates on two operands
Ternary
operates on three operands
Unary
operates on one operand
Constants
are fixed values that cannot be changed during program execution. They represent actual data values used directly in the code and are also called Literals.
Storage/Form
Literal Constants
Directly written into the code.
Do not have identifiers (names), only values.
Symbolic Constants
Defined using #define or const.
Have identifiers/names (e.g. const int pi = 3; and #define MAX 10).
Data Type
Integer Constants
Whole numbers, no decimals.
Subtypes include Decimal (42), Octal (052), and Hexadecimal (0x2A).
Can be signed or unsigned, must have at least one digit, and no decimal.
Floating-point Constants
Real numbers, include decimal points or exponent (1.23, 3.0e8).
Subtypes include Float, double, long double based on precision (3.1415f, 6.022e23).
Must use a decimal or exponent, can be positive/negative.
Character Constants
Single character in single quotes ('A', '7', '#')
Escape sequences: '\n' (newline), '\t' (tab), etc.
String Constants (String Literals)
Sequence of characters in double quotes ("Hello", "abc123")
Automatically ends with \0 (null character)
Definition Method
const Keyword Constants
Declared with const keyword (const int days = 7;). Typed, scope-bound, initialized at declaration, can't be changed.
#define Constants
Declared with #define. Untyped (simple text replacement), global scope, no memory allocation.
Usage or Structure
Enumeration Constants
Defined with enum.
Represent a group of related integer constants
Improves code readability.
Array/Structure Constants
Read-only arrays/structs with const.
Allows compound data to be constant.
Keywords
are reserved words that have predefined meanings in the C language. They are fundamental building blocks used to define the structure and control flow of programs. Depending on your C standard version and compiler, modern C can have between 32 and 54 keywords
Originally, ANSI C or C90 began with 32 keywords.
Data Type Keywords
Define the type of data a variable holds.
int
integer numbers
char
single characters
float
single-precision floating-point numbers
double
double-precision floating-point numbers
void
absence of type (used for functions that do not return any value)
short
,
long
specify the size of an integer
signed
,
unsigned
specify whether data types can represent negative values
User-Defined Type Keywords
Used for user-defined and composite data types.
struct
structures (collection of variables under one name)
union
similar to structures, but members share the same memory
enum
enumerated types (named integer constants)
typedef
create an alias for existing data types
Decision & Looping Keywords
Direct the sequence of execution in a program.
if, else
conditional branching
switch, case, default
multi-way branching
for, while, do
looping and repetition
break
exit immediately from a loop or switch case
continue
skip remaining statements in current loop iteration
goto
jump to a labeled statement
Storage Class Keywords
Control the scope, lifetime, and storage location of variables.
auto
default storage class for local variables
register
hints the compiler to store the variable in a CPU register (for fast access)
extern
variable is defined elsewhere (outside current file)
static
variable retains value between function calls or limits scope to current file/block
Type Qualifiers
Specify certain properties of variables.
const
value cannot be changed once assigned
volatile
value might be changed unexpectedly (especially used for hardware-related variables)
Return and Size Keywords
Help in defining or handling functions.
sizeof
a compile-time operator used to determine the size, in bytes, of a data type or a variable
return
exit from a function and optionally return a value
void
used for functions that don’t return a value*
In C99 version, five more keywords were added.
inline
function inlining
_Imaginary
imaginary number support
_Complex
complex number support
_Bool
boolean data type
These keywords were introduced with underscore names to preserve compatibility by avoiding clashes with existing code that may have already used bool, complex, or imaginary as identifiers or type names.
In C11, 7 more keywords are added.
_Alignas
set desired memory alignment
Alignof
get alignment requirement of a type
_Atomic
declare atomic (thread-safe) variables
_Generic
compile-time generic selection (type-specific)
_Noreturn
indicate function never returns
_Static_assert
compile-time assertions
_Thread_local
declare per-thread-local storage
In C23, 14 more keywords will be introduced.
alignas
same as _Alignas
alignof
same as Alignof
bool
same as _Bool
constexpr
declares that a variable or function should be evaluated at compile time
false
boolean constant representing the logical value false
nullptr
type-safe keyword representing a null pointer
static_assert
performs a compile-time assertion
thread_local
same as _Thread_local
true
boolean constant representing the logical value true
typeof
infers the type of an expression
typeof_unqual
like typeof, but removes any type qualifiers
_Decimal128
128-bit decimal floating-point
_Decimal32
32-bit decimal floating point,
_Decimal64
64-bit decimal floating point
Identifiers
are user-defined names used to identify variables, functions, arrays, structures, or any other program elements. They serve as labels that uniquely identify program components and allow programmers to refer to them throughout the code
Internal Identifiers
Names for entities (variables, functions, constants, etc.) whose scope is limited to a specific block or function (local scope).
Examples
:
Local variables within a function, parameters passed to functions.
Usage
:
Used for data and logic that should only be accessible inside specific functions or blocks, aiding modularity and information hiding.
External Identifiers
Names for entities (variables, functions, constants, etc.) that have global scope, meaning they can be accessed across multiple files.
Examples
: Global variables and functions defined outside any function.
Usage
: Used for data and logic that need to be shared across different files or modules in a large program
Special Symbols
are symbols that have specific syntactic meanings in C. Also called punctuators, they organize program structure and define code blocks
Punctuation and Structural Symbols
[] Square Brackets
arrays, subscripts
() Parentheses
function calls/parameters, grouping
{} Curly Braces
block code boundaries
, Comma
separating multiple values/parameters
: Colon
case labels in switch, ternary operator
; Semicolon
statement terminator
Hash/Number #
sign preprocessor directives
_ Underscore
allowed in identifiers
" ' Quotes
string and character literals
Escape Sequences & Special String Symbols
\n Newline
\t Tab
\ Backslash
\' Single Quote
\" Double Quote
\0 Null character
\a Alert/Bell
\b Backspace
\r Carriage Return
moves to line start
\f Form Feed
page break
\v Vertical Tab
moves cursor down in output
\ooo
octal value
\xhh
hex value
Tokens are the smallest units of meaningful data in a programming language that are recognised and processed by the compiler. They represent the fundamental building blocks of any program, similar to how words are the building blocks of a sentence.
During lexical analysis, a phase in the compilation process, a component called the lexer scans the source code and groups characters into tokens based on predefined rules. These tokens serve as the foundation for understanding and processing the program.