0% found this document useful (0 votes)
72 views72 pages

PPA Unit - 1

Uploaded by

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

PPA Unit - 1

Uploaded by

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

BCA First Year

(First
Semester)
Subject Code: Subject Name- Faculty Name-
BCA-102 Programming Mr. Naveen
Principle & Kumar
Algorithm(PPA)
Program Outcomes(Pos)

PO1 : Students will be Able to Understand and apply mathematical foundation, computing and domain knowledge to solve
complex computing problems

PO2: Students will be Able to understand the nature, scope and application of computer and computer languages.

PO3: Students will be Able to develop essential skills to create interactive software as demanded by the global software
industry.

PO4: Students will be able to develop Professional ethics and human values to serve the society.

PO5: Students will be Able to Develop Leadership skills and Communicate effectively with the computing community as
well as society.

PO6: Students will be Able to use innovative ideas to be entrepreneur in the field of computer applications.
Course Outcomes(Cos)
CO1: Understand fundamental constructs of C.
CO2: Get the knowledge of Operators and function in C language.
CO3: Utilize C language Constructs to implement control Structures and loops.
CO4: Problem solving techniques (Trail & Error, Divide & Conquer).
CO5: Solve the Simple Arithmetic Problems.
CO6: Basic types of function, Declaration and definition, Function call, Types of function, Parameter passing.
Syllabus
BCA-102 PROGRAMMING PRINCIPLE & ALGORITHM
Unit – I: Introduction to ‘C’ Language and its fundamental:
History, Structures of ‘C’ Programming, Function as building blocks, Character set, C Tokens, Keywords,
Identifiers, Variables, Constant, Data Types, Comments.
Unit – II: Operators, Build in Operators and function:
Types of operators, Precedence and Associativity, Expression, Statement and types of statements Console
based I/O and related built in I/O function: printf(), scanf(), getch(), getchar(), putchar(); Concept of header
files, Preprocessor directives: #include, #define.
Unit – III: Control structures:
Decision making structures: If, If-else, Nested If-else, Switch; Loop Control structures: While, Do-while,
for, Nested for loop; Other statements: break, continue, goto, exit.
Unit– IV: Introduction to problem solving:
Concept: problem solving, Problem solving techniques, Steps in problem solving (Define Problem, Analyze
Problem, Explore Solution), Algorithms and Flowcharts (Definitions, Symbols), Characteristics of an
algorithm Conditionals in pseudo-code, Loops in pseudo-code Time complexity: Big-Oh notation,
efficiency Simple Examples: Algorithms and flowcharts (Real Life Examples).
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Syllabus Cont...
BCA-102 PROGRAMMING PRINCIPLE & ALGORITHM

Unit – V: Simple Arithmetic Problems:


Addition / Multiplication of integers, Determining if a number is +ve / -ve / even / odd, Maximum of 2
numbers, 3 numbers, Sum of first n numbers, given n numbers, Integer division, Digit reversing, Table
generation for n, ab , Factorial, sine series, cosine series, nCr , Pascal Triangle, Prime number, Factors of a
number, Other problems such as Perfect number, GCD numbers etc (Write algorithms and draw flowchart),
Swapping
Unit – VI: Functions:
Basic types of function, Declaration and definition, Function call, Types of function, Parameter passing,
Call by value, Call by reference, Scope of variable, Storage classes, Recursion.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
What is Programming
language?
A programming language is a set of commands, instructions, and other syntax used
to create a software program.
Programming languages are of two types:
a) High-level programming language: Languages that programmers used to
write code are known as High-level Languages. These are designed to be easy
to read and understand. Examples - C, C++, Java, Perl, Python, PHP etc.
b) Low-level programming language:
This code can be compiled into low-level language which is recognized
directly by the computer hardware. It includes assembly language and machine
languages.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
GENERAL PURPOSE PROGRAMMING LANGUAGE:
● Before the development of computer or programming, people did their jobs manually. It
used to take a lot of time but they had no choice. Then the computer era came, and now
the jobs to be done were fed on the system. It considerably reduced the amount of time
taken for the completion of the same task.
● Many different categories of programming languages came into existence based on the
needs of the programmer or the purpose of program development.
● Some of them were found efficient for a wide range of purpose, some for specific. Hence
the programming languages based on purpose were categorized as: General purpose and
domain specific programming languages.
● Some programming languages are designed specially to suit or a meet a particular need,
they are called as Domain specific programming languages.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
But while learning about general purpose programming languages, it is important to note that every
language has its specialty or a domain in which it is the best. Such as Python is best as a scripting
language, C++ is best for system programming.
A list of various general purpose programming languages:

