2. Features of C Language
C is the widely used language. It provides many features that are given below.
• Simple
• Machine Independent or Portable
• Mid-level programming language
• structured programming language
• Rich Library
• Memory Management
• Fast Speed
• Pointers
• Recursion
• Extensible
• Rich set of controlling statements
• Rich sent of Operators
3. Ares of C Language
C is the widely used language. It is used to develop sofwares in following
areas,
• Scientific Application
• Environmental Application
• Mathematical calculations
• Embedded Systems
• Space satellite Application
• Data Structure Application
• Computer Graphics Application
• Computer Network Applications
4. History of C Language
• Dennis Ritchie - founder of C language
• C programming language was developed in 1972 by
Dennis Ritchie at bell laboratories of AT&T (American
Telephone & Telegraph), located in the U.S.A.
• Dennis Ritchie is known as the founder of the c
language.
5. History of C Language
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K & R C 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
Let's see the programming languages that were developed before C language.
7. My First C Program
#include <stdio.h>
int main()
{
printf("Hello C Language");
return 0;
}
Execution of First C Program
8. Comments in C Language
• Comments are non Executable statements in c program.
• C introduces a new comment symbol // (double slash). Comments start with a
double slash symbol and terminate at the end of line.
• A comment may start any where in the line and what ever follows till the end of line
is ignored.
• Note that there is no closing symbol.
• The double slash comment is basically a single line comment. Multi line comments
can be written as follows:
// this is an example of
// c++ program
// thank you
• The c comment symbols /* ….*/ are still valid and more suitable for multi line
comments.
/* this is an example of c++ program */
9. Steps in C Language study
Let's see the programming languages that were developed before C language.
10. Steps in C Language study
Let's see the programming languages that were developed before C language.
Token
12. 1) Characters Used in C Programs
• Lowercase letters
• a b c . . . z
• Uppercase letters
• A B C . . . Z
• Digits
• 0 1 2 3 4 5 6 7 8 9
• Other characters
• + - * / = ( ) { } [ ] < > ‘ “
• ! @ # $ % & _ ^ ~ . , ; : ?
• White space characters
• blank, newline, tab, etc.
13. 2) The Six Kinds of Tokens / Words in C
1) Keywords
2) Constants / Literals
3) Identifiers - variables / function / structure / array name
4) Statements/ Instructions – e.g. scanf, printf, getch etc.
5) Operators - +, -, * etc.
6) Punctuators - , : “ ‘ . ! ? % etc.
• The smallest individual units/words in program are known as tokens.
• C has the following Six different types of tokens.
14. 2) Tokens / words in C Language
2.1) KEYWORDS :
Keywords are C tokens that have a strict meaning in c language to perform certain tasks.
They are explicitly reserved words and can’t be used as names for the program variables or other
user defined program elements.
ANSII C has 32 key words.
Some implementations such as Borland’s C or Microsoft’s C have additional key words.
auto do goto signed unsigned
break double if sizeof void
case else int static volatile
char enum long struct while
const extern register switch
continue float return typedef
default for short union
15. What is a Constant?
Constant is a value (an entity) whose value cannot change throughout the
execution of a program,is known as constants.
Like variables constants have data types.
2) Tokens / words in C Language
2.2) Constants / Literals :
1) Using const keyword
const data_type <variable_name>=<val>;
(or)
data_type const <variable_name>=<val>;
2) Using Define preprocerssor
#define <variable> <Value>
Syntax (Grammar) Example
const int x = 10;
int const x = 10;
#define PI 3.14
16. Types of Constants in C
2) Tokens / words in C Language
2.2) Constants / Literals :
Integer constant - (complete / Whole Number)
Real constant - (Fractional Number)
Character Constant - (Single Letter)
String Constant - (multiple letters)
17. 2.2.1) Integer Constants
Decimal Constants Octal Constants Hexa-Decimal Constants
10 012 0xA
1024 02000 0x400
12789845 060624125 0xC32855
2) Tokens / words in C Language
2.2) Constants / Literals :
In C language integer constant is a complete / Whole Number.
integer constant are represented in one of following formats,
a) Decimal
b) octal (Value starts with 0)
c) or hexa-decimal number (Value starts with 0x).
Integer constants are always positive until you specify a negative(-) sign.
18. Example Program
18
2) Tokens / words in C Language
2.2) Constants / Literals :
#include<stdio.h>
void main ( )
{
const int a = 10; //Decimal
int const b = 012; //Octal
const int c = 0xA; //Hexadecimal
printf(" %d t %d t %d ", a, b, c);
}
OUTPUT: 10 10 10
19. Representation Fractional form Exponential form Type
0 0.0 0.0e-10 or 0.0E-10 double
6.77 6.77 0.677e1 or 0.677E1 double
-6.0f -6.0 -0.6 e1 or -0.6E1 float
3.1415926536L 3.1415926536 0.31415926536 e1 long double
2.2.2) Real Constants
2) Tokens / words in C Language
2.2) Constants / Literals :
In C language real constant is a fractional Number.
We can represent the negative numbers in real constants.
The default form of real constant is double and it must have a decimal
point.
It may be in fractional form or exponential form.
Ex: 3.45, -2.58, 0.3E-5 (equal to 0.3 x 10-5)
20. ASCII Character Symbol
Alert(bell) 'a'
Null character '0'
Backspace 'b'
Horizontal Tab 't'
New line 'n'
2.2.3) Character Constants
2) Tokens / words in C Language
2.2) Constants / Literals :
Character Constants are in single letter form and must be enclosed with in
single quotes. e.g. ‘a’, ‘Z’, ‘1’, ‘4’, ‘&’, ‘%’,’#’ etc.
We use escape character along with the character constants, followed by .
The escape character says that it is not a normal character constants as they do
some tasks on user(output) screen.
ASCII Character Symbol
Vertical tab 'v'
Form Feed 'f'
Carriage Return 'r'
Single Quote '''
Double Quote '"'
21. 21
2.2.4) String Constants
2) Tokens / words in C Language
2.2) Constants / Literals :
A string constant is a sequence of characters enclosed in a double
quotes.
Examples:
“ ” // Null String
"programming9" // a full string with 12 characters
"wel come" // string with 8 characters including space
“Value= %d" // format string with 8 characters including space
“a=%d n b=%d" // format string with 9 characters including space and
escape sequence character.
22. 3) IDENTIFIERS:
Identifiers refers to the name of variable, functions, array, structure etc. created by programmer.
Variable is an entity in c program whose value can changes several time throughout the execution
of program.
i.e. Variables are entities whose values can not remain constant in C program.
Each language has its own rule for naming the identifiers. The following rules for naming the
identifiers in C and C++.
1. Identifier name should start with alphabet character or underscore _.
2. Other characters in identifier can be letters, digits and underscore are permitted.
3. A variable name should not consist of whitespace.
4. 'C' is a case sensitive language that means a variable named 'age' and 'AGE' are
different.
5. A variable name should not consist of a keyword.
6. It can have 8, 32 letter long in size.
7. It must be declared first (locally / Globally) before using in program.
3) Tokens / words in C Language
23. 3) IDENTIFIERS:
Following are the examples of valid variable names in a 'C' program:
3) Tokens / words in C Language
o height or HEIGHT
o _height
o _height1
o My_name
Following are the examples of invalid variable names in a 'C' program:
o 1height
o Hei$ght
o My name
o case
o switch
24. 3) IDENTIFIERS:
Following are the examples of valid variable names in a 'C' program:
3) Tokens / words in C Language
o height or HEIGHT
o _height
o _height1
o My_name
Following are the examples of invalid variable names in a 'C' program:
o 1height
o Hei$ght
o My name
o case
o switch
25. 3) IDENTIFIERS:
3) Tokens / words in C Language
1) Declaration without initialization:
data_type <variable_name1> [,<variable_name2>, …… ] ;
2) Declaration with initialization:
data_type <variable_name1>=<val1> [,<variable_name2>=<val2>, …… ] ;
Declaration Syntax (Grammar)
1) Declaration without initialization:
int x;
float x,y,z;
2) Declaration with initialization:
int a=10;
float x=20, y=30, z=-40;
Declaration example
26. 3) BASIC DATA TYPES IN C
3) Tokens / words in C Language
27. 3) BASIC DATA TYPES IN C
3) Tokens / words in C Language
29. 3) BASIC DATA TYPES IN C
3) Tokens / words in C Language
30. IDENTIFIERS BASIC DATA TYPES / DATA STRUCTURES IN C++
2.3) IDENTIFIERS:
3) Tokens / words in C Language
31. 4) Tokens / Instructions(statements) in C Language
• Sequential execution means that each
command in a program script executes in the
order in which it is listed in the program.
• The first command in the sequence executes
first and when it is complete, the second
command executes, and so on.
33. 4) Tokens / Instructions(statements) in C Language
C has following types of statement.
i) Variable Declaration Statements
ii) Assignment statements
• Initialization statement
• Arithmetical / Mathematical Statements
iii) Input Statements
iv) Selection (branching)
• if (expression) statement
• if (expression) else statement
• Nesting of if (expression) else statement
• if (expression) else ladder
• switch (expression) case statement
v) iteration (looping) statements
• while (expression){ block }
• for(expression ;expression ;expression){ block }
• do {block} while (expression);
vi) Output statements
vii) Normal / Abnormal termination statement;
viii) Null Statements
34. 5) Tokens / Operators in C Language
• An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.
• C language is rich in built-in operators and provides the following types of operators −
35. 5) Tokens / Operators in C Language
• Classification of Each Operators
Unary Operators Binary Operators
The operators which act upon a single
operand are called unary operators.
The operators which require two
operands for their action are
called binary operators.
Syntax
i) operator <operand>
ii) <operand> operator
Syntax
i) operand1 operator operand2
Example
i ++, j - -
++c
- 46
Example
a + b
x - 4
36. 5) Tokens / Operators in C Language
• The following table shows all the arithmetic operators supported by the C language.
• Assume variable A = 10 and variable B = 20, then
5.1) Arithmetic Operators in c Language
Operator Description Example Result
+ Adds two operands. A + B 30
− Subtracts second operand from the first. A − B -10
* Multiplies both operands. A * B 200
/ Divides numerator by de-numerator. B / A 2
% Modulus Operator and remainder of after an integer division. B % A 0
++ Increment operator increases the integer value by one. A++ 11
-- Decrement operator decreases the integer value by one. A-- 9
37. 5) Tokens / Operators in C Language
5.1) Classification of Arithmetic Operators in c Language
38. 5) Tokens / Operators in C Language
• Program for Study of Arithmetic operators
5.1) Arithmetic Operators in c Language
#include <stdio.h>
main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Line 1 - Value of c is %dn", c );
c = a - b;
printf("Line 2 - Value of c is %dn", c );
c = a * b;
printf("Line 3 - Value of c is %dn", c );
c = a / b;
printf("Line 4 - Value of c is %dn", c );
c = a % b;
printf("Line 5 - Value of c is %dn", c );
c = a++;
printf("Line 6 - Value of c is %dn", c );
c = a--;
printf("Line 7 - Value of c is %dn", c );
}
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
Output
39. 5) Tokens / Operators in C Language
• Priority / Precedence / Hierarchy of Arithmetic Operators in C
5.1) Arithmetic Operators in c Language
40. 5) Tokens / Operators in C Language
• Priority / Precedence / Hierarchy of Arithmetic Operators in C Example
5.1) Arithmetic Operators in c Language
41. 5) Tokens / Operators in C Language
• Priority / Precedence / Hierarchy of Arithmetic Operators in C Example
5.1) Arithmetic Operators in c Language
42. DATA INPUT AND OUTPUT INSTRUCTIONS IN C
As we know that any c program is made up of 1 or more
then ONE standard function.
Likewise it use some functions for input output
process. The most common function
printf()
scanf().
43. printf() Function
printf() function is used to display something on the console or to display the value of
some variable on the console.
The general syntax for printf() function is as follows
printf(<”format string”>[,<list of variables>]);
To print some message on the screen
printf(“God is great”);
This will print message “God is great” on the screen or console.
Format specifiers in “format string” must match with respective variable
data type.
Number of format specifiers must be equal to number of variables in list of
variables.
44. printf() Function
To print the value of some variable on the screen
Integer Variable :
int a=10;
printf(“%d”,a);
Here %d is format string to print some integer value and a
is the integer variable whose value will be printed by
printf() function.
This will print value of a “10” on the screen.
45. printf() function
• To print multiple variable’s value one can use printf() function in
following way.
int p=1000,n=5;
float r=10.5;
printf(“amount=%d rate=%f year=%d”,p,r,n);
• This will print “amount=1000 rate=10.5 year=5” on the screen
46. scanf() Function
• scanf() function is use to read data from keyboard and to
store that data in the variables during runtime.
• The general syntax for scanf() function is as follows.
scanf(“Format String”, &variable);
• Here format string is used to define which type of data it is taking as input.
• This format string can be %c for character, %d for integer
variable and %f for float variable.
• Format specifiers in “format string” must match with respective variable data
type.
• Number of format specifiers must be equal to number of variables in list of
variables.
47. scanf() Function
scanf(“Format String”, &variable1,…);
• Where as variable the name of memory location or
name of the variable
• and & sign is an operator that tells the compiler the
address of the variable where we want to store the
value.
48. scanf() Function
• For Integer Variable : int rollno;
printf(“Enter rollno=”);
scanf(“%d”,&rollno);
Here in scanf() function %d is a format string for integer variable and
&rollno will give the address of variable rollno to store the value at variable
rollno location.
• For Float Variable : float per;
printf(“Enter Percentage=”);
scanf(“%f”, &per);
• For Character Variable : char ans;
printf(“Enter answer=”);
scanf(“%c”, &ans);
50. clrscr() function in C
• Function "clrscr" (works in Turbo C / C++ compiler only)
clears the screen and moves the cursor to the upper left-
hand corner of the screen.
• It is an output statement in c.
• Syntax:
void clrscr( );
• For Example
clrscr( );
51. getch() function in C
• getch() is a nonstandard function and is present in conio.h header file which
is mostly used by MS-DOS compilers like Turbo C.
• It is an input statement in c.
• Syntax:
int getch(void);
Parameters: This method does not accept any parameters.
Return value: This method returns the ASCII value of the key pressed.
For Example
int main()
{
int a;
a=getch();
printf("%c", a);
return 0;
}
Input: g (Without enter key)
Output: Program terminates immediately. But
when you use DOS shell in Turbo C, it shows a
single g, i.e., 'g'
Output with explanation
52. getch() function in C
• getch() method pauses the Output Console untill a key is pressed.
• It does not use any buffer to store the input character.
• The entered character is immediately returned without waiting for the enter
key.
• The entered character does not show up on the console.
• The getch() method can be used to accept hidden inputs like password, ATM
pin numbers, etc.
53. Single character input – the getchar () function :
• Single characters can be entered into the computer using the “C” library
function getchar.
• In general terms, a reference to the getchar function is written as.
character variable=getchar();
For example
char c;
c=getchar();
printf(“Given Character is:%c ”, c);
54. Single character output – The putchar function
• Single character can be displayed (i.e. written out of
the computer) using the C library function putchar.
• In general a reference to the putchar function is
written as
putchar (character variable);
• For Example
char c=’a’;
putchar(c);
55. RELATIONAL OPERATORS IN C:
• Relational operators are used to find the relation between two
variables. i.e. to compare the values of two variables in a C program.
Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the
condition becomes true.
(A == B) is not true.
!= Checks if the values of two operands are equal or not. If the values are
not equal, then the condition becomes true.
(A != B) is true.
> Checks if the value of left operand is greater than the value of right
operand. If yes, then the condition becomes true.
(A > B) is not true.
< Checks if the value of left operand is less than the value of right
operand. If yes, then the condition becomes true.
(A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value
of right operand. If yes, then the condition becomes true.
(A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of
right operand. If yes, then the condition becomes true.
(A <= B) is true.
Assume variable A holds 10 and variable B holds 20 then