Last Minute Notes: C and C++

Here I will share some of the very important Last minute notes for C and C++ programming languages. These are very useful if you want to brush up before taking any interview. Let’s start:

History:

  • C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming.
  • C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features.
  • C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983.
  • C++ is a superset of C, and that virtually any legal C program is a legal C++ program.

OOPS Concept:

C++ fully supports object-oriented programming, including the four pillars of object-oriented development:

  • Encapsulation
  • Data hiding
  • Inheritance
  • Polymorphism

Usage:

  • C/C++ is used by hundreds of thousands of programmers in essentially every application domain.
  • C/C++ is being highly used to write device drivers and other software that rely on direct manipulation of hardware under realtime constraints.
  • C/C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts.
  • Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++.

Important and Distinguished Pointed:

  • In C, goto statement can be used inside functions only and its label can point to anywhere in the same function only.
  • In switch body, two case can’t result in same value though having only one case or only default is okay. In fact, switch body can be empty also.
  • As per C standard, a jump statement causes an unconditional jump to another place and all gotocontinuebreakreturn are jump statements.
  • In C, typedef is used to create alias of any other type. It can be used to create alias for ‘array’ and ‘function pointer’ as well.
  • Multiple aliases of different types can be created using one typedef only. For example, ‘typedef int INT, *INTPTR, ONEDARR[10];’ is completely valid.
  • Important point is that array size can be derived from its initialization but that’s applicable for first dimension only. For example, ‘int arr[][2] = {1,2,3,4}’ is valid but ‘int arr[2][] = {1,2,3,4}’ is not valid.
  • Dereferencing of void pointer isn’t allowed because void is an incomplete data type.
  • In C, initialization of array can be done for selected elements as well. Specific elements in array can be initialized using []. For example, ‘int arr[10] = {100, [5]=100,[9]=100};’ is legal in C. This initializes arr[0], arr[5] and arr[9] to 100. All the remaining elements would be 0.
  • As per C standard, if array size is defined using variable, the array can’t be initialized at definition itself. For example, ‘int size = 2, arr[size];’ is valid but ‘int size = 2, arr[size] = {1,2};’ is invalid. Also, an array whose size is specified using variable can’t be defined out any function i.e. this array can’t be global.
  • In C, struct members can be initialized even out of order using field name and using dot operator. For example, ‘struct {int i; char c;} myVar = {.c =’A’,.i = 100};’ is valid.
  • In modern systems, size of all pointer types on a given platform is same i.e. whether it is ‘pointer to char’ or ‘pointer to int’ or ‘pointer to pointer to int’ etc., the size of all these pointers is same.
  • In C, an identifier (e.g. variable) can consists of letters, digits and underscores only. But it can’t start with a digit e.g. 9var is invalid.
  • Unlike ‘long long int’, there’s nothing like ‘long long double’ in C. Besides, real data types (i.e. floatdoublelong double) can’t be unsigned.
  • In C, ‘printf(“%d”,090);‘ is invalid because an octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only.
  • Real constants (e.g. 1.414) has data type as double. To convert them to float, we need to suffix them with f or F (e.g. 1.414f or 1.414F).
  • The only storage-class specifier that shall occur in a parameter declaration is register. That’s why even ‘fun(auto int arg)’ is incorrect.
  • In C, signedunsignedshort and long are Type specifiers and when they are used, int is implicitly assumed in all of these. So ‘signed i; unsigned j; short k; long l;’ is valid.
  • Though const and volatile look opposite to each other but a variable can be both const and volatile.
  • const is a Type qualifier and a variable qualified with const means that the value of variable isn’t modifiable by the program.
  • volatile is a Type qualifier and a variable qualified with volatile means that value of variable is subject to sudden change (possibly from outside the program)
  • Modulus operator (%d) isn’t defined for real types however library functions fmod(), fmodf(), fmodl() from math.h can be used for doublefloat and long double respectively.
  • In C, any of the 3 expressions of for loop i.e. for(exp1 ; exp2 ; exp3) can be empty. Even all of the three can be empty.
  • The controlling expression of a switch statement i.e. switch(exp) shall have integer type (i.e. int, char etc.) only.
  • When continue statement is hit in while or do-while loops, the next executed statement is controlling expression of while or do-while loops. But when continue statement is hit in for loop, the next executed statement is expression3 which is called increment expression as well.
  • As per C standard, continue can be used in loop body only. And break can be used in loop body and switch body only.