What are the different ways to print a string in the C language?
There are several main ways to print strings in the C language.
- Print the string “Hello, World!” using the printf function.
- Print the string using the puts function:
puts(“Hello, World!”); - Use the putchar function to print characters in a string one by one:
char str[] = “Hello, World!”;
int i;
for (i = 0; i < strlen(str); i++) { putchar(str[i]); } - Copy a string to a character array using the strcpy function, and then print the character array using the printf function:
char str[50];
strcpy(str, “Hello, World!”);
printf(“%s”, str); - Format the string into a character array using the sprintf function, and then print the character array using the printf function:
char str[50];
sprintf(str, “Hello, %s!”, “World”);
printf(“%s”, str);
These are common ways to print strings, the specific choice depends on the specific requirements and scenarios.