Important Functions
Important Functions
system("cls");
In C programming, system("cls"); is used to clear the console screen in Windows
environments. The system() function allows you to execute a command as if you were typing it
in the command line.
Explanation:
Syntax:
#include <stdlib.h>
system("cls");
The _getch() function in C is used to read a single character from the keyboard input. It is part
of the conio.h library, which is specific to certain older compilers like Turbo C/C++.
_getch():
1. Reads a Single Character: It waits for the user to press a key and returns the
corresponding character.
2. No Echo: Unlike getchar(), the character input by the user is not displayed on the
screen (i.e., no echo). This is useful for situations like password input.
3. Platform-Specific: _getch() is typically used in DOS-based environments or compilers
like Turbo C, and it may not be available or work in modern C compilers like GCC or
Clang without specific setups.
Syntax:
#include <conio.h>
int _getch(void);
Return Value: It returns the ASCII value of the character that was pressed.
Blocking: The program will pause and wait until the user presses a key.
void (*games[])(void) = {code_quiz, syntax_sprint, bug_hunter, type_master, mix_mode};
is an array of function pointers. Let's break down what it does and how it's used.
Explanation:
1. Function Pointers:
o A function pointer is a variable that stores the address of a function. It allows us
to call a function indirectly via the pointer. In this case, the array games[] is an
array of pointers to functions that take no arguments and return no value (void
(*)(void)).
2. Array of Function Pointers:
o The array games[] stores function pointers. Each element in the array
corresponds to one of the game functions (code_quiz, syntax_sprint,
bug_hunter, type_master, and mix_mode).
3. Using Function Pointers:
o The array games[] can be used to call the corresponding game function based on
user input. For example, if the user chooses option 1 (for code_quiz), we can call
games[0](), which will invoke the code_quiz function.
How It Works:
char*: This is a pointer to a char. It can hold the memory address of a character or the
beginning of a string (which is a sequence of characters).
ar[]: This is the declaration of an array. The size of the array is not specified, so it's a
flexible array of pointers.
In essence, char* ar[] is an array where each element points to a char (or the first character of
a string).
Syntax:
char* ar[size]; // Declare an array of pointers to char with a specified size
Explanation:
char*: This is a pointer to char. Each element in the array will be a pointer to a string
(which is just an array of characters terminated by a null character \0).
ar[]: This is an array of pointers. The size of the array can be omitted if you are
initializing it at the point of declaration.
{"string1", "string2", "string3"}: Here, you are initializing the array ar with 3
elements, where each element is a pointer to a string literal.