Unit 1-3
Unit 1-3
Programming Language - C
Unit - 1 Introduction
what is computer?
1. Machine language
2. Assembly language
3. High-level language
7
Types of Programming Languages
1. Machine language
It consists of binary code (0s and 1s) that the computer's central
processing unit (CPU) can execute directly.
8
Types of Programming Languages
1. Machine language
9
Types of Programming Languages
1. Machine language
• It is very difficult to resolve bugs and errors present in the codes and
programs.
2. Assembly language
2. Assembly language
12
• It is easy to correct errors and hence, it is easy to modify the code.
Types of Programming Languages
2. Assembly language
12
Types of Programming Languages
2. Assembly language
13
Types of Programming Languages
14
Types of Programming Languages
15
Types of Programming Languages
16
Types of Programming Languages
3. High-Level Language
17
What is compiler?
18
What is interpreter?
19
20
Compiler Vs Interpreter
21
Algorithms
22
Flowcharts
• Shapes:
⚬ Oval: Start/End
23
⚬ Rectangle: Process/Instruction
Flowcharts
24
Algorithm and Flowchart
25
History of C
26
History of C
27
Features of C Program
Libraries:-
C Compiler comes with list of header files which consist of
many built in functions which can be used to develop
program.
Portability:-
We can compile or execute C program in any operating
system(unix, dos,windows). This means that C programs
written for one computer can easily run on another computer 27
Features of C Program
27
Features of C Program
27
Basic structure of c program
1. Documentation:-
This section consists of the description of the program, the
name of the program, and the creation date and time of the
program.
27
Basic structure of c program
2. Preprocessor Section:-
The preprocessor section contains all the header files used in a
program. It informs the system to link the header files to the
system libraries. It is given by:
#include<stdio.h>
#include<conio.h>
#include<math.h>
27
#include<stdio.h> is a command in C programming that
includes a file named stdio.h in your program. This file
contains the code needed for basic input and output functions,
like printing text on the screen (printf) or reading user input
(scanf).
27
#include<math.h> is a header file in C programming that
contains mathematical functions.
When you include math.h in your program,
you can use a variety of mathematical operations that aren't
built into the basic C language.
For example, with math.h,
you can use:sqrt(x):
To calculate the square root of x.pow(x, y): To raise x to the
power of y. 27
Basic structure of c program
3.Define section:-
The define section comprises of different constants declared
using the define keyword. It is given by:
#define a 2
27
Basic structure of c program
4.Global declaration:-
These variables are declared outside all the functions and can
be accessed from any program part.
int a = 5;
char ch ='z';
float num = 2.54;
27
Basic structure of c program
5.Main Function :-
The main function is the entry point of every C program. It's
where the execution of the program starts.
void main()
{ printf("Hello, World!");
27
Basic structure of c program
6. Subprogram section:
If the program is a multi-function program then the
subprogram section contains all the user-defined functions
that are called in the main () function. User-defined functions
are generally placed immediately after the main () function,
although they may appear in any order.
27
Basic Syntax of a c program
38
Basic Syntax of a c program for turbo
c
39
C Tokens
27
C Tokens
Keywords:-
A word in C is named as keyword consisting the set of rules
for Case-sensitivity, reserved word that are mainly used in
writing a C program.
Keywords are basically the specific words of C language.
In C, they have special meaning. Keywords are also called
reserved words. We cannot change the meaning of the
keywords.
They can’t be used as variable name. 27
C Tokens
Keywords:-
for
27
C Tokens
Identifiers:-
Identifiers are used to give name to the variables, functions,
arrays, structure, and union. This name is given by the
programmer. Name of the identifier should be meaningful.
27
Identifiers:-
27
Identifiers:-
27
C Tokens
Constants:-
Constants refer to fixed values that the program may not alter
during its execution. These fixed values are also called boot
literals or just literals.
Example# 1264, 3, 2, π, e, etc.
For example:-
27
Constants
Floating constants:-
A floating-point constants has an integer part, a decimal point,
a fractional part, and an exponent part.For example:-
27
Constants
Character Constants:-
Plain Character: This is a single character enclosed in single
quotes, like 'x'. It's straightforward and represents the
character itself.
Escape Sequence:
characters can't be typed directly or are special, like a tab or a
newline. To represent these, you use an escape sequence,
which is a backslash (\) followed by a specific character.
For example:
'\t' represents a tab space.
'\n' represents a new line. 27
Constants
String constants:-
String constants are enclosed in double quotes " ".
A string contains characters that are similar to character
constants: plain characters, escape sequences,
27
Variables
Variables are used to store a data. A variable is a container
(storage area) to hold data.
27
Variables
Variable is an entity where user can store digits, characters
and strings. It is similar to a box or a container.
27
Variables
A name given to the memory location is known as variable
name.
27
Declaration of the variables
Syntax#
Data-type variableName1, variableName2 … variableNameN;
Example#
int sum, rollno;
float average;
char gender, name[20];
27
Datatypes
40
Datatypes
41
Datatypes
41
Primary data types
41
Datatypes
Example#
int num1,num2;
long int num3;
41
Datatypes
There are various integer data types each has different memory
assignment. This gives you the flexibility of choosing the right type
of variable in the program.
41
Datatypes
1) Primary data type
Data Type No of Bytes Default Range Control string
Used in memory (minimum to maximum
value)
42
Datatypes
41
Datatypes
The double is same as float but provide more memory space for
storing large value with longer precision.
41
Datatypes
float percentage;
double meterreading;
There are various float data types each has different memory
assignment. this gives you the flexibility of choosing the right type
of variable in the program.
41
Datatypes
41
Datatypes
Character type variable can hold a single character. There are signed
and unsigned chars; both occupy 1 byte each, but having different
ranges.
Example#
char ch = ‘a’;
41
Datatypes
41
Datatypes
Example#
void read(); read() function does not return nay value.
void read(void); read() function does not have any arguments.
41
Datatypes
43
Datatypes
User-defined data types are created by the programmer using the primary
data types.
44
Datatypes
45
Datatypes
46
Datatypes
#include <stdio.h>
enum Day { MON, TUE, WED }; // Define an enum with 3 days(0,1,2 default values)
int main()
{
printf("Day index 1 is: %d\n", TUE); // Print 1 the integer value of TUE
return 0;
}
46
Input and output
input and output (I/O) operations are fundamental for interacting with the user
and managing data.
These operations allow a program to read data from the user (input) and display
results (output).
The standard library <stdio.h> provides functions for performing I/O operations.
55
Storage Classes
56
Storage Classes
1. auto(default)
57
Storage Classes
1. auto(default)
58
Storage Classes
2. register
59
Storage Classes
2. register
60
Storage Classes
3. Static
It is initialized only once and exists for the entire lifetime of the
program.
61
Storage Classes
3. Static
62
Storage Classes
4. extern
63
Storage Classes
4. extern
64
Operators and Expressions
Operators
Operators are the symbols that instruct
to perform some mathematical or
logical operations.
Example#
+ - X / &&
66
Operators and Expressions
Expressions
int result = 5 + 3 * 2;
66
Operators and Expressions
Expressions
int result = 5 + 3 * 2;
In this example:
5, 3, and 2 are constants.
+ and * are operators
result is a variable
When evaluated, the expression 5 + 3 * 2 results in the numeric
value 11 (because multiplication is done before addition).
66
Operators
Arithmetic Operators
Increment and Decrement
Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Special Operators
Categories of Operators in C
1. Arithmetic Operators
They are used to perform basic
mathematical operations.
• Examples: +, -, *, /, %
66
Categories of Operators in C
1. Arithmetic Operators
OUTPUT
66
Categories of Operators in C
2. Relational Operators
They are used to compare two values or
expressions.
67
Categories of Operators in
C
3. Logical Operators
They are used to perform logical operations on
boolean values (true or false) or expressions.
68
Categories of Operators in
C
4. Bitwise Operators
They are used to perform operations
on the binary representations of
integers.
69
Categories of Operators in
C 4. Bitwise (OR,<<,>>) Operators
OUTPUT
69
Categories of Operators in
C
5. Assignment Operators
They are used to assign values to variables.
70
Categories of Operators in
C
6. Increment and Decrement Operators
They are used to increase or decrease the value of a
variable by one, respectively.
Examples: ++, --
71
Categories of Operators in
C
6. Increment and Decrement Operators
Symb Meaning Example
ol
72
Categories of Operators in
C
6. Increment and Decrement Operators
Postfix Form a++
72
Categories of Operators in
C
6. Increment and Decrement Operators
Prefix Form ++a
72
Categories of Operators in
C
7. Conditional (Ternary) Operator
The conditional operator, also known as the ternary operator, is a
unique operator in C that takes three operands.
Syntax:-
73
Categories of Operators in
C
7. Conditional (Ternary) Operator
73
Categories of Operators in
C
8. Comma Operator
It is used to separate two or more expressions. It evaluates each
expression from left to right and returns the value of the rightmost
expression.
74
Categories of Operators in
C8. Comma Operator
74
Categories of Operators in
C
9. Sizeof Operator
The sizeof operator in C is used to determine the size, in bytes, of a
data type or a variable.
• Example: sizeof(int),sizeof(variable)
75
Categories of Operators in
9.
C Sizeof Operator
75
Data Type Conversion
1. Implicit Conversion
77
Data Type Conversion
1. Implicit Conversion
77
Data Type Conversion
2. Explicit Conversion
78
Operator Precedence and Associativity
int result = 5 + 3 * 2;
result = 11
79
Operator Precedence and Associativity
Example:-
int result = 10 - 5 - 2;
79
Thank you
for listening