Friday, October 30, 2020

,

Example program for slicing in python - python __slice__

  •  slice() function in python is used to sliced a given sequence or given object 
  • There are three parameters the first one is an starting point which is optional.
  • second parameter for slice function tells about the ending point.
  • Third one will tell the increment between each index.
Syntax:- 
slice(start, stop, step)
  • Create a slice object for slicing

result1 = slice(3)
print(result1)

)
result2 = slice(1, 5, 2)
print(slice(1, 5, 2))
slicing program in python
Example program for slicing


Program for slicing sub-string

py_string = 'Python'

slice_object = slice(3) 
print(py_string[slice_object])  # Pyt

slice_object = slice(1, 6, 2)
print(py_string[slice_object])   # yhn


  • Example program for negative slicing in python

py_str = 'Python'


slice_object = slice(-1, -4, -1)

print(py_str[slice_object])   # noh

In this program we use negative value for slicing the object
  • Example program for sublist and sub-tuple

py_l = ['P', 'y', 't', 'h', 'o', 'n']
py_t = ('P', 'y', 't', 'h', 'o', 'n')


slice_object = slice(3)
print(py_l[slice_obj]) # ['P', 'y', 't']

slice_object = slice(1, 5, 2)
print(py_t[slice_object]) # ('y', 'h')    

,

Program for perror function in c - library function

 perror is a c library function in c this function is used to print a descriptive error message in c.

Program for perror library function in c
Program for perror function

 

Syntax:-

void perror(const char *str)

  • str in c contain a custom message that is printed before the error message itself.
  • perror function in c doesn't return any value

Example Program for perror function in c

#include <stdio.h>

int main () {
   FILE *f;

 
   /* first rename the file */
   rename("file.txt", "updatefile.txt");

   /* now let's open same file */
   f = fopen("file.txt", "r");
   if( fp == NULL ) {
      perror("Error: ");
      return(-1);
   }
   fclose(fp);
      
   return(0);
}

Output

Error: : No such file or directory

Tuesday, October 27, 2020

,

Example program for tmpfile() function in c programming

  •  tmpfile() function in c programming is used to created the temporary file in program and this temporary file will be deleted after  termination of c program.
  • wb+ mode is used to opens file in binary update 
Example program for tmpfile() in c
Example program for tmpfile() function in c programming

 

Syntax :- 

FILE *tmpfile(void) 
  • this function return the file after creating the temporary file.

Program example for tmpfile() function in c


#include <stdio.h>
int main() 
{ 
    char str[] = "Hello function in c"; 
    int i = 0; 
  
    FILE* tmp = tmpfile(); 
    if (tmp == NULL) 
    { 
        puts("Unable to create temp file"); 
        return 0; 
    } 
  
    puts("Temporary file is created\n"); 
    while (str[i] != '\0') 
    { 
        fputc(str[i], tmp); 
        i++; 
    } 
  
    // rewind() function sets the file pointer 
    // at the beginning of the stream. 
    rewind(tmp); 
  
    while (!feof(tmp)) 
        putchar(fgetc(tmp)); 
} 
OUTPUT : -

Temporary file is created
Hello function in c

Monday, October 26, 2020

, ,

Example Program for setbuf Function in c

 setbuf() function in c programming is used to define how stream should be buffered and this function should be call when a file associated with the stream and a already open file but any input or output shouldn't take place.

Example Program for setbuf Function in c
Example Program for setbuf Function in c

 

Parameter for setbuf() function program

steam is an pointer in this program and a allocated buffer is used.

This function doesn't return any thing.

Example program for setbuf() :-

#include <stdio.h>

int main () {
   char buf[BUFSIZ];

   setbuf(stdout, buf);
   puts("This is functioinc.in");

   fflush(stdout);
   return(0);
}

OUTPUT

This is functioinc.in

Sunday, October 25, 2020

,