● Java
● Ruby
● Python
● C
● C++
● C#
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Example:
● FORTAN and APL are suitable for programming related to mathematical purpose.
● C is believed to be suitable only for system programs.
● ML, OCAML, Haskell are appropriate for research work.
● Lisp is favorable for AI related work.
● Java can be used for developing interactive web-pages as well as making games.
● C++ can be used for writing applications as well as developing system programs.
● Python, Perl, Ruby can be used for web programming as well as development of desktop
applications.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Introduction to C:
● C language is a general-purpose, high-level programming language developed
at AT&T's Bell Laboratories of USA in 1972.
● It was designed and written by a man named Dennis Ritchie.
● It is a general-purpose and structured programming.
● It is so popular language because it is reliable, simple, and easy to use.
● It is well-suited for business and scientific applications.
● Many later languages have borrowed syntax or features directly or indirectly
from the C language.
● It has some features like Control Structures, Looping statements, Arrays, etc.
● C language is called Compiled-Languages since the source code must be first
compiled in order to run.
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Facts about C:
● C was invented to write an operating system called UNIX.
● C is a successor of B language which was introduced around the early 1970s.
● The language was formalized in 1988 by the American National Standard Institute
(ANSI).
● The UNIX OS was totally written in C.
● Today C is the most widely used and popular System Programming Language.
● Most of the state-of-the-art software have been implemented using C.

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Rules of C language:
● C is a case sensitive language due to ASCII code, so all C instructions must be written in
lower case letter.
● All C statement must end with a semicolon except some functions including void main()
and header files etc. For example: main is not the same as MAIN.
● Whitespace is used in C to describe blanks and tabs. Whitespace is required between
keywords and identifiers.
● When we write a function, its body is enclosed in curly braces, like for the main()
function. We will learn this in detail when we cover functions.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
History of C Language:
● C programming language was developed to overcome the difficulties found in older
programming languages such as BCPL, BASIC, B, etc.

Programming Language Development Year


Regional Assembly Language 1951

Autocode 1952

IPL (forerunner to LISP) 1954

FLOW-MATIC (led by COBOL) 1955

FORTRAN (First compiler) 1957

COMTRAN (precursor to COBOL) 1957

LISP 1958

Session Subject Code: BCA-102 Programming principle and Algorithm


Mr. Naveen Kumar
2024-25
History of C Language:

Programming Language Development Year


ALGOL 58 1958

FACT (forerunner to COBOL) 1959

COBOL 1959

RPG 1959

APL 1962

Simula 1962

SNOBOL 1962

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
History of C Language:

Programming Language Development Year


CPL (forerunner to C) 1963

Speakeasy (computational environment) 1964

BASIC 1964

PL/I 1964

JOSS 1966

BCPL (forerunner to C) 1967

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Why we learn C programming?

● C language is the most commonly used in programming language. It is case


sensitive language.
● It is used for writing operating systems.
● UNIX was the first operating system written in C.
● Later Microsoft Windows, Mac OS X, and GNU/Linux were all written in C.
● C programming language is a MUST for students and working professionals to
become a great Software Engineer specially when they are working in
Software Development Domain.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Why we learn C programming?
Some of the key advantages of learning C Programming:

● Easy to learn, robust language which can be used to write any complex
program.
● It produces efficient programs, and highly portable that means C program
Written from one computer can be run modification.
● Structured language.
● It can handle low-level activities.
● It can be compiled on a variety of programs.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Applications of C Programming:
C was initially used for system development work, particularly the programs that make-up the
operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as the code written in assembly language.
Some examples of the use of C are -
● Operating Systems
● Language Compilers
● Assemblers
● Text Editors
● Modern Programs
● Databases
● Language Interpreters
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Beginning with C programming- Structure of ‘C’ Programming:
By structure, it is meant that any program can be written in this structure only. Writing a C program in any
other structure will hence lead to a Compilation Error.

Documentation Section

Link Section

Definition Section

Global Declaration Section

main( ) Function Section


{
Declaration
part Executable
part
}
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
❖ Documentation section consists of a set of comment lines giving the name of the
program, the author and other details, which the programmer would like to use later.
❖ The link section provides the instructions to the compiler to link the functions from the
system library.
❖ The definition section defines all symbolic constants.
❖ There are some variables that are used in more than one function. Such variables are
called Global variables. These are declared in the global declaration section that is
outside of all the functions and it also declares all the user-defined functions.
❖ Every C program must have one main() function section. This section contains two
parts:
a) Declaration part- It declares all the variables used in the executable part
b) Executable part- There is at least one statement in this part.
These two part must appear between the opening and closing braces.
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
❖ The program execution begins at the opening brace and ends at the closing
brace.
❖ The closing brace of the main function section is the logical end of the
program.
❖ All the statements in the declaration and executable part ends with a
semicolon.
❖ The subprogram section contains all the user-defined functions that are called
main function. User-defined functions are generally placed immediately after
the main function.
❖ All sections, except main function section may be absent when they are not
required.
Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Hello World using C Programming:

A C program basically consists of the following parts −


● Preprocessor Commands
● Functions
● Variables
● Statements & Expressions
● Comments

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Hello World using C Programming:

#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Hello World in C programming”);
getch();
}
● Instead of getch() we can also use return 0.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Hello World using C Programming:
❖ First line of the program #include <stdio.h> is a preprocessor command, which tells a C
compiler to include stdio.h file before going to actual compilation.
❖ Second line of the program #include <conio.h> is a preprocessor command which tells
stands for Console-Input-Output. It is non-standard header file used in C.
❖ void main() is the main function where the program execution begins.
❖ /*...*/ will be ignored by the compiler and it has been put to add additional comments in
the program. So such lines are called comments in the program.
❖ printf(“...”) is another function available in C which causes the message "Hello, World!"
to be displayed on the screen.
❖ return 0; terminates the main() function and returns the value 0.
❖ getch() is a function which is used to get a character.
Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Definition of Compiler:

Compiler is a translator which takes input i.e., High-Level Language, and produces an
output of low-level language i.e. machine or assembly language.

● A compiler is more intelligent than an assembler it checks all kinds of limits,

ranges, errors, etc.


● But its program run time is more and occupies a larger part of memory.

Session
2024-25 Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
Definition of Compiler:

It has slow speed because a compiler goes through the entire program and then translates
the entire program into machine codes.

Object
Source Compiler Program
Program

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Definition of Interpreter:
An interpreter is a program that translates a programming language into a comprehensible language.

● It translates only one statement of the program at a time.

● Interpreters, more often than not are smaller than compilers .

Pre-Processing Processing
Source Intermediate Interpreter
Program code

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Difference between getch and return:
❖ getch is a function that will wait for input and then read this input. It is often
used at the end of programs to make sure the window you run it in doesn't close
immediately. You can however use it to read input.
❖ return is a statement used to return a value at the end of a function. If you
define a function yourself you will use this to decide what the result of that
function should be. In the main function it is used to give feedback to the caller,
meaning the program that started your program. This is often the command line
or the IDE. If you return 0 at the end and this value is interpreted, 0 means
"everything is OK". Returning a different value is often interpreted as an error,
so this is a method to get error codes.
Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Compile and Execute C Program:

● Open a Turbo C software and click on FILE then NEW.


