0% found this document useful (0 votes)
4 views31 pages

Introduction To C Programming JAP

The document provides an introduction to C programming, covering fundamental concepts such as bits, bytes, and words, as well as character and number representation in various number systems. It outlines the program development cycle, including phases like problem definition, analysis, algorithm development, coding, testing, and maintenance. Additionally, it discusses algorithms, control structures, flowcharts, pseudocode, and the characteristics and advantages of the C programming language.

Uploaded by

piyushdivase0
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)
4 views31 pages

Introduction To C Programming JAP

The document provides an introduction to C programming, covering fundamental concepts such as bits, bytes, and words, as well as character and number representation in various number systems. It outlines the program development cycle, including phases like problem definition, analysis, algorithm development, coding, testing, and maintenance. Additionally, it discusses algorithms, control structures, flowcharts, pseudocode, and the characteristics and advantages of the C programming language.

Uploaded by

piyushdivase0
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

Unit-01

Introduction to C-programming
BITS, BYTES and WORDS

Bit

 Bit = From shortening of the word “binary digit”


 Smallest unit of computer memory.
 1 or 0 are only values.
 Bit can represent only one value
Bytes

 Byte = 8 bits
 Each keyword character is stored as a byte.
 Example = A -> 01000001
Words

 Word = a sequence of N bits where N = 16, 32, 64 depending on the


computer
 The number of bits a particular computer’s CPU can deal with in one go

REPRESENTATION - CHARACTERS, INTEGER AND FRACTION

Everything represented by a computer is represented by binary sequences.

Character Representation
All the characters on keyboard are given a character code. Two standards
commonly use:
1. American Standard Code for Information Interchange (ASCII) - Giving a
character set of 128 characters
2. Unicode - Giving a character set of 65,536 characters
Integer and Fraction Representation

Use fixed number of bits to represent integer.

Number System

 A number system in base r or radix r uses unique symbols for r digits.


One or more digits are combined to get a number.
 The base of the number decides the valid digits that are used to make a
number.
 In a number, the position of digit starts from the right-hand side of the
number. The rightmost digit has position 0, the next digit on its left has
position 1, and so on. The digits of a number have two kinds of values—

Face value and Position value.

The face value of a digit is the digit located at that position. For example, in
decimal number 52, face value at position 0 is 2 and face value at position 1 is
5.

The position value of a digit is (base position). For example, in decimal number 52,
the position value of digit 2 is 100 and the position value of digit 5 is 101. Decimal
numbers have a base of 10.

The number is calculated as the sum of, face value * base position, of each of the
digits. For decimal number 52, the number is 5*10 1 + 2*100 = 50 + 2 = 52

In computers, we are concerned with four kinds of number systems, as follows—

 Decimal Number System —Base 10

 Binary Number System —Base 2

 Octal Number System —Base 8

 Hexadecimal Number System—Base 16

A number in a particular base is written as (number)base of number For example,


(23)10 means that the number 23 is a decimal number, and (345) 8 shows that
345 is an octal number.

Decimal Number System

 It consists of 10 digits—0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

 All numbers in this number system are represented as combination of


digits 0—9. For example, 34, 5965 and 867321.

Binary Number System

 The binary number system consists of two digits—0 and 1.


 All binary numbers are formed using combination of 0 and 1. For example,
1001, 11000011 and 10110101.

Octal Number System

 The octal number system consists of eight digits—0 to 7.

 All octal numbers are represented using these eight digits. For example,
273, 103, 2375, etc.

Hexadecimal Number System

 The hexadecimal number system consists of sixteen digits—0 to 9, A, B,


C, D, E, F, where (A is for 10, B is for 11, C-12, D-13, E-14, F-15).

 All hexadecimal numbers are represented using these 16 digits. For


example, 3FA, 87B, 113, etc.

Decimal, binary, octal and hexadecimal equivalents


Program Development Cycle

The program development process is part of the software lifecycle, characterized


by the following stages:

 Requirements analysis
 Design
 Coding
 Testing
 Implementation and support
 Documentation

Program development life cycle contains 6 phases, they are as follows –

1. Problem Definition

2. Problem Analysis

3. Algorithm Development

4. Coding & Documentation

5. Testing & Debugging

6. Maintenance

Problem Definition:

 In this phase, we define the problem statement and we decide the


boundaries of the problem.
 In this phase we need to understand the problem statement, what is our
requirement, what should be the output of the problem solution.
 These are defined in this first phase of the program development life cycle.