Example program for ftell function in c

 ftell() function in c is used to get the total size of file after move the file pointer to the end of the file and it return the current position in long type.

File can have more than 32767 bytes of data

Syntax : -

long int ftell(FILE *stream)


Let's Check out the Program for ftell() function in c programming.

 

#include <stdio.h>
#include <conio.h>
void main () {
   FILE *f;
   int len;
   f = fopen("one.txt", "r");
   if(f == NULL) {
      perror(“Error opening file”);
      return(-1);
   }
   fseek(f, 0, SEEK_END);
   len = ftell(f);
   fclose(f);
   printf("Size of file: %d bytes", len);
   getch();
}


OUTPUT For ftell() function program

ftell() function
ftell() in c

Saturday, October 24, 2020

, ,

Example program for fsetpos() function in C programming

 In c handling, fsetpos() function will be used to set the position of input file with the help of fsetpos() access that point.

If you use to fix the file indicator position at any time in the given file then we need to use the fsetpos() function.

 Return Value :- fsetpos() function return zero at the time of success or non zero at failure. 

Example program for fsetpos()
Example program for fsetpos() function in C programming

 

Example program for fsetpos() function

#include <stdio.h>
#include <stdlib.h>

int main(){
	
	FILE *f;
	
	char ch[100];
	
	fpos_t pos;

	
	f=fopen("functioninc.txt","w+");
	
	fgetpos(f,&pos);
	printf("Enter any five strings\n");
	for(int i=0;i<4;i++){
		
		scanf("%[^\n]",&ch);
		//write back to the file
		fputs(ch,f);
		
		fputs("\n",f);
		
		fflush(stdin);
	}
	//take the strings from the users
	scanf("%[^\n]",&ch);
	fputs(ch,f);
	
	fsetpos(f,&pos);
	printf("\n...............print the strings..............\n\n");
	while(!feof(f)){
		
		fgets(ch,100,f);
		
		printf("%s",ch);
	}

	//close the file
	fclose(f);

	return 0;
}<4 0="" 100="" a="" after="" and="" array="" back="" buffer="" ch="" character="" clear="" close="" don="" entry.otherwise="" entry="" every="" except="" f="" fclose="" feof="" fflush="" fgets="" file="" first="" for="" fputs="" from="" fsetpos="" i="" if="" in="" indicator="" initial="" is="" last="" line="" n...............print="" n="" new="" of="" or="" pos="" position="" pre="" print="" printf="" return="" s="" scanf="" set="" space="" stdin="" stream="" string="" strings..............="" strings="" t="" take="" takes="" taking="" the="" then="" this="" time="" to="" twice="" users="" waiting="" we="" while="" white="" write="">



,

Example Program for FSEEK() function in c programming

 fseek() function in c programming is used to write data into desired file with the help of pointer which help to find the location into the given file.

Example Program for FSEEK()
Example Program for FSEEK() function in c programming

 

Syntax :-

int fseek(FILE *stream, long int offset, int whence)  

There are three constants are used in fseeks() function in c  SEEK_SET, SEEK_CUR and SEEK_END


 #include <stdio.h>
void main(){  
   FILE *fp;  
  
   fp = fopen("myfile.txt","w+");  
   fputs("This is functioinc", fp);  
    
   fseek( fp, 7, SEEK_SET );  
   fputs("Adam stiffman", fp);  
   fclose(fp);  
} 

Now let see the Example Program for FSEEK() function in c

