Created: 2023-08-20 11:41
Status: #concept
Subject: Programming
Tags: C stdio.h String Literal string.h Conversion Specifier
Format String
It is a string passed into functions for
printf
and scanf
which contains Conversion Specifiers in the form %m.pX
.
- see Minimum Field Width & Precision for more information.
Behavior with %s
When printing C Strings, the precision specifies how many characters should be printed out.
- minimum field width pads spaces to the outputted characters match the minimum width.
char str[] = "Are we having fun yet?";
printf("%.6s", str);
// Are we
printf("%10.6s", str);
// Are we
When using
scanf("%s", &x)
, we skip any white space characters and stop scanning a string when we reach a white space character.
If we want to read an entire string until the \n
character, we should use gets(char *)
.
#include <stdio.h>
int main(void)
{
char s[50], *x;
printf("Enter input: ");
x = gets(s); // returns a pointer to the address passed into it if successful
printf("s = %s\n", s);
printf("x = %s", x); // prints the same thing
}