Problem Analysis:

 In phase 2, we determine the requirements like variables, functions, etc.


to solve the problem.
 That means we gather the required resources to solve the problem defined
in the problem definition phase.
 We also determine the constraints of the solution.
Algorithm Development:

 During this phase, we develop a step-by-step procedure to solve the


problem using the specification given in the previous phase.
 This phase is very important for program development.
 That means we write the solution in step-by-step statements.

Coding & Documentation:

 This phase uses a programming language to write or implement actual


programming instructions for the steps defined in the previous phase.
 In this phase, we construct actual program.
 That means we write the program to solve the given problem using
programming languages like C, C++, Java etc.,

Testing & Debugging:

 During this phase, we check whether the code written in previous step is
solving the specified problem or not.
 That means we test the program whether it is solving the problem for
various input data values or not.
 We also test that whether it is providing the desired output or not.

Maintenance:

 During this phase, the program is actively used by the users.


 If any enhancements found in this phase, all the phases are to be repeated
again to make the enhancements.
 That means in this phase, the solution (program) is used by the end user.
 If the user encounters any problem or wants any enhancement, then we
need to repeat all the phases from the starting, so that the encountered
problem is solved or enhancement is added.
Basics of Algorithm

Algorithm –

 A set of finite rules or instructions to be followed in calculations or other


problem-solving operations.
 A procedure for solving a mathematical problem in a finite number of steps
that frequently involves recursive operations.
 Algorithm is an ordered sequence of finite, well defined, clear-cut
instructions for completing a task. It is English like representation.
What is the need for algorithms?

 Algorithms are necessary for solving complex problems efficiently and


effectively.
 They help to automate processes and make them more reliable, faster, and
easier to perform.
 Algorithms also enable computers to perform tasks that would be difficult
or impossible for humans to do manually.
 It is used in various fields such as mathematics, computer science,
engineering, finance, and many others to optimize processes, analyze
data, make predictions, and provide solutions to problems.

Algorithm for addition of two numbers –

 Step 1: Start.
 Step 2: Declare variables a, b and sum.
 Step 3: Read values a and b.
 Step 4: Add a and b and assign the result to sum.
 sum = a + b
 Step 5: Display sum.
 Step 6: Stop.

Algorithm to display your name and college name –

 Step 1: Start.
 Step 2: Declare variables name and college_name.
 Step 3: Read values name and college_name.
 Step 4: Display name and college_name.
 Step 5: Stop.

What are the Characteristics of an Algorithm?


Properties of Algorithm –

 It should terminate after a finite time.


 It should produce at least one output. It should take zero or more input.
 It should be deterministic means giving the same output for the same
input case. Every step in the algorithm must be effective i.e., every step
should do some work.

Advantages of Algorithms–

 It is easy to understand.
 An algorithm is a step-wise representation of a solution to a given problem.
 In Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual program.

Disadvantages of Algorithms–

 Writing an algorithm takes a long time so it is time-consuming.


 Understanding complex logic through algorithms can be very difficult.
 Branching and Looping statements are difficult to show in Algorithms.

Examples:

 Algorithm to find greatest among 3 numbers


o Algorithm 1:
1. Start.
2. Read three numbers A, B, C.
3. Compare A and B.
o If A is greater perform step 4, else step 5.
4. Compare A and C.
o If A is greater, output “A is greatest” else output “C is greatest”.
o Perform Step 6.
5. Compare B and C.
o If B is greater, output “B is greatest” else output “C is greatest”.
6. Stop.
o Algorithm 2:
1. Start.
2. Read the three numbers A, B, C.
3. Compare A and B.
o If A is greater, store A in MAX, else store B in MAX.
4. Compare MAX and C.
o If MAX is greater, output “MAX is greatest” else output “C is
greatest”.
5. Stop.

First algorithm has a greater number of comparisons, whereas in the second


algorithm an additional variable MAX is used.
Control Structures

 Control structures specify the statements to be executed and the order of


execution of statements.
 Three kinds of control structures –
o Sequential
o Selection
o Iterative (loop)
 Flowchart and Pseudo code use control structures for representation.
Flowchart

What is a Flowchart?

 Flowchart is a graphical representation of an algorithm. Programmers


often use it as a program-planning tool to solve a problem.
 It makes use of symbols which are connected among them to indicate the
flow of information and processing.
 The process of drawing a flowchart for an algorithm is known as
“flowcharting”.
 Example:
 Example:
o Draw a flowchart to input two numbers from the user and display
the largest of two numbers.

