Bca CP Sep Notes Unit I
Bca CP Sep Notes Unit I
Unit - I
1. Introduction and Overview of Computer Programming
What is C?
C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in
1972.
It is a very popular language, despite being old. The main reason for its popularity is because it is a
fundamental language in the field of computer science.
C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Why Learn C?
It is one of the most popular programming languages in the world
If you know C, you will have no problem learning other popular programming languages
such as Java, Python, C++, C#, etc, as the syntax is similar
C is very fast, compared to other programming languages, like Java and Python
C is very versatile; it can be used in both applications and technologies
Overview of C:
History of C:
C is derived from two early programming languages such as BCPL (Basic combined
programming language) and ‘B’ language. In 1972 Dennis Ritchie and Brain
Kernighan together published a detailed description of C language and renamed as ‘K
and R C’. In 1983 ANSI (American National Standards Institute) published a standard
for c language.
Features of C:
C Language is general purpose and high level programming language which is
developed by Dennis Ritchie in 1972. The most important features of C Language are:
1. Portability
2. Modularity
3. Flexibility
4. Speed
5. Extensibility
6. Compactness
7. Case sensitive
Applications of C:
Because of its possibility and efficiency C is used to develop the system as well as
application s/w. Some of the system and application s/w are listed below:
System s/w: Application s/w:
O/S Data base system.
Interpreters. Graphics packages.
Compilers. Spread sheets.
Assemblers. CAD/CAM applications.
Editors. Word processors.
Office automation tools.
Loaders.
Scientific and engineering application.
Linkers.
Characteristics of c language
C Program Structure
1. Preprocessor directives
2. Global Declaration
3. void main ( )
4. {
5. Declarations;
6. Statements;
7. }
8. User created sub- program
Step 1: Pre-processor statements: The statements begin with # are called pre-
processor directives. They direct the C preprocessor to include the header files and
symbolic constants. e.g.: #include<stdio.h>
#include<math.h>
# define pi 3.142 used for symbolic constants.
Step 2: Global Declaration: The variables declared outside the main function and used in
more than one functions are called global variables.
Step 3: void main (): main is a special function used in to identify the beginning of
the program; every program should have only one main. It should be written in lower
case and should not be terminated by a semicolon (;). The parenthesis after main
indicates that it refers to a function.
Step 4 & 7: The start and end of the program is indicated by open braces ({) and
closing brace (}).
Constants:
A constant is a quantity that does not change during the execution of a program.
C constants
Integer Constant Floating point constant Single character constant String constant
Integer Constant:
It is a sequence of digits without a decimal point. It is prefixed with plus or minus sign.
There are 3 types.
1. Decimal integer constants containing digits from 0 to 9. e.g.: 246, + 25, -30 etc.
2. Octal integer constant containing digit from 0 to 7. e.g: 0673, 057 etc.
3. Hexadecimal integer constants containing digits from 0 to 9, A to F.
e.g.: 0x246, 0xaf, etc.
Floating constants:
Real or floating point number can contain both an integer part and fractional part in the
number. Floating point numbers may be represented in 2 from either in the fractional
form in the exponent form.
A floating point numbers in fractional from consists if signed or unsigned digits
including a decimal point between digits. Eg: 25.6, 0.568.
Character constant:
Single character constant contains a single character enclosed within a pair of single
quote marks. e.g: ‘a’, ‘?’, ‘#’ etc.
String constant is a sequence of characters enclosed within a pair of double quotes.
e.g: “hello”, “x+2”, “2000” etc.
Variables:
The quantity that changes during the execution of a program is called variable. It is a
named location in the memory that is used to hold a value. The rules for forming variables
names are same are same as identifiers.
Syntax: data type variable list;
Ex: int x, y;
Data types: Data type is an attribute of a variable that indicates the type of data that a
variable can hold. Each variable must be declared in the beginning of a program. C
support three classes of data type
Data types
Built in data types Derived data types User defined data types
e.g: int, float, char, double e.g: array, strings, structures, unions, pointer etc e.g: typedef, enum etc.
Type conversions: c allows the conversion of one data type. The process of
converting one data type into another is called type conversion. There are 2 types of
conversions
1. Implicit type conversion
2. Explicit type conversion
1. Implicit type conversion: This type conversion performed automatically by the
complier is called implicit type conversion. This conversion is applied in arithmetic
manipulations when variables of different types are intermixed in an expression.
e.g: i) int x;
float y;
If x+y is performed, int converts to float and returns float.
ii) If two variables are int and long int , int converts into long.
2. Explicit type conversion (Type casting): The data type conversion performed
by the user is called explicit type conversion or type casting.
Syntax : (data type) expression;
e.g: int x,y;
float m;
m= (float) x/y; This assignment statement causes x to be converted to float
before division.
Declaring and Initializing (Assigning values) C Variables:
Variables should be declared in the C program before to use.
Memory space is not allocated for a variable while declaration. It happens only on variable
definition.
Variable initialization means assigning a value to the variable.
Variable int x, y, z;
data_type variable_name;
1 declaration char ch;
GLOBAL VARIABLE IN C:
The scope of global variables will be throughout the program. These variables can be accessed
from anywhere in the program.
This variable is defined outside the main function. So that, this variable is visible to main
function and all other sub functions.
#define PI 3.141593
#define TRUE 1
#define FALSE 0
#define PI 3.141593
#define PI 3.141593 defines a symbolic constant PI whose value is 3.141593. When the program is
preprocessed, all occurrences of the symbolic constant PI are replaced with the replacement text
3.141593.
Note that the preprocessor statements begin with a # symbol, and are not end with a semicolon. By
convention, preprocessor constants are written in UPPERCASE.
Example: 1
#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define PI 3.141593
void main()
{
float a;
float b;
float c;
float d=PI;
clrscr();
if(TRUE)
{
a=100;
b=a*10;
c=b-a;
}
printf("\na=%f\nb=%f\nc=%f\nPI=%f",a,b,c,d);
getch();
}
Formatted I/O: C standard library provides two powerful functions for performing
formatted input-output. They are
1. printf( ) - formatted output
2. scanf( ) - formatted input
1. printf() Function:
printf() function is used to print the “character, string, float, integer, octal and
hexadecimal values” onto the output screen.
We use printf() function with %d format specifier to display the value of an integer
variable.
Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable.
To generate a newline,we use “\n” in C printf() statement.
Note:
C language is case sensitive. For example, printf() and scanf() are different from
Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower
case.
2. scanf(); function :
scanf() function is used to read character, string, numeric data from keyboard
Consider below example program where user enters a character. This value is assigned to
the variable “ch” and then displayed.
Then, user enters a string and this value is assigned to the variable “str” and then
displayed.
Example program for printf() and scanf() functions:
#include <stdio.h>
int main()
{
char ch;
char str[100];
printf(“Enter any character \n”);
scanf(“%c”, &ch);
printf(“Entered character is %c \n”, ch);
printf(“Enter any string ( upto 100 character ) \n”);
scanf(“%s”, &str);
printf(“Entered string is %s \n”, str);
}
Output :
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai
The format specifier %d is used in scanf() statement. So that, the value entered is received
as an integer and %s for string.
Ampersand is used before variable name “ch” in scanf() statement as &ch.
It is just like in a pointer which is used to point to the variable.
Where, control string specifies the sequence of one or more format specifiers.
address_list specifies the address of memory locations where the values of variables should
be stored. e.g: 1. scanf(“%d%f”,&x,&y);
2. scanf (%c%s”,&ch,st);
These functions are used to read character type data from the keyboard. The getchar( ) and
gets( ) are used for this purpose. They are included in the header file stdio.h.
getch() and getchar() are used to read a character from screen. putch() and putchar() are used
to write a character to screen. getch() and putch() are non-standard functions defined
in conio.h, mostly used in turbo C/dev C++ environment.
Most of the program is ending with getch(),and so we think that getch() is used to display the
output...but it is wrong. It is used to get a single character from the console. Just see the
behaviors of various single character input functions in c. getchar() getche() getch().
Function : getchar()
getchar() is used to get or read the input (i.e a single character) at run time.
Example Program:
void main()
{
char ch;
ch = getchar();
printf("Input Char Is :%c",ch);
}
Function : getche()
getche() is used to get a character from console, and echoes to the screen.
Example Program:
void main()
{
char ch;
ch = getche();
printf("Input Char Is :%c",ch);
}
Function : getch()
getch() is used to get a character from console but does not echo to the screen.
Example Program:
void main()
{
char ch;
ch = getch();
printf("Input Char Is :%c",ch);
}
The getchar() function: This function reads a single character from the keyboard. There is
no parameter within the parenthesis.
Syntax: var = getchar( );
Where var specifies the character type variable to which an accepted character is assigned.
The gets() function: This function reads all characters entered from the keyboard until the
ENTER key or RETURN key is pressed.
Syntax: gets(string);
The putchar()function: This function points a single character on the screen. The variable to
be displayed is of type char.
Syntax: putchar (var);
Where: var specifies a character type variable which is enclosed within the parenthesis.
The puts( ): This function points a string of characters on the screen.
Comment Statements:
Comment lines are written for the easy accessibility for the user. These comment lines
are ignored by compliers. There are two types of comments
Examples:
1.Write a program to print Hello world and your name
#include<stdio.h>
main()
{
printf("Hello");
printf("world");
printf("my name is Surya");
}
2.Write a program to print hello world and your name using escape sequence character.
#include<stdio.h>
void main()
{
clrscr();
printf("\nHello");
printf("\nworld");
printf("\nmy name is Surya");
getch();
}
#include<stdio.h>
void main()
{
int sum,a,b;
clrscr();
a=10;
b=20;
sum=a+b;
printf("\nsum=%d",sum);
getch();
}
/*WAP to find the sum of 2 numbers with using I/O functions*/
#include<stdio.h>
void main()
{
int sum,a,b;
clrscr();
printf("\nenter the values of a and b:");
scanf("%d%d",&a,&b);
sum=a+b;
printf("\nsum=%d",sum);
getch();
}
5. /*WAP to find the area of triangle*/
#include<stdio.h>
void main()
{
int b,h,area;
clrscr();
printf("enter base,height: \n");
scanf("%d%d",&b,&h);
area=0.5*b*h;
printf("\n area=%d",area);
getch();
}
6. /*WAP to accept an integer, to print the right most digit*/
#include<stdio.h>
void main()
{
int n,r;
clrscr();
printf("\n enter any integer");
scanf("%d",&n);
r=n%10;
printf("\n right most digit=%d",r);
getch();
}
7. /*WAP to find the area of circle*/
#include<stdio.h>
void main()
{
float r,area;
clrscr();
printf("\nEnter the radius:");
scanf("%f",&r);
area=3.142* r * r;
printf("\nArea of circle =%f",area);
getch();
}
8. /*WAP to calculate the area and perimeter of a circle*/
#include<stdio.h>
void main()
{
float r,area,peri;
clrscr();
printf("\n enter radius of circle");
scanf("%f",&r);
area=3.14*r*r;
peri=2*3.14*r;
printf("\n area=%f",area);
printf("\n peri=%f",peri);
getch();
}
9. /*WAP to accept the employ number and basic salary of an employ. To find HRA, DA& Net
salary, if HRA=10%,DA=12% of basic salary and */
#include<stdio.h>
void main()
{
int eno;
float bsal,hra,da,nsal;
clrscr();
printf("\n enter eno, bsal");
scanf("%d%f",&eno,&bsal);
hra=bsal*0.10;
da=bsal*0.12;
nsal=bsal+hra+da;
printf("\n hra=%f",hra);
printf("\n da=%f",da);
printf("\n nsal=%f",nsal);
printf("\n eno=%d",eno);
getch();
}
10. /*WAP to find the sum, diff, product and quotient of 2 numbers*/
#include<stdio.h>
void main()
{
int a,b,sum,diff,product,quotient;
clrscr();
printf("enter a=");
scanf("%d",&a);
printf("enter b=");
scanf("%d",&b);
sum=a+b;
diff=a-b;
product=a*b;
quotient=a/b;
printf("sum=%d\n",sum);
printf("diff=%d\n",diff);
printf("product=%d\n",product);
printf("quotient=%d\n",quotient);
getch();
}
11. /*WAP to accept uppercase alphabet and print the equivalent lowercase alphabet*/
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("\n enter any uppercase alphabet and print the equivalent lowercase alphabet ");
ch=getchar();
ch=ch+32;
printf("\n equivalent lowercase=%c",ch);
getch();
}
12. /*WAP to accept two integer number stored in A &B, interchange these two numbers using
3rd variable */
#include<stdio.h>
void main()
{
int A,B,Temp;
clrscr();
printf("\n enter any two integers: ");
scanf("%d%d",&A,&B);
Temp=A;
A=B;
B=Temp;
printf("\n after interchanging A=%d, B=%d",A,B);
getch();
}
13. /*WAP to accept two integer numbers stored in A&B, interchanging these two numbers
without using 3rd variable*/
#include<stdio.h>
void main()
{
int A,B;
clrscr();
printf("\n enter any two integers");
scanf("%d%d",&A,&B);
A=A+B;
B=A-B;
A=A-B;
printf("\n after interchanging A=%d \t B=%d",A,B);
getch();
}
14. /*WAP to find the Simple Interest*/
#include<stdio.h>
void main()
{
float SI, P, R, T;
clrscr();
printf("\n enter the values of P R and T");
scanf("%f%f%f",&P,&R,&T);
SI=P*R*T/100;
printf("\n Simple Interest=%f",SI);
getch();
}
Case Studies:
Compilation Process in C
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. The compilation process can be divided into four
steps, i.e., Pre-processing, Compiling, Assembling, and Linking.
The preprocessor takes the source code as an input, and it removes all the
comments from the source code. The preprocessor takes the preprocessor
directive and interprets it. For example, if <stdio.h>, the directive is available
in the program, then the preprocessor interprets the directive and replace this
directive with the content of the 'stdio.h' file.
The following are the phases through which our program passes before being
transformed into an executable form:
o Preprocessor
o Compiler
o Assembler
o Linker
Preprocessor
The source code is the code which is written in a text editor and the source code file is given
an extension ".c". This source code is first passed to the preprocessor, and then the
preprocessor expands this code. After expanding the code, the expanded code is passed to the
compiler.
Compiler
The code which is expanded by the preprocessor is passed to the compiler. The compiler
converts this code into assembly code. Or we can say that the C compiler converts the pre-
processed code into assembly code.
Assembler
The assembly code is converted into object code by using an assembler. The name of the
object file generated by the assembler is the same as the source file. The extension of the
object file in DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file
is 'hello.c', then the name of the object file would be 'hello.obj'.
Linker
Mainly, all the programs written in C use library functions. These library functions are pre-
compiled, and the object code of these library files is stored with '.lib' (or '.a') extension.
The main working of the linker is to combine the object code of library files with the object
code of our program. Sometimes the situation arises when our program refers to the functions
defined in other files; then linker plays a very important role in this. It links the object code
of these files to our program. Therefore, we conclude that the job of the linker is to link the
object code of our program with the object code of the library files and other files. The output
of the linker is the executable file. The name of the executable file is the same as the source
file but differs only in their extensions. In DOS, the extension of the executable file is '.exe',
and in UNIX, the executable file can be named as 'a.out'. For example, if we are using
printf() function in a program, then the linker adds its associated code in an output file.
In the above flow diagram, the following steps are taken to execute a
program:
o Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the
preprocessor converts the source code into expanded source code. The
extension of the expanded source code would be hello.i.
o The expanded source code is passed to the compiler, and the compiler
converts this expanded source code into assembly code. The extension of
the assembly code would be hello.s.
o This assembly code is then sent to the assembler, which converts the
assembly code into object code.
o After the creation of an object code, the linker creates the executable file.
The loader will then load the executable file for the execution.
Function Meaning
sqrt(x) the sqrt() function returns the square root of x
1. sqrt(25) returns 5
2. pow(4,2) returns 16
3. floor(3.4) returns 4
*****