0% found this document useful (0 votes)
16 views27 pages

FPL 1

Uploaded by

grishmabarhate5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views27 pages

FPL 1

Uploaded by

grishmabarhate5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

HISTORY & IMPORTANCE OF C LANGUAGE

• Introduced to write routines of UNIX by Denise


Ritchie
See students, I wrote an awfully
complex language. F*** off
and suffer! idk
Cluster heck of bits and bytes.

■ Memory is organised in fundamental unit called bit


■ One Byte consist of 8 bits

1 0 1 1 0 0 1 0

■ Representation of 1
0 0 0 0 0 0 0 1
■ Representation of 9
0 0 0 0 1 0 0 1
Letters: Alphabets
uppercase A….Z and
lowercase a….z
Legal characters i.e.
characters that are allowed
in C
Digits:
0,1,2,3,4,5,6,7,8,9

Character Types of char set in C:

set in C Special Chars: !, ”, #, $, %,


&, ‘, (, ), *, +, -, _, . , /, \, :, ;,
>, <, ?, @, [, ], ^, {, }, ~

White spaces: Blank


space, Tab, Carriage return,
New line
C Tokens:
Smallest unit of C that has the meaning

Types of
tokens:

Special
Keywords Identifiers Constants Strings Symbols Operators
Keywords:
Identifiers:

User defined names consisting arbitrary Example: name of variable, function or


sequence of letters and numbers structure
int total_student
float addition();
struct value_pair
Constants

■ Variable with fixed value


■ They can be initialised only once. If tried to
initialise with different value, compiler would
throw an error.

const int roll_num


const float percent_marks;
Strings

■ Set of characters ending with ‘/0’

char name[20] = “Long live Comrade”;


char Ozymandias[] = “my name is Ozymandias,
king of the kings”;
char terminator[25] = “I am optimus prime”;
char strings[] = “no_strings_attached”;
Special symbols
• 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(~): Bitwise One’s Complement Operator.
Operators
(Detailed in next unit)

Unary operators: Requires single operand. i.e. increment (++),


Decrement (- -)

Binary operators: Requires two operands to perform operation:


• Arithmetic operators: +, -, *, %, /
• Relational Operators: ==, <=, >=, <, >, !=
• Logical Operators: AND (&&), OR (||), NOT (!)
• Assignment Operators: =, +=, -=, *=, %=, /=
• Bitwise Operator: &, |, !, >>, <<, ~
Assignment of memory space to
certain identifiers i.e. It’s a name
of memory location used for store
data

Variables

Variables follow certain rules:


Variable In C, variable is
shouldn’t be No case sensitive:
Variable Variable consist blanks/spaces
shouldn’t begin of digits, defined with •int Var and int var is
reserved should be used
with digit i.e. int alphabets and while declaring
not same. The
compiler would treat
8egss; (Error) underscore (_) keywords i.e. them differently.
float, int, auto, … variable
Variable
Declaration vs
Initialisation
■ Variable Declaration is
introduction of variable in the
program
■ Variable Initialisation is
assigning value to the given
variable
■ In case if we try to access the
variable which has been
declared but not initialised, the
program would produce a
garbage value.
Data Types:

■ Variable comes in different types based on the value it has been assigned:
– int roll_num = 4; //This is variable is integer type variable since it has been
declared as “int”
– float percent = 76.46; //Since we are assigning fractional to the variable, we need
to use ”float” as variable indentifier
– char alpha = ‘a’; //To represent character values, char identifier has been used.
– ‘int’ to represent integer values, float to represent floating point/fractional values
or ‘char’ to represent character type values of variables are called to data types.
– According to Wikipedia data types “constitute the semantics and characteristics of
storage of data elements”
Primitive data types
■ We must assign a data type to
all the variables that are
present in the C language.
These define the type of data
that we can store in any
variable. If we do not provide
the variable with a data type,
the C compiler will ultimately
generate a syntax error or a
compile -time error.
■ The variables can be of the
basic types, based on the
name and the type of the
variable.
■ There are three Data Types
used in C:
– Primitive
– Derived types
– User defined
Integer data type

■ Represents integers only.


■ Range: -2,147,483,648 to 2,147,483,647 (-- 231 to 231 – 1)
■ Size: 4 bytes (or 2 bytes, depending on the processor used)
– int num = 1729; int num2 = --2121;
■ If ‘unsigned’ used before int, it means only variable can only have positive values. Its
range is 0 to 4,294,967,295
– unsigned int wheat_grains = 18290;
■ If ‘long’ used before ‘int’, it means only variable can store values ranging from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (--263 to 263 --1)
– long int avagadro_number = 602200000000000000000000;
Float data type

■ Used to represent floating points. Its size is 4 byte long. Range is -2,147,483,648 to
2,147,483,647. It has precision of 7 digits
– float pi = 3.141592;
■ In case higher precision needed to represent floating values, ‘double’ keyword is
used. The size of double is 8 byte long. It has precision of 15 digits.
– double pi = 3.14159265358979323846;
Char data type

■ Character data type allows its variable to store only a single character. The size of
the character is 1 byte. Range is -128 to 127 or 0 to 255 if ‘unsigned’ used before
char.
– char beta = ‘b’; char delta = ’D’;
Scope:

■ What is Scope: Scope refers to the visibility and lifetime of variables and functions
within a program.
Storage class

■ Every variable that is declared in a C program has its scope and lifetime. Storage
class allows us to determine a variable's scope, visibility, and lifetime.
■ Whenever we define a variable in the program, we also define its data type and its
storage class.
■ data type talks about the size and the binary representation of a variable inside the
memory whereas storage class tells us variable's scope, visibility, and lifetime.
Storage class

■ Every variable that is declared in a C program has its scope and lifetime. Storage
class allows us to determine a variable's scope, visibility, and lifetime.
■ Whenever we define a variable in the program, we also define its data type and its
storage class.
■ data type talks about the size and the binary representation of a variable inside the
memory whereas storage class tells us variable's scope, visibility, and lifetime.
Summary of Storage Classes in C
Auto storage class

■ Default storage class for all variables. No need to declare ‘auto’ explicitly.
■ When variable is of ‘auto’ storage class, variable is stored on RAM.
■ Scope and lifetime is limited to block only,
– int db_id = 24;
– char name = ‘z’;
– auto char version = ‘a’;
– float fraction;
Register storage class

■ Same as ‘auto’ storage class except variable is allocated on register


■ When execution speed is priority, ‘register’ storage class should be used as registers
provides fastest access.
– register double volatility_index;
Static storage class

■ When variable goes out scope but the state


of variable needs to be maintained until
when it’s scope comes back, ‘static’ storage
class is used.
■ Static variables follow the scope rules but
they have lifetime till the end of the
program.
■ Static variables when declared are
automatically initialised to zero.
– static count = 0;
– static temp;
extern storage class

extern variable is
■ extern is useful for declaring variables that declared in header file
needs to be visible to all source files that are dynamic.h and being
linked into given project. accessed in c file
■ A variable cannot be initialised in an extern dynamic.c
declaration, as no space is actually allocated
during the declaration.
volatile keyword

■ volatile tells the compiler that the variable is explicitly changeable, and
seemingly useless accesses of the variable (for instance, via pointers) should not be
optimized away.
■ In other words, only user can assign or reassign values of variable which is declared
as volatile and compiler can’t optimise or change value of given variable on its
own.
– volatile float currentTemperature = 40.2;

You might also like