L 1.1 Final
L 1.1 Final
Dr.Bharanidharan N
Asst. Professor Sr. Grade I, SCORE
C Language
C Language:
• C is general purpose programming language
• Has economical expressions, modern control
flow, new data structures, rich operators
• Designed for and implemented by Dennis
Ritchie in 1972
• Has high portability. Runs on multiple OS (IBM,
Honeywell, Sperry-Rand, etc)
• UNIX OS, C compiler and all libraries were
written in C language itself
Structure of C program
• Initially, APL – A Programming Language was
written.
• Then refined to get BCPL – Basic Computer
Programming Language or B. Then C was
developed
• C is highly structured language
• Most of repetitive jobs has different functions
• Starts its execution at ‘main(argument) ‘ function
• All keywords are in lower case as it is case
sensitive
Structure of C program
Structure of C program
Structure of C program
C tokens
7
Types of token
8
Keywords
• Keyword is a reserved word that has a
particular meaning in the programming
language.
• The meaning of keyword is predefined.
• A keyword cannot be used as an identifier
name in C language.
• There are 32 keywords available in C.
9
Standard Keywords
10
Identifiers
• An identifiers refers to the name of an object.
• It can be a variable name, a label name, a
function name, a typedef name, a macro
name or a macro parameter, a tag or a
member of a structure, a union or an
enumeration.
11
Identifiers Rules
1. Identifier name in C can have letters, digits or
underscores.
2. The first character of an identifier name must
be a letter (either uppercase or lower case) or
an underscore and cannot be a digit.
3. No special character (except underscore), blank
space and comma can be used in an identifier
name.
4. Keywords or reserved words cannot form a
valid identifier name. 12
5. The maximum number of characters allowed
in an identifier name is compiler dependent,
but the limit imposed by all the compilers
provides enough flexibility to create
meaningful identifier names.
• The following identifier names are valid in C:
Student_Name, StudentName, student_name,
student1, _student.
• The following identifier names are invalid in C:
Student Name, Name&Rollno, 1_student, for.
13
Variables
Rules for constructing variable names
Declaring & Initializing variables
Declaration:
int emp_num,b;
float salary;
char grade;
double balance_amt;
23
Classification of Data types
• They are classified into
1) Basic data types
i) Character char
ii) Integer int
iii) Single precision floating point float (occupies
32bit in computer memory)
iv) Double precision floating point double
(occupies 64bit in computer memory).
v) No value available void
24
Classification of Data types
2) Derived data types
i) Array type char[], int[] etc
ii) Pointer type char*,int* etc
iii) Function type int(int,int) , float(int) etc
signed char 1 %c
unsigned char 1 %c
at least 10, usually 12 or
long double %Lf
16
Type conversion
• Converting an expression of a given type into
another type is known as type-casting.
Typecasting is more useful in C programming.
• Here, It is best practice to convert lower data
type to higher data type to avoid data loss.
• Data will be truncated when the higher data
type is converted to lower. For example, if a
float is converted to int, data which is present
after the decimal point will be lost.
28
Implicit conversion
• Implicit conversions do not require any
operator for to be converted. They are
automatically performed when a value is
copied to a compatible type in the program.
• Here, the value of “a” has been promoted
from int to double and we have not had to
specify any type-casting operator. This is
known as a standard conversion.
29
Implicit conversion
30
31
Explicit conversion
• In C language, Many conversions, especially
those that imply a different interpretation of
the value, require an explicit conversion.
• They are not automatically performed when a
value is copied to a compatible type in the
program.
• This process is also called type casting and it
Syntax: (type) expression
is user defined.
32
Explicit conversion
#include<stdio.h>
int main()
{
int i=20;
float p;
p = (float) i; // Explicit conversion
printf("Explicit value is %f",p);
return 0;
}
33
Input / Output statements
printf(): writing output function
scanf(): getting user input