Friday, December 18, 2020

, ,

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 : - 

int factorial(int );

int main()

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

    printf("Enter your name: ");

    printf("Enter your 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


Post a Comment