0% found this document useful (0 votes)
4 views3 pages

Important Functions

The document explains important functions in C programming, including system('cls') for clearing the console in Windows, _getch() for reading a single character without echo, and an array of function pointers for calling game functions. It details the syntax and usage of these functions, highlighting their platform-specific characteristics. Additionally, it covers the declaration of an array of pointers to char for handling string literals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Important Functions

The document explains important functions in C programming, including system('cls') for clearing the console in Windows, _getch() for reading a single character without echo, and an array of function pointers for calling game functions. It details the syntax and usage of these functions, highlighting their platform-specific characteristics. Additionally, it covers the declaration of an array of pointers to char for handling string literals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

 Function: system("cls"); clears the console screen.


 Windows-Specific: The cls command is specific to Windows and clears the screen in
the Command Prompt.
 General Use: The system() function can run any command supported by the operating
system's command line (like dir, echo, etc.).

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:

 The user is prompted to select a game from a menu.


 The corresponding game function is called using the games[] array by passing the user
input as an index.
o For example, if the user enters 1, games[0]() is called, which corresponds to the
code_quiz() function.

char* ar[] (string literal array)


In C, the declaration char* ar[] represents an array of pointers to char (i.e., an array where
each element is a pointer to a char).

 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

or, more commonly, if you want to initialize it with string literals:

char* ar[] = {"string1", "string2", "string3"};

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.

You might also like