You are on page 1of 35

THE TIMES INTERNATIONAL COLLEGE

Dillibazar ,Kathmandu

Internal signature…..…. External Signature……….


ACKNOWLEDGEMENT
Under the prescribed syllabus by NEB, Computer Science students are required to
prepare their project work, showing their skills and knowledge in the field. It’s my
great pleasure to have been able to show my innate confidence in practical as well
as theoretical knowledge. I would like to extend my sincere thanks to my teacher
Mr. Sanjay Dhungana for his valuable suggestions and inspiration that assigned
me to prepare my work.

Every organized report requires a lot of hard work and seeks a lot of helping hands.

Everything I’ve done will be worthless if I forget my parents; I am very thankful


for their support, love and care since the day of my birth.

Secondly, I would also like to thank my friends and colleagues who helped me a lot
in finalizing this project with limited time and for helping to bring this project
work to existence.
Declaration

I hereby declare that the work presented in this project work has been
done by myself under the supervision of Mr. Sanjay Dhungana & has
not been submitted elsewhere for any examination. All sources of
information have been specifically acknowledged by reference to the
author or institution.

Date:2023/11/24 Mohammad Altaf Khan


Registration no:
Recommendation Letter

This is certified that the project works in title “C-Programing” has been
carried by “Mohammad Altaf Khan” as partial fulfillment of grade 11
in Computer Science under the supervision to the best of my knowledge,
this work has not been submitted for any other purpose in the institute. I,
therefore, recommend project work for appeal.

Supervisor:
Mr. Sanjay Dhungana
Department of Computer
Science/ +2 TTIC secondary
School, Charkhal, Dillibazar (KTM)
APPROVAL LETTER

Date:2079/11/24
Respected Sir, Sanjay Dhungana
With due all respect
I want to request you to approve my project “C Programming” of the Department
of Computer Science. I hope the project meets expectations, qualifications, and
standards of the department.
In my project I have mentioned the materials as instructed like related letters,
contents etc. I hope it will be approved. I will be eager to complete other tasks as
asked.
I wish and am hopeful for the best, and may this bring an image of a good student
to you

Your Sincerely:
Mohammad Altaf Khan
Table of Contents
Introduction of C 1
Structure of C Program 2
Character set used in C 3
Data type in C language 4
Constant and Variables 4
Constants 4
Variables 6
Selection Control Statement 6
Conditional Statement 6
Switch case statement 12
Looping 14
Array 18
String 20
Conclusion 25
Reference 26
THE

Programming
language
PROGRAMMING IN C

Introduction of C
⮚ ‘C’ seems a strange name for a programming language. But this strange
sounding language is one of the most popular computer languages today
because it is a structured, high-level, machine independent language. It
allows software developers to develop programs without worrying about
the hardware platforms where they will be implemented.
⮚ ‘C’ language was developed by Dennis Ritchie at Bell laboratories in 1972.

Some facts about C:


⮚ In 1988, the American National Standards Institute (ANSI) had formalized
the C language.
⮚ C was invented to write the UNIX operating system.
⮚ C is a successor of 'Basic Combined Programming Language' (BCPL) called B
language.
⮚ Linux OS, PHP, and My SQL are written in C.
⮚ C has been written in assembly language.

C has been popular for the following reasons:


• One of the early programming languages.
• Still, the best programming language to learn quickly.
• C language is reliable, simple and easy to use.
• C language is a structured language.
• Modern programming concepts are based on C.
• It can be compiled on a variety of computer platforms.
• Universities preferred to add C programming in their courseware.
Page | 1
Programming
Advantages of C
• C is the building block for many other programming languages.
• Programs written in C are highly portable.
• Several standard functions are there (like in-built) that can be used to develop
programs.
• C programs are collections of C library functions, and it's also easy to add
functions to the C library.
• The modular structure makes code debugging, maintenance and testing easier.

