Open Means Open Means

Function prototype

No comments on “Function prototype”
////////////////////////////////////////
#include
// Function prototype.
void ErrorMsg();
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    // Call the function.
    ErrorMsg();
    return 0;
}
////////////////////////////////////////
// Display an error message.
////////////////////////////////////////
void ErrorMsg()
{
    std::cout << "\aError!";
}

Function Prototype Example (with Function arguments)

No comments on “Function Prototype Example (with Function arguments)”
////////////////////////////////////////
#include
// Prototype
void show(int = 1, float = 2.3, long = 4);
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
main()
{
    show();              // all three arguments default
    show(5);             // provide 1st argument
    show(6, 7.8);        // provide 1st two
    show(9, 10.11, 12L); // provide all three arguments
}
////////////////////////////////////////
// Display results of default arguments.
////////////////////////////////////////
void show(int first, float second, long third)
{
    std::cout << "  first = "  << first;
    std::cout << ", second = " << second;
    std::cout << ", third = "  << third << std::endl;
}

An inline function.

No comments on “An inline function.”
#include 
#include
////////////////////////////////////////
// An inline function.
////////////////////////////////////////
inline void error_message(char* s)
{
    std::cout << '\a' << s;
    std::exit(1);
}
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
main()
{
    error_message("You called?");
}

String copy function in c++

No comments on “String copy function in c++”
////////////////////////////////////////
#include
////////////////////////////////////////
// The first version of string_copy().
////////////////////////////////////////
void string_copy(char *dest, const char* src)
{
    while((*dest++ = *src++) != '\0')
        ;
}
////////////////////////////////////////
// The second version of string_copy().
////////////////////////////////////////
void string_copy(char* dest, const char* src, int len)
{
    while (len && (*dest++ = *src++) != '\0')
        --len;
    while (len--)
        *dest++ = '\0';
}
char misspiggy[20], kermit[20];
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
main()
{
    string_copy(misspiggy, "Miss Piggy");
    string_copy(kermit,
        "Kermit, the file transfer protocol", 6);
    std::cout << kermit << " and " << misspiggy;
}

Display time

No comments on “Display time”
////////////////////////////////////////
#include
#include
////////////////////////////////////////
// The first version of display_time().
////////////////////////////////////////
void display_time(const struct std::tm* tim)
{
    std::cout << "1. It is now " << std::asctime(tim);
}
////////////////////////////////////////
// The second version of display_time().
////////////////////////////////////////
void display_time(time_t* tim)
{
    std::cout << "2. It is now " << std::ctime(tim);
}
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    std::time_t tim = std::time(0);
    struct std::tm* ltim = std::localtime(&tim);
    display_time(ltim);
    display_time(&tim);
    return 0;
}

More Articles …

  1. Array of string pointers
  2. Call C function from C++ program
  3. Assignment and Expression
  4. Increment and Decrement Operator in c++
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Page 18 of 21

  • About Us
  • Faqs
  • Contact Us
  • Disclaimer
  • Terms & Conditions