#include <stdio.h>
int main ()
{
   FILE *f;
   char data[60];
   fp = fopen ("test.c","w");
   fputs("functioinc.in is a programming tutorial website", f);
   fgets ( data, 60, fp );
   printf(Before fseek - %s", data); 
 
   // To set file pointet to 21th byte/character into the file
   fseek(f, 21, SEEK_SET);
   fflush(data);
   fgets ( data, 60, fp );
   printf("After SEEK_SET to 21 - %s", data);
 
   // To find backward 10 bytes 
   fseek(f, -10, SEEK_CUR);
   fflush(data);
   fgets ( data, 60, fp );
   printf("After SEEK_CUR to -10 - %s", data);
 
   // To find 7th byte before the end of file
   fseek(f, -7, SEEK_END); 
   fflush(data);
   fgets ( data, 60, f );
   printf("After SEEK_END to -7 - %s", data);
 
   // now set file pointer to the start of the file
   fseek(f, 0, SEEK_SET); 
 
   fclose(f);
   return 0;
}

Friday, October 23, 2020

, , ,

Example program for fprintf() function in c

 

  •  The fprintf() function in c is used to write the formatted data into the file.
  • This didn't print the data on the console.
  • All the arguments of function fprintf() is same as the printf() but fprintf() has an extra arguments which is pointer that is used to find the location where the formatted data is written in a file.

 Return value for fprintf() function:- This function return the total number of characters written to the file.

On the other hand it return the EOF when any error occurred. 

Program for fprintf() function in c : -  


#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *f;
    char Name[50];
    int roll_num, chars, i, n;
    float mark;

    fp = fopen("records.txt", "w");

    if(f == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    printf("Testing fprintf() function: \n\n");

    printf("Enter the number of records you want to enter: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++)
    {
        fflush(stdin);
        printf("\nEnter the details of student %d \n\n", i +1);

        printf("Enter name of the student: ");
        gets(name);

        printf("Enter roll no: ");
        scanf("%d", &roll_num);

        printf("Enter marks: ");
        scanf("%f", &mark);

        chars = fprintf(f, "Name: %s\t\tRoll no: %d\t\tMarks: %.2f\n",
            Name, roll_num, mark);
       printf("\n%d characters successfully written to the file\n\n", chars);
    }

    fclose(f);
    return 0;
}

 Output fprintf() program: -

 

Example Program For fprintf() function in c
Example Program For fprintf() function in c

Wednesday, October 21, 2020

, ,

List of function program in c programming language

 Well as all of you can see in previous tutorial of function in c programming we try to cover all in-build function in c not just only c but also other programming language like python, javascript, SQL, and other languages.

function program in c programming language
List of function program in c programming language

 

 

In this tutorial you can find all the function program in c and also learn about HOW TO USE IN-BUILD FUNCTION IN C with program example with explanation for these programs.

Check out our list of function program in c 

Tuesday, October 20, 2020

, ,

How to use the fflush in c programming with example

 fflush() function in c programming is use to  flushes the contents of output to given file.

Understand the fflush function:-

To discuses the common issue due to the output buffering after the execute the c program.

#include <stdio.h>
 
int main() {
    fprintf(stdout, "This is to stdout. ");
    fprintf(stderr, "This is to stderr. ");
    fprintf(stdout, "This is also to stdout. ");
}
Out for this code snippet comes in different order.

fflush function program in c
fflush function program in c


The reason behind for this random order is buffered of any file in computer.

Well by default stdout is newline-buffered and on the other hand stderr is unbuffered

For stdout the content will be stored inside the temporary buffer so the output will be displayed when counters a newline or end of the file.

 On the other hand  stderr is not buffered the out will be written immediately.

That's why the stderr will be displayed first in upper program.

But if you want to displayed the standout output first the you have to flush the output with the fflush program in c.

#include <stdio.h>
 
int main() {
    fprintf(stdout, "This is to stdout. ");
    // output stream immediately
    fflush(stdout);
    fprintf(stderr, "This is to stderr. ");
   
    fprintf(stdout, "This is also to stdout. ");
    // output stream immediately
    fflush(stdout);
}

Output Of fflush program in c


Output of fflush program
Output

Monday, October 19, 2020

, , ,

Example program for fscanf function in c

 fscanf() function in c programming is used to read input from a file. 

Well this function works same as the scanf() function in c programming but instead of read the given input in c program fscanf() function read the data from file.

Most of the arguments of fscanf() function in c are same as scanf() function.

  •  Return Value For fscanf() function program in c:-

This function return the number of value and complete the read operation or return the end of file EOF and -1 on error occurred.

 

Example program for fscanf() function in c


#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *f;
    char Name[50];
    int roll_num, chars;
    float mark;

    fp = fopen("records.txt", "r");

    if(f == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    printf("Testing fscanf() function: \n\n");
    printf("Name:\t\tRoll\t\tMarks\n");

    while( fscanf(f, "Name: %s\t\tRoll num: %d\t\tMarks: %f\n"
                    , Name, &roll_num, &mark) != EOF )
    {
        printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
    }

    fclose(f);
    return 0;
}


Output of c program
Output of c program

Friday, October 16, 2020

, ,

C program for ferror() function

 ferror() function in C Programming language is used to identify the error in given c file. 

C program for ferror() function
C program for ferror()

 

In this tutorial you will learn about how to use the ferror() function and programing syntax.

 Prototype :-  

 int ferror(FILE *filename);

Parameters :-

FILE *filename

C program for ferror() function

#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE *fr;
	char str[100];

	if((f=fopen("help.txt","r"))==NULL){
		printf("Cannot open the file...");
		//file is not exist program is terminated
		exit(1);
	}

	// Check if here is some error in the file
	if(ferror(f))
		printf("print the Error to read the file\n");
	else	
		printf("No error\n");

	printf("File content is--\n");
	//print the strings until EOF is encountered
	while(!feof(fr)){
		fgets(str,100,fr);
		//print the string
		printf("%s",str);
	}

	//remember to close the opened file
	fclose(f);

	return 0;
}

Wednesday, October 14, 2020

,

System() function program in c

 In System library function in c use the command int system(const char *command) name or program name to the host environment and also executed command processor and returns the command after complete the task that is given in the c program.

 Parameters For System() function program in c :

Command:-  This string in c program containing the name of requested variable.  
 
Return the status of the command at the success and  returned is -1 on error.
 
Example:-

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main () { char command[50]; strcpy( command, "-l" ); system(command); return(0); }


Example:-

This function list down the dir file with the help of c program

#include <stdio.h>
#include <string.h>

int main () {
   char command[50];

   strcpy( command, "dir" );
   system(command);

   return(0);
} 


system program in c
system program in c

Sunday, October 11, 2020

, ,

Example Program For Lab() Function In C Programming

 Lab() Function in c programming gives the absolute value of the integer and it return the absolute value of the long integer argument.

Showing program will tell how you can use the LAB function in c programming.

Example Program For  Lab() Function In C Programming
Example Program For  Lab() Function In C Programming

 

Parameters:-

Y − is a integral value. 

Return Value:-

This lab function returns the absolute value of y. 

LAB Function Program Example :-

#include <stdio.h>
#include <stdlib.h>

int main () {
   long int e,d;

   e = labs(64887L);
   printf("Value for e = %ld\n", a);

   d = labs(-1003080L);
   printf("Value for d = %ld\n", b);
   
   return(0);
}

OUTPUT:-

Value for e = 64887
Value for d = 1003080

Sunday, October 4, 2020

, ,

fput() program in C programming language

 fput() function in c programming is a file string function which is used to print a string to the file.

fput() function use two arguments pointer one of them is used for string and other one for file. 

A null terminated string pointed by str in to a file.A null character is not written into a given and at the time of success it returns the 0 and  EOF or -1.

fput() program in c language : -


#include<stdio.h>
#include<string.h>

int main()
{
    char str[50];
    FILE *fp;
    fp = fopen("yourfile2.txt", "w");

    if(fp == NULL)
    {
        printf("Error for opening file\n");
        exit(1);
    }

    printf("Testing with the help of fputs() function: \n\n");
    printf("To stop reading press Ctrl+Z in windows and Ctrl+D in Linux :");

    while( gets(str) != NULL )
    {
        fputs(str, fp);
    }

    fclose(fp);
    return 0;
}


Output :-

fput() program in C programming language
fput() program in C programming language



Saturday, October 3, 2020

,

bsearch function program in c programming language

 bsearch function program in c language

bsearch function in c used to perform the search in the array and array will be sorted by the ascending order.

 A pointer will be used to sorted the array and the base point to the initial element for the given array and the key pointed to the object that containing the value that going to be sorted with the comparison function.

Return value for the bsearch program:-

If search goes successful, bsearch() returns a pointer to an identical element of the array. If two or more elements are equal, the element pointed to that it is not  specified.

If
search goes unsuccessful finding the key, bsearch() returns NULL.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
 
#define NOSTRING 5
#define SIZE 26
 
int string_compare(void const *, void const *);
 
int main(void)
{
    int i;
    char strings[NOSTRING][SIZE];
    char search_str[SIZE];
    char *status;
 
    /* read strings from the user */
    printf("User, write in %d character strings...\n", NOSTRING);
    for (i= 0; i < NOSTRINGS; i++)
        fgets(strings[i], SIZE, stdin);
 
    printf("\n\n");
 
    /* display array of unsorted strings */
    printf("Before Sorting:\n");
    for (i = 0; i < NOSTRING; i++)
        fputs(strings[i], stdout);
 
    /* sort the array */
    qsort(strings, NOSTRING, SIZE, string_compare);
    printf("\n");
 
    /* array of sorted strings */
    printf("After Sorting:\n");
    for (i = 0; i < NOSTRING; i++)
        printf(strings[i], stdout);
 
    printf("\n\n");
 
    /* read the string to be searched for */
    printf("programer write about string you wish to search for...\n");
    fgets(search_str, SIZE, stdin);
    printf("\n");
 
    /* calling bsearch() to locate given string in the array */
    status = bsearch(search_str, strings, NOSTRINGS, SIZE, 
                       string_compare);
 
    /* verify if given value is found */
    assert(status != NULL);
    printf("your desired string is found!\n");
 
    return 0;
}
 
int string_compare(void const *str1, void const *str2)
{
    return strcmp((char *)str1, (char *)str2);
}

OUTPUT : -

bsearch function program in c programming language
 bsearch function program in c programming language

Friday, October 2, 2020

, ,

Strcspn function in c with example program

 Strcspn in c

Strcspn function in c programming use to find the first character in the given string that is equal to the character of the other string.
 
Strcspn function in c with example program
 Strcspn function in c with example program
 
 
Null characters are not included in this string search. 
 
strcspn function also know the end of the string with the null character (/0) 
 
This function return the index of the first character in the string.
 

Example

#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char string[SIZE] = "source string";
  char * substring = "alax";
 
  printf( "The first %i characters in the string \"%s\" "
          "are not in the string \"%s\" \n",
            strcspn(string, substring), string, substring);
 
}

OUTPUT

the first 10 character from string1("source string") are not in the given sting2 ("alax")

, , ,

strchr function in c with program example

strchr example in c

strchr function in c programming find the first appearance of the character in the string.
 
strchr function in c with program example
strchr function in c with program example

 
 
 The character in c programming function can be null character (/0) and the ending null char. also include in this string search. 
 
A string argument also contain the null character so that the end of the string can be noticed.
 
Return value for strchr program in c  

strchr function return a pointer if specified character are appears in the given string otherwise it return the NULL.
 

Example Program for strchr function in c

 
#include <stdio.h>
#include <string.h>
 
 
int main(void)
{
  char buffer1[SIZE] = "computer program";
  char * ptr;
  int    ch = 'p';
 
  ptr = strchr( buffer1, ch );
  printf( "The first occurrence of %c in '%s' is '%s'\n",
            ch, buffer1, ptr );
 
}