Disadvantage of C
• C does not provide Object Oriented Programming (OOP) concepts.
• There are no concepts of Namespace in C.
• C does not provide binding or wrapping up of data in a single unit.
• C does not provide Constructor and Destructor.

Structure of C Program
The following program is written in the C programming language. Open a text file
hello.c using TC editor.
#include<stdio.h>
Int main()
{
/*My first program*/
printf(“Hello, World! \n”);
Return 0;
}
Page | 2
Programming
Character set used in C
A character denotes any alphabets, digit or special symbol used to represent
information.
Letters Digits Special Characters
Uppercase A….Z All decimal digits , . ; ? ( ) {} $ % *
Lower case a……z 0…………..9

⮚ Use of comments: Comment lines are not executing statements.


//Program written by NA sir
⮚ Identifiers: Identifiers refer to the name of variables, functions. Both
uppercase and lower case letters are permitted, though lower case letters
are commonly used.
⮚ Keywords: Keywords are the words whose meaning has already been
explained to the C compiler. All keywords must be written in lower case.
There are 32 keywords in C. Keywords cannot be used as variable names.

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
signed continue for void
default goto sizeof volatile
do if static while

⮚ Token: The smallest individual unit in a program is called C token. C token


has six tokens.

Page | 3
Programming
Keywords Constant String Operators Special Identifier
symbol
int, float 3.14, 10 “ABC” +-*/ {}()[] a, b, l, h
“Kajol”

Data type in C language


Data types are mainly used to define the type and nature of data such that
compiler detects and proceeds.
Data types used in C are as follows:
i) int: - int is the keyword for integer. It contains the whole numbers between
-32,768 to 32,767. It requires 2 bytes memory and its type specifier is %d.
Examples: - int a=10;
ii) float: - float is the keyword for floating numbers i.e. decimal numbers. It
contains number 3.4e – 38 to 3.4e+38. It requires 4 bytes memory and its
type specifier is %f.
Example: - float pi=3.14;
iii) char: - char is the keyword for character. It represents the single alphabet.
It requires 1 byte memory and its type specifier is %c.
Example: - char choice;

Constant and Variables


Constants
Constants are the expressions with fixed values that do not change during the
execution of the program. To put it up simply , constant means whose value
cannot be changed.

Syntax:

const type constant_name;

Example:
Page | 4
Programming
#include <stdio.h>
#define value 10
void main() {
int data;
data = value*value;
printf("value of data : %d",data);
}

Output
value of data : 100

❖ Following are the different types of constants we can use in C.

1. Integer Constants
Integer constants refer to the sequence of digits with no decimal points. The three
kinds of integer constants are:

● Decimals constant: Decimal numbers are set of digits from 0 to 9 with +ve


or -ve sign. For example: 11, -11 etc.
● Octal constant: Octal numbers are any combination of digits from set 0 to 7
with leading 0. For example: 0213, 0123 etc.
● Hexadecimal constant: Hexadecimal numbers are the sequence of digits
preceding 0x or 0X. For example: 0xBD23, OX543 etc.

2.Floating point Constant

These types of constants contain a decimal point or an exponent. They can be


either negative or positive. Commas or blanks are not allowed within a real
constant.

Example:
+3.2f-4     -0.3e-3    325.0     -33.75
3. Character constant
Character constants are the set of alphabet enclosed in single quotes.

Page | 5
Programming
For example: ‘A’, ‘f’, ‘i’ etc.

4. String Constant
String constants are the sequence of characters enclosed in a pair of double
quotes (” “).

For example: “Hello”

Variables

Variables are used to store the value. As the name indicates, its value can be
changed or also it can be reused many times. We have to define its Data Types as
shown below.
Syntax 
Data type variable_name;

e.g. 
int a;

Where ‘a’ is the variables.

Selection Control Statement


Conditional Statement

i. if Statement

If statement is used to test a condition, if condition is true then the code inside
the if statement is executed otherwise that code is not executed.

Syntax:

If(condition)

Statement;

Page | 6
Programming
}