● Write a source code program.
● Save the file as hello.c.
● Run/Execute the code.
● You will see the output "Hello World" printed on the console
screen.

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Some concepts in programming:
A C program can vary from 3 lines to millions of lines and it should be written into one or
more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any other
text editor to write your C program into a file. Text Editor: This will be used
to type your program. Few examples include: Windows Notepad, OS Edit command, Brief,
Epsilon, and vim or vi. The name and version of text editors can vary on different operating
systems. For example, Notepad will be used on Windows, and vim or vi can be used on
windows as well as on Linux or UNIX. The C Compiler: The source code written in
source file is the human readable source for your program. It needs to be "compiled", into
machine language so that your CPU can actually execute the program as per the instructions
given.
The compiler compiles the source codes into final executable programs.
Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Character set:- The character set is the fundamental raw material of any
language and they are used to represent information. Like natural languages,
computer language will also have well defined character set, which is useful to
build the programs.
•It denotes any alphabet, digit or special symbol used to represent information.
•These characters can be combined to form variables. C uses constants, variables,
operators, keywords and expressions as building blocks to form a basic C program.

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
The characters in C are grouped into the following two
categories:
1. Source character set:
a. Alphabets
b. Digits
c. Special Characters
d. White Spaces
2. Execution character set:
a. Escape Sequence

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Functions as building block in
C:
● C Programming is known as procedure-oriented programming language.
It contains different procedures or functions which accomplish some
specific task.
● Any C program must contain a function named main(), which actually acts
as an entry point or it is the first program to be executed.
● Functions can share and exchange some data.
● Functions should be made to achieve one specific task.
● One function should not be assigned to many functionalities. This is
considered as bad programming practice.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Functions as building block in C:

● The concept of giving only one task to a function is known as cohesion.


● Functions can shared data by passing arguments and returning values.
● Passing data between functions is known as coupling i.e. concept of
joining two function.
● Function are of two types:
a) User-defined functions
b) Library functions

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Tokens in C:
a) A C program consists of various tokens and a token is either a keyword, an identifier, a
constant, a string literal, or a symbol.
b) For example, the following C statement consists of five tokens −

printf("Hello, World! \n");


c) The individual tokens are
printf
(
"Hello, World! \n"
)
;

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Tokens in C:
d) Semicolons: The semicolon is a statement terminator.
printf("Hello, World!”); \
return 0;
n");
e) Comments:Comments are like helping text in your C program and they are ignored by
the compiler. They start with /* and terminate with the characters */ as shown below −
/* my first program in C */

You cannot have comments within comments and they do not occur within a string or
character literals.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Identifiers:
A C identifier is a name used to identify a variable, function, or any other user-defined item.
An identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more
letters, underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-
sensitive programming language.
Here are some examples of acceptable identifiers −
Abc xyz_231 car_name _temp c f45d2 hatVal

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Keywords:
Also called Reserved Words. These reserved words may not be used as constants or variables
or any other identifier names.
auto float double unsigned _Packed
break extern const typedef union
case for long sizeof
enum goto register static
char if int continue typedef return struct while
else default short signed volatile void
switch do

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Constants (or Literals) in C :
A name given to a variable whose values cannot be changed such variables is known as
constants. These are also called as literals in C programming language as they are similar
To variables except that their value never changes during execution once defined.

Constants can be any of the data types.


Syntax:
const type constant_name;
const keyword defines a constant in C.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
#include<stdio.h>
#include<conio.h>
void main()
{
//Putting int const side = 10; or const int side = 10 are same; const int side=10;
int area;
area=side*side;
printf("The area of the square with side: %d is: %d sq. units", side, area); getch();
}

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Types of Constants (or Literals) in C:

Two major categories are:


a)Primary constants: First type is numeric constants which further differentiate as an
integer constants, real constants, and second type are character constants etc.

b)Secondary constants: In secondary constants, we have Array, structure, union, pointer,


etc.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Integer constants and Real constants:
Integer Constant: It's referring to a sequence of digits. Integers are of three types viz:
a) Decimal Integer
b) Octal Integer
c) Hexadecimal Integer
Example:
15, -265, 0, 99818, +25, 045, 0X6

