Advertisement
Formatted functions | Description |
---|---|
sscanf() |
The sscanf() function reads the values from a char[] array and store each value into variables of matching data type by specifying the matching format specifier. |
sprintf() |
The sprintf() function reads the one or multiple values specified with their matching format specifiers and store these values in a char[] array. |
#include<stdio.h>
int main()
{
char ar[20] = "User M 19 1.85";
char str[10];
char ch;
int i;
float f;
/* Calling sscanf() to read multiple values from a char[] array and store each value in matching variable */
sscanf(ar, "%s %c %d", &str, &ch, &i, &f);
printf("The value in string is : %s ", str);
printf("\n");
printf("The value in char is : %c ", ch);
printf("\n");
printf("The value in int is : %d ", i);
printf("\n");
printf("The value in float is : %f ", f);
sscanf(ar, "%s %c %d", &str, &ch, &i);
return 0;
}
The value in string is : User
The value in char is : M
The value in int is : 19
The value in float is : 1.850000
Advertisement
#include<stdio.h>
int main()
{
char target[20];
char name[10] = "Andrea";
char gender = 'F';
int age = 25;
float height = 1.70;
printf("The name is : %s", name);
printf("\n");
printf("The gender is : %c", gender);
printf("\n");
printf("The age is : %d", age);
printf("\n");
printf("The height is : %f", height);
/* Calling sprintf() function to read multiple variables and store their values in a char[] array i.e. string.*/
sprintf(target, "%s %c %d %f", name, gender, age, height);
printf("\n");
printf("The value in the target string is : %s ", target);
return 0;
}
The name is : Andrea
The gender is : F
The age is : 25
The height is : 1.700000
The value in the target string is : Andrea F 25 1.700000
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement