Computer Programming
C – Language Basics
2021-’22 Winter B.Tech
C – the Programming Language
• Developed in the 1970s by Dennis Ritchie at Bell
Laboratories
• General-purpose, structured programming language.
• Procedure-oriented programming language
C Fundamentals
C CHARACTER SET
• uppercase letters A to Z,
• lowercase letters a to z,
• digits 0 to 9,
• special characters (e.g., * & % ; )
KEYWORDS
• reserved words
• have pre-defined meanings
• can be used only for their intended purpose
• keywords are all lowercase
E.g., if, auto, extern, sizeof, break, float, static, case, for, struct,
char (see page 25)
KEYWORDS
IDENTIFIERS
• User-defined names that are given to various program elements, such as
variables, functions and arrays
• Sequence of letters, digits and underscore character ( _ )
• Must start with a letter or underscore
• Should not be a keyword
• Both uppercase and lowercase
• Usually max length is 31 characters
Correct Wrong
Pi 4th
area2 +d
Sum_1 Order-no
_temp Error flag
DATA TYPES
• C supports several types of data.
• Basic/ Fundamental data types in table:
Basic Data Type Description Memory
char Single character 1 byte
int Integer quantity 2 bytes
float Floating point number 4 bytes
double Double precision FP number 8 bytes
• void - means "nothing" or "no type"
DECLARATIONS
• A declaration introduces one or more identifiers into the program and
specifies their meaning and properties.
• A declaration associates a group of variables with a specific data type.
• All variables must be declared before use.
• Data type followed by one or more variable names, ending with a
semicolon.
• Examples :
char flag;
int a,b,c;
float root1,root2;
double x;
DECLARATIONS WITH INITIALIZATION
• Initial values can be assigned to variables within a type
declaration.
• Examples :
char star=‘*’;
int a=3,c=12;
float sum=0.4;
VARIABLES
• Identifiers used to represent some specified type of
information in a program.
• E.g., an identifier used to store a number or a character value.
• The data item can be accessed in the program by referring to
the variable name.
• A variable can be assigned different values at various places in
the program.
• i.e., values stored in a variable may change.
Variables (Example 1)
Instruction executed Memory location allocated
X = 10; to a variable X
T
i X = 20; 10
m
e
X = X +1;
X = X*5;
Variables (Example 1)
Instruction executed Memory location allocated
X = 10; to a variable X
T
i X = 20; 20
m
e
X = X +1;
X = X*5;
Variables (Example 1)
Instruction executed Memory location allocated
X = 10; to a variable X
T
i X = 20; 21
m
e
X = X +1;
X = X*5;
Variables (Example 1)
Instruction executed Memory location allocated
X = 10; to a variable X
T
i X = 20; 105
m
e
X = X +1;
X = X*5;
Variables (Example 2)
X = 20; Y=15; X = Y+3; Y=X/6;
X 20 X 20 X 18 X 18
Y 15 Y 15 Y 3