0% found this document useful (0 votes)
8 views

C in

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C in

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C is a mid-level and procedural programming language.

The Procedural programming language is


also known as the structured programming language is a technique in which large programs are
broken down into smaller modules, and each module uses structured code. This technique
minimizes error and misinterpretation

Dennis Ritchie. 1972.at bell laboratories of AT&T

It can be defined by the following ways:

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

C language is a system programming language because it can be used to do low-level


programming (for example driver and kernel). It is generally used to create hardware
devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C.

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.

C is considered as a middle-level language because it supports the feature of both low-level


and high-level languages. it supports pointer arithmetic (low-level), but it is machine
independent (a feature of high-level).

A Low-level language is specific to one machine, i.e., machine dependent. It is machine


dependent, fast to run. But it is not easy to understand.

A High-Level language is not specific to one machine, i.e., machine independent. It is easy
to understand.

The main features of C language are given below:

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

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 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

If the identifier is used in the external linkage, then it is known as an external


identifier. The external identifiers can be function names, global variables.

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.

There are following types of operators to perform different types of operations in C


language.

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).

Operator Meaning of Operator


+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)

C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =

Operator Example Same as


= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

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.

Relational operators are used in decision making and loops.

Operator Meaning of Operator Example


== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 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.

Operator Meaning Example


&& Logical AND. True only if all operands are true If c = 5 and d = 2 then,
expression ((c==5) && (d>5)) equals to 0.
|| Logical OR. True only if either one operand is true If c = 5 and d = 2 then,
expression ((c==5) || (d>5)) equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression
!(c==5) equals to 0.

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.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators


& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Conditional Operator in C

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

Literals are the constant values assigned to the constant variables.

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.

Rules for defining variables


A variable can have alphabets, digits, and underscore.
A variable name can start with the alphabet, and underscore only. It can't start with a
digit.
No whitespace is allowed within the variable name.
A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Types of Variables in C

There are many 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.

Types Data Types


Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void

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.

Single Line Comments


Multi-Line Comments

Single Line Comments


Single line comments are represented by double slash \\.

Mult Line Comments


Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many lines of
code, but it can't be nested.

printf(): The printf() function is used to print the integer, character, float and string
values on to the screen.

Following are the format specifier:

%d: It is a format specifier used to print an integer value.


%s: It is a format specifier used to print a string.
%c: It is a format specifier used to display a character value.
%f: It is a format specifier used to display a floating point value.

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

the variable can hold only positive value.

%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 '.'.

%e/%E It is used for scientific notation. It is also known as Mantissa or Exponent.

%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.

%p It is used to print the address in a hexadecimal form.

%c It is used to print the unsigned character.

%s It is used to print the strings.

%ld It is used to print the long-signed integer value.

Escape Sequence in C

An escape sequence in C language is a sequence of characters that doesn't represent itself


when used inside string literal or character.

It is composed of two or more characters starting with backslash \. For example: \n


represents new line.

\\ 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.

There are mainly five types of errors exist in C programming:

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

A function is a block of code that performs a specific task.

Uses of C function are:

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:

Standard library functions


User-defined functions

Standard library functions


The standard library functions are built-in functions in C programming.
ex: print(f),sqrt() etc

User-defined function
functions created by the user are known as user-defined functions.

Advantages of user-defined function:


The program will be easier to understand, maintain and debug.
Reusable codes that can be used in other programs
A large program can be divided into smaller modules. Hence, a large project can be divided
among many programmers.

There are two methods to pass the data into the function in C language, i.e., call by value
and call by reference

Call by value: A copy of the variable is passed to the function.

Call by reference: An address of the variable is passed to the function.

A function that calls itself is known as a recursive function. And, this technique is known
as recursion.

Recursive function comes in two phases:

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.

What is the usage of the pointer in C?


Accessing array elements: Pointers are used in traversing through an array of integers and
strings. The string is an array of characters which is terminated by a null character '\0'.
Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during
the execution of a program.
Call by Reference: The pointers are used to pass a reference of a variable to other
function.
Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct
different data structures like tree, graph, linked list, etc.

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

Typecasting is the process to convert a variable from one datatype to another.

An Array is a group of similar types of elements. It has a contiguous memory location. It


makes the code optimized, easy to traverse and easy to sort. The size and type of arrays
cannot be changed after its declaration.

Arrays are of two types:

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.

What is static memory allocation?

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

What functions are used for dynamic memory allocation in C language?

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

You might also like