limits.h in C Language
The limits.h header provides macros that define the range of values for various integer types, including character types. Here are the key macros: CHAR_BIT: Number of bits in a character. SCHAR_MIN: Minimum value of a signed char. SCHAR_MAX: Maximum value of a signed char. UCHAR_MAX: Maximum value of an unsigned char. CHAR_MIN: Minimum value of a char. CHAR_MAX: Maximum value of a char. MB_LEN_MAX: Maximum number of bytes in a multibyte character. SHRT_MIN: Minimum value of a short...
iso646.h in C Language
The iso646.h header file specifies alternative spellings for some common operators. For example, it uses the keyword and as a substitute for the logical operator &&. Here’s how the following condition can be expressed using these alternatives: 123if (x > 6 and x < 12)// is equivalent toif (x > 6 && x < 12) The defined alternative spellings are as follows: and replaces && and_eq replaces &= bitand replaces & bitor replaces | compl replaces ~ not...
inttypes.h in C Language
In C, the inttypes.h header file provides format specifiers for integer types defined in stdint.h that can be used with printf() and scanf(). Here are the four types of integer types available: Fixed-width integer types, e.g., int8_t Minimum-width integer types, e.g., int_least8_t Fastest minimum-width integer types, e.g., int_fast8_t Maximum-width integer types, e.g., intmax_t The format specifiers for printf() are formed by combining PRI + original specifier + type keyword/width. For...
float.h in C Language
The header file float.h defines various macros related to the floating-point types float, double, and long double, specifying their ranges and precision. FLT_ROUNDSThis macro indicates the rounding direction used in floating-point addition. Its possible values are: -1: Undetermined. 0: Round towards zero. 1: Round to the nearest integer. 2: Round towards positive infinity. 3: Round towards negative infinity. FLT_RADIXThis macro represents the base of the exponent in scientific notation,...
errno.h in C Language
The errno VariableThe errno.h header file declares the errno variable, which is an int used to store error codes (positive integers). If errno has a non-zero value, it indicates that an error occurred during the execution of a program. 123456789int x = -1;errno = 0; // Reset errno to 0int y = sqrt(x); // Attempt to calculate the square root of a negative numberif (errno != 0) { fprintf(stderr, "sqrt error; program terminated.\n"); exit(EXIT_FAILURE);} In this...
ctype.h in Language
The <ctype.h> header file defines a set of prototypes for character handling functions. Character Testing FunctionsThese functions are used to determine whether a character belongs to a specific type: isalnum(): Checks if the character is alphanumeric. isalpha(): Checks if the character is a letter. isdigit(): Checks if the character is a digit. isxdigit(): Checks if the character is a hexadecimal digit. islower(): Checks if the character is a lowercase letter. isupper(): Checks if...
assert.h in C Language
assert()The assert.h header file defines the assert() macro, which is used to verify that a program meets certain conditions at runtime. If a condition is not met, the program will terminate with an error message. This macro is often referred to as an “assertion.” For example, consider the following code: 1assert(PI > 3); When the program reaches this line, it checks whether the variable PI is greater than 3. If it is, the program continues to run; if not, the program will terminate and...
Multibyte Character in C Language
Overview of UnicodeWhen the C language was first developed, it primarily focused on English characters, utilizing 7-bit ASCII to represent all characters. The ASCII range spans from 0 to 127, which means it can represent a maximum of about 128 characters, with each character fitting into a single byte. However, dealing with non-English characters requires more than one byte. For example, just the Chinese language includes tens of thousands of characters, necessitating a character set that...
CLI in C Language
Command Line ArgumentsC programs can receive parameters from the command line. For example, running: 1$ ./foo hello world The program foo receives two command line arguments: hello and world. How does the program access these command line arguments? C stores the command line input in an array, which can be accessed through the parameters of the main() function. 1234567#include <stdio.h>int main(int argc, char* argv[]) { for (int i = 0; i < argc; i++) { ...
Enum in C Language
When a data type has only a few possible values, each with its own specific meaning, defining these values as an enum (short for “enumeration”) can enhance code readability. 12345enum colors { RED, GREEN, BLUE };printf("%d\n", RED); // 0printf("%d\n", GREEN); // 1printf("%d\n", BLUE); // 2 In the example above, if a program requires three colors, we can use the enum command to define these colors as a single enumeration type called colors, which...
Union in C Language
Sometimes, you need a data structure that can represent different data types depending on the situation. For instance, if you use a single structure to represent the quantity of fruit, it may need to be an integer (like 6 apples) at times and a float (like 1.5 kilograms of strawberries) at others. In C, you can use a union to create a flexible data structure. A union can contain various members that share the same memory space, meaning only one member can hold a meaningful value at any given...
Typedef Command in C Language
OverviewThe typedef command is used to create an alias for a specific type. 1typedef type name; In this code, type represents the original type, while name is the alias. For example: 1typedef unsigned char BYTE; In this example, typedef creates an alias BYTE for the type unsigned char, allowing you to declare variables using BYTE: 1BYTE c = 'z'; Multiple AliasesYou can use typedef to define multiple aliases at once: 1typedef int antelope, bagel, mushroom; Here, antelope, bagel, and...
String in C Language
OverviewC doesn’t have a dedicated string type. Instead, strings are treated as arrays of characters (char arrays). For example, the string “Hello” is processed as the array {‘H’, ‘e’, ‘l’, ‘l’, ‘o’}. The compiler allocates a continuous block of memory for the array, with all characters stored in adjacent memory units. C automatically adds a null byte (‘\0’) at the end of the string to indicate its termination. This null character is different from the character ‘0’. The null character has...
Array in C Language
IntroductionAn array is a collection of values of the same type stored in a sequential manner. Arrays are represented by a variable name followed by square brackets, with the number inside the brackets indicating the number of elements in the array. 1int scores[100]; In this example, an array named scores is declared with 100 elements, each of which is of type int. Note: When declaring an array, you must specify its size. Array elements are indexed starting from 0. Thus, in an array...
Functions in C Language
OverviewA function is a reusable block of code that can accept different parameters and perform specific tasks. Here’s an example of a function: 123int plus_one(int n) { return n + 1;} This code declares a function named plus_one(). Key Points of Function Declarations: Return Type: Specify the return type at the start of the declaration. In the example, int indicates the function returns an integer. Parameters: Declare the types and names of parameters in parentheses following...
Pointer in C Language
Pointers are one of the most important and challenging concepts in the C programming language. OverviewWhat is a pointer? In essence, it’s a value representing a memory address, acting as a signpost to a specific location in memory. The asterisk * represents a pointer and is usually placed after the type keyword to indicate what type of value the pointer is referencing. For example, char* is a pointer to a character, and float* is a pointer to a float. 1int* intPtr; The above example...
Types in C Language
In C, every piece of data has a type that the compiler must understand to properly operate on it. The “type” refers to the shared characteristics of similar data, allowing you to know its properties and operations once its type is known. There are three basic data types: char (character), int (integer), and float (floating-point). More complex types are built from these. Character TypeThe character type represents a single character and is declared with the char keyword. 1char c =...
Flow Control in C Language
In C programming, execution is sequential, meaning statements are executed one after the other. To control the flow of execution, developers use flow control structures, primarily conditional and loop statements. if StatementThe if statement is used for conditional execution; it executes a specified statement if the condition is true. Syntax: 1if (expression) statement Here, expression must be true (non-zero) for statement to execute. The condition inside the parentheses must be enclosed in...
Operators in C Language
C language includes a wide variety of operators, more than 50 in total, which can be categorized into several types. Arithmetic OperatorsArithmetic operators are used for mathematical operations. The main ones are as follows: +: Unary plus (positive sign) -: Unary minus (negative sign) +: Addition (binary operator) -: Subtraction (binary operator) *: Multiplication /: Division %: Modulus (remainder of division) Unary + and -:The + and - can act as unary operators, requiring...
Understanding Variables in C Programming
A variable can be understood as the name of a memory location. By using the variable name, you can reference this memory and access the stored value. The value might change, which is why it’s called a variable. If the value remains constant, it’s referred to as a constant. Variable NamesIn C, variable names are considered identifiers, and there are strict rules for naming them: They can only contain letters (both uppercase and lowercase), digits, and underscores (_). They cannot begin with...