Posts

Showing posts from August, 2025

Pointers and Arrays in C

Image
Learn how pointers and arrays work together in C on Debian 12 using Vim. Understand their relationship, how to traverse arrays using pointers, and practical examples. Pointers and Arrays in C In C, pointers and arrays are closely related. The name of an array acts like a constant pointer to its first element. This relationship allows pointers to be used for traversing and manipulating arrays efficiently. Array Name as Pointer When you use an array name in an expression, it is converted to the address of its first element. Example: int numbers[3] = 10, 20, 30; printf(\\"%p\\", numbers); // address of first element printf(\\"%p\\", &numbers[0]); // same address Accessing Array Elements with Pointers Pointers can be incremented to move through array elements without using square brackets. int *ptr = numbers; printf(\\"%d\\", *ptr); // first element ptr++; printf(\\"%d\\", *ptr); // second element Complete Example The following ...

Pointers and Arrays in C

Image
Learn how pointers and arrays work together in C on Debian 12 using Vim. Understand their relationship, how to traverse arrays using pointers, and practical examples. Pointers and Arrays in C In C, pointers and arrays are closely related. The name of an array acts like a constant pointer to its first element. This relationship allows pointers to be used for traversing and manipulating arrays efficiently. Array Name as Pointer When you use an array name in an expression, it is converted to the address of its first element. Example: int numbers[3] = 10, 20, 30; printf(\\"%p\\", numbers); // address of first element printf(\\"%p\\", &numbers[0]); // same address Accessing Array Elements with Pointers Pointers can be incremented to move through array elements without using square brackets. int *ptr = numbers; printf(\\"%d\\", *ptr); // first element ptr++; printf(\\"%d\\", *ptr); // second element Complete Example The following ...

User Input scanf and getchar in C

Image
Learn how to get user input in C using scanf and getchar on Debian 12 using Vim. Includes practical examples and clear explanations to understand both methods. User Input in C: Using scanf and getchar Getting input from users is a fundamental skill in C programming. It lets your program interact with users dynamically rather than using fixed data. Two commonly used functions for input are scanf() and getchar() . scanf() Function scanf() reads formatted input from the standard input (keyboard). It is ideal for reading integers, floats, characters, and strings. Example: int age; scanf(\\"%d\\", &age); Here, %d tells scanf() to read an integer, and &age passes the address of the variable to store the input. getchar() Function getchar() reads a single character from the user. Unlike scanf() , it is more low-level and does not require format specifiers. char ch; ch = getchar(); C...

User Input scanf and getchar in C

Image
Learn how to get user input in C using scanf and getchar on Debian 12 using Vim. Includes practical examples and clear explanations to understand both methods. User Input in C: Using scanf and getchar Getting input from users is a fundamental skill in C programming. It lets your program interact with users dynamically rather than using fixed data. Two commonly used functions for input are scanf() and getchar() . scanf() Function scanf() reads formatted input from the standard input (keyboard). It is ideal for reading integers, floats, characters, and strings. Example: int age; scanf(\\"%d\\", &age); Here, %d tells scanf() to read an integer, and &age passes the address of the variable to store the input. getchar() Function getchar() reads a single character from the user. Unlike scanf() , it is more low-level and does not require format specifiers. char ch; ch = getchar(); C...

Formatted Input Output in C

Image
Learn how to use formatted input and output in C with printf and scanf. This tutorial explains format specifiers and how they help in displaying and receiving data properly in C programs on Debian 12 using Vim. Formatted Input and Output in C Formatted input and output allow you to control how data is displayed or received in C. The printf() function is used to display formatted output, while scanf() is used to read formatted input from the user. What Are Format Specifiers? Format specifiers define how different types of data are handled in printf and scanf . They act as placeholders for variables. %d : Integer %f : Floating-point number %c : Single character %s : String Example Code Using printf and scanf This example demonstrates reading and displaying an integer, float, character, and string using formatted input and output. #include int main() int age; ...

Formatted Input Output in C

Image
Learn how to use formatted input and output in C with printf and scanf. This tutorial explains format specifiers and how they help in displaying and receiving data properly in C programs on Debian 12 using Vim. Formatted Input and Output in C Formatted input and output allow you to control how data is displayed or received in C. The printf() function is used to display formatted output, while scanf() is used to read formatted input from the user. What Are Format Specifiers? Format specifiers define how different types of data are handled in printf and scanf . They act as placeholders for variables. %d : Integer %f : Floating-point number %c : Single character %s : String Example Code Using printf and scanf This example demonstrates reading and displaying an integer, float, character, and string using formatted input and output. #include int main() int age; ...

