Created: 2023-08-16 16:00
Status: #concept
Subject: Programming
Tags: C C String Conversion Specifiers

stdio.h

#include <stdio.h>

int main(void)
{
    int height, length, width, volume, weight;
    
    printf("Enter the height of the box: ");
    scanf("%d", &height);
    printf("Enter the length of the box: ");
    scanf("%d", &length);
    printf("Enter the width of the box: ");
    scanf("%d", &width);
  
    volume = height * width * length;
    weight = (volume+165) / 166;


    printf("Volume (cubic inches): %d\n", volume);
    printf("Dimensional weight (pounds): %d\n", weight);

    return 0;
}

printf(*formatString, ...expr?)

Prints the formatString and supplies any % Conversion Specifiers in order of the ...expr.

printf("This is an integer: %d and this is a float: %f", int_number, float_value);
/* %d is replaced with int_number; %f is replaced with float_value */

/* this results in printing i & a random big integer */
printf("%d %d", i);                    /*** WRONG ***/
1 159204224

/* this results in just printing i & ignoring j  */
printf("%d", i, j);                /*** WRONG ***/
1

Variable Min Field Width & Precision

We can use * in place of the m or p in %m.pX to accept Integers in the ...args as the values for them.

int i = 7;

printf("%6.4d", i);
printf("%*.4d", 6, i); 
printf("%6.*d", 4, i); 
printf("%*.*d", 6, 4, i);

//  0007

Printing String Literals Only

We don't always have to have a Conversion Specifier or ...expr arguments to embed in our string.

We can simply use a String Literal Constant to print.

printf("I'm a normal string literal!");

scanf(*formatString, ...addr?)

It reads the stdin (typically the user keyboard input) string as a formatString and parses the Conversion Specifiers and stores the data into &addr which are Pointers to Variable Memory Addresses.

int i, j;
float x, y;

scanf("%d%d%f%f", &i, &j, &x, &y);
/* store the 1st & 2nd integers to variables i & j respectively */
/* store the 1st & 2nd floats to variables x & y respectively */

scanf Parsing Order.png

/* Adds two fractions */

#include <stdio.h>

int main(void)
{
    int num1, denom1, num2, denom2, result_num, result_denom;

    printf("Enter the first fraction: ");
    scanf("%d/%d", &num1, &denom1);

    printf("Enter the second fraction: ");
    scanf("%d/%d", &num2, &denom2);

    result_num = num1 * denom2 + num2 * denom1;
    result_denom = denom1 * denom2;

    printf("The sum is %d/%d\n", result_num, result_denom);

    return 0;
}

sprintf(char *dest, const char *format, ...args)

Prints a Format String *format with the passed ...args into a C String array pointer *dest.

#include <stdio.h>

int main () {
   char str[80];

   sprintf(str, "My name is %s", "Ian de Jesus");
   puts(str);
   
   return(0);
}

sscanf(const char *target, const char *fmt, ...addrs)

We can scan existing string variables for patterns set by the Format String fmt and store the values into ...addrs.

#include <stdio.h>

int main(void)
{
  const char *str = "hi world 69";
  int storage;

  sscanf(str, "%*s %*s %d", &storage);

  printf("%d", storage);
}

// outputs 69
* after the % in the Conversion Specifier when not providing any Constant integers in the ...args will make it skip any data that matches it.

References