Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

CPP Interview programs

=============================
#create a variable
type variableName = value;
char a = 'A';

When you don't want others (or yourself) to override existing variable values, use
the const keyword
const float PI = 3.14;

Modulus Returns the division remainder

x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Operators.
1. Arithmetic Operators 2. Assignment Operators 3. Logical Operators 4. Comparison
Operators
5. Sizeof Operator

if ( a > b )
printf("hi");
else
printf("bye");

Short Hand If...Else (Ternary Operator) ?:


variable = (condition) ? expressionTrue : expressionFalse;

switch:

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

The default keyword specifies some code to run if there is no case match:

while (condition) {
// code block to be executed
}

do {
// code block to be executed
}
while (condition);
for (statement 1; statement 2; statement 3) {
// code block to be executed
}

The break statement can also be used to jump out of a loop.


The continue statement breaks one iteration (in the loop), if a specified condition
occurs,
and continues with the next iteration in the loop.

Arrays

Arrays are used to store multiple values in a single variable,


instead of declaring separate variables for each value.

To access an array element, refer to its index number.

// Declare an array of four integers: //array size


int myNumbers[4];

c strings are char array (null terminated)


size of array should consider the null terminating char \0

// Create an integer variable that will store the number we get from the user
int myNum;

// Ask the user to type a number


printf("Type a number: \n");

// Get and save the number the user types


scanf("%d", &myNum);

// Output the number the user typed


printf("Your number is: %d", myNum);

// %p for pointer/address
int myAge = 43;
printf("%p", &myAge); // Outputs 0x7ffe5367e044

// dereference in c using *

Declaration: the function's name, return type, and parameters (if any)
Definition: the body of the function (code to be executed)

Recursion

Recursion is the technique of making a function call itself. This technique


provides a way to break complicated problems down into simple problems which are
easier to solve.

Recursion may be a bit difficult to understand. The best way to figure out how it
works is to experiment with it.

Math Functions
sqrt()
printf("%f", ceil(1.4)); //2 round upward
printf("%f", floor(1.4)); //1 round downwards
printf("%f", pow(4, 3)); //64 4 cube
exp(x)

struct MyStructure { // Structure declaration


int myNum; // Member (int variable)
char myLetter; // Member (char variable)
}; // End the structure with a semicolon

a=10;
b=20;
a = a+b; //30
b= a-b; //10
a= a-b //20
----------------------------------------------------
#include <stdio.h>
int fprintf(FILE *stream, const char *format, …);
int printf(const char *format, …);
int sprintf(char *buffer, const char *format, …);

Error return
A negative value indicates that an output error has occurred.
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------

You might also like