Command Line Arguments in C

Image
Learn how to use command line arguments in C using argc and argv. Understand how to read input from the terminal when starting a program on Debian 12 using Vim. Command Line Arguments in C Command line arguments allow you to pass values to a C program when you run it from the terminal. This is very useful for tools, scripts, and automation tasks. These arguments are accessed using argc and argv . What Are argc and argv? argc : Argument Count. It shows how many arguments were passed to the program, including the program name itself. argv : Argument Vector. It is an array of strings that holds each argument passed to the program. Basic Example This program prints all the command line arguments provided by the user. #include int main(int argc, char *argv[]) printf(\\"Total arguments: %d\\\ \\", argc); for (int i = 0; i Explanation The argc value tells...

Command Line Arguments in C

Image
Learn how to use command line arguments in C using argc and argv. Understand how to read input from the terminal when starting a program on Debian 12 using Vim. Command Line Arguments in C Command line arguments allow you to pass values to a C program when you run it from the terminal. This is very useful for tools, scripts, and automation tasks. These arguments are accessed using argc and argv . What Are argc and argv? argc : Argument Count. It shows how many arguments were passed to the program, including the program name itself. argv : Argument Vector. It is an array of strings that holds each argument passed to the program. Basic Example This program prints all the command line arguments provided by the user. #include int main(int argc, char *argv[]) printf(\\"Total arguments: %d\\\ \\", argc); for (int i = 0; i Explanation The argc value tells...

Auto-Register Subclasses Using Metaclasses

Image
Learn how to auto-register subclasses in Python using metaclasses for better organization and dynamic class management. Auto-Registering Subclasses in Python with Metaclasses Metaclasses in Python allow you to control class creation. One powerful use case is auto-registering subclasses for dynamic management. This technique is helpful in frameworks, plugins, or any system where you want to keep track of all child classes automatically. What Are Metaclasses? Metaclasses are the classes of classes. They define how classes behave. By overriding specific methods like __init__ or __new__ , we can hook into the class creation process and add custom logic. Why Auto-Register Subclasses? Auto-registering subclasses is beneficial when you have many subclasses and want to keep track of them without manually maintaining a list. It is common in plugin systems, serialization frameworks, or command dispatchers. Basic Example o...

Auto-Register Subclasses Using Metaclasses

Image
Learn how to auto-register subclasses in Python using metaclasses for better organization and dynamic class management. Auto-Registering Subclasses in Python with Metaclasses Metaclasses in Python allow you to control class creation. One powerful use case is auto-registering subclasses for dynamic management. This technique is helpful in frameworks, plugins, or any system where you want to keep track of all child classes automatically. What Are Metaclasses? Metaclasses are the classes of classes. They define how classes behave. By overriding specific methods like __init__ or __new__ , we can hook into the class creation process and add custom logic. Why Auto-Register Subclasses? Auto-registering subclasses is beneficial when you have many subclasses and want to keep track of them without manually maintaining a list. It is common in plugin systems, serialization frameworks, or command dispatchers. Basic Example o...

Data Hiding with Name Mangling __var

Image
Learn how to hide data in Python classes using name mangling with double underscores for better encapsulation. Data Hiding in Python with Name Mangling Python does not enforce strict access restrictions to class variables and methods like private or protected modifiers in other languages. However, it uses a technique called name mangling to discourage direct access to variables that are meant to be private. What is Name Mangling? Name mangling in Python means that any identifier with two leading underscores (e.g., __var ) and at most one trailing underscore will be transformed to include the class name as a prefix. This makes it harder to accidentally override or access such variables from outside the class. Why Use Name Mangling? This feature is useful for data hiding, encapsulation, and avoiding name conflicts in subclasses. It allows developers to communicate intent that certain attributes are not part of the class’s ...

Data Hiding with Name Mangling __var