o Examples:
Basic Symbols used in Flowchart Designs:
Rules For Creating Flowcharts –

 A flowchart is a graphical representation of an algorithm.


 It should follow some rules while creating a flowchart
 Flowchart opening statement must be ‘start’ keyword.
 Flowchart ending statement must be ‘end’ keyword.
 The direction of flow in a flowchart must be from top to bottom and left to
right.
 The relevant symbol must be used while drawing a flowchart.
 All symbols in the flowchart must be connected with an arrow line.
 The decision symbol in the flowchart is associated with the arrow line.








Advantages of Flowchart:

 Flowcharts are a better way of communicating the logic of the system.


 Flowcharts act as a guide for blueprint during program designed.
 Flowcharts help in debugging process.
 With the help of flowcharts programs can be easily analyzed.
 Flowcharts serve as a good proper documentation.
 Easy to trace errors in the software.
 Easy to understand.
 The flowchart can be reused for inconvenience in the future.
 It helps to provide correct logic.

Disadvantages of Flowchart:

 It is difficult to draw flowcharts for large and complex programs.


 There is no standard to determine the amount of detail.
 Difficult to reproduce the flowcharts.
 It is very difficult to modify the Flowchart.
 Making a flowchart is costly.
 Some developer thinks that it is waste of time.
 It makes software development processes low.
 If changes are done in software, then the flowchart must be redrawn.

Pseudo Code

 A Pseudocode is defined as a step-by-step description of an algorithm.


 Pseudocode does not use any programming language in its representation.
 It uses the simple English language text as it is used for human
understanding rather than machine reading.
 Pseudocode is the intermediate state between an idea and its
implementation (code) in a high-level language.

What is the need for Pseudocode?

 Pseudocode is an important part of designing an algorithm, it helps the


programmer in planning the solution to the problem as well as the reader
in understanding the approach to the problem.
 Pseudocode is an intermediate state between algorithm and program that
plays supports the transition of the algorithm into the program.
Programming
What is a Program?

 A program is a set of instructions that tell a computer what to do.


 A computer cannot do anything unless it has a program to tell it what to
do.
 Programs are used to operate the components of a computer, solve
problems or satisfy a want/need.
 Programs can also be called software.

What is a Programming?

 Just like we use Hindi or English to communicate with each other, we use
a Programming language to communicate with the computer.
 Programming is a way to instruct the computer to perform various tasks.

What is a Programming Language?

 A programming language is a set of rules that provides a way of telling a


computer what operations to perform.
 A programming language is a set of rules for communicating an algorithm.
 Series of instructions to a computer to accomplish a task.
o Instructions must be written in a way the computer can
understand.
o Programming languages are used to write programs.

C Programming

 About C Programming
o Procedural Language
 Instructions in a C program are executed step by step.
o Portable
 You can move C programs from one platform to another, and
run it without any or minimal changes.
o Speed
 C programming is faster than most programming languages
like Java, Python, etc.
o General Purpose
 C programming can be used to develop operating systems,
embedded systems, databases, and so on.
o C is a general purpose, procedural, high level programming
language used in the development of computer software and
applications, system programming, games, web development, and
more.
o C language was developed by Dennis M. Ritchie at the Bell
Telephone Laboratories in 1972.
o It is a powerful and flexible language which was first developed for
the programming of the UNIX operating System.

What is C?

 C is one of the most widely used programming language.


 C programming language is known for its simplicity and efficiency.
 It is the best choice to start with programming as it gives you a
foundational understanding of programming.
 The main features of C language include lowlevel access to memory, a
simple set of keywords, and a clean style, these features make C language
suitable for system programming like an operating system or compiler
development.

Why Learn C Programming?

 C helps you to understand the internal architecture of a computer, how


computer stores and retrieves information.
 After learning C, it will be much easier to learn other programming
languages like Java, Python, etc.
 Opportunity to work on open-source projects.
 Some of the largest opensource projects such as Linux kernel, Python
interpreter, SQLite database, etc. are written in C programming.

What are the Most Important Features of C Language?


The main features of the C language include –

 General Purpose and Portable


 Low level Memory Access
 Fast Speed
 Clean Syntax

These features make the C language suitable for system programming like an
operating system or compiler development.

Why Should We Learn C?

 Many languages have borrowed syntax/features directly or indirectly from


the C language.
 Like syntax of Java, PHP, JavaScript, and many other languages are
