How to print a limited number of characters in C

2 Answers

0 votes
#include <stdio.h>

int main() {
    char s[] = "abcdefghijklmnop";

    printf("%.5s", s);
    
    return 0;
}


/*
run:

abcde

*/

 



answered Apr 10, 2025 by avibootz
0 votes
#include <stdio.h>

int main() {
    char s[] = "abcdefghijklmnop";
    int num = 6;

    printf("%.*s", num, s);
    
    return 0;
}


/*
run:

abcdef

*/

 



answered Apr 10, 2025 by avibootz

Related questions

...