Вы находитесь на странице: 1из 27

INTRODUCTION TO PROGRAMMING C

MODULE 1
KEYWORDS
 There are certain reserved words called keywords that have
standard predefined meaning in ‘C’.
 These keywords can be used only for their intended purpose.
 The C Keywords must be in your information because you
can not use them as a variable name.
 These are reserved words in C library and used to perform
an internal operation.
 The meaning and working of these keywords are already
known to the compiler.
C Keywords List

 A list of 32 reserved keywords in c language is 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
IDENTIFIERS
 Identifiers refers to the names of variables, functions and
arrays.
 They are user defined names consisting of sequence of letters
and digits with the letter as the first character.
 Lower case letters are preferred but upper case letters are
also permitted.
 The underscore(_) symbol can also be used as identifiers.
Rules for Identifiers

 An identifier can only have alphanumeric characters (a-z , A-


Z , 0-9) (i.e. letters & digits) and underscore( _ ) symbol.
 Identifier names must be unique
 The first character must be an alphabet or underscore.
 You cannot use a keyword as identifiers.
 Must not contain white spaces.
 Identifiers are case-sensitive.
Data Types

 A data-type in C programming is a set of values that a variable can


store along with a set of operations that can be performed on that
variable.
 C provides various types of data-types which allow the
programmer to select the appropriate type for the variable to set
its value.
 C Data Types are used to:
 Identify the type of a variable when it is declared.
 Identify the type of the return value of a function.
 Identify the type of a parameter expected by a function.
 C provides three types of data types:
 Primary(Built-in) Data Types:
void, int, char, double and float.
 Derived Data Types:
Array, References, and Pointers.
 User Defined Data Types:
Structure, Union, and Enumeration.
Primary Data Types

 Every C compiler supports five primary data types:

As the name suggests it holds no value and is


generally used for specifying the type of
void function or what it returns. If the function
has a void type, it means that the function will
not return any value.

int Used to denote an integer type.


char Used to denote a character type.
float, double Used to denote a floating point type.
Declaration of Primary Data Types with Variable Names

 After taking suitable variable names, they need to be assigned


with a data type. This is how the data types are used along
with variables:
 Example:
int age;
char letter;
float height, width;
 We can use type qualifiers with these basic types.
 There are 2 types of Type qualifier.
1) size qualifier (short, long)
2) sign qualifier (signed, unsigned).
 When the qualifier unsigned is used, number is always
positive.
 When the qualifier signed is used, the number may be
positive or negative.
DATA TYPES DATA TYPES WITH TYPE SIZE RANGE
QUALIFIER (BYTES)

Char 1) Char or signed char 1 -128 to 127


2) Unsigned char
1 0 to 255

Int 1)Int or signed int 2 -32768 to 32767


2)Unsigned int 2 0 to 65535
3)Short int or signed short 1 -128 to 127
int
4)Unsigned short int 1 0 to 255
5)Long int or signed long int 4 -2147483648 to
6)Unsigned long int 2147483647
4 0 to 4294967295
Float Float 4 -3.4E-38 to
3.4E+38
Double 1)Double 8 -1.7E-308 to
1.7E+308
2)Long double 10 3.4E-4932 to
1.1E+4932
Derived Data Types
 C supports three derived data types:

Data Types Description

Arrays are sequences of data items having


Arrays homogeneous values. They have adjacent memory
locations to store values.

Function pointers allow referencing functions


References
with a particular signature.

These are powerful C features which are used to


Pointers
access the memory and deal with their addresses.
User Defined Data Types
 C allows the feature called type definition which allows
programmers to define their own identifier that would represent
an existing data type. There are three such types:

Data Types Description

It is a package of variables of different types under a single name. This is


Structure done to handle data efficiently. “struct” keyword is used to define a
structure.