Image
Learn how to hide data in Python classes using name mangling with double underscores for better encapsulation. Data Hiding in Python with Name Mangling Python does not enforce strict access restrictions to class variables and methods like private or protected modifiers in other languages. However, it uses a technique called name mangling to discourage direct access to variables that are meant to be private. What is Name Mangling? Name mangling in Python means that any identifier with two leading underscores (e.g., __var ) and at most one trailing underscore will be transformed to include the class name as a prefix. This makes it harder to accidentally override or access such variables from outside the class. Why Use Name Mangling? This feature is useful for data hiding, encapsulation, and avoiding name conflicts in subclasses. It allows developers to communicate intent that certain attributes are not part of the class’s ...

Dynamic Method Creation at Runtime

Image
Learn how to create and bind methods dynamically at runtime in Python using types and functions for more flexible and dynamic programming patterns. Dynamic Method Creation at Runtime in Python Python is a dynamic language that allows developers to modify classes and objects at runtime. This includes the ability to create methods dynamically and bind them to classes or instances. This feature is especially useful for metaprogramming, creating plugins, or designing flexible APIs. Why Create Methods Dynamically? Dynamic method creation lets you modify behavior without modifying original source code. It’s helpful in frameworks where behaviors need to be injected at runtime or when creating objects with custom behavior based on runtime conditions. Adding Methods to Instances Let's start by adding a new method to an existing object at runtime. We’ll define a simple function and bind it to an instance: class Person: ...

Dynamic Method Creation at Runtime

Image
Learn how to create and bind methods dynamically at runtime in Python using types and functions for more flexible and dynamic programming patterns. Dynamic Method Creation at Runtime in Python Python is a dynamic language that allows developers to modify classes and objects at runtime. This includes the ability to create methods dynamically and bind them to classes or instances. This feature is especially useful for metaprogramming, creating plugins, or designing flexible APIs. Why Create Methods Dynamically? Dynamic method creation lets you modify behavior without modifying original source code. It’s helpful in frameworks where behaviors need to be injected at runtime or when creating objects with custom behavior based on runtime conditions. Adding Methods to Instances Let's start by adding a new method to an existing object at runtime. We’ll define a simple function and bind it to an instance: class Person: ...

String Input and Output in C

Image
Learn how to handle string input and output in C programming using scanf and printf, with examples on Debian 12 using Vim. String Input and Output in C Programming In C, strings are handled as arrays of characters. You can read strings from input using scanf and print them using printf . Reading Strings Using scanf Use scanf(\\"%s\\", str); to read a string. It stops reading at whitespace. char name[50]; scanf(\\"%s\\", name); Printing Strings Using printf Use printf(\\"%s\\", str); to print a string. printf(\\"Name: %s\ \\", name); Example Program #include int main() char name[50]; printf(\\"Enter your name: \\"); scanf(\\"%s\\", name); printf(\\"Hello, %s!\ \\", name); return 0; Compile and Run gcc string_io.c -o string_io ./string_io Notes scanf(\\"%s\\", str); ...

String Input and Output in C

Image
Learn how to handle string input and output in C programming using scanf and printf, with examples on Debian 12 using Vim. String Input and Output in C Programming In C, strings are handled as arrays of characters. You can read strings from input using scanf and print them using printf . Reading Strings Using scanf Use scanf(\\"%s\\", str); to read a string. It stops reading at whitespace. char name[50]; scanf(\\"%s\\", name); Printing Strings Using printf Use printf(\\"%s\\", str); to print a string. printf(\\"Name: %s\ \\", name); Example Program #include int main() char name[50]; printf(\\"Enter your name: \\"); scanf(\\"%s\\", name); printf(\\"Hello, %s!\ \\", name); return 0; Compile and Run gcc string_io.c -o string_io ./string_io Notes scanf(\\"%s\\", str); ...

Enforcing Interface Contracts in Python

Image
Learn how to enforce interface contracts in Python using abstract base classes for robust and maintainable code. Enforcing Interface Contracts in Python In software engineering, enforcing interface contracts ensures that classes adhere to a specific structure. In Python, although it's a dynamically typed language, we can achieve this behavior using abstract base classes (ABCs). ABCs allow you to define methods that must be implemented by any subclass, promoting consistent and predictable APIs. Why Enforce Interface Contracts? Interface contracts help with maintainability, code clarity, and reducing runtime errors by forcing derived classes to provide implementations for certain methods. This is particularly useful in large projects or collaborative environments where multiple developers are working on different parts of the codebase. Using Abstract Base Classes in Python The abc module provides tools for defining ab...

