pstc
pstc
.
Tokens
• 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.
• One or more characters are grouped together
to form basic elements of c known as Tokens
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. Keywords can’t be used as
variable names 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
Rules for Keywords
• All the keywords are lower case letters.Thus
int is a keyword but Int and INT are not.
• Keywords are also called as reserved words.
• All the keywords have pre-defined
meanings.They cannot be redefined or used in
other contexts.
• Keywords are reserved ,that is,we cannot use
them as identifiers
Identifiers
• A C Identifier is a name given to a variable,function,or any other
user defined item.
• RULES FOR IDENTIFIERS
• An identifier can include letters (a-z or A-Z), and digits (0-9).
• An identifier cannot include special characters except the ‘_’
underscore.
• Spaces are not allowed while naming an identifier.
• An identifier can only begin with an underscore or letters.
• We cannot name identifiers the same as keywords because they
are reserved words to perform a specific task. For example, printf,
scanf, int, char, struct, etc. If we use a keyword’s name as an
identifier the compiler will throw an error.
• The identifier must be unique in its namespace.
• C language is case-sensitive so, ‘name’ and ‘NAME’ are different
identifiers.
Constants
• What is a constant in C?
• As the name suggests, a constant in C is a variable that
cannot be modified once it is declared in the program.
We can not make any change in the value of the
constant variables after they are defined.
• How to Define Constant in C?
• We define a constant in C language using
the const keyword. Also known as a const type qualifier,
the const keyword is placed at the start of the variable
declaration to declare that variable as a constant.
• Syntax to Define Constant
• const data_type var_name = value;
Integer Constants
• Integer constants are whole numbers without
any decimal point. They can be represented in
different number systems:
• Decimal: A number in base 10 (e.g., 10, 0, -25).
• Octal: A number in base 8, starting with a 0
(e.g., 012 is the octal for decimal 10).
• Hexadecimal: A number in base 16, starting with
0x or 0X (e.g., 0x1A is the hexadecimal for
decimal 26).
Real Constants or Floating Point constants
• Real constants are numbers with a fractional part or in
scientific notation.
• Decimal notation: E.g., 10.5, -3.14, 0.001.
• Exponential (Scientific) notation: E.g., 3.2e5 (which means
3.2×1053.2 \times 10^53.2×105).
• Rules:
• Must have a decimal point (e.g., 5.0) or be in exponential
form.
• Use suffixes to specify the type:
– f or F for float (e.g., 3.14f)
– l or L for long double (e.g., 3.14L)
Character Constants
• Character constants are single characters enclosed in single quotes.
• Example: 'a', '1', '$‘
• Rules:
• Only one character is allowed inside the single quotes.
• The value of a character constant is the ASCII value of the character.
For example, 'A' has the value 65 in ASCII.
• Escape sequences can be used for special characters:
– '\n' for newline
– '\t' for tab
– '\0' for null
– '\‘ for a single quote
– '\b' for backspace
String Constants
• String constants are sequences of characters enclosed in
double quotes.
• Example: "Hello", "C programming".
• Rules:
• A string constant is always null-terminated, meaning there
is an extra '\0' character automatically added to the end of
the string.
• String constants can be assigned to character arrays or
used in functions like printf.
• A string can include escape sequences, like "\n" for a new
line.
Variables
• In programming, we often need a named
storage location to store the data or values.
Using variables, we can store the data in our
program and access it afterward.
• Syntax:data_type variable_name;
• Ex: int age;
• float salary;
• char grade;
Cont….
Data types
• The datatypes are used to inform the type of value
that can be stored in a variable.
• Syntax : datatype var1, var2….varn;
• Ex : int first , second, sum ;
• C has basic 4 datatypes
• Int
• Char
• Float
• double
Int Data type
• int stands for integer, which is a data type used to
represent whole numbers (both positive and
negative).
• Syntax: int variable_name;
• Example:int age = 25; int count = -10;
• Explanation:
• int typically uses 4 bytes of memory (though this can
vary based on the system), and it can store values in
the range of -2,147,483,648 to 2,147,483,647 (for a
32-bit system).
Char Data type
• char stands for character, used to store
individual characters.
• Syntax: char variable_name;
• Example: char initial = 'A';
char grade = 'B';
• Explanation:
• char typically uses 1 byte of memory and can
store values from 0 to 255, representing ASCII
characters.
float
• float is used to store floating-point numbers,
which are numbers with a decimal point.
• Syntax: float variable_name;
• Example: float pi = 3.14;
float temperature = -4.5;
• Explanation:
• float typically uses 4 bytes of memory and can
represent numbers with about 6-7 decimal
places of precision.
double
• double is used to store floating-point numbers
with double precision.
• Syntax: double variable_name;
• Example: double e = 2.718281828459;
double distance = 123456.789;
• Explanation:
• double typically uses 8 bytes of memory and
can represent numbers with about 15-16
decimal places of precision.
Qualifiers/Modifiers
• Qualifiers or Modifiers are keywords,used to alter the basic
datatypes based on the size and sign.
• The qualifiers that can be applied to the basic datatypes are
• Signed: A modifier that allows both positive and negative
integer values.
• Unsigned: A modifier that restricts a variable to only hold
positive values (including zero)
• Short: A modifier that reduces the storage size of an integer
• Long: A modifier that increases the storage size of an integer
to accommodate larger values
Syntax and ex for Modifiers
• Synatx:<modifier> <basic data type>
<var1>,<var2>,….<varn>;
• int main() {
• // Variable declarations
• int age; // Integer declaration
• float height; // Float declaration
• char grade; // Character declaration
• return 0;
• }
Reading data from keyboard
• One more way of assigning values to variables
is to input data from keyboard using scanf
function
• Syntax:scanf(“format string”,&var1,var2,…&varn);
• Ex: int x;
printf(“enter the value of x”);
scanf(“%d”, &x);
(note here x is only declared but not
initialized .once the value is been entered by the
user then value will be assigned)
Defining symbolic constants
•Any Questions ??