Professional Documents
Culture Documents
CPP Interview Pgms
CPP Interview Pgms
=============================
#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;
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");
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
}
Arrays
// Create an integer variable that will store the number we get from the user
int 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 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)
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.
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------