This question already has an answer here:
What is the meaning of "%-*.*s" in a printf format string?
(1 answer)
Closed 3 years ago.
I don't know how this code is working?
#include<stdio.h>
int main()
{
char *s = "PRO coder";
int n = 7;
printf("%.*s", n, s);
return 0;
}
The result I am getting is "PRO cod"
printf format string %.*s takes two arguments, * for the number and finally s for a string, so it prints the first 7 characters of the string pointer s. In general, anytime there is a number, you may use * instead to read it as an argument.
%7s would print seven characters or more if the string were longer, while %.7s would print up to seven characters. So sometimes one would write "%*.*s", 7, 7, s to print exactly 7 characters.
Related
This question already has answers here:
How should character arrays be used as strings?
(4 answers)
Closed 1 year ago.
I'm trying to practice declaring Strings but I'm not getting correct output.
#include <stdio.h> //Im using vscode and gcc
int main()
{
char k[]= "prac c";
char l[6]= "stop c"; //first initializing as size 6
char m[6]= "nice c";
printf("%s \n%s \n%s",k,l,m);
return 0;
}
output: prac c
stop cprac c
nice cstop cprac c
But when I change the size from 6 to 7 this does not happen.
#include <stdio.h>
int main()
{
char k[]= "prac c";
char l[7]= "stop c"; //changing size to 7
char m[7]= "nice c"; //changing size to 7
printf("%s \n%s \n%s",k,l,m);
return 0;
}
output: prac c
stop c
nice c
The string "stop c" is really seven characters long, including the ending null-terminator. This goes for all your strings.
If there's no space in your arrays for the terminator then it won't be added, and the arrays are no longer what is commonly called strings.
Using such arrays as string will lead the code to go out of bounds of the arrays and give you undefined behavior.
"prac c", "stop c", "nice c" all occupy 7 bytes separately, as there's a terminating \0 for a string literal.
This question already has answers here:
Sizeof string literal
(2 answers)
Closed 2 years ago.
I want to know the reason behind why I am getting 2 bytes as a size of the character and at the same time 1 byte for the similar character when stored in a place-holder.
#include <stdio.h>
void main(void) {
char a = "$";
printf("%d\n", sizeof("$")); // Here output is 2 which should be 1
printf("%d", sizeof(a)); // Here output is 1 which is correct
}
it treats "$" as a string, so it's NULL terminated and has a sizeof 2.
same would happen if you would do sizeof "a".
if you want to get the sizeof the character itself you should do sizeof with '$', that's the symbol for character.
This question already has answers here:
How to repeat a char using printf?
(12 answers)
Closed 2 years ago.
I want to print a char '*' repeatedly where I give the no. of times the asterisk should be repeated.
Example: count = 20 and I want to print ******************** using printf() and format specifiers.
There is certainly no way to achieve that using only format specifier. You could hide your loop in a macro maybe but you'll definitely need a loop somewhere.
You cannot do that with standard format specifiers provided by printf(). However, there's a hacky solution (assuming the maximum padding length is reasonable), if you are willing to waste some space to store a filler string in your program.
#include <stdio.h>
int main(void) {
const char *fill = "********************"; // 20 chars
printf("%.*s\n", 10, fill);
printf("%.*s\n", 15, fill);
int n = 20;
printf("%.*s\n", n, fill);
return 0;
}
This works using .* to provide the maximum length of the string to print as first parameter.
Output:
**********
***************
********************
NOTE: you will only get up to strlen(fill) characters of padding (20 in the above example), anything more and printf will stop at the \0 terminator of fill.
I hope this is what you are looking for:
#include <stdio.h>
int main()
{
printf("%.*s", 20, "********************************");;
return 0;
}
This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 7 years ago.
I'm trying to convert an integer to a string in C but the current code doesn't make it.
I'm not seeking to display it in the screen, so all the functions printf, sprintf... are irrelevant.
int X = 15;
char *T;
T = (char*)X;
// Expected result : "15"
Can anyone help please ?
Thanks.
Not displaying it to screen doesn't invalidate functions like sprintf() since they literally "print to string".
int X = 15;
char buffer[10];
memset(&buffer, 0, sizeof(buffer)); // zero out the buffer
sprintf(buffer, "%d", X);
// Expected result : "15"
printf("contents of buffer: %s\n", buffer);
sprintf will print to a string, not the screen.
It's exactly what you're looking for.
This question already has answers here:
Print part of a string in C
(4 answers)
Closed 9 years ago.
Is it possible in C to set a number of chars to be output from string?
char const *str = "abcdefgh";
printf("%???s", str);
What should be placed instead of ??? to output abc?
Yes, you could use a precision specifier, which would look something like this
printf("%.3s", str); // this prints "abc"
yes there is , you can use printf("%.3s" , str) , or printf("%.*s" , 3 , str);
printf has the following definition:
int printf ( const char * format, ... );
A format specifier follows this prototype:
%[flags][width][.precision][length]specifier
You can take a look here to get an overview about these specifiers.
.number
For s: this is the maximum number of characters to be printed. By
default all characters are printed until the ending null character is
encountered.
However, as written by Happy Yellow Face, printf("%.3s", str); works fine.
This question does say "set number of chars" and not "limit number of chars".
The 3 solutions presented do the second. This is apparent when str is small like "ab".
To "set" the number to 3 use
printf(">%3.3s<\n" , "abcdef");
printf(">%3.3s<\n" , "ab"); // right justify
printf(">%-3.3s<\n" , "ab"); // left justify
printf(">%*.*s<\n" , 3, 3, "ab"); // right justify
printf(">%*.*s<\n" , -3, 3, "ab"); // left justify
>abc<
> ab<
>ab <
> ab<
>ab <
> < added for display purpose only.