Home / Programs / String printing : %s Example Program
Programming Example

String printing : %s Example Program

👁 1,037 Views
💻 Practical Program
📘 Step by Step Learning
String printing : %s Example Program

Program Code

#include <stdio.h> 
int main() 
{ 
    char a[] = "atnyla"; 
    printf("%s\n", a); 
    return 0; 
} 

Output

atnyla

Explanation

The %s format specifier is implemented for representing strings. This is used in printf() function for printing a string stored in the character array variable. When you have to print a string, you should implement the %s format specifier.

Syntax:
printf("%s",<variable name>);

The given code is a C program that declares a character array a and initializes it with the string "atnyla". The printf() function is then used with the %s format specifier to print the contents of the a array to the console.

Here's how it works:

  • The #include line includes the standard input/output library in the program.
  • The main() function is the entry point of the program.
  • The character array a is declared and initialized with the string "atnyla".
  • The printf() function is used to display the contents of a using the %s format specifier.
    • %s is a format specifier used to display a string of characters.
  • The \n character is used to insert a newline after the output is printed.
  • Finally, the return 0; statement terminates the program and returns 0 to the operating system indicating that the program executed successfully.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.