mainly based on the C language.
 C++ is nearly a superset of C language (Only a few programs may compile
in C, but not in C++).
 So, if a person learns C programming first, it will help him to learn any
modern programming language as well.
 As learning C help to understand a lot of the underlying architecture of
the operating system.
 Like pointers, working with memory locations, etc.

Application of C

C is widely used for developing –

 Operating Systems
 Embedded Systems
 System Software
 Networking Applications
 Database systems
 Gaming
 Artificial Intelligence and machine learning applications
 Scientific applications
 Financial applications

Executing a ‘C’ Program

Executing a program written in C involves a series of steps.

 Creating the program.


 Save it.
 Compile the program.
 Linking the program.
 Run the program.
Beginning with C programming

#include <stdio.h>

int main() {

int a = 10;

printf("%d", a);

return 0;

Output = 10

Let’s analyze the structure of our program line by line.

Structure of the C program

Components

preprocessor directives

global declarations

main ()

local variable deceleration

statement sequences

function invoking

}
Components of a C Program

1. Header Files Inclusion – Line 1 [#include <stdio.h>]

 The first and foremost component is the inclusion of the Header files in a
C program.
 A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source
files.
 All lines that start with # are processed by a preprocessor which is a
program invoked by the compiler.
 E.g., the preprocessor copies the preprocessed code of stdio.h to our file.
o The .h files are called header files in C.
2. Main Method Declaration – Line 2 [int main ()]

 The next part of a C program is to declare the main () function.


 It is the entry point of a C program and the execution typically begins with
the first line of the main ().
 The empty brackets indicate that the main doesn’t take any parameter.
 The int that was written before the main indicates the return type of
main().
o The value returned by the main indicates the status of program
termination.

3. Body of Main Method – Line 3 to Line 6 [enclosed in {}]

 The body of a function in the C program refers to statements that are a


part of that function.
 It can be anything like manipulations, searching, sorting, printing, etc.
 A pair of curly brackets define the body of a function.
o All functions must start and end with curly brackets.

4. Statement – Line 4 [printf(“Hello World”);]

 Statements are the instructions given to the compiler.


 In C, a statement is always terminated by a semicolon (;).
 In this particular case, we use printf() function to instruct the compiler to
display “Hello World” text on the screen.

5. Return Statement – Line 5 [return 0;]

 The last part of any C function is the return statement.


 The return statement refers to the return values from a function.
 This return statement and return value depend upon the return type of
the function.
 The returned value may be used by an operating system to know the
termination status of your program.
 The value 0 typically means successful termination.

Compiler & Interpreter


 Compiler and Interpreter are two different ways to translate a program
from programming language to machine language/code.
 Machine code / language –
o It is the elemental language of computers.
o It is read by the computer's central processing unit (CPU), is
composed of digital binary numbers and looks like a very long
sequence of zeros and ones.

A compiler

 It takes entire program and converts it into object code which is typically
stored in a file.
 The object code is also referred as binary code and can be directly
executed by the machine after linking.

An interpreter

 It directly executes instructions written in a programming or scripting


language without previously converting them to an object code or
machine code.

Executing a ‘C’ Program

 Before going to write sample C programs we need to have a basic


knowledge of the following –
o C’s Character set
o C’s Tokens

C’s Character Set

 The only characters that ‘C’ uses for its programs are –
 Letters: A-Z all alphabets, a-z all alphabets
 Digits: 0-9 digits
 Special Characters: # % & ! _ ,- *+ () & . , : ; ' $ “+ - / * =
 White Spaces: blank space, horizontal tab, new line etc.
‘C’ Tokens

The smallest individual units in a C program are known as tokens

C has 6 different types of tokens –

 Keywords [e.g. float, int, while]


 Identifiers [e.g. sum, amount]
 Constants [e.g. -25.6, 100]
 Strings [e.g. “SMIT”, “year”]
 Special Symbols [e.g. { }, [ ] ]
 Operators [e.g. +, -, *]

Keywords

 Keywords are words that have special meaning to the C compiler.


 Their meaning cannot be changed.
 Serve as basic building blocks for program statements.
 All keywords are written in only lowercase.

Identifiers

 Identifiers are user-defined names to represent parts of the program:


variables, functions, array etc.
 Cannot use C keywords as identifiers.
 Must begin with alpha characters.
 Must consist of only letters, digits or underscore ( _ ).
 Only first 31 characters are significant.
 Must NOT contain spaces ( ).
Constants

 Constants in C are the fixed values that do not change during the
execution of a program.

 Integer Constants
o Refers to sequence of digits.
o Some of the examples are 112, 0551, 56579u, 0X2 etc.
 Real Constants
o The floating-point constants such as 0.0083, -0.78, +67.89 etc.
 Single Character Constants
o A single char const contains a single character enclosed within pair
of single quotes [ ‘ ’ ]
o For example, ‘8’, ‘a’ , ‘i’ etc.
 String Constants
o A string constant is a sequence of characters enclosed in double
quotes [ “ ” ]
o For example, “0211”, “Stack Overflow” etc.

Variables

 A Variable is a data name that is used to store any data value.


 Variables are used to store values that can be changed during the program
execution.
 Rules for representing Variables –
o May only consist of letters, digits and underscores.
o May be as long as you like, but only the first 31 characters are
significant.
o May not begin with a number.
o May not be a C reserved word (keyword).
o Should start with a letter or an underscore(_).
o White spaces are not allowed.
o C is a case sensitive language.
o It matters whether an identifier, such as a variable name, is
uppercase or lowercase.
o Example: area, Area, AREA, ArEa are all seen as different variables
by the compiler.
 Some examples of valid variable –
o Sum, value, num1, total_marks.
 Some examples of invalid variables –
o 123, 1rollno, (area)

Data Types

Data Type Size Description

int 2 or 4 bytes Stores whole numbers, without decimals

Stores fractional numbers, containing one or


float 4 bytes more decimals.
Sufficient for storing 6-7 decimal digits

Stores fractional numbers, containing one or


double 8 bytes more decimals.
Sufficient for storing 15 decimal digits

Stores a single character/letter/number, or


char 1 byte
ASCII values
Operators

 Operators are used to perform operations on variables and values.


 The symbols which are used to perform logical and mathematical
operations are called operators.
 E.g.: A + B * 5
 where, +, * are operators,
o A, B are variables, 5 is constant,
o A + B * 5 is an expression.

Types of C Operators

 C divides the operators into the following groups:


o Arithmetic operators
o Assignment operators
o Comparison operators
o Logical operators
o Bitwise operators
 Arithmetic Operators
o Used to perform common mathematical operations.

Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y


Operator Name Description Example

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x

 Assignment Operators
o Assignment operators are used to assign values to variables.

Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3


 Comparison Operators
o Comparison operators are used to compare two values or variables.
o This is important in programming, because it helps us to find
answers and make decisions.
o The return value of a comparison is either 1 or 0, which means true
(1) or false (0).
o These values are known as Boolean values.

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

 Logical Operators
o You can also test for true or false values with logical operators.
o Logical operators are used to determine the logic between variables
or values.

Operator Name Description Example

Returns true if both statements


&& Logical and x < 5 && x < 10
are true

Returns true if one of the


|| Logical or x < 5 || x < 4
statements is true

Reverse the result, returns false !(x < 5 && x <


! Logical not
if the result is true 10)

 Sizeof Operator
o The memory size (in bytes) of a data type or a variable can be found
with the sizeof operator
Managing Input and Output operations

Input and Output

 A program without any input or output has no meaning.

Input  Process  Output

 Eg. marks sheet of students

Input –

 Provide the program with some data to be used in program.

Output –

 Display data on screen as result or information.

Input functions are used to read data from keyboard are called standard input
functions.

 scanf(), getchar(), getche(), getch() etc.

Output functions are used to display the result on the screen are called standard
output functions.

 printf(), putchar(), putch(), puts() etc.

In C, the standard library stdio.h provides functions for input and output.

The instruction #include<stdio.h> tells the compiler to search for a file named
stdio.h and places its contents at this point in the program.

The input/output functions are classified into two types –

1. Formatted Input/Output functions


2. Unformatted Input/Output functions
Formatted Input/Output functions

 Formatted console input/output functions are used to take a single or


multiple inputs from the user at console and it also allows us to display
one or multiple values in the output to the user at the console.
 Format specifiers allow us to read or display any value of a specific data
type.
 Input function: scanf()
 Output function: printf()

Formatted Input

 Consider the following data:


o 50  Int,
o 13.45  float,
o Ram  char variables.
 The built-in function scanf() can be used to enter input data into the
computer from a standard input device.
o This function is used to read one or multiple inputs from the user.
 Example: scanf(“ %d", &n1);
 The general form of scanf is,
o scanf(“control string” , arg1, arg1,….. argn);
 Control string: format in which data is to be entered.
 arg1,arg2… : location where the data is stored.
preceded by ampersand (&)

You might also like