C examples for Statement:while
The body of the loop will not execute if the condition is false.
The following pad() function adds spaces to the end of a string to fill the string to a predefined length.
If the string is already at the desired length, no spaces are added.
#include <stdio.h> #include <string.h> void pad(char *s, int length); int main(void) { char str[80];// www . j a va 2 s. c om strcpy(str, "this is a test"); pad(str, 40); printf("%d", strlen(str)); return 0; } /* Add spaces to the end of a string. */ void pad(char *s, int length) { int l; l = strlen(s); /* find out how long it is */ while (l < length) { s[l] = ' '; /* insert a space */ l++; } s[l] = '\0'; /* strings need to be terminated in a null */ }