0% found this document useful (0 votes)
127 views21 pages

Bca CP Sep Notes Unit I

The document provides an overview of the C programming language, detailing its history, features, and applications. It covers fundamental concepts such as data types, variables, constants, and the structure of a C program, along with input and output functions. The document serves as an introductory guide for students learning programming in C.

Uploaded by

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

Bca CP Sep Notes Unit I

The document provides an overview of the C programming language, detailing its history, features, and applications. It covers fundamental concepts such as data types, variables, constants, and the structure of a C program, along with input and output functions. The document serves as an introductory guide for students learning programming in C.

Uploaded by

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

I BCA 1st Sem.

(SEP Scheme) Programming in C (BCAT 105) - KVN

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 a High Level Language. It is used both as general purpose programming language


and special purpose programming language. It was developed at Bell laboratory (AT
and T’s) in 1972; it is the out come of the efforts of Dennis Ritchie.

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

Dept. of CS, GFGCW, Tumkur. 1 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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

 Structured programming language


 Case sensitive language
 Portable language
 Middle level language
 C has a rich set of operators
 C uses a variety of data types

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 (}).

Dept. of CS, GFGCW, Tumkur. 2 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

Step 5: Declaration statements. It determines the type of the variable.


Step 6: Contains all executable statements that must ends with a semicolon (;)
Step 8: User defined functions: It is optional. User defines the task, which is not
available in the c-compiler.

e.g.: #include <stdio.h>


void main( )
{
printf(“\n Welcome to C programming”);
}
Output: Welcome to C programming

2. C Programming Basic Concepts

C Character Set: The characters in c grouped into following categories.


1. Letters (Alphabets): Upper case A.....Z, Lower case a......z
2. Digits: All decimal digits 0.........9
3. Special characters: #, +,&...etc....
4. White space: Blank space, new line (\n), horizontal tab (\t), vertical tab (\v)
etc...
C Tokens:
A token is the basic and smallest unit of a c program. One or more characters are grouped
together to form token. There are four tokens in C.
1. Identifiers
2. Keywords
3. Constants
4. Variables
Identifiers:
An identifier is a name given to the programming element like variables, arrays, stings,
function etc. Basically identifiers are sequence of alphabets and digits and underscore.
Rules for Identifiers:
1) First character must be alphabet or an underscore.
2) Keywords should not be used as identifiers.
3) Identifier should not contain blank space.
4) Identifiers consist of letters, digits and underscore (no other special symbol is used).

Keywords (reserve words):


Keyword is a word that has one or more fixed meaning. Keywords must be written in
lower case. There are 32 keywords in C language.

Dept. of CS, GFGCW, Tumkur. 3 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

auto double int break const continue do for


else long case enum float unsigned if void
register char extern return short signed goto volatile
sizeof default struct static switch typedef union while

Constants:
A constant is a quantity that does not change during the execution of a program.
C constants

Numeric constant Character/ Non Numeric constant

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;

Dept. of CS, GFGCW, Tumkur. 4 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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.

a. Primitive or Fundamental or Basic data types:


These are the data type whose meaning is already defined in the compiler. There are 4
fundamental date types in C namely char , int , float and double.
int datatype:
An integer data type is a whole number without a decimal point. int is a keyword used to
indicate integer data type. It occupies 2 bytes of computer memory.
e.g.: int x, y, z;
float data type:
The float is the keyword used to indicate the real numbers. These are the numbers with
decimal point. They occupy 4 bytes of computer memory. e.g.: float area;
char data type:
Char is a keyword used to declare character type data. It occupies 1 byte of memory e.g:
char ch;
double:
It is a keyword to indicate double precision floating point numbers. It is used to declare the
variable to hold large real numbers with higher precision. It occupies 8 bytes of memory.
e.g.: double m;
Data modifiers (Qualifiers):
C allows the use of data type modifiers which modify the existing basic data types
according to requirements.
There are four modifiers supported in C-language they are:
signed, unsigned, short, long.
 Signed: it is a default data type that include both +ve and -ve values
 Unsigned: it is used to store only positive data elements.
 short: the short data type is a useful and efficient way to store and
manipulate small integer values in C programs.
 Long: it is used to double the size of an existing data type.

The size and ranges of basic data types are as follows:


Data type Size in Bytes Range of values
char 1 -128 to +128
unsigned char 1 0 to 256
int 2 -32768 to +32767
unsigned int 2 0 to 65535
short int 2 -32768 to +32767

Dept. of CS, GFGCW, Tumkur. 5 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

long int 4 -21147483648 to +21147483647


float 4 -3.4X10E-48 to +3.4X10E48
double 8 -1.7X10E-308 to +1.7X10E308
unsigned short int 2 0 to 65535
unsigned long int 4 0 to 4294967295

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.

Sl.No Type Syntax Example

Variable int x, y, z;
data_type variable_name;
1 declaration char ch;

Variable int x = 10, y = 20;


data_type variable_name = value;
2 initialization char ch = ‘A’, ch=’a’;

TWO TYPES OF VARIABLES IN C PROGRAM THEY ARE;


1. Local variable
2. Global variable
LOCAL VARIABLE IN C:
 The scope of local variables will be within the function only.
 These variables are declared within the function and can’t be accessed outside the function.

Dept. of CS, GFGCW, Tumkur. 6 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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.

Symbolic Constant in C Language:


A symbolic constant is name that substitute for a sequence of character that cannot be changed.
The character may represent a numeric constant, a character constant, or a string. When the
program is compiled, each occurrence of a symbolic constant is replaced by its corresponding
character sequence. They are usually defined at the beginning of the program. The symbolic
constants may then appear later in the program in place of the numeric constants, character
constants, etc., that the symbolic constants represent.
For example
A C program consists of the following symbolic constant definitions.

#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;

Dept. of CS, GFGCW, Tumkur. 7 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

c=b-a;
}
printf("\na=%f\nb=%f\nc=%f\nPI=%f",a,b,c,d);
getch();
}

3. Input and output with C


Formatted and unformatted I/O statements: The I/O statements involving format
specifiers are called formatted I/O statements. Those without format specifiers are
unformatted I/O statements.

TYPE Input functions Output functions


Formatted scanf( ) printf( )
Unformatted getch( ) putch( )
getche( ) --
getchar( ) putchar( )
gets( ) puts( )

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

printf( ) and scanf( )


 printf() and scanf() functions are inbuilt library functions in C which are available in
C library by default. These functions are declared and related macros are defined in
“stdio.h” which is a header file.
 We have to include “stdio.h” file as shown in below C program to make use of these
printf() and scanf() library functions.

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:

Dept. of CS, GFGCW, Tumkur. 8 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

 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.

Example for printf(); function:


#include <stdio.h>
int main()
{
char ch = ‘A’;
char str[20] = “[email protected]”;
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf(“Character is %c \n”, ch);
printf(“String is %s \n” , str);
printf(“Float value is %f \n”, flt);
printf(“Integer value is %d\n” , no);
printf(“Double value is %lf \n”, dbl);
printf(“Octal value is %o \n”, no);
printf(“Hexadecimal value is %x \n”, no);
return 0;
}
Output:
Character is A
String is [email protected]
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96
You can see the output with the same data which are placed within the double quotes of printf
statement in the program except
 %d got replaced by value of an integer variable (no),
 %c got replaced by value of a character variable (ch),
 %f got replaced by value of a float variable (flt),
 %lf got replaced by value of a double variable (dbl),
 %s got replaced by value of a string variable (str),
 %o got replaced by a octal value corresponding to integer variable (no),
 %x got replaced by a hexadecimal value corresponding to integer variable
 \n got replaced by a newline.

2. scanf(); function :

Dept. of CS, GFGCW, Tumkur. 9 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

 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);

Unformatted I/P functions:

Dept. of CS, GFGCW, Tumkur. 10 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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;

Dept. of CS, GFGCW, Tumkur. 11 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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);

Unformatted o/p functions:


These functions are mainly concerned with printing the character type data on the monitor.
The putchar( ) and puts( ) are used for this purpose. Both these functions are defined in the
header file stdio.h.

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.

Syntax: puts (string); where: string specifies a sequence of characters.

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

1) Single line comment: Written with //

2) Multiple comment lines: Written with /* and */.

Dept. of CS, GFGCW, Tumkur. 12 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

Escape sequence characters or Backslash character constants:


C supports back slash character constants that are used in o/p statements. They use a
back slash (\) followed by a character. Constant Meaning
\n new line
\t horizontal tab space
\v vertical tab space
\b back space
\r carriage return
\\ back space
\” quotation marks
\’ apostrophe
\0 NULL

Format Specifier or Conversion specifiers:


Depending on the nature of data types the following format specifiers are used
Format Specifiers Data type
%i signed integer
%d signed decimal integer
%c character
%f signed float value
%e float value in exponential form
%o unsigned octal value
%x or %X unsigned hexa decimal integer value
%u unsigned integer
%p pointer value
%ld signed long integer decimal

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();
}

3. /*WAP to find the sum of 2 numbers without using I/O functions*/

Dept. of CS, GFGCW, Tumkur. 13 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

#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*/

Dept. of CS, GFGCW, Tumkur. 14 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

#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*/

Dept. of CS, GFGCW, Tumkur. 15 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

#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();
}

Dept. of CS, GFGCW, Tumkur. 16 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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();
}

Dept. of CS, GFGCW, Tumkur. 17 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

Case Studies:

Creating and Executing a C Program

1. Type the program in C editor and save with . ...


2. Press Alt + F9 to compile the program.
3. If there are errors, correct the errors and recompile the program.
4. If there are no errors, then press Ctrl + F9 to execute/run the program.
5. Press Alt + F5 to open User Screen and check the result.

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

Dept. of CS, GFGCW, Tumkur. 18 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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.

Dept. of CS, GFGCW, Tumkur. 19 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

Let's understand through an example hello.c


#include <stdio.h>
int main()
{
printf("Hello C");
return 0;
}

Now, we will create a flow diagram of the above program:

Dept. of CS, GFGCW, Tumkur. 20 Aug. 2025


I BCA 1st Sem.(SEP Scheme) Programming in C (BCAT 105) - KVN

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.

Note: header file <math.h> :


A header file in c is a standard library that is placed before main() function. The math
library <math.h> consists of some commonly used mathematical functions. Some of
the standard library functions in <math.h> are

Function Meaning
sqrt(x) the sqrt() function returns the square root of x

pow(a,b) the pow() function returns the a power b

floor(n) the floor() function returns the the largest


integer greater than n
Example

1. sqrt(25) returns 5
2. pow(4,2) returns 16
3. floor(3.4) returns 4

*****

Dept. of CS, GFGCW, Tumkur. 21 Aug. 2025

You might also like