Basic C++ Syntax

  Home  C++ Programming  Basic C++ Syntax


“C++ Syntax frequently Asked Questions in various C++ Syntax job Interviews by interviewer. The set of questions here ensures that you offer a perfect answer posed to you. So get preparation for your new job hunting”



27 Basic C++ Syntax Questions And Answers

21⟩ What is switch case in C++ Syntax?

Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

switch ( ) {

case this-value:

Code to execute if == this-value

break;

case that-value:

Code to execute if == that-value

break;

...

default:

Code to execute if does not equal the value following any of the cases

break;

}

 188 views

22⟩ How one would use switch in a program?

#include

using namespace std;

void playgame()

{

cout << "Play game called";

}

void loadgame()

{

cout << "Load game called";

}

void playmultiplayer()

{

cout << "Play multiplayer game called";

}

int main()

{

int input;

cout<<"1. Play gamen";

cout<<"2. Load gamen";

cout<<"3. Play multiplayern";

cout<<"4. Exitn";

cout<<"Selection: ";

cin>> input;

switch ( input ) {

case 1: // Note the colon, not a semicolon

playgame();

break;

case 2: // Note the colon, not a semicolon

loadgame();

break;

case 3: // Note the colon, not a semicolon

playmultiplayer();

break;

case 4: // Note the colon, not a semicolon

cout<<"Thank you for playing!n";

break;

default: // Note the colon, not a semicolon

cout<<"Error, bad input, quittingn";

break;

}

cin.get();

}

 169 views

23⟩ What is prototype for that C string function?

The prototype for that function is:

istream& getline(char *buffer, int length, char terminal_char);

The char *buffer is a pointer to the first element of the character array, so that it can actually be used to access the array. The int length is simply how long the string to be input can be at its maximum (how big the array is). The char terminal_char means that the string will terminate if the user inputs whatever that character is. Keep in mind that it will discard whatever the terminal character is.

It is possible to make a function call of cin.getline(arry, 50); without the terminal character. Note that 'n' is the way of actually telling the compiler you mean a new line, i.e. someone hitting the enter key.

For a example:

#include

using namespace std;

int main()

{

char string[256]; // A nice long string

cout<<"Please enter a long string: ";

cin.getline ( string, 256, 'n' ); // Input goes into string

cout<<"Your long string was: "<< string <

cin.get();

}

 181 views

24⟩ What is C strings syntax?

Strings are arrays of chars. String literals are words surrounded by double quotation marks.

"This is a static string"

To declare a string of 49 letters, you would want to say:

char string[50];

 185 views

25⟩ How to access a variable of the structure?

To access a variable of the structure it goes:

name_of_single_structure.name_of_variable;

For example:

struct example {

int x;

};

struct example an_example; //Treating it like a normal variable type

an_example.x = 33; //How to access its members

 189 views

26⟩ What is general format for a prototype?

The general format for a prototype is simple:

return-type function_name ( arg_type arg1, ..., arg_type argN );

arg_type just means the type for each argument -- for instance, an int, a float, or a char. It's exactly the same thing as what you would put if you were declaring a variable.

 193 views

27⟩ What is format for defining a structure?

The format for defining a structure is:

struct Tag {

Members

};

Where Tag is the name of the entire type of structure and Members are the variables within the struct. To actually create a single structure the syntax is

struct Tag name_of_single_structure;

 188 views