Real Constant: The numbers containing fractional parts are called real or floating points

constant. Example: 8.475, -57.452...etc

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Single character constants:

It simply contains a single character enclosed within ' and ' (a pair of single quote). It is to be
noted that the character '8' is not the same as 8. Character constants have a specific set of integer
values known as ASCII values (American Standard Code for Information Interchange).
Example:
'X', '5', ';'

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
String constants:

These are a sequence of characters enclosed in double quotes, and they may include letters,
digits, special characters, and blank spaces. It is again to be noted that "M" and 'M' are different -
because "M" represents a string as it is enclosed within a pair of double quotes whereas 'M'
represents a single character.
Example:
"HelloWorld!", "2015", "2+1",

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Backslash character constants:

C supports some character constants having a backslash in front of it. The lists of backslash
characters have a specific meaning which is known to the compiler. They are also termed as
"Escape Sequence".
The figure is given on next page.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Escape Sequence Characters in C:

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
1. \n (New Line): It is used to create a new line and place the cursor there.
2. \t (Horizontal Tab): This is the escape sequence for the horizontal tab.
3.\b (Back Space): This is the escape sequence for backspace. A word that precedes \b’ will be
removed.
4.\r (Carriage Return): This is the escape sequence to position the cursor at the beginning of
the line.
5.\a (Audible bell): This is the escape sequence to generate a bell sound to denote the
execution of the program.
6.\’ (Printing single quotation): This escape sequence is used to print the single quotation
mark.
7.\” (printing double quotation): This escape sequence is used to print the single quotation
mark.
Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
8. \? (Question Mark Sequence): This escape sequence is used to print the question mark(?).
9. \\ (Back Slash): This escape sequence is used to print the backslash (\).
10. \f (Form Feed): This escape sequence is used for a form feed.
11. \v (Vertical Tab): This is used to print the vertical tab.
12. \0 (Null Value): This is used to print null value. The statement after \0 will be omitted.
13. \nnn (Print octal value): This is used to print the octal value equivalent character.
14. \xhh(Print Hexadecimal value): This sequence is used to print the hexadecimal value.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Comments in C:
Comments in C language are used to provide information about lines of code. Mainly, it is used
for documenting code. There are 2 types of comments in the C language.

1. Single Line Comments


2. Multi-Line Comments

Single Line Comments: They are represented by double slash // .

Multi-Line Comments: They are represented by slash asterisk /* ... */. It can occupy many lines
of code, but it can't be nested.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Single line comments in C:
// Program of printing the statement “This is a C program” #include<stdio.h>

#include<conio.h> void main()

printf("This is a C program"); //printing information

getch();
}

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Multi line comments in C: #include<stdio.h>
#include<conio.h>
Syntax:-
void main()
/*
{
Code
/*printing information Multi-Line
to be commented
Comment*/
*/
printf("Hello C");

getch();
}
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Variables in C:
● A variable is a name of the memory location. It is used to store data.
● Its value can be changed, and it can be reused many times. It is a way to represent memory
location through symbol so that it can be easily identified.
● Syntax: type variable_list;
type means data_type, variable_list is made through characters.
● Examples of variables:
int a;
float b;
char c;

where int, float and char are data types and a,b,c are variables.
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Rules for defining variables in C:
● A variable can have alphabets, digits, and underscore.
● A variable name can start with the alphabet, and underscore only. It can't start with a digit.
● No whitespace is allowed within the variable name.
● A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Valid/Correct variable names:

1. int a;
2. int _ab;
3. int a30;
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Some more examples of Variables in C:
Invalid/Wrong variable names:

a) int 2;
b) int a b;
c) int long;

We can also provide values while declaring the variables as:

● int a=40,b=50; //declaring 2 variable of integer type


● float f=30.25;
● char c='K';

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Types of Variables in C:
Many types of variables in c:

a) Local variable
b) Global variable
c) Static variable
d) Automatic variable
e) External variable

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Local variable in C:
A variable that is declared inside the function or block is called a local variable.

It must be declared at the start of the block.

