vivek vivek - 2 years ago 91
C Question

How to print only some characters in C?

I have an array:

char arr[]="This is the string";

For instance, if I want to print only first 5 characters of that string, I have tried the following:


But it is printing whole string. Why?

Answer Source

- is a printf formater for justification, not precision.

What you want is the . formater which is used for precision :

printf("%.5s", arr);

This will print the first 5 elements of arr.

If you want to learn more about printf formaters, take a look at this link.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download