How to find range of data types in C programming?

In C programming data types play a major role, so is their size and range. The sizeof() operator gives you bytes required to store value of some type in memory. However, in programming you must be aware of range of a type to avoid overflow and underflow errors.

The size of a data type is compiler dependent and so is its range. So, you must not hardcode size and range values in your program.

There are two ways to find minimum and maximum range of a type. You can use any of the approach to get range of a type.

Read more

sizeof() operator in C programming

Size of a data type is machine dependent and vary from compiler to compiler. However, in programming there exists situations when we need to know total bytes a type occupies in memory. To find exact size of a type in C programming we use sizeof() operator.

sizeof() is a special operator used to find exact size of a type in memory. The sizeof() operator returns an integer i.e. total bytes needed in memory to represent the type or value or expression.

The sizeof() is much used operator by programmers. It is very useful for developing portable programs.

Read more

Data types in C programming

Data type is a system for defining various basic properties about the data stored in memory. Properties such as, type of data, range of data, bytes occupied, how these bytes are interpreted etc.

For example: int is a data type used to define integer type variables.
int a; here a is an integer type variable. It can store numbers from -2,147,483,648 to +2,147,483,647.

Data types in C is classified in three broad categories.

Read more

Comments in C programming

Comment is non-executable line in source code used to describe a piece of code or program. Comments provides inline documentation of source code and enhances readability of the code. It describes what a piece of code does.

Comments are for readers not for compilers. They make source code more developer friendly.

The compiler has nothing to do with comments, it is non-executable piece of code. Therefore, during the compilation process, pre-processor removes all comments from the source code.

Read more

Literals in C programming

Constant values used within a program are known as Literals. These constant values occupy memory but do not have any reference like variables. Or as Wikipedia speaks literal is a notation for representing a fixed value within a source code.

There are four types of literals in C programming.

  1. Integer literal
  2. Float or real literal
  3. Character literal
  4. String literal

Read more

Operator precedence and associativity in C

Order of evaluation of operations can alter the final result. For example, consider evaluating following expression without any mathematics rule.

5 + 3 * 4 / 2

If you do not follow any specific rule to evaluate such expressions, then you probably lead to ambiguous result. Such as one might think it will evaluate either 32 or 11 or 18.5 etc.

To overcome such ambiguity, in mathematics we follow BODMAS rule. Similarly, in computer programming we follow operator precedence and associativity rule.

Read more

Increment and Decrement operator in C language

Increment and Decrement operator are used to increment or decrement value by 1.

There are two variants of increment/decrement operator.

  • Prefix (pre-increment and pre-decrement)
  • Postfix (post-increment and post-decrement)

Syntax of increment/decrement operator

SyntaxDescriptionExample
++<variable-name>Pre increment++a
<variable-name>++Post incrementa++
--<variable-name>Pre decrement--a
<variable-name>--Post decrementa--

Important note: ++ and -- operators are used with variables. Using ++ or -- with constant will result in error. Such as expressions like 10++, (a + b)++ etc. are invalid and causes compilation error.

Read more

Bitwise operators in C

We use Bitwise operators to manipulate data at its lowest level (bit level). Bitwise operators works on each bit of the data.

Data in the memory (RAM) is organized as a sequence of bytes. Each byte is a group of eight consecutive bits. Bitwise operators are useful when we need to perform actions on bits of the data.

Bitwise operators work with integer type. They do not support float or real types.

Read more