void function()
{
int x=10;//local variable
}

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Global variable in C:
A variable that is declared outside the function or block is called a global variable. Any function can
change the value of the global variable. It is available to all the functions.

It must be declared at the start of the block.

int value=30; //global variable

void function(){

int x=10;//local variable

Session Programming principle and Algorithm


Subject Code: BCA-102 Mr. Naveen Kumar
2023-24
Static variable in C:
A variable that is declared with the static keyword is called static variable. It retains its value
between multiple function calls.

void function1(){
int x=10;//local variable

static int y=10;//static variable

x=x+1;

y=y+1; printf("%d,

%d",x,y); }
Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Static variable in C:

Note: If you call the previous function many times, then the local variable will print the same
value for each function call, e.g, 21,21,21 and so on. But the static variable will
print the incremented value in each function call, e.g. 11, 12, 13 and so on.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Automatic variable in C:

All variables in C that are declared inside the block, are automatic variables by default. We can
explicitly declare an automatic variable using auto keyword.

void main(){

int x=10; //local variable (also automatic)

auto int y=20; //automatic variable


}

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
External variable in C:
We can share a variable in multiple C source files by using an external variable. To declare an
external variable, you need to use extern keyword.

Syntax: extern int x=10;//external variable (also global)

#include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", global_variable);
}
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Data Types in C:
A data type specifies the type of data that a variable can store such as integer, floating, character,
etc.

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Data Types in C:
Basic Data Type: int, char, float, double

Derived Data Type: array, pointer, structure, union

Enumeration Data: Type, enum

Void Data Type: void

The basic data types are integer-based and floating-point based. C language supports both
signed and unsigned literals.

The memory size of the basic data types may change according to 32 or 64-bit operating system.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Data Types in C: According to basic data types, size is given according to 32-bit architecture
below:
Data Types Memory Size Range

char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255

short 2 byte −32,768 to 32,767

signed short 2 byte −32,768 to 32,767

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
More Data Types in C:
unsigned short 2 byte 0 to 65,535

int 2 byte −32,768 to 32,767

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535

short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
More Data Types in C:
unsigned short int 2 byte 0 to 65,535

long int 4 byte -2,147,483,648 to 2,147,483,647

signed long int 4 byte -2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

float 4 byte

double 8 byte

long double 10 byte

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
C Format Specifier:
This is a string used in the formatted input and output functions. The format string determines
the format of the input and output. The format string always starts with a '%' character.

%d or %i - It is used to print the signed integer value where signed integer means that the

variable can hold both positive and negative values.

%f - It is used for printing the decimal floating-point values. By default, it prints the 6 values
after '.'.

%c - Character Format Specifier

%s - It is used to print the strings.

%p - It is used to print the address in a hexadecimal form.


Session
. Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
One Example of %d:
int main()

int b=7; int c=6;

printf(“ Value of b is:%d", b);

printf("\n Value of c is:%d",c);

return 0;

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
C Input and Output:

There are three essential functions of a computer are reading, processing and writing data. Majority of

the programs take data as input, and then after processing the processed data is being displayed which

is called information. In C programming you can use scanf() and printf() as predefined function to read

and print data.

Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
C Input and Output:
#include<stdio.h>

#include<conio.h>

void main(){

int a,b,c;

printf("Please enter any two numbers: \n");

scanf("%d %d", &a, &b);

c = a + b;

printf("The addition of two number is: %d",

c); getch(); }
Session
Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Managing Input/Output:
I/O operations are useful for a program to interact with users. stdlib is the standard C library for
input-output operations. While dealing with input-output operations in C, two important streams play
their role. These are:
a) Standard Input (stdin)
b) Standard Output(stdout)

Standard input or stdin is used for taking input from devices such as the keyboard as a data stream.

Standard output or stdout is used for giving output to a device such as a monitor. For using I/O

functionality, programmers must include stdio header-file within the program.

Session Subject Code: BCA-102 Programming principle and Algorithm Mr. Naveen Kumar
2024-25
Unit-1 Complete

You might also like