0% found this document useful (0 votes)
173 views20 pages

BCA Ist Sem. Unit 1st Notes ..

The document discusses various topics related to algorithms and C language including: 1. It provides an example algorithm to add two numbers in pseudocode and C code format. 2. It describes common flowchart elements and symbols used to represent algorithms such as processes, inputs/outputs, and decisions. 3. It explains some basic tokens in C like keywords, identifiers, strings, operators, constants, and special characters.

Uploaded by

romje
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)
173 views20 pages

BCA Ist Sem. Unit 1st Notes ..

The document discusses various topics related to algorithms and C language including: 1. It provides an example algorithm to add two numbers in pseudocode and C code format. 2. It describes common flowchart elements and symbols used to represent algorithms such as processes, inputs/outputs, and decisions. 3. It explains some basic tokens in C like keywords, identifiers, strings, operators, constants, and special characters.

Uploaded by

romje
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/ 20

Unit = 1st

Algorithm in C Language

Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain


order to get the desired output. Algorithms are generally created independent of underlying languages,
i.e. an algorithm can be implemented in more than one programming language.

Example :-

Problem − Design an algorithm to add two numbers and display the result.

Step 1 − START

Step 2 − declare three integers a, b & c

Step 3 − define values of a & b

Step 4 − add values of a & b

Step 5 − store output of step 4 to c

Step 6 − print c

Step 7 – STOP

Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written
as −

Step 1 − START ADD

Step 2 − get values of a & b

Step 3 − c ← a + b

Step 4 − display c

Step 5 – STOP

Flowchart Elements :-

Flowchart is a diagrammatic representation of sequence of logical steps of a program. Flowcharts use


simple geometric shapes to depict processes and arrows to show relationships and process/data flow.

Flowchart Symbols

Here is a chart for some of the common symbols used in drawing flowcharts.
Symbol Symbol Name Purpose

Used at the beginning and end of the algorithm to show start


Start/Stop
and end of the program.

Indicates processes like mathematical operations.


Process

Used for denoting program inputs and outputs.


Input/ Output

Stands for decision statements in a program, where answer is


usually Yes or No.
Decision

Shows relationships between different shapes.


Arrow

Example Flowcharts :-
start

input a/b

c=a+b

print c

End

#include <stdio.h> includes the standard input


output library functions. The printf() function is
defined in stdio.h .

int main() The main() function is the entry point of


every program in c language.

printf() The printf() function is used to print data on


the console.

return 0 The return 0 statement, returns execution


status to the OS. The 0 value is used for successful
execution and 1 for unsuccessful execution.

printf() and scanf() in C

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt
library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

printf("format string",argument_list);

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.


scanf() function

The scanf() function is used for input. It reads the input data from the console.

scanf("format string",argument_list);

Program to print cube of given number

Let's see a simple example of c language that gets input from the user and prints the cube of the given
number.

#include<stdio.h>

