echo '' ;

Archives

C Programming Language

C – File management

File management in the C programming language involves operations related to reading from and writing to files. Here are some key concepts and functions for file management in C:

File Pointers

  • C uses a file pointer to keep track of the current position in a file.
  • FILE is a structure in C that represents a file stream. You declare a pointer to this structure to work with files.
  • Syntax: FILE *filePointer;

File Functions in C

Table File Functions Summary

Opening a File: Fopen() Function

Syntax: fopen(“file_name”,"mode”);

File Modes: generic file handling

  • r – Open an existing file for Read purpose
  • wOpen an existing file for Write purpose
  • a – Open a file for writing in append mode. If file not exist, then create new file.
  • r+ – Open a file for both Read and Write
  • w+ – Opens a file for Read and Write. If a file is not existing it creates one, else if the file is existing it will be over written.
  • a+

File Modes: Binary file handling

  • rb – Read (Binary file)
  • wb – write (Binary file)
  • ab – append (Binary file)
  • rb+
  • wb+
  • ab+
  • r+b – reading (Binary file)
  • w+b – (Binary file)
  • a+b – (Binary file)

Closing a File: fclose() Function

  • After using a file, it’s important to close it using the fclose function.
  • Syntax: fclose(filePointer);

Reading from a File

  • For reading from a file, you use functions like fscanf, fgets, or fread.
  • Syntax: fscanf(filePointer, "%s", buffer); // Read a string

Writing to a File:

  • For writing to a file, you use functions like fprintf, fputs, or fwrite.
  • Syntax: fprintf(filePointer, "Hello, World!\n");

Error Handling:

  • It’s important to check if file operations are successful. The functions usually return NULL on failure.
  • Syntax: if (filePointer == NULL) { // Handle error }

File Positioning:

  • fseek and ftell functions can be used to set the file position indicator and get the current position.

Syntax:

fseek(filePointer, 0, SEEK_SET); // Move to the beginning of the file long position = ftell(filePointer); // Get the current position

Removing a File:

  • The remove function can be used to delete a file.
  • Syntax: remove("example.txt");

These are basic file management operations in C. Remember to always handle errors and close files after using them to avoid data corruption or loss.

NEXT

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example
C Programming Language

C – Storage Classes

Storage classes in C determine a variable’s scope, visibility, lifetime, and storage location. They include auto, extern, static, and register. The default initial value varies based on the storage class. Scope defines variable visibility, while lifetime dictates its persistence in memory.

Read more: C – Storage Classes
  • Scope: The extent to which different parts of a program have access to the variable, determining its visibility.
  • Lifetime refers to the duration for which the variable persists in memory, encompassing the allocation and deallocation of its storage. The scope also influences a variable’s lifetime.
  • variable types(Global variable)
Variable TypeLifetimeScope
Global VariableOnly destroyed when the program terminatesOnly the program
Local VariableAllocated memory when entering the function, destroyed when leavingLimited to the function
  • syntax: storage_class_specifier data_type variable_name;
SpecifiersLifetimeScopeDefault Initialize
autoBlock (inside function)BlockUninitialized
registerBlock (stack or CPU register)BlockUninitialized
staticProgramBlock or compilation unitZero
externProgramBlock or compilation unitZero
(none)Dynamic (heap)nilUninitialized

Auto Keyword

A variable declared inside a function without any specified storage class is termed an auto variable.

  • Only usable within functions.
  • Created and destroyed automatically upon function call and exit, respectively.
  • Compiler assigns them garbage values by default.
  • The stack memory stores local variables.
  • By default, every local variable in a function is of auto storage class.

Example:

void f() {
    int i;    // auto variable
    auto int j;   // auto variable
}

Global variable

  • A variable declared outside any function is a global variable.
  • Any function in the program can change its value.
  • initializing
    • int – 0
    • char – \0
    • float – 0
    • double -0
    • pointer – null

Register Keyword

The “register” keyword specifies that local variables are stored in a register for rapid access, particularly for counters. These variables offer quicker access than normal ones, but only a limited number can be placed in registers. While the compiler sees “register” as a suggestion, it ultimately decides. Typically, compilers optimize and determine variable allocation.

Register Note 1:

Attempting to access the address of a register variable results in a compile error. For instance:

int main() {
    register int i = 10;
    int *a = &i; // Compile error
    printf("%d", *a);
    getchar();
    return 0;
}

Register Note 2:

You can use the register keyword with pointer variables. Below is a valid example:

int main() {
    int i = 10;
    register int *a = &i;
    printf("%d", *a); // Output: 10
    getchar();
    return 0;
}

Resistor Note 3:

Register is a storage class, and C prohibits multiple storage class specifiers for a variable. Therefore, you cannot use register with static.

int main() {
    int i = 10;
    register static int *a = &i; // Error
    printf("%d", *a);
    getchar();
    return 0;
}

Register Note 4:

There’s no limit to the number of register variables in a C program. However, the compiler may choose to allocate some variables to registers and others not.


static keyword

  • static means its take only single or same memory.
  • static is initialized only once and remains into existence till the end of program
  • static assigned 0 (zero) as default value by the compiler.
  • A static local variables retains its value between the function call and the default value is 0.
  • If a global variable is static then its visibility is limited to the same source code.
  • Static variables stored in Heap(Not in Stack memory)
  • The following function will print 1 2 3 if called thrice.
  • Example
    • void f() { static int i; ++i; printf(“%d “,i); }
  • In “static int a[20];”, variable “a” is declared as an integer type and static. When a variable is declared as static, it is automatically initialized to the value ‘0’ (zero).

What is a static function?

  • A function prefixed with the static keyword is called a static function.
  • You would make a function static if it should only be called within the same source code or be visible to other functions in the same file.
  • static has different meanings in in different contexts.
  • When specified on a function declaration, it makes the function local to the file.
  • When specified with a variable inside a function, it allows the vairable to retain its value between calls to the function

storage class specifier

  • typedef – typedef is the storage class specifier.
    • It does not reserve the storage.
    • It is used to alias the existing type. Also used to simplify the complex declaration of the type.
  • reference links

volatile

  • The compiler omits optimization for objects declared as volatile because code outside the current scope can change their values at any time.

Advantages and Disadvantages

Storage ClassAdvantageDisadvantage
autoAutomatically allocated and deallocated within functionsGarbage values if not explicitly initialized
registerFaster access due to storage in CPU registersLimited availability of register space; Compiler-dependent
staticPersistent value across function callsGlobal scope may lead to namespace pollution; May increase memory usage
externAllows sharing of variables across multiple filesGlobal scope may lead to namespace pollution

Interview Questions

What are storage classes in ‘C’ language?

  • automatic class
  • static class

How many storage class specifiers in “C” language?

  • auto,
  • register,
  • static
  • extern

How many variables scopes are there in “C” language?

  • Body.
  • Function.
  • Program.
  • File.

NEXT

C Programming Language

C – Functions

In C programming, functions are essential building blocks for organizing and reusing code.

  • Rule: In C, you cannot define a function inside another function.
  • Rule: But you can call a function inside another function.
  • Rule: Functions cannot return more than one value at a time because, after returning a value, control is passed back to the calling function.
  • Any function, including main(), can call itself recursively.
  • C has two kinds of functions User Defined Functions,  Pr-Defined Libarary Functions.
Read more… →
C Programming Language

C – Compilers

Compilers are playing important role in software development. compilers are converting human-written code into another runnable software in machines.

CompilersDescriptions
Microsoft – Visual StudioVisual Studio is a free source from Microsoft. You can download and install from the visual studio side.

Supports in windows (offline).
Bortland   –  Turbo C, Turbo C++Supports in windows(offline).

You can download the portable version from this link
Dev C++Dev C++ compiler not support void in main function, main function should be mention int type and return type.

Supports in windows(offline).

you can download the portable version by here

you can do the debug of program refer the video
Gcc compilerSupport in Linux(offline).
C Compliers
Read more… →
C Programming Language

C Quiz-1

Participating in a C quiz can offer several benefits for individuals looking to improve their programming skills and knowledge of the C programming language. Here are some potential advantages:

  1. Skill Enhancement: Quizzes often cover a range of topics within C programming, allowing participants to test and enhance their skills in areas such as syntax, data types, control structures, functions, and more.
  2. Knowledge Validation: Quizzes can help individuals gauge their understanding of key concepts in C programming. By answering questions, participants can identify areas where they need to focus on improving their knowledge.
Read more… →