Professional Documents
Culture Documents
Dillibazar ,Kathmandu
Every organized report requires a lot of hard work and seeks a lot of helping hands.
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.
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.
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
Page | 3
Programming
Keywords Constant String Operators Special Identifier
symbol
int, float 3.14, 10 “ABC” +-*/ {}()[] a, b, l, h
“Kajol”
Syntax:
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
1. Integer Constants
Integer constants refer to the sequence of digits with no decimal points. The three
kinds of integer constants are:
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;
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
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
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
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
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.
1. while loop
2. do while loop
3. for loop
1. while Loop
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
data_type array_name[array_size];
e.g.
int a[5];
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;
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];
#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
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
return 0;
}
Output
Page | 21
Programming
Length of string a = 7
Length of string b = 7
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";
puts(str1);
puts(str2);
return 0;
}
Output
This is programiz.com
programiz.com
int main() {
char str1[20] = "C programming";
char str2[20];
puts(str2); // C programming
return 0;
}
Output
C programming
Example:
#include <stdio.h>
#include <string.h>
Page | 23
Programming
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int 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();
}
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
Page | 27
Programming
Reference
A textbook of Computer Science, Grade XI, Sulav Books House Pvt. Ltd.
www.google.com
Page | 28
Programming