C in
C in
Mother language
System programming language
Procedure-oriented programming language
Structured programming language
Mid-level programming language
C programming is considered as the base for other programming languages, that is why it is
known as mother language. most of the compilers and JVMs are written in C language. Most of
the languages which are developed after C language has borrowed heavily from it like C++,
Python, Rust, javascript, etc. It introduces new core concepts like arrays, functions, file
handling which are used in these languages
A procedural language specifies a series of steps for the program to solve the problem.
A procedural language breaks the program into functions, data structures, etc.
Structure means to break a program into parts or blocks so that it may be easy to
understand.
A High-Level language is not specific to one machine, i.e., machine independent. It is easy
to understand.
Simple: C is a simple language because it follows the structured approach, i.e., a program
is broken into parts
Portable: C is highly portable means that once the program is written can be run on any
machine with little or no modifications.
Mid Level: C is a mid-level programming language as it combines the low- level language with
the features of the high-level language.
Structured: C is a structured language as the C program is broken into parts.
Fast Speed: C language is very fast as it uses a powerful set of data types and operators.
Memory Management: C provides an inbuilt memory function that saves the memory and improves
the efficiency of our program.
Extensible: C is an extensible language as it can adopt new features in the future.
the keyword ‘int’ is used in a type declaration to give a variable an integer type.
The int type in C is a signed integer, which means it can represent both negative and
positive numbers
In the case of an unsigned integer, only positive numbers can be stored.
Compiler converts a C program into an executable. There are four phases for a C program to
become an executable:
Pre-processing
Compilation
Assembly
Linking
Pre-processing
This is the first phase through which source code is passed. This phase include:
Removal of Comments
Expansion of Macros
Expansion of the included files.
Conditional compilation
The individual elements of a program are called Tokens. There are following 6 types of
tokens are available in C:
Identifiers
Keywords
Constants
Operators
Special Characters
Strings
Keywords in C
Keywords in C can be defined as the pre-defined or the reserved words having its own
importance, and each keyword has its own functionality. Since keywords are the pre-defined
words used by the compiler, so they cannot be used as the variable names
Identifiers in C
Identifiers in C are used for naming variables, functions, arrays, structures, etc.
Identifiers in C are the user-defined words. It can be composed of uppercase letters,
lowercase letters, underscore, or digits, but the starting letter should be either an
underscore or an alphabet. Identifiers cannot be used as keywords. Rules for constructing
identifiers in C are given below:
The first character of an identifier should be either an alphabet or an underscore, and then
it can be followed by any of the character, digit, or underscore.
It should not begin with any numerical digit.
In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say
that identifiers are case sensitive.
Commas or blank spaces cannot be specified within an identifier.
Keywords cannot be represented as an identifier.
The length of the identifiers should not be more than 31 characters.
Identifiers should be written in such a way that it is meaningful, short, and easy to read.
Types of identifiers
Internal identifier
External identifier
Internal Identifier
If the identifier is not used in the external linkage, then it is known as an internal
identifier. The internal identifiers can be local variables.
External Identifier
strings
The string can be defined as the one-dimensional array of characters terminated by a null
('\0').
This null character denotes the end of the string. Strings in C are enclosed within double
quotes, while characters are enclosed within single characters.
operators
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise, etc.
Arithmetic Operators
Relational Operators
Shift Operators
Logical Operators
Bitwise Operators
Ternary or Conditional Operators
Assignment Operator
Misc Operator
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true,
it returns 1; if the relation is false, it returns value 0.
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in
C programming.
C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and saves power.
The conditional operator is also known as a ternary operator. The conditional statements are
the decision-making statements which depends upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator
constant
A constant is a value assigned to the variable which will remain the same throughout the
program, i.e., the constant value cannot be changed.
List of Constants in C
Constant Example
Decimal Constant 10, 20, 450 etc.
Real or Floating-point Constant 10.3, 20.2, 450.6 etc.
Octal Constant 021, 033, 046 etc.
Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.
Character Constant 'a', 'b', 'x' etc.
String Constant "c", "c program", "c in javatpoint" etc.
Special characters in C
Some special characters are used in C, and they have a special meaning which cannot be used
for another purpose.
Square brackets [ ]: The opening and closing brackets represent the single and
multidimensional subscripts.
Simple brackets ( ): It is used in function declaration and function calling. For example,
printf() is a pre-defined function.
Curly braces { }: It is used in the opening and closing of the code. It is used in the
opening and closing of the loops.
Comma (,): It is used for separating for more than one statement and for example, separating
function parameters in a function call, separating the variable when printing the value of
more than one variable using a single printf statement.
Hash/pre-processor (#): It is used for pre-processor directive. It basically denotes that we
are using the header file.
Asterisk (*): This symbol is used to represent pointers and also used as an operator for
multiplication.
Tilde (~): It is used as a destructor to free memory.
Period (.): It is used to access a member of a structure or a union
LITERALS
Types of literals
There are four types of literals that exist in C programming:
Integer literal
Float literal
Character literal
String literal
Integer literal
It is a numeric literal that represents only integer type values. It represents the value
neither in fractional nor exponential part.
float literal
It is a literal that contains only floating-point values or real numbers. These real numbers
contain the number of parts such as integer part, real part, exponential part, and
fractional part. The floating-point literal must be specified either in decimal or in
exponential form.
character literal
A character literal contains a single character enclosed within single quotes. If multiple
characters are assigned to the variable, then we need to create a character array.
String literal
A string literal represents multiple characters enclosed within double-quotes. It contains
an additional character, i.e., '\0' (null character), which gets automatically inserted.
This null character specifies the termination of the string. We can use the '+' symbol to
concatenate two strings.
VARIABLES
A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.
Types of Variables in C
local variable
global variable
static variable
automatic variable
external variable
Local Variable
A variable that is declared inside the function or block is called a local variable.
Global Variable
A variable that is declared outside the function or block is called a global variable. Any
function can change the value of the global variable.
Static Variable
A variable that is declared with the static keyword is called static variable
Automatic Variable
All variables in C that are declared inside the block, are automatic variables by default.
We can explicitly declare an automatic variable using auto keyword.
External Variable
We can share a variable in multiple C source files by using an external variable. To declare
an external variable, you need to use extern keyword.
Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
C Data Types
There are the following data types in C language.
Comments in C
Comments in C language are used to provide information about lines of code. It is widely
used for documenting code. There are 2 types of comments in the C language.
printf(): The printf() function is used to print the integer, character, float and string
values on to the screen.
scanf(): The scanf() function is used to take input from the user.
The Format specifier is a string used in the formatted input and output functions. The
format string determines the format of the input and output. The format string always starts
with a '%' character.
%d or %i It is used to print the signed integer value where signed integer means that
the variable can hold both positive and negative values.
%u It is used to print the unsigned integer value where the unsigned integer means that
%o It is used to print the octal unsigned integer where octal integer value always
starts with a 0 value.
%x It is used to print the hexadecimal unsigned integer where the hexadecimal integer
value always starts with a 0x value. In this, alphabetical characters are printed in small
letters such as a, b, c, etc.
%X It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical
characters in uppercase such as A, B, C, etc.
%f It is used for printing the decimal floating-point values. By default, it prints the
6 values after '.'.
%g It is used to print the decimal floating-point values, and it uses the fixed
precision, i.e., the value after the decimal in input would be exactly the same as the value
in the output.
Escape Sequence in C
\\ Backslash
\' Single Quote
\" Double Quote
\? Question Mark
\0 Null
ASCII is the American Standard Code for information interchange. It is a character encoding
scheme used for electronics communication. Each character or a special character is
represented by some ASCII code, and each ascii code occupies 7 bits in memory.
In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically,
the bool type value represents two types of behavior, either true or false. Here, '0'
represents false value, while '1' represents true value.
Static is a keyword used in C programming language. It can be used with both variables and
functions,
Programming errors are also known as the bugs or faults, and the process of removing these
bugs is known as debugging.
Syntax error
Run-time error
Linker error
Logical error
Semantic error
Compile-time is the time at which the source code is converted into an executable code while
the run time is the time at which the executable code is started running.
FUNCTIONS
C functions are used to avoid the rewriting the same code again and again in our program.
C functions can be called any number of times from any place of our program.
When a program is divided into functions, then any part of our program can easily be
tracked.
C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks
so that it makes the C program more understandable.
Types of function
There are two types of function in C programming:
User-defined function
functions created by the user are known as user-defined functions.
There are two methods to pass the data into the function in C language, i.e., call by value
and call by reference
A function that calls itself is known as a recursive function. And, this technique is known
as recursion.
Winding phase
Unwinding phase
Winding phase: When the recursive function calls itself, and this phase ends when the
condition is reached.
Unwinding phase: Unwinding phase starts when the condition is reached, and the control
returns to the original call.
C Pointers
Pointers (pointer variables) are special variables that are used to store addresses rather
than values.
A double pointer or pointer to pointer is such a pointer. The address of a variable is
stored in the first pointer, whereas the address of the first pointer is stored in the
second pointer.
A pointer that doesn't refer to any address of value but NULL is known as a NULL pointer.
When we assign a '0' value to a pointer of any type, then it becomes a Null pointer.
A pointer which can access all the 16 segments (whole residence memory) of RAM is known as
far pointer.
If a pointer is pointing any memory location, but meanwhile another pointer deletes the
memory occupied by the first pointer while the first pointer still points to that memory
location, the first pointer will be known as a dangling pointer
One-dimensional array: One-dimensional array is an array that stores the elements one after
the another.
Multidimensional array: Multidimensional array is an array that contains more than one
array.
In case of static memory allocation, memory is allocated at compile time, and memory can't
be increased while executing the program. It is used in the array.
The lifetime of a variable in static memory is the lifetime of a program.
The static memory is allocated using static keyword.
The static memory is implemented using stacks or heap.
The pointer is required to access the variable present in the static memory.
The static memory is faster than dynamic memory.
In static memory, more memory space is required to store the variable.
What is dynamic memory allocation?
In case of dynamic memory allocation, memory is allocated at runtime and memory can be
increased while executing the program. It is used in the linked list.
The malloc() or calloc() function is required to allocate the memory at the runtime.
An allocation or deallocation of memory is done at the execution time of a program.
No dynamic pointers are required to access the memory.
The dynamic memory is implemented using data segments.
Less memory space is required to store the variable
malloc()
The malloc() function is used to allocate the memory during the execution of the program.
It does not initialize the memory but carries the garbage value.
It returns a null pointer if it could not be able to allocate the requested space.
calloc()
The calloc() is same as malloc() function, but the difference only is that it initializes
the memory with zero value.
realloc()
The realloc() function is used to reallocate the memory to the new size.
If sufficient space is not available in the memory, then the new block is allocated to
accommodate the existing data.
free():The free() function releases the memory allocated by either calloc() or malloc()
function.
The structure is a user-defined data type that allows storing multiple types of data in a
single unit. It occupies the sum of the memory of all members.
The structure members can be accessed only through structure variables.
Structure variables accessing the same structure but the memory allocated for each variable
will be different.
The union is a user-defined data type that allows storing multiple types of data in a single
unit. However, it doesn't occupy the sum of the memory of all members. It holds the memory
of the largest member only.
In union, we can access only one variable at a time as it allocates one common space for all
the members of a union