0% found this document useful (0 votes)
44 views125 pages

Unit 1-3

A college life and I have seen in the right side where your work space for me

Uploaded by

ailogin1297
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views125 pages

Unit 1-3

A college life and I have seen in the right side where your work space for me

Uploaded by

ailogin1297
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 125

Introduction to

Programming Language - C

Unit - 1 Introduction
what is computer?

A computer is an electronic device that can perform various


operations and tasks. A task is based on a set of instructions,
known as programs.

It is capable of processing, storing, and retrieving data, as well as


2
executing complex calculations and logical operations.
what is programming?

Programming is the process of creating a set of instructions that tell


a computer how to perform a task.

This code is written by programmers.


3
• A programming language is a formal system used by humans to communicate
instructions to a computer.

• It provides a writing code that can be executed by a computer to perform


specific tasks.

• programming language used by humans to communicate instructions to a 4


5
Unit - 1 Introduction
Index
• Type of programming Language
• Algorithms and Flowchart
• Overview in C
• Categories of Operators in C
• Data Type Conversion
• Operator Precedence and associativity
6
Types of Programming
Languages

1. Machine language
2. Assembly language
3. High-level language

7
Types of Programming Languages

1. Machine language

Machine language, also known as machine code, is the lowest level of


programming language.

It consists of binary code (0s and 1s) that the computer's central
processing unit (CPU) can execute directly.

Example: 10110000 01100001

8
Types of Programming Languages

1. Machine language

Advantage of machine language:

• As machine language is the only language understood by the computer, it


does not require any translator or mediator.

• Machine language is very fast and utilizes the computer efficiently.

9
Types of Programming Languages

1. Machine language

Disadvantage of machine language:

• Machine language are complex to understand and memorize.

• Writing codes in machine language is time-consuming.

• It is very difficult to resolve bugs and errors present in the codes and
programs.

• Machine languages are not easy to modify 10


Types of Programming Languages

2. Assembly language

Assembly language is a low-level programming language that is one step


above machine language.

It uses mnemonic codes and symbols to represent machine-level


instructions, making it slightly easier for humans to read and write
compared to binary code.

• Uses short, human-readable codes (mnemonics) to represent


machine instructions. 11
Types of Programming Languages

2. Assembly language

Adantage of assembly language:

• The Assembly Language is easier to use and understand as compared


to the machine language.

• It saves a lot of time and effort of the programmer, because it uses


character sets. This provides some flexibility compare to machine
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

Disadvantage of assembly language:

• Like machine language, assembly language is also machine dependent.

• As it is machine dependent, the programmer also needs to


understand the hardware.

13
Types of Programming Languages

Differences between Machine-Level language and Assembly language

14
Types of Programming Languages

Differences between Machine-Level language and Assembly language

15
Types of Programming Languages

Differences between Machine-Level language and Assembly language

16
Types of Programming Languages

3. High-Level Language

High-level programming languages are designed to be easy for humans to


read and write.

They provide abstraction from the computer’s hardware, making it easier


to develop complex programs quickly.

Examples include Python, Java, C++, and many others.

17
What is compiler?

• A compiler is a software tool that translates the entire source code


written in a high-level programming language like C into machine code
or intermediate code (such as assembly code) all at once.

• The resulting machine code is then executed directly by the


computer's hardware.

18
What is interpreter?

• An interpreter is a software tool that translates and executes


high-level programming code line-by-line or
statement-by-statement.

• It directly interprets the code at runtime, without producing a


separate executable file.

19
20
Compiler Vs Interpreter

21
Algorithms

• An algorithm is a step-by-step procedure or formula


for solving a problem or accomplishing a task.

• It is a sequence of well-defined instructions that,


when executed, performs a specific function or task.

22
Flowcharts

A flowchart is a visual representation of an algorithm.

It uses symbols and arrows to illustrate the sequence


of steps and the flow of control in a systematic way.

Flowcharts are helpful for understanding and


