Posts

Showing posts from November, 2024
 45 day Memory Management Memory management is the process of handling how much memory a program uses through allocation, reallocation and deallocation (often referred to as "freeing"). We will introduce each of these topics in the following chapters. Understanding how memory works in C is important. When you create a basic variable, C will automatically reserve space for that variable. An int variable for example, will typically occupy 4 bytes of memory, while a double variable will occupy 8 bytes of memory.  
 44 day  Enumeration Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. enum State {Working = 1, Failed = 0}; 
 43 day Unions The Union is a user-defined data type in C language that can contain elements of the different data types just like structure. But unlike structures, all the members in the C union are stored in the same memory location. Due to this, only one member can store data at the given instance. Syntax of Union in C The syntax of the union in C can be divided into three steps which are as follows: C Union Declaration In this part, we only declare the template of the union, i.e., we only declare the members’ names and data types along with the name of the union. No memory is allocated to the union in the declaration. union union_name {     datatype member1;     datatype member2;     ... };
 42 day  C Structures The structure in C is a user-defined data type that can be used to group items of possibly different types into a single type. The struct keyword is used to define the structure in the C programming language. The items in the structure are called its member and they can be of any valid data type. Additionally, the values of a structure are stored in contiguous memory locations. Syntax struct structure_name {     data_type member_name1;     data_type member_name1;     ....     .... };
 41 day Pointers Pointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. What is a Pointer in C? A pointer is defined as a derived data type that can store the address of other C variables or a memory location. We can access and manipulate the data stored in that memory location using pointers. Syntax of C Pointers The syntax of pointers is similar to the variable declaration in C, but we use the ( * ) dereferencing operator in the pointer declaration. datatype * ptr;
 40 day  Arrays  Properties of Arrays in C It is very important to understand the properties of the C array so that we can avoid bugs while using it. The following are the main properties of an array in C: 1. Fixed Size The array in C is a fixed-size collection of elements. The size of the array must be known at the compile time and it cannot be changed once it is declared. 2. Homogeneous Collection We can only store one type of element in an array. There is no restriction on the number of elements but the type of all of these elements must be the same. 3. Indexing in Array The array index always starts with 0 in C language. It means that the index of the first element of the array will be 0 and the last element will be N – 1. 4. Dimensions of an Array A dimension of an array is the number of indexes required to refer to an element in the array. It is the number of directions in which you can grow the array size. 5. Contiguous Storage All the elements in the array are sto...
 39 day  Arrays C Array Initialization Initialization in C is the process to assign some initial value to the variable. When the array is declared or allocated memory, the elements of the array contain some garbage value. So, we need to initialize the array to some meaningful value. There are multiple ways in which we can initialize an array in C. 1. Array Initialization with Declaration In this method, we initialize the array along with its declaration. We use an initializer list to initialize multiple elements of the array. An initializer list is the list of values enclosed within braces { } separated b a comma. data_type array_name [size] = {value1, value2, ... valueN}; 2. Array Initialization with Declaration without Size If we initialize an array using an initializer list, we can skip declaring the size of the array as the compiler can automatically deduce the size of the array in these cases. The size of the array in these cases is equal to the number of elements present...
 38 day Arrays in C Array in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array syntax, advantages and disadvantages, and many more. Syntax of Array Declaration data_type array_name [size];          or data_type array_name [size1] [size2]...[sizeN];
 37 day   Storage classes 2.exter Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead, we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
 36 day  Storage classes 1. aute This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto variable was declared. However, accessing auto variables outside their scope using pointers to their exact memory location is unsafe and results in undefined behavior. Auto variables are also assigned a garbage value by default when they are declared. Likewise, auto keyword is not used in front of functions as functions are not limited to block scope.
 35 day Storage classes C Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility, and lifetime which help us to trace the existence of a particular variable during the runtime of a program. C language uses 4 storage classes, namely
 34 day  Memory Layout of C Programs A typical memory representation of a C program consists of the following sections. Text segment  (i.e. instructions) Initialized data segment  Uninitialized data segment  (bss) Heap  Stack 
 33 day 2. Local Scope in C The local scope refers to the region inside a block or a function. It is the space enclosed between the { } braces. The variables declared within the local scope are called local variables. Local variables are visible in the block they are declared in and other blocks nested inside that block. Local scope is also called Block scope. Local variables have no linkage.
 32 day  1. Global Scope in C The global scope refers to the region outside any block or function. The variables declared in the global scope are called global variables. Global variables are visible in every part of the program. Global is also called File Scope as the scope of an identifier starts at the beginning of the file and ends at the end of the file. // filename: file1.c #include <stdio.h> // Define the global variable int a; // Define the function to use the global variable void myfun() {     printf("%d\n", a); }
 31 day  Types of Scope Rules in C C scope rules can be covered under the following two categories: Global Scope Local Scope To fully grasp scope rules and memory management in C, our C programming course provides detailed tutorials on how to properly use variables, pointers, and functions in different scopes