Can we print text in C without using printf() function ? Yes we can. Below we have shown 3 ways to print text without using printf(),
1st: Using puts
puts is function which works similar to printf, This is the best way which can be used in place of printf. While writing code just substitute puts in place of prinft. Below is a example in which we have used puts to print Hello World!
//Using Puts
#include<stdio.h>
#include<conio.h>
void main()
{
puts("Hello World!");
return 0;
}
2nd: Using write
we can also print text using "write". While using "write" to print text you need to mention string length using "strlen()" or else by entering number of characters, example: SlashMyCode=11, Hello=5, How=3.
//using write
#include<stdio.h>
#include<string.h>
int main()
{
write(1, "Hello World",strlen("hello world")); //OR write(1, "Hello World",11);
return 0;
}
3rd: Using system echo
Another way to print text is using system echo.this is bit similar to puts, Here replace printf with system and before starting the actual text add echo and then type the text which is going to appear on screen.
//using system echo
#include<stdio.h>
#include<string.h>
int main()
{
system("echo Hello world!");
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
system("echo Hello world!");
return 0;
}
Now you know printf is not only option to print text, These 3 way can also be used, If you think it was helpful then please do share with your friends because sharing knowledge is gaining knowledge :)
No comments:
Post a Comment
Feel free to ask us any question regarding this post