You are on page 1of 98

Course In C

Contents

 Contents
Fundamentals of C
Contents

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a „C‟ program
 Character Set, Tokens, identifiers, keywords,
constant and escape sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Where are we?

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a „C‟ program
 Character Set, Tokens, identifiers, keywords,
constant and escape sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Introduction

 What is „C‟?
 „C‟ is a programming language developed at AT&T‟s Bell
Laboratories of USA in 1972. It was designed and written by
Dennis M. Ritchie.
Where are we?

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a „C‟ program
 Character Set, Tokens, identifiers, keywords,
constant and escape sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Basics of Algorithm

 What is an Algorithm?
 An algorithm is a step by step procedure for solving a problem.
Basics of Algorithm

 Ex1. Suppose we want to calculate addition of two


numbers then algorithm could be,

Algorithm: Addition of two numbers.


Step 1. Read two numbers a & b.
Step 2. Calculate addition c=(a+b).
Step 3. Print addition i.e. print c.
Basics of Algorithm

 Ex2. Write an algorithm to calculate average of 3


numbers.

Algorithm: Average of 3 numbers.


Step 1. Read a, b & c.
Step 2. d=(a+b+c)/3
Step 3. Print d
Basics of Algorithm

 Ex.3 Write an algorithm to read two numbers and


print the bigger.

Algorithm: Read two numbers and print the bigger


Step 1. Read a & b
Step 2. If a>b print a
Else print b.
Basics of Algorithm

 Ex.4 Write an algorithm to read mark of the subject


and print the result. Given that a candidate must
score at least 40 marks to pass the subject.
Algorithm: Read mark of the student and print the result.
Step 1. Read mark
Step 2. If mark<40 print “FAIL”
Else print “PASS”.
Basics of Flowchart

 What is the flowchart?


 The flowchart is a diagrammatic/graphical/pictorial
representation of the algorithm.
 Flowcharts are useful to study the problem because
diagrammatic representation can be easily understood by
human beings.
 In a flowchart each type of symbol has a specific
meaning. Some flowchart symbols are given below,
Basics of Flowchart

 Ex.1 Draw a flowchart for calculating addition of


two numbers.
Basics of Flowchart

 Ex.2 Draw flowchart for calculating average of 3


numbers.
Basics of Flowchart

 Ex.3 Draw flowchart to read two numbers and


print the bigger.
Basics of Flowchart

 Ex.4 Draw flowchart to read mark of the subject


and print the result. Given that a candidate must
score at least 40 marks to pass the subject.
Where are we?

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a ‘C’ program
 Character Set, Tokens, identifiers, keywords,
constant and escape sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Structure of a ‘C’ program

 The basic building blocks of every „C‟ program are


functions.
 A function is a group of statements that perform
some task.
 Every „C‟ program consists of one or more functions.
 And every „C‟ program must have exactly one main()
function.
 Program execution starts from the main() function
and ends when the instructions in the main()
function have been executes.
Structure of C program

 The basic structure of a „C‟ program is as shown


below,
1. /*My First C Program*/
2. /*Kapil Vhatkar*/
3. /*8th July 2013*/
4. #include<stdio.h>
5. main()
6. {
7. printf("Welcome to C");
8. }

Fig:- Sample C program.


Where are we?

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a ‘C’ program
 Character Set, Tokens, identifiers, keywords,
constant and escape sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Compilation and Execution of a C Program
1. #include<stdio.h>
2. #include<conio.h>
3. main()
4. {
5. clrscr();
6. printf(“Hello! Welcome to C!”);
7. getch();
8. }

Fig:-First.c
Source File Header File
#include<stdio.h>
STDIO.H
First.C #include<conio.h>
CONIO.H

Compiler

First.OBJ Object File Library File CS.LIB

Linker

First.OBJ Executable File


1. #include<stdio.h>
2. #include<conio.h>
3. main()
4. {
5. clrscr();
6. printf(“Hello! Welcome to C!”);
7. getch();
8. }

Hello! Welcome to C!

Fig:-First.c
1. #include<stdio.h>
2. #include<conio.h>
3. #include<math.h>
4. main()
5. {
6. int a=25,b;
7. clrscr();
8. b=sqrt(a);
9. printf(“Square Root is %d”,b);
10. getch();
11. }

Fig:-First.c
Source File Header File
#include<stdio.h>
STDIO.H
First.C #include<conio.h>
CONIO.H

MATH.H
Compiler

First.OBJ Object File Library File CS.LIB

Linker

First.OBJ Executable File


1. #include<stdio.h>
2. #include<conio.h>
3. #include<math.h>
4. main()
5. {
6. int a=25,b;
7. clrscr();
8. b=sqrt(a);
9. printf(“Square Root is %d”,b);
10. getch();
11. }

Square Root is 5.

Fig:-First.c
Where are we?

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a „C‟ program
 Character Set, Tokens, identifiers, keywords,
