Read your first name and format it - C Language Basics

C examples for Language Basics:Hello World

Introduction

  • Prints it enclosed in double quotation marks
  • Prints it in a field 20 characters wide, with the whole field in quotes and the name at the right end of the field
  • Prints it at the left end of a field 20 characters wide, with the whole field enclosed in quotes
  • Prints it in a field three characters wider than the name

Demo Code

#include <stdio.h>
#include <string.h>

int main(void)
{
  char name[20];/*from   w ww . j  a v a  2 s. c  om*/
  int name_length;

  printf("Enter your first name: ");
  scanf("%s", name);
  name_length = strlen(name);
  printf("\"%s\"\n", name); // a. enclosed in double quotes
  printf("\"%20s\"\n", name); // b. double quotes, 20 char wide, right-justified
  printf("\"%-20s\"\n", name); // c. double quotes, 20 char wide, left-justified
  printf("\"%*s\"\n", name_length + 3, name); // d. double quotes, 3 char wider than name

  return 0;
}

Related Tutorials