The Astonishing standard C library in full detail

Hi there, the blog covers the standard C library with all its  library files in C also we tried to include all the C header files with functions, you think it is a C library tutorial  

C header files

standard C library is a collection of header files

<stdio.h> <float.h> <math.h> <string.h> <time.h> <limits.h> <assert.h>

The header file is a collection of function’s and variables

syntax for header file

# include <header>

Input and output <stdio.h>

HeaderfileFunctionsDescription
<stdio.h>printf( )write the O/P on the display
scanf( )read the I/P from a keyboard
main ( )always return an integer value

FILE operations

HeaderfileFunctionsDescription
<stdio.h>FILE *fopen( )opens the given file
fclose( )closes the file
rename ( )changes the old file name or directory to new name
remove ( )used to delete a file

Formatted input

To read a content in the file use fscanf( ), it is same as scanf(). plus,the scanf( ) function read from keyboard and the fscanf( ) function read from the given text file

syntax:

Rendered by QuickLaTeX.com

Formatted output

To write a content in the file use fprintf( )function, it is same as printf() function. plus,the printf() function display the content on the screen and the fprintf() function write the content in the given text file

syntax:

Rendered by QuickLaTeX.com

fopen( ) function

syntax:

Rendered by QuickLaTeX.com

Rendered by QuickLaTeX.com

Write an integer into a file With  “w” mode

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n =6630; // integer number
    FILE *fp; // file pointer fp
    fp=fopen("file.txt","w");
    if(fp==NULL) // check if the file contain NULL character
    {
        printf("\n file cannot be open"); // if yes then print, file cannot be open
        exit(1);
    }
    fprintf(fp,"number=%d",n);
    fclose(fp); //close file
    return 0;
}

output

A file.txt with integer value number =6630  will be created in the current project folder  

Read an integer from a file.txt with “r” mode

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n; 
    FILE *fp; // file pointer fp
    fp=fopen("file.txt","r");
    if(fp==NULL) // check if the file contain NULL character
    {
        printf("\n file cannot be open"); // if yes then print, file cannot be open
        exit(1);
    }
    fscanf(fp,"%d",&n);
    printf("\n the number= %d",n);
    fclose(fp); //close file
    return 0;
}

Output

the number =6630

If a file is opened with “w” mode the existing content of the file will be replaced with given content

Ex: suppose if a file.txt has integer value 20 and if you write the file.txt of 20 value with “w” mode with integer value 50. next, The value 10 will be replaced with 50

Opening a file with append(add) “a” mode

With append(add) “a” mode the content of the existing file.txt will not be replaced with the new content. plus, The new content will be added at the end of the existing text file content

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n =5530; // integer number
    FILE *fp; // file pointer fp
    fp=fopen("file.txt","a");
    if(fp==NULL) // check if the file contain NULL character
    {
        printf("\n file cannot be open"); // if yes then, print file cannot be open
        exit(1);
    }
    fprintf(fp,"number=%d",n);
    fclose(fp); //close file
    return 0;
}

Output

Traversing an array with file

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i;
    int arr[10]={1,2,3,4,5,6,7,8,9,10}; // array elements
    FILE *fp; // file pointer fp
    fp=fopen("file.txt","w");
    if(fp==NULL) // check if the file contain NULL character
    {
        printf("\n file cannot be open"); // if yes then print, file cannot be open
        exit(1);
    }
    for(i=0;i<=9;i++)
    {
    fprintf(fp,"number=%d\n",arr[i]);
    }
    fclose(fp); //close file
    return 0;
}
Output

Character input  and output functions

fgets( ) function

fgets( ) reads a string

Syntax

Rendered by QuickLaTeX.com

# include<stdio.h>
int main()
{
    FILE *fp;
    char string[10];
    fp =fopen("text.txt","r");
    fgets(string,8,fp);
    printf("the content of the file %s", string);
    fclose(fp);
    return 0;
}

fputs( ) function

writes a string

Syntax

Rendered by QuickLaTeX.com

# include<stdio.h>
int main()
{
    FILE *fp;
    char string[10];
    fp =fopen("text.txt","w");
    fp =fopen("text.txt","r");
    printf("\nenter the text%s", string);
    gets(string);// work same as scanf()
    fputs(string,fp);
    printf("\nthe content of the file %s",string);
    fclose(fp);
    return 0;
}

Direct input and output functions

fprintf() and fscanf() are the functions to open a file in text mode. plus, fread() and fwrite() are the functions to open a file in binary mode

Syntax

Rendered by QuickLaTeX.com

writing and reading an int value from a file
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num=6630;
    FILE *fp;
    fp=fopen("sample.txt","wb");
    fwrite(&num, sizeof(num),1,fp);
    fclose(fp);
    fp =fopen("sample.txt", "r");
    fread(&num, sizeof(num),1,fp);
    printf("num=%d",num);
    fclose(fp);
    return 0;
}
Output
num=6630
writing and reading an array from a file
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int arr[8]={1,2,3,4,5,6,7,8};
    int i;
    FILE *fp;
    fp=fopen("sample.txt","wb");
    fwrite(&arr, sizeof(arr),1,fp);
    fclose(fp);
    fp =fopen("sample.txt", "rb");
    fread(&arr, sizeof(arr),1,fp);
    for(i=0;i<8;i++)
    {
    printf("num=%d\n",arr[i]);
}
    fclose(fp);
    return 0;
}
Output
num=1
num=2
num=3
num=4
num=5
num=6
num=7
num=8
writing and reading first 4 elements of an array
fp=fopen("sample.txt","wb");
   fwrite(&arr, sizeof(arr),4,fp);
