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

String printing : %s Example Program

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

💻 Program Code

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

🖥 Program 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.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.