constant and escape sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Character Set, Tokens, identifiers, keywords,
constant and escape sequences

 What is „C‟ Character Set?


Character Set, Tokens, identifiers, keywords,
constant and escape sequences
 What are Tokens?
Character Set, Tokens, identifiers, keywords,
constant and escape sequences
 What are identifiers?
 An identifier is a user-defined name given to a program
element such as variable, function and symbolic constants.
 Ex.

 int marks;

 float area;

 void add();
Character Set, Tokens, identifiers, keywords,
constant and escape sequences
 What are keywords?
 Keywords are reserved words whose meaning has already been
explained to C compiler.
 There are 32 keywords available in C.
Character Set, Tokens, identifiers, keywords,
constant and escape sequences

 What are constants?


 A constant is quantity which doesn‟t change during the execution of
program.
 Types of Constants
 Numeric Constants
 Integer Constants
• Ex. 1, 89, 100, 555, 63, -78, -888.
 Real Constants
• Ex. 3.142, 6.33, 99.78, -888.99
 Character Constants
 Ex. ‘A’, ‘a’, ‘Z’, ‘z’, ‘=’, ‘5’
Character Set, Tokens, identifiers, keywords,
constant and escape sequences
 What are escape sequences?
1. #include<stdio.h>
2. main()
3. {
4. printf(“Hi\nHow are you?”);
5. }

Hi
How are you?

Fig:-Escape Sequence \n.


1. #include<stdio.h>
2. main()
3. {
4. printf(“Hi\bHow are you?”);
5. }

HHow are you?

Fig:-Escape Sequence \b.


1. #include<stdio.h>
2. main()
3. {
4. printf(“Hi\tHow are you?”);
5. }

Hi How are you?

Fig:-Escape Sequence \t.


1. #include<stdio.h>
2. main()
3. {
4. printf(“Hi\aHow are you?”);
5. }

HiHow are you?

Fig:-Escape Sequence \a.


1. #include<stdio.h>
2. main()
3. {
4. printf(“Hi\‟How are you?”);
5. }

Hi‟How are you?

Fig:-Escape Sequence \’.


1. #include<stdio.h>
2. main()
3. {
4. printf(“\‟Hi\‟How are you?”);
5. }

‟Hi‟How are you?

Fig:-Escape Sequence \’.


1. #include<stdio.h>
2. main()
3. {
4. printf(“\”Hi\”How are you?”);
5. }

“Hi”How are you?

Fig:-Escape Sequence \”.


1. #include<stdio.h>
2. main()
3. {
4. printf(“Hi\\How are you?”);
5. }

“Hi”How are you?

Fig:-Escape Sequence \”.


1. #include<stdio.h>
2. main()
3. {
4. printf(“Hello\rBye”);
5. }

Byelo

Fig:-Escape Sequence \”.


Where are we?

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a „C‟ program
 Tokens, identifiers, keywords, constant and escape
sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Data types, Variables, Operators and Expressions

 What is a Variable?
 A variable is a name given to memory location in computer
where we can store constant values.
Data types, Variables, Operators and Expressions

 What are data type?


 When a variable is used in a program, you have to specify
what type of data it can hold/contain.
 And data type specifies what type of data a variable can
hold.
 The C programming language supports five
fundamental/basic/primary/built-in data types. And they
are
Fig:- Fundamental Data Types in C
Data types, Variables, Operators and Expressions

 How to declare a variable?


 A variable can be declare using following syntax
Data types, Variables, Operators and Expressions

 How to assign value to variable?


 A value to a variable can be assigned using assignment
operator as given below,
Data types, Variables, Operators and Expressions

 How to initialize a variable?


 Assigning value to variable during its declaration is called
initialization.
Where are we?

 Introduction
 Basics of Algorithm, Flowchart and examples……
 Structure of a „C‟ program
 Tokens, identifiers, keywords, constant and escape
sequences.
 Data types, Variables, Operators and Expressions
 Operators continued & programs
 Compilation & Execution
Data types, Variables, Operators and Expressions

 What is an Operator?
 An operator is a symbol that represent an operation.
 Example
 + symbol represents addition
 * symbol represent multiplication

 What is an Expression?
 An expression is a combination of variable, constants and operators.
 Example
 (a+b)
 (a+b-10)
 (a+10)*b
 (m+n)*c
 (a*b)/c
 (a+b)*(c+d)
Data types, Variables, Operators and Expressions

 In „C‟ language operators are classified as,


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment and Decrement Operators
5. Assignment Operators
6. Conditional Operator
7. Bitwise Operators
8. Other Operators
Data types, Variables, Operators and Expressions

1. Arithmetic Operator
 There are 5 arithmetic operators
Data types, Variables, Operators and Expressions
Data types, Variables, Operators and Expressions
Data types, Variables, Operators and Expressions