int main(){

int number;

printf("enter a number:");

scanf("%d",&number);

printf("cube of number is:%d ",number*number*number);

return 0;

Output

enter a number:5

cube of number is:125

The scanf("%d",&number) statement reads integer number from the console and stores the given value
in number variable.

The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on
the console.

Program to print sum of 2 numbers :-


Let's see a simple example of input and output in C language that prints addition of 2 numbers.

#include<stdio.h>

int main(){

int x=0,y=0,result=0;

printf("enter first number:");

scanf("%d",&x);

printf("enter second number:");

scanf("%d",&y);

result=x+y;

printf("sum of 2 numbers:%d ",result);

return 0;

Tokens in C :-

Tokens in C is the most important element to be used in creating a program in C. We can define the
token as the smallest individual element in C. For `example, we cannot create a sentence without using
words; similarly, we cannot create a program in C without using tokens in C. Therefore, we can say that
tokens in C is the building block or the basic component for creating a program in C language

Classification of tokens in C

Tokens in C language

can be divided into the following categories:


Keywords in C

Identifiers in C

Strings in C

Operators in C

Constant in C

Special Characters in C

Let's understand each token one by one.

Keywords in C

Keywords in C

can be defined as the pre-defined or the reserved words having its own importance, and each keyword
has its own functionality. Since keywords are the pre-defined words used by the compiler, so they
cannot be used as the variable names. If the keywords are used as the variable names, it means that we
are assigning a different meaning to the keyword, which is not allowed. C language supports 32
keywords given below:

auto double int struct


break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

Identifiers in C

Identifiers in C

are used for naming variables, functions, arrays, structures, etc. Identifiers in C are the user-defined
words. It can be composed of uppercase letters, lowercase letters, underscore, or digits, but the starting
letter should be either an underscore or an alphabet. Identifiers cannot be used as keywords. Rules for
constructing identifiers in C are given below:

The first character of an identifier should be either an alphabet or an underscore, and then it can be
followed by any of the character, digit, or underscore.

It should not begin with any numerical digit.

In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers
are case sensitive.

Commas or blank spaces cannot be specified within an identifier.

Keywords cannot be represented as an identifier.

Identifiers should be written in such a way that it is meaningful, short, and easy to read.

Strings in C

Strings in C
are always represented as an array of characters having null character '\0' at the end of the string. This
null character denotes the end of the string. Strings in C are enclosed within double quotes, while
characters are enclosed within single characters. The size of a string is a number of characters that the
string contains.

Now, we describe the strings in different ways:

char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a' array.

char a[] = "javatpoint"; // The compiler allocates the memory at the run time.

char a[10] = {'j','a','v','a','t','p','o','i','n','t','\0'}; // String is represented in the form of characters.

Constants in C

A constant is a value assigned to the variable which will remain the same throughout the program, i.e.,
the constant value cannot be changed.

There are two ways of declaring constant:

Using const keyword

Using #define pre-processor

Special characters in C

Some special characters are used in C, and they have a special meaning which cannot be used for
another purpose.

Square brackets [ ]: The opening and closing brackets represent the single and multidimensional
subscripts.

Simple brackets ( ): It is used in function declaration and function calling. For example, printf() is a pre-
defined function.

Curly braces { }: It is used in the opening and closing of the code. It is used in the opening and closing of
the loops.

Comma (,): It is used for separating for more than one statement and for example, separating function
parameters in a function call, separating the variable when printing the value of more than one variable
using a single printf statement.

Hash/pre-processor (#): It is used for pre-processor directive. It basically denotes that we are using the
header file.

Asterisk (*): This symbol is used to represent pointers and also used as an operator for multiplication.

Tilde (~): It is used as a destructor to free memory.


Period (.): It is used to access a member of a structure or a union.

Operators in C

Operator

C operator is a symbol that is used to perform mathematical or logical manipulations.

Arithmetic Operators

Increment and Decrement Operators

Relational Operators

Logical Operators

Bitwise Operators

Assignment Operators

Arithmetic Operators

There are following arithmetic operators supported by C++ language −


Assume variable A holds 10 and variable B holds 20, then −

Show Examples

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

/ Divides numerator by de-numerator B / A will give 2

% Modulus Operator and remainder of after B % A will give 0


an integer division

Relational Operators

There are following relational operators supported by C++ language

Assume variable A holds 10 and variable B holds 20, then −

Show Examples

Operator Description Example

== Checks if the values of two operands are (A == B) is not true.


equal or not, if yes then condition becomes
true.

!= Checks if the values of two operands are (A != B) is true.


equal or not, if values are not equal then
condition becomes true.

> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand, if yes then
condition becomes true.
< Checks if the value of left operand is less (A < B) is true.
than the value of right operand, if yes then
condition becomes true.

>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand,
if yes then condition becomes true.

<= Checks if the value of left operand is less (A <= B) is true.


than or equal to the value of right

Logical Operators

There are following logical operators supported by C++ language.

Assume variable A holds 1 and variable B holds 0, then −

Show Examples

Operator Description Example

&& Called Logical AND operator. If both the (A && B) is false.


operands are non-zero, then condition
becomes true.

|| Called Logical OR Operator. If any of the (A || B) is true.


two operands is non-zero, then condition
becomes true.

! Called Logical NOT Operator. Use to !(A && B) is true.


reverses the logical state of its operand. If a
condition is true, then Logical NOT operator
will make false.

Increment/decrement operators

Increment/decrement operators increment or decrement the value of the object.


Operator name Syntax

pre-increment ++a

post- increment a++

pre-decrement --a

post-decrement a--

Assignment Operators

It is used to assign a particular value to a variable. We will discuss it in detail in the later section with its
shorthand notations.

= (Assignment)- Used to assign a value from right side operand to left side operand.

+= (Addition Assignment)- To store the sum of both the operands to the left side operand.

-= (Subtraction Assignment) – To store the difference of both the operands to the left side operand.

*= (Multiplication Assignment) – To store the product of both the operands to the left side operand.

/= (Division Assignment) – To store the division of both the operands to the left side operand.

%= (Remainder Assignment) – To store the remainder of both the operands to the left side operand.

Table for Assignment Operators in C and C++

Operator Operand Operation Elucidation

= a, b a=b Used to assign a value from right side operand to left side operand

+= a, b a+=b a=a+b: The value of a+b is stored in a

-= a, b a-=b a=a-b: The value of a-b is stored in a

*= a, b a*=b a=a*b: The value of a*b is stored in a


/= a, b a/=b a=a/b: The value of a/b is stored in a

%= a, b a%=b a=a %b: The value of a%b is stored in a

Bitwise Operators

It is based on the principle of performing operations bit by bit which is based on Boolean algebra. It
increases the processing speed and hence the efficiency of the program.

The Bitwise Operators in C/C++ Includes –

& (Bitwise AND) – Converts the value of both the operands into binary form and performs AND
operation bit by bit.

| (Bitwise OR) – Converts the value of both the operands into binary form and performs OR operation
bit by bit.

^ (Bitwise exclusive OR) – Converts the value of both the operands into binary form and performs
EXCLUSIVE OR operation bit by bit.

~ (One’s complement operator): Converts the operand into its complementary form.

<< – Left shift

>> – Right shift

C Expressions

An expression is a formula in which operands are linked to each other by the use of operators to
compute a value. An operand can be a function reference, a variable, an array element or a constant.

Let's see an example:

a-b;

Data Types :- (1) Primary (Built in Data type) :-

char: The most basic data type in C. It stores a single character and requires a single byte of memory in
almost all compilers.
A,b,c………..

int: As the name suggests, an int variable is used to store an integer.

1,2,3…………

float: It is used to store decimal numbers (numbers with floating point value) with single precision.

3.4….

double: It is used to store decimal numbers (numbers with floating point value) with double precision.

Void

(2) Derived Data Types:-

Array , Function, Pointer (3) User define data types:-

Structure, Union,

Data Types format string Memory Size Range

Char %C 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

unsigned short 2 byte 0 to 65,535

int %d 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

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 %f 4 byte

double 8 byte

long double 10 byte

What is Program Debugging ?


Programming Errors in C

Errors are the problems or the faults that occur in the program, which makes the behavior of the
program abnormal, and experienced developers can also make these faults. Programming errors are
also known as the bugs or faults, and the process of removing these bugs is known as debugging.

Error :- Three types

Syntax error
Syntax errors are also known as the compilation errors as they occurred at the compilation time, or we
can say that the syntax errors are thrown by the compilers. These errors are mainly occurred due to the
mistakes while typing or do not follow the syntax of the specified programming language. These
mistakes are generally made by beginners only because they are new to the language. These errors can
be easily debugged or corrected.

For example:

If we want to declare the variable of type integer,

int a; // this is the correct form

Int a; // this is an incorrect form.

Commonly occurred syntax errors are:

If we miss the parenthesis (}) while writing the code.

Displaying the value of a variable without its declaration.

If we miss the semicolon (;) at the end of the statement.

Let's understand through an example.

#include <stdio.h>

int main()

a = 10;
printf("The value of a is : %d", a);

return 0;

Output

In the above output, we observe that the code throws the error that 'a' is undeclared. This error is
nothing but the syntax error only.

There can be another possibility in which the syntax error can exist, i.e., if we make mistakes in the basic
construct. Let's understand this scenario through an example.

#include <stdio.h>

int main()

int a=2;

if(.) // syntax error

printf("a is greater than 1");

return 0;
}

In the above code, we put the (.) instead of condition in 'if', so this generates the syntax error as shown
in the below screenshot.

Output

Run-time error

Sometimes the errors exist during the execution-time even after the successful compilation known as
run-time errors. When the program is running, and it is not able to perform the operation is the main
cause of the run-time error. The division by zero is the common example of the run-time error. These
errors are very difficult to find, as the compiler does not point to these errors.

Let's understand through an example.

#include <stdio.h>

int main()

int a=2;

int b=2/0;

printf("The value of b is : %d", b);

return 0;

}
Output

In the above output, we observe that the code shows the run-time error, i.e., division by zero.

Logical error

The logical error is an error that leads to an undesired output. These errors produce the incorrect
output, but they are error-free, known as logical errors. These types of mistakes are mainly done by
beginners. The occurrence of these errors mainly depends upon the logical thinking of the developer. If
the programmers sound logically good, then there will be fewer chances of these errors.

#include <stdio.h>

int main()

int sum=0; // variable initialization

int k=1;

for(int i=1;i<=10;i++); // logical error, as we put the semicolon after loop

sum=sum+k;

k++;

printf("The value of sum is %d", sum);

return 0;

}
Output

In the above code, we are trying to print the sum of 10 digits, but we got the wrong output as we put
the semicolon (;) after the for loop, so the inner statements of the for loop will not execute. This
produces the wrong output.

What is a compilation?

The compilation is a process of converting the source code into object code. It is done with the help of
the compiler. The compiler checks the source code for the syntactical or structural errors, and if the
source code is error-free, then it generates the object code.

The c compilation process converts the source code taken as input into the object code or machine
code.

You might also like