C examples for Function:Function Definition
Create a function with parameter that types 40 asterisks in a row.
#include <stdio.h> #include <string.h> /* for strlen() */ #define NAME "Book2s.com." #define ADDRESS "Main Str." #define PLACE "New York, CA 99999" #define WIDTH 40/*w ww. j a v a 2s . c o m*/ #define SPACE ' ' void show_n_char(char ch, int num); int main(void) { int spaces; show_n_char('*', WIDTH); putchar('\n'); show_n_char(SPACE, 12); printf("%s\n", NAME); spaces = (WIDTH - strlen(ADDRESS)) / 2; show_n_char(SPACE, spaces);/* use a variable as argument */ printf("%s\n", ADDRESS); show_n_char(SPACE, (WIDTH - strlen(PLACE)) / 2); printf("%s\n", PLACE); show_n_char('*', WIDTH); putchar('\n'); return 0; } void show_n_char(char ch, int num){ int count; for (count = 1; count <= num; count++) putchar(ch); }