Example:
#include<stdio.h>
#include<conio.h>
void main(){
int i=10;
clrscr();
if(i<=10){
printf(“value of i is less than or equal to 10”);
}
getch();
}

Output
value of i is less than or equal to 10

ii. if else Statement

If Else is also used to test a condition, if condition is true then the code inside the
if statement is executed otherwise else part is executed.

Syntax:

If(condition)

statement1;

else

Page | 7
Programming
statement2;

Example:
#include<stdio.h>
#include<conio.h>
void main(){
int i=10;
clrscr();
if(i<=10){
printf(“value of i is less than or equal to 10”);
}
else
{
printf(“value of i is greater than 10”);
}
getch();
}

Output
value of i is less than or equal to 10

iii. if…else if…else Statement

After if statement else if is used to check the multiple conditions.

Syntax:

If(condition)
{
Statement1;
Page | 8
Programming
}
Else if (condition)
{
Statement2;
}
Else
{
Statement N;
}

Example:
#include<stdio.h>
#include<conio.h>
void main(){
int i=10;
clrscr();
if(i<10){
printf(“value of i is less than 10”);
}
else if(i==10)
{
printf(“value of i is equal to10”);
}
else
{
printf(“value of i is greater than 10”);
}
getch();
Page | 9
Programming
}

Output
value of i is equal to10

iv. Nested If Statement

It contains multiple if else condition. It is used to check the multiple conditions.


This statement is like executing an if statement inside an else statement.

Syntax:

If(condition)

If(condition)

Statement 1;

Else

Statement 2;

}}

Else

Statement N;

Page | 10
Programming
Example:
#include<stdio.h>
#include<conio.h>
void main(){
int i = 20;
int j = 10;
if ( i < 30 ){
printf ("Value of i is less than 30");
if ( j == 10 ){
printf ("Value of j is equal to 10");
}
}
else
{
printf ("Value of i is not less than 30");
}
getch();
}
Output
Value of i is less than 30
Value of j is equal to 10

Page | 11
Programming
Switch case statement
Switch statement is alternative of nested if else. It is executed when there are

many choices and only one is to be executed.

Syntax :

switch( expression )
{
case value 1:
//Block of code;
break;
case value 2:
//Block of code;
break;
case value N:
//Block of code
break;
default:
//Block of code
break;

Page | 12
Programming
#include <stdio.h>
#include <conio.h>
main()
{
int week;
printf("Enter week number(1-7): ");
scanf("%d", &week);

switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}

return 0;
Page | 13
Programming
}

Looping
Loop is used to execute the block of code several times according to the condition
given in the loop. It means it executes the same code multiple times so it saves
code and also helps to traverse the elements of an array.

There are 3 types of loop.

1. while loop
2. do while loop
3. for loop

1. while Loop

While loop execute the code until condition is false.

Syntax:
while(condition){
//code
}

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 20;
while( i <=20 ) {
printf ("%d " , i );
Page | 14
Programming
i++;
}
getch();
}

Output
20

2. do while loop

It also executes the code until condition is false. In this at least once, code is
executed whether condition is true or false but this is not the case with while.
While loop is executed only when the condition is true.

Syntax:
do{
//code
}while(condition);

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 20;
do{
printf ("%d " , i );
i++;
}
while( i < =20 );
getch();
}

Output

Page | 15
Programming
20
21

3. for Loop

It also executes the code until condition is false. In this three parameters are given
that is

● Initialization
● Condition
● Increment/Decrement

Syntax:
for(initialization;condition;increment/decrement)
{
//code
}

It is used when number of iterations are known where while is used when number
of iterations are not known.

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for( i = 20; i < 25; i++) {
printf ("%d " , i);
}
getch();
}

Page | 16
Programming
Output
20
21
22
23
24

Page | 17
Programming
Array
Array is a collection of elements which are of similar types. Array is very useful in
C.
Suppose we want to store 50 students marks then for this purpose we need to use
50 variable which is not possible and hard to manage so to avoid this situation we
use array in which 50 students marks can store in only one variable easily.