fp =fopen("sample.txt", "rb");
    fread(&arr, sizeof(arr),4,fp);

File positioning function’s

Ftell ( ) function

Ftell( ) function return’s the present file location

Syntax

Rendered by QuickLaTeX.com

text.txt
One,two,three
#include <stdio.h>
#include <stdlib.h>
int main () {
   FILE *fp;// file pointer fp
   int length;
   fp = fopen("text.txt", "r");
    if(fp==NULL) // check if the file contain NULL character
    {
        printf("\n file cannot be open"); // if yes then print, file cannot be open
        exit(1);
    }
   fseek(fp, 0, SEEK_END);
   length = ftell(fp);
   fclose(fp);
   printf("Total size of text.txt = %d bytes\n", length);
   return(0);
}
Output
Total size of text.txt =13 bytes
fseek() function

help us to seek an arbitrary position in the file

syntax

Rendered by QuickLaTeX.com

Origin

Rendered by QuickLaTeX.com

Sample.txt file

Rendered by QuickLaTeX.com

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char str [20];
fp =fopen("sample.txt","r");
if(fp==NULL)
{
    printf("\n cannot open file");
    exit(1);
}
fgets(str,20,fp);
printf("before SEEK_SET %s\n",str);
fseek(fp,4,SEEK_SET);
fgets(str,20,fp);
printf("after SEEK_SET%s\n", str);
fseek(fp,4,SEEK_CUR);
fgets(str,20,fp);
printf("after SEEK_CUR %s\n", str);
fseek(fp,-4,SEEK_END);
fgets(str,20,fp);
printf("after SEEK_END %s\n", str);
fclose(fp);
return 0;
}
Output
before SEEK_SET one,two,three
after SEEK_SET two,three
after SEEK_CUR two,three
after SEEK_END  hree
String Functions <string.h>

Rendered by QuickLaTeX.com

Strncmp()function

Strncmp compares two string first n characters

Syntax
Strncmp(s1,s2,n);

S1 is string1

S2 is string2

n is the number of character that you like to compare

return’s

firstly, <0 means s1 is less than s2

secondly, >0 means s1 is greater than s2

thirdly,=0 means s1 and s2 are equal

#include <stdio.h>
#include<string.h>
int main()
{
    char a[10]= "hello";
    char b[10]="hello";
    int c;
     c=strncmp(a,b,4);
    printf("c=%d", c);
    return 0;
}
Output
c=0
strcpy() function

the strcpy () copies the source string to the target string

syntax
char* strcpy(char* target, const char* source);
#include <stdio.h>
#include <string.h>
int main() {
   char s1[10] = "hi_there";
   char s2[10];
   // copies the  s1 to s2
   strcpy(s2, s1);
   printf("%s",s2);
  
   return 0;
}
Output
hi_there

remember: in comparison, the size of the target string has to be bigger then the source string

Math functions <math.h>

Rendered by QuickLaTeX.com

Utility functions <stdlib.h>

HeaderfileFunctionsDescription
stdlib.hexit( )terminate the program
abort( )terminate the program abnormally
free( )delete the space
malloc ( )used to allocate memory to structures
calloc ( )used to allocate memory to arrays
realloc ( )used to increase or Decrease the size of the array
rand ( )returns random integers in the range 0 to RAND_MAX

Implementation defined limits <limits>

Header files are a collection of functions and variables where as limits.h contains only variables in standard C library. plus,The variable in the limits.h is “global“(access anywhere in the program )and”constant“(values can not be changed )

Rendered by QuickLaTeX.com

What is an Assertion <assert.h>

Assert are a pre-processor macro present in standard C library, ideally used for finding the bugs faster and to use assert, you need to include header file such as <cassert> or <assert.h>

Syntax

assert(check_the _condition);

program more defensively

a common misconception about assertion that once’s the code is tested and shipped.next, assertions are no longer needed and Assertions should be turn off, to make code faster

According to “the pragmatic programmer book”(page no 123) for any complex program turning assertion’s off , ahead you give your code to production. it’s like walking on the high wire without a safety wire

Sample program

#include <iostream>
#include <cassert>
using namespace std;
int main()
{
    int n;
    cout<<" Enter a number";
    cin>>n;
    assert(n!=0);
    cout<<"Condition  is true";
    return 0;
}

Assertion failed error

The above program check’s a Boolean condition that ‘n’ should not be equal to zero. first of all, If the condition is true then not a thing will change

second, if the condition is false. next, the program will crash( abort)

the compiler will show you the line number, where the assertion statement failed,

It will be easy for you find the bug in the code.To sum up, assertions check the things which should not happen. plus, if something does not make any sense you will be the first one to know

when the assert statement condition is false.for that reason,the error assertion failed happened

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.