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?)
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
What happens when we call more
%
conversion specifiers than ...expr
?It will result in Unexpected Behavior.
- The compiler will not check if there is sufficient amount of conversion specifications or whether they have matching C Data Type.
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 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 */
What is the most common error C programmers do with
scanf
?
scanf("%d", i); /*** WRONG ***/
scanf("%d", &i); /*** CORRECT ***/
They omit the&
operating on the...addr
arguments.- the
&
prefix is actually an Operator that says "get me the Memory Address of the variable". scanf
needs to read aformatString
and place the data into a Variable's memory address so it cannot accept variables directly.
What happens when we place ANY white space inside the
formatString
of scanf
?It will repeatedly read white space in the stdin string until it reaches a non-whitespace character (which is put back to be read in the next call).
The number & type of whitespace in the format string is irrelevant & will lead to the same outcome.
What happens when we place ANY character inside the format string of scanf?
When it encounters a non-whitespace-character inside a format string, it compares it with the next input character if they match, if they don’t, scanf
immediately returns.
/* 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)
#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.