These allow storing various data types in the same memory location.
Union Programmers can define a union with different members but only a
single member can contain a value at given time.
Enumeration is a special data type that consists of integral constants and
Enum each of them is assigned with a specific name. “enum” keyword is used
to define the enumerated data type.
VARIABLES
 Variable is a name that can be used to store values.
 Variables can take different values but one at a time.
 These values can be changed during execution of a program.
 A data type is associated with each variable.
 A variable name may be declared based on the meaning of
operation like sum, product, average, etc.
Rules for Defining Variables
 A variable name can consist of Capital letters A-Z, lowercase
letters a-z, digits 0-9, and the underscore character.
 The first character must be a letter or underscore.
 Blank spaces cannot be used in variable names.
 Special characters like #, $ are not allowed.
 C keywords cannot be used as variable names.
 Variable names are case sensitive.
 Values of the variables can be numeric or alphabetic.
 Variable type can be char, int, float, double or void.
Variable Definition in C
 Syntax:
datatype variable_name;
or
datatype variable_name1, variable_name2, variable_name3;
Variable Definition and Initialization
 Example:
int width, height=5;
char letter='A';
float age, area;
double d;
Variable Assignment-
 Variable assignment is a process of assigning a value to a
variable.
 Example:
int width = 60;
int age = 31;
 C Program to Print Value of a Variable
Example:
#include<stdio.h>
void main()
{
int age = 23;
printf("I am %d years old.\n", age);
}
Program Output:
I am 23 years old.
SCOPE OF A VARIABLE
 The scope of a variable determines the area of the program
where that variable is valid i.e. the part of the program that
can access to that variable.
 It depends upon where it is declared.
 It is categorized as:
1) Global variable
2) Local variable
 Global Variable:
 They are declared outside the function.
 Its scope is throughout the program
 Its life span is throughout the entire program

 Local Variable:
 The declaration is placed after the opening of curly breces of any
function including main() and before any function statement.
 Scope is limited to a function in which it is declared.
 Life span of a local variable is within the block in which it is
declared
CONSTANTS
 Constants in ‘C’ refers to fix values that do not change during
execution of a program.
 Constants are also called literals.
 Constants can be any of the data type.
 It is considered best practice to define constants using only
upper-case names.
Constant Types in C

 Constants are categorized into two basic types and each of


these types has own subtypes/categories. These are:
1) Numeric Constants
 Integer Constants
 Real Constants
2) Character Constants
• Single Character Constants
• String Constants
Integer Constant

 It refers to a sequence of digits. Integers are of three types :


 Decimal Integer
 Octal Integer
 Hexadecimal Integer
 Decimal integer consist of a set of digits 0-9 preceded by an
optional of negative(-) or positive(+) sign.
Ex-123,-321,+78,0,654321,etc
 An octal Integer consist of any combination of digits from 0
to 7 with a leading 0.
Ex- 037,0453,0551,etc
 A sequence of digits preceded by ‘0X’ is considered as
hexadecimal integer. They may also include alphabet (A to F)
or (a-f) which represents the numbers 10 to 15.
Ex-0x2,0x9f,0xbcd,etc.
Real constant

 Real constants are called floating point constants.


 Ex-0.0083, 3.14,-0.75,etc
 A real number may also be expressed in exponential
notation.
 Ex- 215.65 may be written as 2.1565e2 in exponential
notation where e2 means multiplication of 100.
 The general form is mantissa e exponent.
Single Character Constants

 It simply contains a single character enclosed within a pair of


single quote(‘ ‘).
 Ex-’9’,’D’,’$’,etc.
 It is to be noted that the character ‘8‘ is not the same as 8.
String Constants

 These are a sequence of characters enclosed in double quotes


(“ “) and they may include letters, digits, special characters,
and blank spaces.
 Ex- “CUTM”, “567”,”8”,” “, “ A”.
 It is again to be noted that “X” and ‘X‘ are different because
“X” represents a string as it is enclosed within a pair of
double quotes whereas ‘X’ represents a single character.

Вам также может понравиться