Unit 2 Elements of C
Unit 2 Elements of C
Unit 2 Elements of C
Hrs)
1
Content of Unit
2
Character Set
• Set of characters that are used to from words, numbers and
expression in C is called C character set
• Characters in C are grouped into the following categories:
1. Letters or Alphabets : Uppercase (A,B,…Z) and lowercase(a,…z)
2. Digits : All decimal digits:- 0,1,2,3,…..9
3. Special Characters: comma(,), semicolon(;), ampersand (&),
quotation marks(“abc”)
4. White Spaces: Blank spaces, horizontal tab, vertical tab etc
3
C Tokens
• C token are the basic building blocks in C language which are constructed
together to write a C program
• Each and every smallest individual unit in a C program is known as C tokens.
• C tokens are of five types
1. Keyword
2. Identifier
3. Constants
4. Operators
5. Special Symbols
4
1 Keyword
• Keywords are predefined words for a C programming language
• All keywords have fixed meaning and these meanings cannot be
changed.
• The keywords cannot be used as variables names
• Examples are:
5
C keyword List (32)
auto break case char const continue default do
6
2 Identifier
• Every word used in C program to identify the name of variables,
functions, arrays, pointers and symbolic constants are known as
identifier.
• Identifiers are the user defined names consisting of arbitrarily long
sequence of letters and digits with either letter or underscore as a
first character.
7
2 Identifier
• There are some rules for defining identifiers
1. They must begin with a letter of underscore
2. They must consist of only digits, letters and underscore.
3. It should not be a keyword
4. It must not contain white space
5. Uppercase and lower case are considered distinct
6. It should be up to 31 characters long as only first 31 characters are
significant
8
2 Identifier
Valid Invalid
Sum 1n
n1 X-name
N_1
TRUE Radius of circle
Radius_circle Xy&
_num1 P*y
9
3 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
10
C Arithmetic Operators.
Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
% Modulus Division
11
Arithmetic Operators
• #include<stdio.h>
• int main()
•{
• int a ,b,sum,diff,product,quotient,rem;
• a = 12;
• b = 5;
• sum = a+b;
• diff = a-b;
• product = a*b; Output:
• quotient= a/b; Sum = 17
• rem = a%b; Difference = 7
• printf("sum = %d\n",sum); Quotient = 2
• printf("Difference = %d\n",diff); Remainder = 2
• printf("Product = %d\n",product);
• printf("Quotient = %d\n",quotient);
• printf("Remainder = %d\n",rem);
•}
12
4 Constants
• A C constant refers to the data item that do not change their values
during the program execution.
• These fixed values are also called literals
• Several types of C constants that allowed in C are:
13
Constants
14
Integer Constants
• Integer constants are whole numbers without any fractional part.
• It must have at least one digit and may contain either + or – sign.
• A number with out any sign is assumed as positive.
• There are three types of integer constants
a. Decimal integer constant (1,4,9,838,-5758)
b. Octal integer constant (045 ,0677)
c. Hexadecimal integer constant (0x4, 0x24A)
15
Real Constants
• The numbers having fractional parts are called real or floating point
constants.
• These constants may be represented either in fractional form or
exponential form and may also have either + or – sign preceeding it.
• Example: 0.0262, -0.4567, 244E35, 0.13E-10
16
Character Constants
• A character constant contains one single character enclosed within
single quotes
• Example : ‘a’, ‘Z’ etc
17
Symbolic Constants
• A symbolic constants is a name given to some numeric value or a
character constant or string constant
• There two ways to define symbolic constant
• Using preprocessor directive
• #define PI 3.1416
• Using keyword const
• const PI = 3.1416
18
Example 1
• // Write a C program to add to decimal numbers
• #include<stdio.h>
• int main()
•{
• int a,b,c;
• printf("Enter any two decimal numbers\n");
• scanf("%d%d",&a,&b);
• c = a+b;
• printf("The sum = %d",c);
•}
19
Example 2
• //Write a C program to add to octal numbers
• #include<stdio.h>
• int main()
•{
• int a,b,c;
• printf("Enter any two octal numbers\n");
• scanf("%o%o",&a,&b);
• c = a+b;
• printf("The sum = %o",c);
•}
20
Example 3
• //Write a C program to add to hexadecimal numbers
• #include<stdio.h>
• int main()
•{
• int a,b,c;
• printf("Enter any two hexadecimal numbers\n");
• scanf("%x%x",&a,&b);
• c = a+b;
• printf("The sum = %X",c);
•}
21
Example 4
• //Write a C program to add two real numbers
• #include<stdio.h>
• int main()
•{
• float a = 4.456;
• float b = 2.5456;
• float c = a+b;
• printf("c = %f",c);
•}
22
String Constants
• String constants are sequence of characters enclosed within double
quotes
• String constants may contain letters, numbers, special characters or
blank spaces
• For example, “Hello”, “2021”
23
Example 5
• //Write a C program to find area and circumference of circle
• #include<stdio.h>
• #include<math.h>
• int main()
•{
• float radius, area, cir;
• const float PI = 3.1416;
• printf("Enter radius of circle\n");
• scanf("%f",&radius);
• area = PI*radius*radius;
• cir = 2*PI *radius;
• printf("The area of circle = %f\n",area);
• printf("The circumeference = %f\n",cir);
• return 0;
•}
24
Special Symbols
• The following special symbols are used in C having some special
meaning and thus cannot be used some other purpose.
• []. () , { }.;,:, *,#
• Braces { } : These opening and ending curly braces marks the start and
end of a block of code containing more than one executable
statement
• Parentheses () : To indicate function calls
• Brackets [] : Array scripts
25
Delimiters
• A delimiter is a unique character or series of characters that indicates
the beginning or end of a specific statement, string or function body
set.
• Delimiters examples
• Parentheses ( )
• Braces { }
• Comments /* ………. */
• Double quotes for the defining string literals “ ”
26
Data Types in C
• A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
• Following data types are in common
• Primary Data Types
• User Defined Data Type
• Derived Data Type
27
Data Types in C
28
Basic Data Types
• Primary data types are of following types
• integer
• float
• double
• character
• void
29
Integer Type
• Integers are whole numbers
• Size and range of Integer type on 16-bit machine:
Type Size(bytes) Range
int or signed int 2 -32,768 to 32767
unsigned int 2 0 to 65535
short int or signed short 1 -128 to 127
int
unsigned short int 1 0 to 255
long int or signed long int 4 -2,147,483,648 to
2,147,483,647
unsigned long int 4 0 to 4,294,967,295
30
Floating point type
31
Character Type
32
void Type
• The void type has no value.
• This is usually used to specify a type of function when it does not
return any value to the calling function.
33
Derived Data Types
• Arrays, function, pointers structures and unions are derived data
types and will be discussed in coming topics.
34
User defined Data Type
• C supports a features called type definition which allows programmer
to define an identifier that would represent an existing data type.
• For that we use typedef keyword
• Syntax
• typedef float real;
• real x, y, z;
35
Enumerated data Types
• Enumeration is a user defined datatype in C language.
• It is used to assign names to the integral constants which makes a
program easy to read and maintain.
• The keyword “enum” is used to declare an enumeration.
• Here is the syntax of enum in C language
• enum enum_name { list of names}
36
Example
• #include<stdio.h>
• #include<math.h>
• #include<conio.h>
• int main()
•{
•
• int a;
• ;
• enum rollno {KAMAL=1,BHUSHAN,LAXMAN,RAVIN=20, ROHESH,SUMAN };
• printf("Roll of LAXMAN is %d",LAXMAN);
• return 0;
•}
37
Variable
• An entity that is used to store data item in a program is called
variable.
• Its value changes during the execution of program.
• Example:
• int x, y, z
• Here x, y and z are variables
• Since variables are identifiers, rules for defining variables the variables
is as identifiers
38
Statement
• A statement is a command given to the computer that instructs the computer
to take a specific action, such as display to the screen, or collect input.
• A computer program is made up of a series of statements.
• Labeled Statements
• Compound Statement
• Simple Statement
• Selection Statement
Iteration Statement
Jump Statement
39
Expression
• C expression is the combination of variables, constants, operators etc
satisfying the grammar of C.
40
• /*Write a C program that reads length of sides of tringle and then compute area of triangle
*/
• #include<stdio.h>
• #include<math.h>
• #include<conio.h>
• int main()
•{
•
• float a,b,c,area , s;
• printf("Enter length of sides of triangle\n");
• scanf("%f%f%f",&a,&b,&c);
• s = (a+b+c)/2;
• area = sqrt(s*(s-a)*(s-b)*(s-c));
• printf("Area of triangle = %f",area);
• return 0;
•}
41
Format Specifier
• The format specifiers are used in C for input and output purposes.
•
• Using this concept the compiler can understand that what type of
data is in a variable during taking input using the scanf() function and
printing using printf() function.
• Here is a list of format specifiers.
42
Format Specifier
Format Specifier Type
%c Character
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar as %e or %E
%hi Signed integer (short)
%hu Unsigned Integer (short)
%i Unsigned integer
%l or %ld or %li Long
%lf Double
43
Format Specifier
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
44
Examples:
1 Write a C program to find total and percentage when marks
in 5 subjects are given
• #include<stdio.h>
• int main()
•{
• int eng, math, sci,com,omat,total,p;
• printf("Enter marks in five subjests\n");
• scanf("%d%d%d%d%d",&eng, &math, &sci, &com, &omat);
• total = eng+math+sci+com+omat;
• p = (total)/100
• printf("Total marks = %d\n",total);
• printf("Percentage = %d\n",p);
•}
45
Output
46
2 Write a C program that reads length
and breadth of rectangle and finds its area
• #include<stdio.h>
• int main()
•{
• int len,br,area;
• printf("Enter length and breadth of rectangle\n");
• scanf("%d%d",&len,&br);
• area = len*br;
• printf("Area of rectangle = %d",area);
•}
47
Output
48
Exercise
1. Write a C program that reads length of square and finds its area and
perimeter.
2. Write a C program that reads radius of circle and finds its area
3. Write a C program that reads length, breadth and height of a room
and find its volume
4. Write a C program reads a number and tests whether is even or odd
5. Write a C program that reads a number then tests whether is it
negative or positive
49