C++ Flashcards
Difference b/w \n and endl
\n - Just puts a newline
endl - Will also flush the buffer
What happens when two operators of equal precedence occur in an expression?
The Associativity will apply from left to right.
What is a Comma(,) operator in C++?
In C and C++, comma (, ) can be used in two contexts:
1) Comma as an operator:
The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator and acts as a sequence point.
/* comma as an operator */
int i = (5, 10); /* 10 is assigned to i*/
int j = (f1(), f2()); /* f1() is called (evaluated) first followed by f2().
The returned value of f2() is assigned to j */
2) Comma as a separator:
Comma acts as a separator when used with function calls and definitions, function-like macros, variable declarations, enum declarations, and similar constructs.
/\* comma as a separator \*/ int a = 1, b = 2; void fun(x, y);
More info: Comma Operator
What is the output of following program? #include using namespace std;
int main()
{
~~~
int a = 5; a = 2, 3, 4; cout << a; return 0; } ~~~
Output: 2
The following expression in the code:
a = 2, 3, 4;
is evaluated as:
(((a = 2), 3), 4);
This is because the reason that assignment operator has high precedence over the comma operator.
What is the output?
#include
using namespace std;
~~~
int main() { int arr[2] = {10, 20, 30};
return 0; } ~~~
Compile error:
prog.cpp: In function ‘int main()’: prog.cpp:6:29: error: too many initializers for ‘int [2]’ int arr[2] = {10, 20, 30}; ^
What is the output?
#include
using namespace std;
~~~
int main() { int arr[2] = {10, 20};
cout << arr[0] << " " << arr[1] << " " << arr[2]; return 0; } ~~~
10 20 1294526976
If trying to access the array out of bound(arr[2]), it prints a random value.
What is the output?
#include
using namespace std;
~~~
int main() { int arr1[2] {10, 20}; int arr2[6] {10, 20};
cout << arr1[0] << " " << arr1[1] << " "; cout << arr2[0] << " " << arr2[1] << " " << arr2[2]; return 0; } ~~~
10 20 10 20 0
When Array is partially initialized, it stores 0 values in the non-initialized values.
What is the output?
#include
using namespace std;
~~~
int main() { int arr[] = {10, 20, 30, 40};
cout << sizeof(arr); return 0; } ~~~
16
Size of 4 int elements
What is the output?
#include
using namespace std;
~~~
int main() { int arr[] = {10, 20, 30, 40};
cout << sizeof(arr)/sizeof(arr[0]); return 0; } ~~~
4
Size of 4 int elements = 16 / Size of single int element
Output?
#include < iostream >
using namespace std;
~~~
int main()
{
char str[] = “gfg”;
cout «_space;str;
return 0;
}
~~~
O/p: gfg
This is a C-style string with a null termination at the end. It stores as “gfg\0”.
Output?
#include < iostream >
using namespace std;
~~~
int main() { char str[] = "gfg"; cout << sizeof(str); return 0; }
~~~
O/p: 4
C-style strings include the null character also in the size.
Output?
#include < iostream >
using namespace std;
~~~
int main() { char str[3] = "gfg"; cout << sizeof(str); return 0; }
~~~
Compile error:
prog.cpp: In function ‘int main()’: prog.cpp:6:19: error: initializer-string for array of chars is too long [-fpermissive] char str[3] = “gfg”; ^
Output?
#include < iostream >
using namespace std;
~~~
int main()
{
char str[] = {‘g’, ‘f’, ‘g’};
cout «_space;str;
return 0;
}
~~~
O/p: gfg¸ü
Some random characters at the end
#include using namespace std;
int main()
{
char str[] = {‘g’, ‘f’, ‘g’, ‘\0’};
cout «_space;str «_space;endl;
cout «_space;sizeof(str) «_space;endl;
return 0;
}
~~~
~~~
O/p:
gfg
4
Output?
#include < iostream >
#include < cstring >
using namespace std;
~~~
int main()
{
char s1[] = “abc”;
char s2[] = “bcd”;
int res = strcmp(s1, s2);
if (res > 0)
cout «_space;“Greater”;
else if(res == 0)
cout «_space;“Same”;
else
cout «_space;“Smaller”;
}
~~~
O/p: Smaller
strcmp compares the string lexicographically, i.e “aa” is greater than “a”.
“ab” is greater than “aa”. “bcc” is smaller than “bcd”.