communicating how an algorithm works and are
commonly used in programming and process design.

• Shapes:
⚬ Oval: Start/End
23
⚬ Rectangle: Process/Instruction
Flowcharts

24
Algorithm and Flowchart

25
History of C

• C was developed in the early 1970s by Dennis Ritchie at


AT&T(American Telephone and Telegraph Company) Bell
Labs.

• It was created to improve the UNIX operating system and


has since become one of the most widely used
programming languages.

26
History of C

Powerful programming language


• C is very efficient and powerful programming language, it is best
used for data structures and designing system software.

Case Sensitive language.


• C is case sensitive language. It is essential to write a code according
the case sensitively.

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

Powerful programming language:-


C is very efficient and powerful programming language, it is
best used for data structures and designing system software.
Case Sensitive language:-
C is case sensitive language. It is essential to write a code
according the case sensitively.

27
Features of C Program

Procedure Oriented Language:-


C Language is procedure oriented language, means in the C
language user creaes procedures or functions to execute their
specific task.
Usually C programs are divided into small functions.
Procedure oriented language is follows algorithm to execute
your statements.

27
Basic structure of c program

The structure of a C program means the specific structure to


start the programming in the C language.
Without a proper structure, it becomes difficult to analyze the
problem and the solution.

The basic structure of a C program is divided into 6 parts


which makes it easy to read, modify, document, and
understand in a particular format.
27
Basic structure of c program

Sections of the C Program


There are 6 basic sections responsible for the proper execution
of a program. Sections are mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs 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.

It is specified at the start of the program in the form of


comments. Documentation can be represented as:

27
Basic structure of c program

// description, name of the program, programmer name, date,


time etc.

/* description, name of the program, programmer name, date,


time etc. */

Anything written as comments will be treated as


documentation of the program and this will not interfere with
the given code. Basically, it gives an overview to the reader of 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).

#include<conio.h> is a C header file that stands for "Console


Input/Output." It includes functions for handling input and
output operations in a console (the text screen). 27
Some of the functions provided by conio.h include:getch():
Reads a single character from the keyboard, without waiting
for the Enter key to be pressed.clrscr(): Clears the console
screen.

getche(): Similar to getch(), but it also displays the character


on the screen as you type it.

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

In a C program, smallest individual part or unit is known as


token. It is recognized by the compiler. We write a program
using tokens.
C tokens are the basic buildings blocks in C language which
are constructed together to write a C program.

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.

✓ Constants can be of any of the basic data types like


✓ an Integer constant (value in numbers without any
decimal)
✓ a Floating constant (value in numbers with decimal points)
✓ a Character constant (a single letter value) 27
Constants
Integer constants:-
An integer constants can be a decimal, octal, or hexadecimal
constant. A prefix specifies the base or radix: 0x or 0X for
hexadecimal, 0 for octal, and nothing for decimal. Here are
some examples of integer constants–

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.

We can think of a variable like a bucket and values are like a


water in bucket which can easily change.

27
Variables
Variable is an entity where user can store digits, characters
and strings. It is similar to a box or a container.

How variable stores the value?

27
Variables
A name given to the memory location is known as variable
name.

There are rules that must be followed before giving a variable


name.

They are as under.

It must start with an alphabet or underscore (_) and following


characters can be letter, underscores and digits only.
Both uppercase and lowercase letters can be used. 27
Variables
Spaces are not allowed in variable name.
Certain keywords (these are C language reserved words) cannot
be used in a variable name.
The variable names should not be very long. (maximum 32
characters)
The variable names should be meaningful.
Example#
Valid A, a1, a_ ,_ a
Invalid If, 1a, a#, a bc

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

For example, in Our secular nation we can generally


identify a person simply looking at their costumes,
their identity belonging, their language, their religion,
their daily eating etc.

Similarly, in C language we can get idea about


variable category from its data type.

41
Datatypes

Data types represent the type of data to be stored in a variable.


