Skip to main content

Posts

Storage classes In c

Whenever we declare a variable in our program there comes an idea of a variable's visibility. Then we think about what will be its initial value? In c every variable have its storage class. Four types of storage classes in c: Automatic Static Extern Register Automatic storage class( variable) : Automatic variable's visibility can be  within block of local function. And it will be stored in stack segment. All local variables resides in stack segment and belongs to automatic storage class. Initial value of automatic variable will be garbage. Static storage class(variable) : Static variable will be visible within block of local function or within block of function. It will be stored in data segment. All static variables resides in data segment block.  Initial value of static variable will be 0. And keeps updated value. Extern storage class(variable) : Extern variable used to link a variable within multiple files. Scope or visibility of extern variable is globally. It is also stor...

Structure In C

Structure is a user-defined data type in c. Structures used to store different types  of data items. Structures used for keeping records(student records, books etc.). So structure is easy to use for group of data items. Here is an example using of structure:

Volatile qualifier in c

Volatile variable is a confusing term because most of people (even programmers) knows about or listened about volatile qualifier but not used that much enough. So volatile qualifier is using when there is any chance of any variable get changes during running a program and any other interruption. So here comes idea of volatile qualifier which is used to stop optimization of any variable which we do not want to get changes unexpectedly. Here is an example of volatile qualifier:

Instantiated a variable not initialized :

#include<stdio.h> int main(int argc,char *argv[]) {      int ch = 1;      switch(ch) {      int v = 100;      case 1:                printf("Value of v is = %d\n",v);                break;      default:                printf("Value of v is = %d\n",v);                break;             }           return 0; } In this code if we compile and run this code we will get garbage value or (0). Reason behind this we have instantiated but not initialized this variable. We can check this thing through using gdb. In beow image we can see after switch control directly jump on case 1: and print value of variable.

File I/O (How to write in file)

I am going to share an example of how to write in file: fopen() call used to open a file and  const char* filename and const char* mode which is used to open file in write mode. fprintf() used to write data through getting a file pointer. fclose() call will close the file pointer.

File I/O

A file represents a sequence of bytes on disk where a group of related data is stored. File is created for permanent storage of data. It is a ready made structure. we have used File input/output operations on files (like open file, write in file, read from file). So here i am going to share example of File input/output operations in c: How to open file ? How to read from file?

Enums in c

An enum in C is a user-defined data-type and it consists set of named constant integer. Two enums couldn't hold duplicate members but its member can have same value. Enums can hold an enum within it. Enums can hold negative values. Enum's index always starts from 0(same as array)   👉Drawback of Enums are: we have to inialize enum's object with all enum members again in main after declare in enums itself. An enumeration obeys the scope rules. we can declare unnamed enumerator data type. An enumeration tag also follows the scope rules. So enumeration tag should be different from the tag of structure, union or the enumeration type.       If we do not assign the value to the enumeration constant then the compiler automatically assigns the value to the enumeration constant.       We can assign the value to the enumeration constant in any order, unassigned constant get the value from the previous constant and plus one. The valu...