Introduction To C Programming
Introduction To C Programming
BSC NOTES
PDF
COLLECTION
www.avashkattel.com.np/bscnotes
Unit 3
1
Brief History of C
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(“)
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 six types
1. Keyword
2. Identifier
3. Operators
4. Strings
5. Special Symbols
6. Constants
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 or 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
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
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;
• quotient= a/b; Output:
• rem = a%b; Sum = 17
• printf("sum = %d\n",sum); Difference = 7
• printf("Difference = %d\n",diff); Quotient = 2
• printf("Product = %d\n",product); Remainder = 2
• printf("Quotient = %d\n",quotient);
• printf("Remainder = %d\n",rem);
• }
12
3 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
3 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 preceding 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
String Constants
• String constants are sequence of characters enclosed within double
quotes
• May contain letters, numbers, special characters or blank spaces
• For example, “Hello”, “2021”
18
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
19
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
20
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 “ “
21
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
•
22
Data Types in C
23
Basic Data Types
• Primary data types are of following types
• integer
• float
• double
• character
• void
24
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
25
Floating point type
26
Character Type
27
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.
28
Derived Data Types
• Arrays, function, pointers , structure and union are derived data types
and will be discussed in coming topics.
29
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;
30
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}
31
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
33
Expression
• C expression is the combination of variables, constants, operators etc
satisfying the grammar of C.
• s = ut+1/2at2; (invalid)
• Example: x+y*x/2, 2*3+x-y/z
• s = u*t+(a*t*t)/2
•
34
Example
• //write a program that finds the area of triangle
• #include<stdio.h>
• #include<math.h>
• int main()
• {
• float a,b,c,s,area;
• printf("Enter value of a,b,c\n");
• scanf("%f%f%f",&a,&b,&c);
• s = (a+b+c)/2;
• area = sqrt(s*(s-a)*(s-b)*(s-c));
• printf("The area of triangle = %f",area);
• return 0;
• }
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.
36
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
37
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
38
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)/5;
• printf("Total marks = %d\n",total);
• printf("Percentage = %d\n",p);
• }
39
Output
40
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);
•}
41
Output
42
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
43
C Programming
NM