Sunday, May 17, 2020

,

miscellaneous function in c programming (scanf(),printf(),sprintf() and more)

miscellaneous function in c programming (scanf(),printf(),sprintf() and more)
miscellaneous function in c programming (scanf(),printf(),sprintf() and more)

printf() Function in c programming

printf is a predefined function in "stdio.h" header file with the help of this function we can print data or user-defined messages on a computer screen. printf() function can take a number of argument and every argument should separate with a comma (, ) Within the double cotes but the first argument must be within the double cotes (" ").

Syntax : - 

 printf("user defined message"); 
 or 
 prinf("Format specifers",value1,value2,..); 

examples for printf() function in c programming  : -  

int a=20;
double d=12.4;
printf("%f%d",d,a);

scanf() Function in c programming:-

 scanf() is a predefined function in "stdio.h" header file. It can be used to read the input value from the keyword.

Syntax: - 

scanf("format specifiers",&value1,&value2,.....);

examples for scanf() function in c programming  : - 

int a;
float b;
scanf("%d%f",&a,&b);

sprintf() function in c programming

sprintf() function is as printf() but insted of sending output to console it returns the formatted string.

Syntax: -
 int sprintf(char *str, const char *control_string, [ arg_1, arg_2, ... ]); 

The first argument in sprintf() function is a pointer which is pointed out towards the target string. All other arguments are the same as for printf() function in c.

sprintf() function write the data in string pointer str and return the number of characters written to str excluding the null character. if an error occurs in operation it returns -1.

  • Example to learn about how to use sprintf() function:-


#include <stdio.h>
#include <string.h>
int factorial(int );
 
int main()
{
 
    int sal;
    char name[30], designation[30], info[60];
 
    printf("your name: ");
    gets(name);
 
    printf("your designation: ");
    gets(designation);
 
    printf("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;
}

memcpy() function in c programming :-

memcpy() function in c programming using to copy n characters from the object pointed to s2 in the object pointed by s1. It returns a pointer to the destination. 

if objects overlap then memcpy() function may not work. 

  The syntax for c language: - 

void *memcpy(void *s1, const void *s2, size_t n);

s1:- an array where s2 will be copied.

s2:- a string that is going to copy in s1.

n:- number of characters to copy.

NOTE : - <string.h> header file should be included 

  • Example for memcpy() function

/* Example using memcpy by TechOnTheNet.com */

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

int main(int argc, const char * argv[])
{

    return 0;
}

memmove() function in c programming :-

memmove() function in c programming use to copy n characters from the object pointed to s2 in the object pointed by s1. its returns a pointer to the destination.

memmove() function in c programming will work for objects overlap. 

Syntax: -

void *memmove(void *s1, const void *s2, size_t n);

memchr() function in c programming :-

memchr() function in c programming using to searches a character in an object and return a pointer for that character.

 Syntax :- 

void *memchr(const void *s, int c, size_t n);
  •  Example for memchr() function in c : - 
/* Example using memchr by functioninprogramming.com */

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

int main(int argc, const char * argv[])
{
    char search[] = "functioninprogramming";
    char *ptr;

    /* Return a pointer to the first 'm' within the search string */
    ptr = (char*)memchr(search, 'm', strlen(search));

    /* If 'm' was found, print its location (This should produce "17") */
    if (ptr != NULL) printf("Found 'm' at position %ld.\n", 1+(ptr-search));
    else printf("'m' was not found.\n");

    return 0;
}

memset() function in c programming:-

memset() function in c programming use to stores a character into the first n character of object.

 syntax :- 

void *memset(void *s, int c, size_t n);

 strpbrk() function in c programming : - 

strbrk() function in c used to search a string for one of the set of characters. 

Syntax: - 

char *strpbrk(const char *s1, const char *s2);

 strbrk() function return a pointer of leftmost matching characterin the string. If due to any reason match not found  then it return NULL pointer.

 strspn() function in c  :- 

strspn() function in c programming is used to Search String for Initial Span of Characters in Set and return the index of the first character that is not in the set.

 strtok() function in c  : -

strtok() function in c programming splits the function in the number of tokens and returns a pointer to the first character of the token.

strtok() function stores a null character as the end of tokens as a termination.

syntax: - 

char *strtok(char *s1, const char *s2);

we hope these all function will help you to learn about c programming and its functions.

0 comments:

Post a Comment