We store a data in computer memory through the variables.
So variables type must be specified before Storing data into the
computer memory.
Computer memory is organized in bytes, and byte stores the
smallest amount of data. C language supports different types of
data (integer, float, character etc.) and need to store the values in
the program.
In C, there is fixed amount of memory assigned to each data type.
41
Data types can be broadly
classified as:

41
Primary data types

41
Datatypes

1) Primary data type


Integer types:-

Integers are whole numbers with a range of values.


Generally, an integer occupies 2 bytes memory and its value range
limited to -32768 to +32767.

Example#
int num1,num2;
long int num3;
41
Datatypes

1) Primary data type


Integer types:-

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)

Int 2 -32768 to 32767 %d

long int 4 -2147483648 to 2147483647 %ld

Unsigned long int 4 0 to 4294967295 %lu

Unsigned short int 1 0 to 255 %hd

short int 1 -128 to 127 %hd

unsigned int 2 0 to 65535 %u

42
Datatypes

1) Primary data type


Meaning of “unsigned” is that only positive number can be stored in
the variable.
Here the “signed” keyword is not compulsory if the you have
declared a variable
without it complier will assume as “signed” by default another wise
write unsigned” keyword explicitly.

41
Datatypes

1) Primary data type


Floating Point Types:-

The float data type is used to store fractional numbers (real


numbers) with up to 6 digits of precision.

When the accuracy of the floating-point number is insufficient, we


can use the double type instead of float.

The double is same as float but provide more memory space for
storing large value with longer precision.
41
Datatypes

1) Primary data type


Floating Point Types:-
Example#

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

1) Primary data type


Float data type

Data Type No of Bytes Default Range Control string


Used in memory (minimum to maximum
value)

Float 4 -3.4e38 to +3.4e38 %f or


%e (with exponent) or
%g (trailing zero or
decimal point will not
appears)

Double 8 -1.7e308 to +1.7e308 %lf

41
Datatypes

1) Primary data type


Character Type:-

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

1) Primary data type


Character Type:-
Data Type No of Bytes Default Range Control
Used in memory (minimum to maximum value) string

Char 1 128 to 127 (in this range ASCII value of %c


the character is stored.

Unsigned char 1 0 to 255 %c

41
Datatypes

1) Primary data type


void Type :-
This data type is used return type of the function and it means
function does not return any value.

Another use with the as an argument of the function and meaning is


that function does not has any arguments.

Example#
void read(); read() function does not return nay value.
void read(void); read() function does not have any arguments.
41
Datatypes

2) Derived data type

43
Datatypes

3) User-defined data type

User-defined data types are created by the programmer using the primary
data types.

44
Datatypes

3) User-defined data type


• typedef: typedef in C is used to create an alias for an existing data type. It
simplifies code by providing more descriptive names for complex types or
making code more readable.

45
Datatypes

3) User-defined data type


#include <stdio.h>
//This line creates an alias named Age for the int data type.
typedef int Age;
int main()
{
Age myAge = 25; // Use the new type alias
printf("My age is %d.\n", myAge);
return 0;
} 45
Datatypes

3) User-defined data type

• enum: (short for "enumeration") is a user-defined data type that


consists of a set of named integer constants. An enum is defined
using the enum keyword followed by a name and a list of constants

46
Datatypes

3) User-defined data type

#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.

Input: Refers to reading data provided by the user(scanf)


Output: Refers to displaying or writing data from the program to the user(printf)

• printf: Used for outputting formatted data to the screen. 54


Format specifiers

• The symbol we use in every format specifier is %.

• Format specifiers tell the compiler about the type of data


that must be given or input and the type of data that must
be printed on the screen.

• %d - Used to take Integer


• %c - Used to take Character
• %f - Used to take float
• %lf - Used to take double

55
Storage Classes

•Storage classes define the scope, lifetime, and


visibility of variables and functions.

•There are four primary storage classes in C:


auto (default), register, static, extern

56
Storage Classes
1. auto(default)

It is used to define local variables within functions. It is the


default storage class for all local variables.