Enforcing Interface Contracts in Python

Image
Learn how to enforce interface contracts in Python using abstract base classes for robust and maintainable code. Enforcing Interface Contracts in Python In software engineering, enforcing interface contracts ensures that classes adhere to a specific structure. In Python, although it's a dynamically typed language, we can achieve this behavior using abstract base classes (ABCs). ABCs allow you to define methods that must be implemented by any subclass, promoting consistent and predictable APIs. Why Enforce Interface Contracts? Interface contracts help with maintainability, code clarity, and reducing runtime errors by forcing derived classes to provide implementations for certain methods. This is particularly useful in large projects or collaborative environments where multiple developers are working on different parts of the codebase. Using Abstract Base Classes in Python The abc module provides tools for defining ab...

Enforcing Singleton per Thread with Thread-Local Storage

Image
Learn how to enforce the Singleton design pattern per thread in Python using thread-local storage to ensure each thread maintains its own Singleton instance. Enforcing Singleton Per Thread in Python Using Thread-Local Storage In this tutorial, we explore how to enforce the Singleton design pattern per thread in Python. This approach ensures that each thread maintains its own unique instance of a Singleton object, which is particularly useful in multi-threaded applications that require thread-specific data isolation. Why Thread-Local Singleton? Normally, the Singleton pattern restricts instantiation to a single object globally. However, in multi-threaded programs, there are scenarios where each thread needs its own Singleton instance, isolated from other threads. This prevents shared state conflicts and race conditions. What is Thread-Local Storage? Thread-local storage (TLS) allows data to be stored such that each thread...

Enforcing Singleton per Thread with Thread-Local Storage

Image
Learn how to enforce the Singleton design pattern per thread in Python using thread-local storage to ensure each thread maintains its own Singleton instance. Enforcing Singleton Per Thread in Python Using Thread-Local Storage In this tutorial, we explore how to enforce the Singleton design pattern per thread in Python. This approach ensures that each thread maintains its own unique instance of a Singleton object, which is particularly useful in multi-threaded applications that require thread-specific data isolation. Why Thread-Local Singleton? Normally, the Singleton pattern restricts instantiation to a single object globally. However, in multi-threaded programs, there are scenarios where each thread needs its own Singleton instance, isolated from other threads. This prevents shared state conflicts and race conditions. What is Thread-Local Storage? Thread-local storage (TLS) allows data to be stored such that each thread...

Viewing Commit History git log

Image
Learn how to view the commit history in Git using the `git log` command with useful formatting options for better readability. Viewing Commit History with git log One of Git’s strengths is its detailed and accessible project history. The git log command allows you to view past commits, helping you track changes and debug issues. Basic Usage of git log To see a list of recent commits in your repository: git log This shows each commit’s hash, author, date, and message. Common git log Options 1. Compact One-Line Summary git log --oneline Shows each commit in a single line, useful for quick browsing. 2. Show Last 3 Commits git log -3 --oneline Limits the output to the last three commits in one-line format. 3. Graph View git log --graph --oneline --all Displays commit history as a branch graph, handy for visualizing branching and merging. Viewing Specif...

Viewing Commit History git log

Image
Learn how to view the commit history in Git using the `git log` command with useful formatting options for better readability. Viewing Commit History with git log One of Git’s strengths is its detailed and accessible project history. The git log command allows you to view past commits, helping you track changes and debug issues. Basic Usage of git log To see a list of recent commits in your repository: git log This shows each commit’s hash, author, date, and message. Common git log Options 1. Compact One-Line Summary git log --oneline Shows each commit in a single line, useful for quick browsing. 2. Show Last 3 Commits git log -3 --oneline Limits the output to the last three commits in one-line format. 3. Graph View git log --graph --oneline --all Displays commit history as a branch graph, handy for visualizing branching and merging. Viewing Specif...

Freeze Class Attributes Using setattr Override

