#include //this includes the C stdio.h library using namespace std; // This program demonstrates the use of C input and output functions in C++ // These are preferred sometimes, due to ease of formatting int main() { //The C input function is scanf, the C output function is printf. //Both of these functions take a format string. Please refer to the slides for details on format strings. int num = 512; double val = 25.987; char ch = 'A'; char str[50] = "Sample string"; //to print, use the appropriate format string. printf doesn't accept endl. Use \n printf("Integer: %d\nFloat: %f\nChar: %c\nCString: %s\n",num,val,ch,str); // The type of the variable in the list must match its corresponding format string, or should be compatible. printf("Enter an integer, a float, a character and a string (single space separated)\n"); //For reading, we need to pass in addresses, unless it is a cstring scanf("%d%lf %c%s",&num, &val, &ch, str); //While printing we can use %f for doubles, but while reading, %lf is preferred. //There is a space between the double and the char to make it ignore the whitespace while we input. //We can specify spacing in the format string printf("Integer: %6d\nFloat: %8.4f\nChar: %c\nCString: %s\n",num,val,ch,str); return 0; }