How to define a string and print it out with the printf() function.
#include <stdio.h> /* Defining a global string. */ #define hi "Hello Sire." int main(void) { /* Printing out the string. */ printf("%s\n", hi); return 0; } |
How to run the /bin/sh executable with some C code.
#include <stdlib.h> #include <unistd.h> int main(void) { execve("/bin/sh", NULL, NULL); return 0; } |
When no parameters are passed to a C program, display a menu.
if (!argc || !argv[1]) { char *myarg; myarg = argv[0]; printf("%s Usage:\n--datetime - Date & Time.\x2e\n" \ "--uname1 - Kernel Information\x2e\n" \ "--uname2 - Information on memory & processes\x2e\n" \ "--fortune - View a fortune cookie.\x2e\n" \ "--about - The Readme for this program\x2e\n\n" \ , myarg); } |
Print the current time and date.
void DateTime() { struct tm *ptr; time_t tm; char str[60]; tm = time(NULL); ptr = localtime(&tm); strftime(str, 100, "%A %d %B %Y. %H:%M:%S %Z.", ptr); printf("\n\n%s\n\n", str); } |
How to define and use a function as well as getting input from the console.
#include <stdio.h> #include <string.h> #define MSG "World" int goku(void) { printf("Hello %s.\n", MSG); return 1; } int main(int argc, char **argv) { char *name; name = argv[1]; if (strlen(name) < 5) { printf("Name too short.\n"); } goku(); printf("%s\n", name); return 0; } |
Here is a simple C program for printing a verbose time and date.
#include /* For time function (random seed). */ #include /* For extra functions. printf(). */ #include /* For getenv(); */ #define format "The time and date is: %A %d %B %Y. The time is: %H:%M:%S, %Z." int print_time(void) { struct tm *ptr; time_t tm; char length[60]; tm = time(NULL); ptr = localtime(&tm); strftime(length, 100, format, ptr); printf("%s\n", length); } int main(void) { print_time(); return 0; } |
Simple Hello World program.
int main() { write(1, "Hello World\n", 14); } |