Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I properly use the printf function?

+8 votes
asked Jan 4 by Kyden Busy (200 points) 1 flag
I've only used the cout and cin functions when I found out that calling functions using <iostream> doesn't really work, I started using printf. But I have no idea how to actually use it.

Please help, Kyden.

2 Answers

0 votes
answered Jan 7 by Peter Minarik (19,180 points)

iostream is C++ library. Maybe you compiled the code for C only (top right corner)?

The following code works fine in C++ (but obviously does not compile in C), check it:

#include <iostream>

int main()
{
    std::cout << "Hello World" << std::endl;
    return 0;
}
0 votes
answered Jan 7 by Pallabesh Maharana (450 points)

printf() function is a function of 'C' language. It is used to print something in output screen (Same as cout function in 'C++' language). This requires stdio.h header file to be executed (Some times it also runs without any header file, depending upon the compiler you use!).

 Syntax for displaying hello world :-

#include<stdio.h>

void main()

{

printf("hello world");

}

Note:- Make sure you give a semicolon(;) after printf statement.

And if you want to display value of a variable. 

Syntax to print value of a :-

#include<stdio.h>

void main()

{

int a=69;

printf("%d",a);

}

Note:- Here %d is a format specifier. If you don't know, search for it...

commented Jan 7 by Pallabesh Maharana (450 points)
Hope you got it.
If not  then click here for more examples....
https://www.w3schools.in/c-program/print-string/
And if you don't know what is format specifier click here...
https://www.tutorialspoint.com/format-specifiers-in-c
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...