A program is a set of instructions written on a computer to perform a specific task.
Simply said, it is a language used to write a program.
It is a well defined medium to instruct the computer.
Based on uses, the Programming languages can be classified under 50 types refer List of Programming Languages by type
Based on ease of readability:
Based on execution method:
A few basic instructions that appear in almost all language (by Allen Downey, in his book How To Think Like A Computer Scientist):
C is a general-purpose, procedural computer programming language supporting structured programming, with a static type system. In static type system, variables are defined to have only a specific type of data and cannot be assigned a different type of data.
By design, C provides constructs that map efficiently to typical machine instructions.
It is generally used to learn basic programming concepts. C language is being used to build other High level programming languages like python.
/**
* Basic Structure of a C program.
* It is the documentation section
*/
#include <stdio.h>
// definition and global declarations
int main(){
//Declaration and Initialization
//Statements
return 0;
}
The first few lines define what is called a documentation. This section contains a multi line comment describing the code. The details on comments are discussed below.
The next is the list of preprocessor directives (indicated by # symbol).
The preprocessor directives are set of commands that are executed before the start of the actual program execution.
They are usually commands that define data required for program execution or are the link for the compiler to indicate header files that are being used from the library. Header files are documents that define the symbols that are used in the program. Here stdio.h
header file is linked.
The next section is the area of definition of Constants and Global variables
Below this is the area of code.
The
main
function is the point of code at which execution of the program startsMore details on
functions
will be discussed later.
Data can be defined as a representation of facts, concepts, or instructions in a formalized manner, which should be suitable for communication, interpretation, or processing by human or electronic machine.
Data type is an attribute which tells the compiler or interpreter how the programmer intends to use the data.
Basic Types:
The following are the basic or primitive data types supported by C language.
true
and false
(n = number of bits supported by the system)
ASCII - American Standard Code for Information Interchange
Derived Data Types:
These are the data types which are derived or are formed by combination of two or more primitive data types.
Additional types in integers (Base):
Derived
orNon primitive
data types and thetypes of integers
will be discussed later
A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows:
Words used for a special purpose in program. Since they have a special function, they must not be used as identifiers. If used, they may cause issues while execution of the program.
E.g: scanf
, printf
, int
, if
, for
, try
, etc.
Literals are data that are directly used or processed in a program. If your program contains direct values like 10
, 3.14159
, 'a'
or "str"
, they are known as literals.
An identifier is a symbol used for any variable, function, data definition, labels in your program, etc.
Before starting any language, you must at least know how you name an identifier in that language.
In C, an identifier is a combination of alphanumeric characters, i.e. first begin with a letter of the alphabet or an underscore, and the remaining are alphabets, numeric digit, underscores.
There are certain rules to be followed while constructing an identifier name:
'_'
) like a1, a2, _name not 1a, 24cNaming Convention is the general practice in a programming language followed by majority of the programmers around the globe for convenience in reading. The list of naming conventions in several languages are listed in the article Naming Convention - Wikipedia article
For Easy readability:
Naming convention for multi word variable names:
camelCase
PascalCase
SCREAMINGCASE
lazycase
kebab-case
snake_case
Of the above, the
camel
andsnake
cases are the most common ones
Eg.:
age, input_values, firstName, number, prime_num
Not this way:
user name, number_to_find_prime
Example file - Variables (data type), Naming Convention
Declaration is the process of defining the variables’ or constants’ names to be used in the program.
Syntax:
<type> variableName;
Multiple variables of same type can be declared on the same line, being separated be comma.
Eg:
int age, num1;
char ch;
Initialization is the process of assigning values to the variables declared. It is done using assignment operator =
. (For Details, check Assignment operators)
Eg:
age = 10;
ch = 'a';
Both Declaration and Initialization can be done in the same statement, for multiple variables
int age = 20, num2 = 10;
char ch = 'z';
In C
, constants are declared and initialized in a single line using the const
keyword. It is advised to have the constants’ name in Uppercase.
Syntax:
const <type> variable_name = <value>;
Eg:
const int MY_CONSTANT = 10;
Used to perform arithmetic and logical operations.
Arithmetic operators are used to perform arithmetic operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 2 + 3 = 5 |
- |
Subtraction | 2 - 3 = -1 |
* |
Multiplication | 2 * 3 = 6 |
/ |
Division (returns quotient) | 2 / 3 = 0 |
% |
Modulo (returns remainder) | 2 % 3 = 2 |
++ |
Increment operator | ++2 or 2++ |
-- |
Decrement operator | --3 or 3-- |
They define the relations between two variables. They result true
if the relation is as defined by the operator and false
otherwise.
Operator | Name | Usage | Example |
---|---|---|---|
== |
Equal | a == b |
2 == 3 (false) |
!= |
Not Equal | a != b |
2 != 3 (true) |
> |
Greater | a > b |
2 > 3 (false) |
>= |
Greater or Equal | a >= b |
2 >= 3 (false) |
< |
Lesser | a < b |
2 < 3 (true) |
<= |
Lesser or Equal | a <= b |
2 <= 3 (true) |
They are used to perform Logical operations on two variables / expressions. They offer boolean results based on logical table of the respective operator.
Operator | Description |
---|---|
&& |
Logical Operator AND |
|| | Logical Operator OR |
! |
Logical operator NOT |
Perform bitwise operations. This class of operators takes the binary form of each integer, and applies the operator of respective bits.
The binary forms are padded by 0
on the left to compensate the differences in number of bits to represent each value.
Operator | Description | Syntax | Example |
---|---|---|---|
& |
Bitwise AND | x & y |
5 & 2 |
| | Bitwise OR | x | y | 5 | 3 |
~ |
Bitwise NOT | ~x |
~1 |
^ |
Bitwise XOR | x ^ y |
6 ^ 1 |
<< |
Shifts y bits in x to the left (Left shift operator) | x << y |
10 << 2 |
>> |
Shifts y bits in x to the right (Right Shift Operator) | x >> y |
15 >> 2 |
1. Bitwise AND (&
):
It applies Binary AND
operation to every bit.
Eg:
5 & 2 = 0
5 = 101
2 = 010 // Padding zeroes on left
5 & 2 = 000 = 0
2. Bitwise OR (|
):
It applies the Bitwise OR
operation to every bit
Eg:
5 | 3 = 7
5 = 101
3 = 011 // Padding zeroes on left
5 | 3 = 111 = 7
3. Bitwise NOT (~
):
It applies the negation/complement/ NOT
operation. Additional to flipping the bits, it changes sign.
Eg:
~5 = 2
5 = 101
~5 = 010 = 2
Anomaly in C
:
The bitwise complement operator acts differently in C
.
Instead of flipping the bits, it provides 2’s complement of the number.
Two’s complement is an operation on binary numbers. The 2’s complement of a number is equal to the negative of the number plus 1, i.e.,
~N = -(N + 1)
So,
~5 = -(5 + 1) = -6
4. Bitwise XOR (^
):
It applies the Exclusive OR
(XOR
) operation for each bit.
Eg:
6 ^ 2 = 4
6 = 110
2 = 010 // Padding zeroes on left
6 ^ 1 = 100 = 4
5. Left Shift Operator (<<
):
Shifts the bits to the left by adding trailing zeros and removing start bits (MSB) which overflows.
Eg:
5 << 2 = 20
5 = 101
5 << 2 = 10100 = 20
6. Right Shift Operator (>>
):
Shifts the bits to the right by adding leading zeroes and removing End bits (LSB).
Eg:
15 >> 2 = 0
15 = 1111
15 >> 2 = 0011 = 3
Assigns value (literal or result of an expression) to a variable.
Operator | Description | Example |
---|---|---|
= |
Assign | c = 30 |
+= |
Add and assign | a += b |
-= |
Subtract and assign | a -= b |
*= |
Multiply and assign | a *= b |
/= |
Divide and assign | a /= b |
%= |
Get remainder and assign | a %= b |
<<= |
Left Shift and assign | a <<= b |
>>= |
Right Shift and assign | a >>= b |
&= |
Bitwise AND and assign | a &= b |
|= | Bitwise OR and assign | a | = b |
^= |
Bitwise XOR and assign | a ^= b |
Besides the operators discussed above, there are a few other important operators supported by C language
Operator | Description | Example |
---|---|---|
& |
Address Operator (returns Address of a identifier) | &a |
* |
Pointer to a variable / Dereferencing operator | *a |
? |
Ternary Operator | condition? a: b |
. |
dot operator | var1.var2 |
-> |
Arrow or Member access operator | (pointer)->(member) |
, |
Comma operator | a, b |
Check pointers topic for more details on dereferencing operator and address operator
The dot ( .
) operator is used for direct member selection via object name. In other words, it is used to access the child object/variable.
Syntax:
<variable_name>.<member_name>
The dot and arrow operator will be addressed in structures and unions.
The comma token (,
) acts as both a separator and an operator.
The comma operator is used to evaluate multiple expressions in the same statement.
Syntax:
(expression1, expression2, expression3, ...)
When used inside a parenthesis, the expressions separated by comma are evaluated sequentially, the result being the result of last expression.
Eg:
#include<stdio.h>
int main() {
int x = 10, y = 20;
int a = (x--, y + 2);
printf("x = %d, y = %d, a = %d", x, y, a);
}
Output:
x = 9, y = 20, a = 22
sizeof()
operationThe sizeof
function is a builtin function available in stdio.h
header file. It returns an integer indicating the size of memory in bytes, allocated to the specific variable.
Syntax:
sizeof(<variable_name>);
// or
sizeof(<type>);