BCA Ist Sem. Unit 1st Notes ..
BCA Ist Sem. Unit 1st Notes ..
Algorithm in C Language
Example :-
Problem − Design an algorithm to add two numbers and display the result.
Step 1 − START
Step 6 − print c
Step 7 – STOP
Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written
as −
Step 3 − c ← a + b
Step 4 − display c
Step 5 – STOP
Flowchart Elements :-
Flowchart Symbols
Here is a chart for some of the common symbols used in drawing flowcharts.
Symbol Symbol Name Purpose
Example Flowcharts :-
start
input a/b
c=a+b
print c
End
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.
printf("format string",argument_list);
The scanf() function is used for input. It reads the input data from the console.
scanf("format string",argument_list);
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);
return 0;
Output
enter a number:5
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.
#include<stdio.h>
int main(){
int x=0,y=0,result=0;
scanf("%d",&x);
scanf("%d",&y);
result=x+y;
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
Identifiers in C
Strings in C
Operators in C
Constant in C
Special Characters in C
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:
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.
In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers
are case sensitive.
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.
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.
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.
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.
Operators in C
Operator
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Arithmetic Operators
Show Examples
Relational Operators
Show Examples
> 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.
Logical Operators
Show Examples
Increment/decrement operators
pre-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.
= a, b a=b Used to assign a value from right side operand to left side operand
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.
& (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.
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.
a-b;
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………..
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
Structure, Union,
float %f 4 byte
double 8 byte
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.
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:
#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;
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.
#include <stdio.h>
int main()
int a=2;
int b=2/0;
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 k=1;
sum=sum+k;
k++;
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.