Friday, December 18, 2020

, ,

Write a program for feof function in c

 feof function in c programming is used for file handling.

 feof c use to find the end of an file.

Syntax for feof c program :- 

int feof(FILE *fp)

Example program for feof function in c

# include <stdio.h>
int main( )
{
   FILE *fp ;
   char c ;
   printf( "Opening the file test.c in read mode" ) ;
   fp = fopen ( "test.c", "r" ) ; // opening an existing file
   if ( fp == NULL )
   {
     printf ( "Could not open file test.c" ) ;
     return 1;
   }
   printf( "Reading the file test.c" ) ;
   while ( 1 )
   {
     c = fgetc ( fp ) ; // reading the file
     if( feof(fp) )
     break ;
     printf ( "%c", c ) ;
   }
   printf("Closing the file test.c as end of file is reached.");
   fclose ( fp ) ; // Closing the file
   return 0;
}

This file handling program use to read a open file and feof c function used to find the end of the file.

feof c
program output for feof c


, ,

Example for rand function in c programming

 rand function in c programming is used to generate the pseudo-random numbers with the help of rand c function.

rand c function return the pseudo-random numbers which is exist between 0 and RAND_MAX.

rand function in c didn't use any parameters.

Syntax for rand c : - 

int rand(void)

Example program for rand c 

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

int main () {
   int i, n;
   time_t t;
   
   n = 5;
   
   /* Intializes random number generator */
   srand((unsigned) time(&t));

   /* Print 5 random numbers from 0 to 49 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 50);
   }
   
   return(0);
}


output program for rand c
output program for rand c

, ,

Example program for strcat() function in c

 If you have a computer programming background then we believe you heard the name of concatenation this word in programming language is known for joins to strings.

program for strcat() function in c
Example program for strcat() function in c

Here in c language strcat() function in c use to joins two given string in c programs. 

Syntax for strcat c

char *strcat(char *destination, const char *source)

strcat c function defines in string.h header file.

strcat() c arguments

strcat() function use two arguments

  • destination - first one is destination string.
  • source - source of the string is second one.

strcat() function in c add  destination string and the source string and final result will be stored in destination string.

Example program for strcat() c

 

#include <stdio.h>
#include <string.h>
int main() {
   char str1[100] = "This is ", str2[] = "functioninc.in";

   // concatenates str1 and str2
   // the resultant string is stored in str1.
   strcat(str1, str2);

   puts(str1);
   puts(str2);

   return 0;
}


, ,

Example program for sprintf c function

  •  sprintf c function in used to convert any integer into a string.
  •   sprintf function in c didn't send the output to the console return an formatted string.

Syntax for sprintf c function 

int sprintf(char *str, const char *control_string, [ arg_1, arg_2, ... ]);
  •  First argument is pointer to a targeted string for sprintf function in c. 
  •  Other arguments are same like printf() function in c.
  • sprintf function in c use a variable str to write the data and give the number characters written to str.
  • sprintf c return the -1 if any error occurs. 

 Example for sprintf c function : - 

#include<stdio.h>
#include<string.h>
int factorial(int );

int main()
{

    int sal;
    char name[30], designation[30], info[60];

    printf("Enter your name: ");
    gets(name);

    printf("Enter your designation: ");
    gets(designation);

    printf("Enter your salary: ");
    scanf("%d", &sal);

    sprintf(info, "Welcome %s !\nName: %s \nDesignation: %s\nSalary: %d",
        name, name, designation, sal);

    printf("\n%s", info);

    // signal to operating system program ran fine
    return 0;
}


program output for sprintf function in c
program output for sprintf function in c

Thursday, December 17, 2020

, , ,

fopen function in c programming with it's example

 fopen c function is used to open a file which is denoted by the file name in c programming.

Syntax for fopen function in c : -

FILE *fopen(const char *filename, const char *mode)

Parameters For for fopen c : -


filename : - this element contain the file name which is going to be open.

Mode :- This string in c contain the method to access the file.

Check the mode for open a file in c programming with the help of fopen c function. 

  • "r" Mode : - This mode opens a file for reading.
  • "w" Mode : - this one use to create a empty file for writing.
  • "a" Mode : - It use to append the data  at the end of the file.
  • "r+" Mode : - This mode open a file to make an update in reading and writing.
  •  "w+" Mode :  - Create an empty file for both reading and writing.
  •  "a+" Mode : - Use to open an file for appending and also for reading.

Return Value for fopen c function : -

  •  fopen function in c return a file pointer otherwise give a null.
  • errno global variable is used to indicate the error in fopen function.

Example program for fopen function in c :

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

int main () {
   FILE * fp;

   fp = fopen ("file.txt", "w+");
   fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
   
   fclose(fp);
   
   return(0);
}


Example for fopen c
Example program for fopen c

, ,

Example program for fread function in c

 fread function in c used to read the data from desired file and stored it into a buffer.

syntax For fread c: -

size_t fread(void * buffer, size_t size, size_t count, FILE * stream)

Parameters For fread in c 

fread c use 4 parameters in it's program and these parameters are follow  :- 

Buffer : -  buffer is also know as the space which is used to temporarily stored the data.

it did the same work for fread function in c. 

 Size: This one is an another parameters which is used to read the size in byte for each elements.

Count: Used to count the number of elements (like file for fread in c).

Stream :- Pointer is used for file object to tell the place where data is going to be read.

Return value For fread function in c: -

It return the integer value at the time of success which is equal to the count otherwise error if  value less than count.

Example for the fread c

#include <stdio.h>

int main() {
  char buffer[20]; // Buffer to store data
  FILE * stream;
  stream = fopen("file.txt", "r");
  int count = fread(&buffer, sizeof(char), 20, stream);
  fclose(stream);
  // Printing data to check validity
  printf("Data read from file: %s \n", buffer);
  printf("Elements read: %d", count);
  return 0;
}
OUTPUT

fread c
fread c

, , , ,

Typecast Functions Like atoi in c,atof, itoa, atol more

 atoi in c is used to convert the string data type into int data type.

atoi function in c
 atoi function in c

 

Syntax for atoi in c:-

int atoi (const char * str);


 atoi in c function in c is used with the “stdlib.h” header file Well this function is also known as the typecast functions in C.

Example program for the atoi in c

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a[10] = "100";
    int value = atoi(a);
    printf("Value = %d\n", value);
    return 0;
}


Lets check the similar typecast functions Like atoi in c.

atof() function in c  :- atof function work similar like atoi the only difference is atof in c used to converts string data type into float data type.

 Example program for atof() function in c :-

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a[10] = "3.18";
    float pi = atof(a);
    printf("Value of pi = %f\n", pi);
    return 0;
}

 atol() function in c  :-  This function is used to change the string data type to long data type like it's name(atol).

Syntax for atol in c:  -

long int atol ( const char * str );

Example program for atol() function in c :-


#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char a[20] = "100000000000";
    long value = atol(a);
    printf("Value = %ld\n", value);
    return 0;
}


 itoa() function in c  :- This function is reversed of atoi in c, itoa function is used to convert the int data type to string data type.

Syntax : - 

char *  itoa ( int value, char * str, int base );

Example program for itoa() function in c


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    int a=54325;
    char buffer[20];
    itoa(a,buffer,2);   // 2 means decimal
    printf("Binary value = %s\n", buffer);
 
    itoa(a,buffer,10);   // 10 means decimal
    printf("Decimal value = %s\n", buffer);
 
    itoa(a,buffer,16);   // 16 means Hexadecimal
    printf("Hexadecimal value = %s\n", buffer);
    return 0;
}