Image
Learn how to freeze class attributes in Python by overriding setattr, preventing accidental changes and ensuring data integrity. Freezing Class Attributes in Python with __setattr__ Override In Python, classes are highly dynamic. Attributes can be added, changed, or removed on the fly. However, in some situations, you may want to make your class attributes immutable after initialization. This prevents accidental modifications that could lead to bugs or inconsistent state. Why Freeze Attributes? Freezing attributes is helpful in data models, configuration objects, or any case where object state should remain constant after creation. By overriding __setattr__ , we can control how and when attributes are assigned. Basic Example of __setattr__ Override The following example shows how to make class attributes immutable after initialization: class Frozen: def __init__(self, name, age): super().__setattr__(...

Freeze Class Attributes Using setattr Override

Image
Learn how to freeze class attributes in Python by overriding setattr, preventing accidental changes and ensuring data integrity. Freezing Class Attributes in Python with __setattr__ Override In Python, classes are highly dynamic. Attributes can be added, changed, or removed on the fly. However, in some situations, you may want to make your class attributes immutable after initialization. This prevents accidental modifications that could lead to bugs or inconsistent state. Why Freeze Attributes? Freezing attributes is helpful in data models, configuration objects, or any case where object state should remain constant after creation. By overriding __setattr__ , we can control how and when attributes are assigned. Basic Example of __setattr__ Override The following example shows how to make class attributes immutable after initialization: class Frozen: def __init__(self, name, age): super().__setattr__(...

Understanding the Git Directory Structure git folder

Image
Learn about the hidden `.git` directory, its internal structure, and how Git stores your project’s history and configuration data. Understanding the Git Directory Structure (.git Folder) Every Git repository contains a hidden .git folder at its root. This folder holds all the internal data Git uses to manage your project, including commit history, branches, and configuration. What is the .git Folder? The .git directory makes a folder a Git repository. Without it, Git commands won't work in that directory. To see it: ls -a Look for a directory named .git . Key Subdirectories and Files inside .git config: Repository-specific configuration settings. HEAD: Points to the current branch reference. objects/: Stores all commits, trees, and blobs (Git’s data model). refs/: Contains references like branches and tags. logs/: Keeps a log of all reference updates....

Understanding the Git Directory Structure git folder

Image
Learn about the hidden `.git` directory, its internal structure, and how Git stores your project’s history and configuration data. Understanding the Git Directory Structure (.git Folder) Every Git repository contains a hidden .git folder at its root. This folder holds all the internal data Git uses to manage your project, including commit history, branches, and configuration. What is the .git Folder? The .git directory makes a folder a Git repository. Without it, Git commands won't work in that directory. To see it: ls -a Look for a directory named .git . Key Subdirectories and Files inside .git config: Repository-specific configuration settings. HEAD: Points to the current branch reference. objects/: Stores all commits, trees, and blobs (Git’s data model). refs/: Contains references like branches and tags. logs/: Keeps a log of all reference updates....

Common String Functions strlen strcpy strcmp strcat in C

Image
Learn common string functions in C: strlen, strcpy, strcmp, strcat with examples on Debian 12 using Vim. Common String Functions in C Programming C provides several standard functions to manipulate strings, declared in . strlen Returns the length of a string (excluding the null terminator). size_t strlen(const char *str); strcpy Copies one string to another. char *strcpy(char *dest, const char *src); strcmp Compares two strings lexicographically. int strcmp(const char *str1, const char *str2); strcat Appends one string to the end of another. char *strcat(char *dest, const char *src); Example Program #include #include int main() char str1[20] = \\"Hello\\"; char str2[] = \\"World\\"; printf(\\"Length of str1: %lu\ \\", strlen(str1)); strcpy(str1, \\"Hi\\"); printf(\\"After strcpy: %s\ \\",...

Common String Functions strlen strcpy strcmp strcat in C

Image
Learn common string functions in C: strlen, strcpy, strcmp, strcat with examples on Debian 12 using Vim. Common String Functions in C Programming C provides several standard functions to manipulate strings, declared in . strlen Returns the length of a string (excluding the null terminator). size_t strlen(const char *str); strcpy Copies one string to another. char *strcpy(char *dest, const char *src); strcmp Compares two strings lexicographically. int strcmp(const char *str1, const char *str2); strcat Appends one string to the end of another. char *strcat(char *dest, const char *src); Example Program #include #include int main() char str1[20] = \\"Hello\\"; char str2[] = \\"World\\"; printf(\\"Length of str1: %lu\ \\", strlen(str1)); strcpy(str1, \\"Hi\\"); printf(\\"After strcpy: %s\ \\",...