chap 2 Flashcards
What are pre processor directives?
what are eg of directives ?
what is a header file?
define – Define Macros
Preprocessor Directives are instructions that are processed before compilation. it prepares/ready the code for compilation. for eg. #define PI 3.1415 here here at every occurrence of Pi will be replaced by 3.14
After this directive is processed by the preprocessor, every occurrence of PI in the code will be replaced by 3.1415.
The replacement happens before the compiler even looks at the code.
#include – File Inclusion
#ifdef / #ifndef – Check if a Macro is Defined
A header file is a file with a .h extension that contains declarations of functions, classes, and macros. The actual implementations (definitions) of these functions and classes are typically found in a corresponding .cpp file.
use ternary operator to find if the value is even or odd
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;</iostream>
int main() {
bool flag = true;
(flag == true )? cout«_space;“yes” : cout«_space;“no!”;
int max ;
int a = 2 ;
int b = 3;
(a>b) ? max = a : max =b;
cout «_space;“max = “ «_space;max;
return 0; }
Write a program to print pyramid of numbers using nested for loop
include <iostream></iostream>
using namespace std;
int main() {
int i, j, k;
int num = 5;
for (i = 1; i <= num; i++) { // Print spaces for (k = 1; k <= num - i; k++) { cout << " "; } // Print numbers for (j = 1; j <= i; j++) { cout << j; } cout << "\n"; } return 0; }
datatypes in cpp
3 types built in , derived and user defined
built in : char, int, float, void
derived datatypes:
array, functions, pointers, references
user defended datatype:
structure, union, class, enum
size of char int float double long double
1,2,4,8,10
how will you use enum
enum weekdays{SUN, MON, TUE, WED, THUR, FRI, SAT}
Enumerators are use to attach numbers to names,
what is typedef?
typedef is used to change the name of datatype eg:
typedef int whole;
now insted of int we can write whole
whole sum = 2 ;
type conversion
automatic :compiler converts data type to larger datatype
typecasting: programmer convert one data type to another datatype
by mentioning the type explicitly
int sum;
average = float(sum) /5;
variable
is name given to some part of memory location
rvalue , lvalue
the value which is assigned
the var that is to be assigned
const (literals)
values cannot be changed onced assigned
operators in cpp
arithmatic
relational
logical operators
bitwize operators
special operators
escape sequence
difference between preincrement and post increment
i++ and ++i
Control statement
selection : if , if-else, switch, nested if
iterative looping : for, while, do-while, nested for
breaking : break, continue, goto exit
check if the no. is odd or even
include<iostream></iostream>
using namespace std;
int main(){
int value = 3;
if (value%2==0){
cout«“odd”;}
else{
cout«“even”;
}
}