Outside this block, the variable is not recognized.

The auto keyword is used to explicitly declare a variable as auto

57
Storage Classes
1. auto(default)

58
Storage Classes
2. register

It is used to define local variables that should be stored in a


CPU register instead of RAM.

This can lead to faster access times for frequently used


variables, as accessing a register is typically faster than
accessing memory.

Once the block is exited, the register variable is destroyed.

59
Storage Classes
2. register

60
Storage Classes
3. Static

It is used to define the scope and lifetime of variables and


functions.

It is initialized only once and exists for the entire lifetime of the
program.

61
Storage Classes
3. Static

62
Storage Classes
4. extern

It is used to declare a global variable or function in another file.

It extends the visibility of variables and functions across


multiple files.

By using extern, you can access variables or functions defined


in one file from another file.

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

Expression means whose evaluation yields numeric value.


Expression contains constant, variable and operators.
Here’s a simple example of an expression that yields a
numeric value:
Example:-

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.

They operate on numerical values and


return numerical results.

• 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.

These operators return a boolean result: 1 (true) if


the comparison is true, and 0 (false) if the
comparison is false.

•Examples: ==, !=, >, <, >=, <=


67
Categories of Operators in C
OUTPUT
2. Relational Operators

67
Categories of Operators in
C
3. Logical Operators
They are used to perform logical operations on
boolean values (true or false) or expressions.

They help in combining multiple conditions,


which is essential for controlling the flow of a
program through conditional statements and
loops.

• Examples: && (AND), || (OR), ! (NOT) 68


Categories of Operators in C
3. Logical Operators
OUTPUT
1
1
1

68
Categories of Operators in
C
4. Bitwise Operators
They are used to perform operations
on the binary representations of
integers.

They operate at the bit level and are


used for tasks that require manipulation
of individual bits within data.

• Examples: &, |, ^, ~, <<, >> 69


Categories of Operators in
C 4. Bitwise (AND) Operators
OUTPUT

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.

The most basic assignment operator is the simple


assignment operator (=), but there are several
compound assignment operators that combine
arithmetic operations with assignment

• Examples: =, +=, -=, *=, /=, %=


70
Categories of Operators in
C 5. Assignment Operators
OUTPUT

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

++ To increment value by one a++,++a


in the operand.

-- To decrement value by one a--,--a


in the operand.

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.

It is used to evaluate a condition and return one of two values


depending on whether the condition is true or false

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.

It allows multiple expressions to be evaluated in a single statement.

• Example: int a = (b = 1, b + 2);

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.

It is a compile-time unary operator that can be applied to any data


type or expression to evaluate its size.

• Example: sizeof(int),sizeof(variable)

75
Categories of Operators in
9.
C Sizeof Operator

75
Data Type Conversion

• Data type conversion in C refers to changing a


variable from one data type to another.

• This can be done either explicitly by the


programmer or implicitly by the compiler.

• There are two main types of data type


conversion in C:
Implicit conversion
Explicit conversion 76
Data Type Conversion

1. Implicit Conversion

Implicit conversion occurs automatically


when the compiler converts one data type
to another without any explicit instruction
from the programmer.

This usually happens in expressions involving


mixed data types.

77
Data Type Conversion

1. Implicit Conversion

77
Data Type Conversion

2. Explicit Conversion

Explicit conversion, or casting, is performed by the


programmer to convert a variable from one type to
another.

This is done using the cast operator (type)

• Example: float x = 5.7; int y = (int)x; // y will be 5

78
Operator Precedence and Associativity

Operator precedence in C decides which


operation is done first when there are many
operators in a statement.

For example, multiplication is done before


addition

int result = 5 + 3 * 2;

result = 11
79
Operator Precedence and Associativity

Associativity in C tells us the direction to


follow when two operators have the same
importance (precedence). It can be
left-to-right (start from the left) or
right-to-left (start from the right)

Example:-
int result = 10 - 5 - 2;

79
Thank you
for listening

You might also like