Advantages of Array

● Code Optimization
● Easy to traverse data
● Easy to sort data
● Random Access

We can declare an array as follows:

data_type array_name[array_size];

e.g.

int a[5];

where int is data type of array a which can store 5 variables.

Initialization of C Array

You can initialize array by using index. Always array index starts from 0 and ends
with [array_size – 1].
Page | 18
Programming
a[0] = 20;
a[1] = 40;

Or you can also declare array with initialization like as follows:-


int a[3] = {20,30,40};

Example
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3], i;   //declaration of array
a[0] = 20; //initialization of array
a[1] = 30;
a[2] = 40;
for(i=0;i<3;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}

Output
20
30
40

String
String is a collection of characters. It is a one dimensional array of characters.

Declaration of String

Page | 19
Programming
Strings are declared in C in similar manner as arrays. Only difference is that, strings
are of char type.

Example:

char s[5];

Initializing Array String


String are initialize into various way in C language.
Example:

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};

Reading String from user


Example: scanf() to read a string

#include <stdio.h>
int main()

Page | 20
Programming
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}

Output

Enter name: Dennis Ritchie


Your name is Dennis.

a) String length i.e. strlen()

The strlen() function calculates the length of a given string.

The strlen() function takes a string as an argument and returns its length.


The returned value is of type size_t (an unsigned integer type).
It is defined in the <string.h> header file.

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};

// using the %zu format specifier to print size_t


printf("Length of string a = %zu \n",strlen(a));
printf("Length of string b = %zu \n",strlen(b));

return 0;
}
Output
Page | 21
Programming
Length of string a = 7
Length of string b = 7

b) String concatenation i.e. strcat()

 The strcat() function contcatenates (joins) two strings.


Example:

#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";

// concatenates str1 and str2


// the resultant string is stored in str1.
strcat(str1, str2);

puts(str1);
puts(str2);

return 0;
}

Output

This is programiz.com

programiz.com

c) String Copy i.e. strcpy()

● The strcpy() function copies the string pointed by source (including the null


character) to the destination.
● The strcpy() function also returns the copied string.
Page | 22
Programming
Example:
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "C programming";
char str2[20];

// copying str1 to str2


strcpy(str2, str1);

puts(str2); // C programming

return 0;
}
Output

C programming

d) String Compare i.e. strcmp()

The strcmp() compares two strings character by character. If the strings are


equal, the function returns 0.

Example:

#include <stdio.h>
#include <string.h>

Page | 23
Programming
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;

// comparing strings str1 and str2


result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);

// comparing strings str1 and str3


result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);

return 0;
}

Output

strcmp(str1, str2) = 1

strcmp(str1, str3) = 0

Page | 24
Programming
Program to check the luck.
#include<stdio.h>
#include<conio.h>
main()
{
int i;
printf(“Enter your lucky number:”);
scanf(“%d”, &i);
if(i==7)
{
printf(“CONGRATULATIONS…!!! You won a prize.”);
}
else
{
printf(“Sorry please try again”);
}
getch();
}

Program to find the smallest number among three numbers.


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;

Page | 25
Programming
printf(“Enter three number:”);
scanf(“%d%d%d”, &a,&b,&c);
if(a<b)
{
if(a<c)
{
printf(“%d is smallest” ,a);
}
else
{
printf(“%d is smallest” ,c);
}}
else if(b<c)
{
printf(“%d is smallest” ,b);
}
else
{
printf(“%d is smallest” ,c);
}
}
getch();
}

Page | 26
Programming
Conclusion

It was a wonderful learning experience for me while working on this


project. This project took me through the various phases of project
development and gave me real insight into the world of programming. The
joy of working and the thrill involved while tackling the various problems
and challenges gave me a feel like a programmer. It was due to this project I
came to know how programming is done.

Page | 27
Programming
Reference

A textbook of Computer Science, Grade XI, Sulav Books House Pvt. Ltd.
www.google.com

Page | 28
Programming

You might also like