Fundamental of Programming in C - 01
Fundamental of Programming in C - 01
3. Modularity
The concept of storing C programming language code in the form of libraries for further future
uses is known as modularity. This programming language can do very little on its own most of its
power is held by its libraries. C language has its own library to solve common problems.
4. Statically Type
C programming language is a statically typed language. Meaning the type of variable is checked
at the time of compilation but not at run time. This means each time a programmer types a
program they have to mention the type of variables used.
5. General-Purpose Language
From system programming to photo editing software, the C programming language is used in
various applications. Some of the common applications where it’s used are as follows:
Operating systems: Windows, Linux, iOS, Android, OXS
Databases: PostgreSQL, Oracle, MySQL, MS SQL Server, etc.
6. Rich set of built-in Operators
It is a diversified language with a rich set of built-in operators which are used in writing complex
or simplified C programs.
8. Middle-Level Language
As it is a middle-level language so it has the combined form of both capabilities of assembly
language and features of the high-level language.
9. Portability
C language is lavishly portable as programs that are written in C language can run and compile
on any system with either no or small changes.
Advantages:
1. Efficiency: C is a fast and efficient language that can be used to create high-performance
applications.
2. Portability: C programs can be compiled and run on a wide range of platforms and
operating systems.
3. Low-level access: C provides low-level access to system resources, making it ideal for
systems programming and developing operating systems.
4. Large user community: C has a large and active user community, which means there are
many resources and libraries available for developers.
5. Widely used: C is a widely used language, and many modern programming languages are
built on top of it.
Disadvantages:
1. Steep learning curve: C can be difficult to learn, especially for beginners, due to its
complex syntax and low-level access to system resources.
2. Lack of memory management: C does not provide automatic memory management,
which can lead to memory leaks and other memory-related bugs if not handled properly.
3. No built-in support for object-oriented programming: C does not provide built-in support
for object-oriented programming, making it more difficult to write object-oriented code
compared to languages like Java or Python.
4. No built-in support for concurrency: C does not provide built-in support for concurrency,
making it more difficult to write multithreaded applications compared to languages like
Java or Go.
5. Security vulnerabilities: C programs are prone to security vulnerabilities, such as buffer
overflows, if not written carefully.
Tokens in C
A token in C can be defined as the smallest individual element of the C programming language
that is meaningful to the compiler. It is the basic component of a C program.
Types of Tokens in C
The tokens of C language can be classified into six types based on the functions they are used to
perform. The types of C tokens are as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
Keywords
The keywords are pre-defined or reserved words in a programming language. Each keyword is
meant to perform a specific function in a program. Since keywords are referred names for a
compiler, they can’t be used as variable names because by doing so, we are trying to assign a
new meaning to the keyword which is not allowed. You cannot redefine keywords. However, you
can specify the text to be substituted for keywords before compilation by using C preprocessor
directives. C language supports 32 keywords which are given below:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers
Identifiers are used as the general terminology for the naming of variables, functions, and arrays.
These are user-defined names consisting of an arbitrarily long sequence of letters and digits with
either a letter or the underscore(_) as a first character. Identifier names must differ in spelling
and case from any keywords. You cannot use keywords as identifiers; they are reserved for
special use. Once declared, you can use the identifier in later program statements to refer to the
associated value. A special identifier called a statement label can be used in goto statements.
Rules for Naming Identifiers
1. They must begin with a letter or underscore(_).
2. They must consist of only letters, digits, or underscore. No other special character is
allowed.
3. It should not be a keyword.
4. It must not contain white space.
5. It should be up to 31 characters long as only the first 31 characters are significant.
Constants
The constants refer to the variables with fixed values. They are like normal variables but with the
difference that their values cannot be modified in the program once they are defined.
Constants may belong to any of the data types.
Examples of Constants in C
const int c_var = 20;
const int* const ptr = &c_var;
Strings
Strings are nothing but an array of characters ended with a null character (‘\0’). This null character
indicates the end of the string. Strings are always enclosed in double quotes. Whereas, a
character is enclosed in single quotes in C and C++.
Examples of String
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
Special Symbols
The following special symbols are used in C having some special meaning and thus, cannot be
used for some other purpose. Some of these are listed below:
Brackets[]: Opening and closing brackets are used as array element references. These
indicate single and multidimensional subscripts.
Parentheses(): These special symbols are used to indicate function calls and function
parameters.
Braces{}: These opening and ending curly braces mark the start and end of a block of
code containing more than one executable statement.
Comma (, ): It is used to separate more than one statement like for separating
parameters in function calls.
Colon(:): It is an operator that essentially invokes something called an initialization list.
Semicolon(;): It is known as a statement terminator. It indicates the end of one logical
entity. That’s why each individual statement must be ended with a semicolon.
Asterisk (*): It is used to create a pointer variable and for the multiplication of
variables.
Assignment operator(=): It is used to assign values and for logical operation
validation.
Pre-processor (#): The preprocessor is a macro processor that is used automatically by
the compiler to transform your program before actual compilation.
Period (.): Used to access members of a structure or union.
Tilde(~): Used as a destructor to free some space from memory.
Operators
Operators are symbols that trigger an action when applied to C variables and other objects.
The data items on which operators act are called operands.
Depending on the number of operands that an operator can act upon, operators can be
classified as follows:
Unary Operators: Those operators that require only a single operand to act upon are known
as unary operators. For Example, increment and decrement operators
Binary Operators: Those operators that require two operands to act upon are called binary
operators. Binary operators can further be classified into:
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operator
Ternary Operator: The operator that requires three operands to act upon is called the ternary
operator. Conditional Operator(?) is also called the ternary operator.
C Variables
A variable in C language is the name associated with some memory location to store data of
different types. There are many types of variables in C depending on the scope, storage class,
lifetime, type of data they store, etc. A variable is the basic building block of a C program that
can be used in expressions as a substitute in place of the value it stores.
C Variable Syntax
The syntax to declare a variable in C specifies the name and the type of the variable.
data_type variable_name = value; // defining single variable
or
data_type variable_name1, variable_name2; // defining multiple
variable