2. Relational Operators
 Relational operators are used to compare two values or
expression.
Sr. No. Example Result
1 25<30
2 3<2
3 3<3
4 25<=90
5 25<=25
6 25<=10
7 2>1
8 2>34
9 3>3
10 3>=3
11 3>=1
12 3>=5
13 3==3
14 3==2
15 3!=2
16 3!=3
Sr. No. Example Result
1 25<30 True
2 3<2 False
3 3<3 False
4 25<=90 True
5 25<=25 True
6 25<=10 False
7 2>1 True
8 2>34 False
9 3>3 False
10 3>=3 True
11 3>=1 True
12 3>=5 False
13 3==3 True
14 3==2 False
15 3!=2 True
16 3!=3 False
Data types, Variables, Operators and Expressions

3. Logical Operator
 Sometimes we need to test more than one condition at a time
and make a decision depending upon the result.
 Logical operators are used to combine two or more expression
(usually relational expression). The entire expression is called
logical expression which evaluate to either true (1) or false (0).
Data types, Variables, Operators and Expressions
Data types, Variables, Operators and Expressions

Sr. No. Example Result

1 (10>25) && (6>9) False


2 (10>3) && (5>45) False
3 (10>34) && (4>1) False
4 (10>1) &&(3>1) True
5 (10>25) || (6>9) False
6 (10>3) || (5>45) False
7 (10>34) || (4>1) False
8 (10>1) || (3>1) True
9 ! (0) True
10 ! (1) False
11 ! (10) False
12 ! (-9) False
Data types, Variables, Operators and Expressions

4. Increment Decrement Operator


Data types, Variables, Operators and Expressions

5. Assignment Operator
 The assignment operator is represented by =. It is used to
assign value to the variable. Value can be assigned
to variable using the assignment operator as given
below,

 The assignment operator (=) assign value on the right to the


variable on the left. Note: - Only one variable must be on the
left of assignment operator.
Data types, Variables, Operators and Expressions

5. Assignment Operator
Data types, Variables, Operators and Expressions

 Shorthand Assignment Operator


 These are obtained by combining certain operators with the
= operator. They have following syntax.
Data types, Variables, Operators and Expressions

6. Conditional Operator
 The conditional operator is in combination of ?:.
 The conditional operators ? and : are sometimes called ternary
operators since they need three arguments. Their general form is,

 What this expression says is “Expression1 is evaluated


first, if expression1 is true then the value returned will
be expression2, otherwise value returned will be
expression3”
Data types, Variables, Operators and Expressions

7. Bitwise Operator
 Bitwise operators are used to manipulate data at bit level.
They can operate only on ints and chars but not on floats
and doubles.
Data types, Variables, Operators and Expressions

 Examples,
 Left shift Operator(<<)
 int a=5,b;
 b=a<<1;
 Right shift Operator(>>)
 int a=5,b;
 b=a>>1;
 One‟s Compliment Operator(~)
 int a=5,b;
 b=~a;
Data types, Variables, Operators and Expressions

 Examples,
 Bitwise And Operator(&)
 int a=5, b=4,c;
 c=a&b;
 Bitwise Or Operator (|)
 int a=5, b=4,c;
 c=a|b;
 Bitwise Xor Operator (^)
 int a=5, b=4,c;
 c=a^b;
Data types, Variables, Operators and Expressions

8. Other Operators
 Sizeof Operator
 The sizeof operator gives/returns the size (in bytes) of the data-type
or variable or constant. The format is,

 sizeof(int); returns 2.
 sizeof(float); returns 4.
 sizeof(10); returns 2.
Data types, Variables, Operators and Expressions

8. Other Operators
 Comma Operator
 The comma operator is represented by ,. It is used to separate a
set of expressions. A pair of expressions separated by a comma
is evaluated left to right.
 Example,
 i=(j=3,j+2);
Data types, Variables, Operators and Expressions

9. Address Operator
 The address operator when used with a variable returns the
address of variable.
 Example,

 b=&a;

 Here the address of variable a is assigned to variable b.


Data types, Variables, Operators and Expressions

 What is Precedence and Associativity of Operators?


 Let‟s see precedence and associativity in simple words.
 An expression is a combination of variables, constants
and operators.
 An expression may contain multiple operators. And an
expression containing multiple operators is solved
according to precedence and associativity of operators.

 Precedence: - In „C‟ language precedence is given to
each operator. Precedence means priority or rank or
level. The operator having higher precedence is
evaluated first
Data types, Variables, Operators and Expressions
Data types, Variables, Operators and Expressions

 Associativity:- If the expression contains multiple


operators of the same priority then operators are
evaluated either from „Left to Right‟ or from „Right to
Left‟ depending on the level/rank. This is known as
the associativity property of an operator.
 For example,
Data types, Variables, Operators and Expressions
Thank You

You might also like