Basic Computer Programming (4310702)
Basic Computer Programming (4310702)
UNIT: 1
Flowchart and Algorithm
Flowchart
Definition and Importance of flowchart:
Flowchart: Flowchart is Graphical or Pictorial Representation of problem.
Symbols of flowchart:
1.Terminal:
o This Symbol is used for the Starting and Ending Process of Flowchart.
2.Input / Output
o This Symbol is used for Receiving input Data From user and displaying output to the user
3.Process
o This Symbol is used for Computing.
o It is Use for Some Arithmetic Calculation.
4.Decision Making
o This Symbol is used for making decision base on Condition.
o This Symbol has one incoming arrow for entry and Two outgoing arrow.
5.Subroutine
o This Symbol is used for user defined function or sub program.
6.Flow / Direction
o Flow line Symbol.
o This Symbol Indicates flow of data from one Symbol to Another Symbol.
7.Page Break
o When flowchart is do not fit into single page then Page Break and Page Continue Symbol is
used.
o Page Break Symbol Indicate break in current page.
8.Page Continue
o Page Continue Symbol Indicate Continue flowchart in another page.
2. Selection
Once the condition is evaluated, the control flows into one of two paths.
3. Repetition
Either the processing repeats or the control leaves the structure.
Limitation of flowchart:
1.Complex Logic: It is very difficult to draw a flowchart for very hard and complex problem
2.Difficulty in Modifications: If a minor change is required then it is difficult to change flowchart
and may be required to redraw flowchart again.
3.Difficult to understand for people who don't know flowchart symbols.
Limitation of Algorithm
1. An algorithm is Time consuming. Developing algorithm for complex problems would be time
consuming and difficult to understand.
2. Understanding complex logic through algorithms can be very difficult.
3. Difficult to show Branching and Looping in Algorithms.
4. Big tasks are difficult to put in Algorithms.
Examples:
Flowchart and algorithm to calculate area of circle.
Flowchart:
Algorithm:
1. START
2. INPUT: r
3. Calculate: Area= 3.14 * r *r
4. PRINT: Area
5. STOP
Pseudo code:
Pseudo code is not a programming language; it is a simple way of describing a set of instructions that
does not have to use specific syntax.
Example:
Print "passed"
else
Print "failed"
UNIT: 2
Basics of ‘C’
General structure of ‘C’ program:
Documentation section
Header File
Statement(s);
Documentation section:
Documentation consists of a set of comment line(/* */) giving the name of the program.
Link section provides instruction to the compiler to link function from the system library(Header
Files like #include<stdio.h>).
Constant Declaration:
There are some variable that are used more than one function. Such Variables are called global
variable and that is outside all the function.
Features of C language:
1) C is portable.
„C‟ Program can be run on any hardware.
4) C is modular language.
The program written in from of functions. A C program is group of one or more functions. Dividing the
program into small functions makes it to easy develop and maintain the programs.
5) C is Robust language.
Character set:
In the C programming language, the character set refers to a set of all the valid characters that we can
use in the source program for forming words, expressions, and numbers.
There are four type of character set in C programming.
Keyword:
Keywords are those words whose meaning is predefined.
The programmer cannot change the meaning of keywords.
There are 32 keywords available in c.
All the keywords must be written in lowercase.
Examples:
Identifiers:
Identifier is the words which are defined by the programmer in a program.
Identifier refers to the name of variable, array, function etc.
It consists of letters, digits and underscore.
First character must be an alphabet or underscore.
Keywords cannot be use as identifier.
Identifier name are case sensitive.
The name of identifier should be meaningful.
Variable
A variable is a name which is used to store a temporary value.
constant variable:
1) Arithmetic operator
2) Relational operator
3) Logical operator
4) Assignment operator
5) Increment and decrement operator
6) Conditional (ternary)operator
7) Bitwise operator
8) Special operator
Arithmetic operator :
Arithmetic Operators:
1) Binary plus(+):
It add two operands.
Example :a+b
2) Binary minus(-):
It subtracts two operands.
Example :a-b
3) Multiplication(*):
Prepared By: Department of Computer Engineering Page 4
Subject Name: Basic Computer Programming Unit No: 02 Subject Code: 4310702
1) a+b =7
2) a-b =3
3) a*b =10
4) a/b =2
5) a%b =1
Relational operator:
i) Equal to(= =)
It compares two operands and return TRUE (1) if both operands are equal.
ii) Not equal to (! =)
It compares two operands and return TRUE (1) if both operands are not equal.
iii) Less than(<)
It compares two operands and return TRUE (1) if operand1 is less than operand2.
iv) Less than equal to(<=)
It compares two operands and return TRUE (1) if operand1 is less than or equal to operand2.
v) Greater than(>)
It compares two operands and return TRUE (1) if operand1 is greater than operand2.
vi) Greater than equal to(>=)
It compares two operands and return TRUE (1) if operand1 is greater than or equal to operand2.
Examples:
1) a= =b false
2) a!=b true
3) a<b true
4) a<=b true
5) a>b false
6) a>=b false
Assignment Operator:
Logical operator:
Logical operators are used when we want to combine more than one condition.
Logical OR ||
Logical NOT !
1)
Logical AND(&&)
The truth table logical AND (&&) operator is given below:
Condition 1 Condition 2 Answer
Logical OR( || )
The truth table logical OR ( || ) operator is given below:
Condition 1 Condition 2 Answer
False True
True False
Examples :
Suppose a=5, b=3, c=6;
Increment Operators are the unary operators used to increment or add 1 to the operand value.
The Increment operand is denoted by the double plus symbol (++).
It has two types, Pre Increment and Post Increment Operators.
Pre-increment Operator:
The pre-increment operator is used to increase the original value of the operand by 1 before assigning it
to the expression.
X = ++A;
In the above example, the value of operand 'A' is increased by 1, and then a new value is assigned to the
variable 'X'.
The post-increment operator is used to increment the original value of the operand by 1 after assigning it
to the expression.
X = A++;
In the above example, the value of operand 'A' is assigned to the variable 'X'. After that, the value of
variable 'A' is incremented by 1.
Bitwise operator:
x=5
y=7
z=x&y
x=5, y=7
z=x&y
x=5, y=7
z=x&y
The conditional operator is also known as ternary operator because it has three operands.
The general form of ternary operator is follow:
o Syntax: e1 ? e2 : e3 ;
If the condition is true then the statement is followed the? Is executed(e2 is executed) otherwise the
statement followed the : is executed(e3 is executed).
Example:
max=(x>y) ? x : y;
First (x>y ) is checked, if it is true then x is assigned to max , otherwise y is assigned to max.
Evaluation of Expressions:
The expressions are evaluated by performing one operation at a time. The precedence and
associativity of operators decide the order of the evaluation of individual operations.
The expressions with parentheses are evaluated first. If two or more parentheses exist in an
expression, the parentheses are evaluated from left to right. In the case of nested parentheses, the
innermost parentheses are evaluated first, while the outermost one is evaluated last.
If there are no parentheses, the order of evaluation of an expression is based on the operator
precedence and associativity:
Example:
7 * (5 + 15) / (2 * 5) – 3
= 7*20/10−3.
= 140/10−3
=14-3
=11
Syntax:
float average;
int sum=25, n=10;
average = (float) sum / n;
where sum / n results into 2.5 rather than 2, because the division is performed after converting sum in to
float and the result of float / int is always float. So,
When we say Input, it means to feed some data into a program. An input can be given in the
form of a file or from the command line. C programming provides a set of built-in functions to
read the given input and feed it to the program as per requirement.
When we say Output, it means to display some data on screen, printer, or in any file. C
programming provides a set of built-in functions to output the data on the computer screen as
well as to save it in text or binary files.
C provides standard functions scanf() and printf(), to perform formatted inputs and outputs.
scanf() function:
Syntax:
scanf(format_specifiers, &data1, &data2,……);
Example:
scanf(“%d %c”,&data1,&data2);
printf() function:
Syntax:
printf(format_specifiers, data1, data2,……);
Example:
printf(“%d %c”,data1,data2);
UNIT: 3
Decision Statements and Control Structure
Decision Statements
Conditional branching statements:
Simple if statement:
Syntax:
if (condition)
{
Statement (s)
}
If the given condition is true then the statements inside the body of “if” will be executed. If the condition
is false then the statements inside the body of “if” will be skipped.
#include<stdio.h>
#include<conio.h>
void main()
{
int marks=45;
if( marks < 35 )
{
printf(“Student is fail”);
}
getch();
}
If-else statement:
Syntax:
if(condition)
{
Statement 1;
}
else
{
Statement 2;
}
True False
Condition?
Statement- 1 Statement- 2
Statement - X
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int marks=45;
if( marks < 35 )
{
printf(“Student is fail”);
}
else
{
printf( “Student is pass” );
}
getch();
}
if (condition1)
{
if (condition2)
stmt1;
else
stmt2;
}
else
{
if (condition3)
stmt3;
else
stmt4;
}
Example:
#include<stdio.h>
#include<conio.h>
void main (){
int a,b,c;
printf("Enter the values of a,b,c: ");
scanf("%d,%d,%d",&a,&b,&c);
if((a>b)&&(a>c))
{
if(a>c)
{
printf("%d is the largest",a);
}
else
{
printf("%d is the largest",c);
}
}
else
{
if(b>c)
{
printf("%d is the largest",b);
}
Prepared By: Department of Computer Engineering Page 4
Subject Name: Basic Computer Programming Unit No: 03 Subject Code: 4310702
else
{
printf("%d is the largest",c);
}
}
if else if…ladder
Syntax:
if(condition-1)
{
Statement - 1;
}
else if(condition-2)
{
Statement - 2;
}
else if(condition-3)
{
Statement - 3;
}
.
.
.
else if(condition-N)
{
Statement -N;
}
else
{
Default statement;
}
3. This procedure repeated until all the condition is checked. if all the condition became false then the
default statement is executed.
Flowchart:
True
Condition 1? Statement - 1
False
True
Condition 2? Statement - 2
True
Condition 3? Statement - 3
.
.
. True
Condition N? Statement - N
Default Statement
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int no;
printf(“enter number\n”);
scanf(“%d”,&no);
if( no > 0 )
{
printf(“Number is Positive”);
}
else if (no < 0)
{
printf( “Number is Negative” );
}
else
{
printf( “Number is Zero” );
}
getch();
}
Switch statement:
It is used to select one among multiple decisions.
The switch statement is also known as multi-choice or multi decision statement.
„switch‟ successively tests a value against a list of integers or character constant.
When a match is found, the statement(s) associated with that value is executed.
Break statement send the control to the next statement after switch statement.
If the case not found then the statement associated with the default case is executed.
Syntax:
Example:
void main()
{
int a=2;
clrscr();
switch(a)
{
case 1:
printf(“One \n”);
break;
case 2:
printf(“Two”);
break;
case 3:
printf(“Three”);
break;
default:
printf(“Wrong choice”);
getch();
}
O/p: Two
Syntax:
goto label ;
…………. Statement;
…………. ………
Label: ………..
Target statement comes after the goto. Target statement comes before the goto.
Control Statements:
When we want to repeatedly run a particular statement until a particular condition is met, then in that case we
use Loop Statements.
While loop
Do while loop
For loop
While loop:
Syntax:
while (Condition)
{
Statements;
}
Example:
i =1;
Do-While loop:
Syntax:
do
{
// statements inside the loop
}
While (condition);
Example:
i =1;
do
{
Prepared By: Department of Computer Engineering Page 10
Subject Name: Basic Computer Programming Unit No: 03 Subject Code: 4310702
printf( “% d \n ” , i );
i = i+1;
} while (i < = 10);
For loop :
In For Loop, we write both the initialization and control condition of the variable together inside
the parentheses “()”.
If the condition of For Loop is true, then the statement inside it is run, or else the statement is not
run.
Syntax:
Initialization
False
Condition?
True
Increment/
Decrement Body
Syntax:
Example:
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
Break statement:
It terminates the loop or switch statement and transfers execution to the statement immediately
following the loop or switch.
Keyword used is "break".
Break from loop:
for( ; ; )
{
for( ; ; )
{
Prepared By: Department of Computer Engineering Page 12
Subject Name: Basic Computer Programming Unit No: 03 Subject Code: 4310702
if(condition)
break;
}
--------------------
}
Example;
for(i=1; i<=5 ; i++)
{
if(i==3)
break;
printf("%d\n",i);
}
Output: 1 2
Continue statements :
It causes the loop to skip the remainder of its body and starts the next iteration.
Keyword used is "continue".
for( ; ; )
{
--------------
if( condition)
continue;
------------
Example:
UNIT: 4
Array and Pointers
Introduction of Array:
Definition: Array is a collection of variables of same data type known by same name.
Types of Arrays:
One-dimensional arrays.
Two-dimensional arrays.
Multidimensional arrays.
Characteristics of an array:
Array store elements that have same data type.
Array store elements in subsequent memory location.
Array size should be mention in the declaration.
Array name represent the address of starting elements.
Declaration of array:
Syntax:
datatype array_name[size];
Example:
int a[5];
float f[5];
Initialization of array:
(1) Compile time:
datatype arrayname[size]={List of value};
int a[5]={10,20,30,40,50};
for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Declaration of array:
Syntax:
datatype Arrayname [row size] [column size];
Example:
int a[3][3] ;
where, int is datatype, a is a arrayname , row size is 3 and column size is 3.
float f[5][10];
Initialization of array:
(1) Compile time:
int a[3][3]={ {11,12,13}, {14,15,16}, {17,18,19} } ;
int a[3][3] = {11,12,13,14,15,16,17,18,19};
col 0 col 1 col 2
row 0 11 12 13
14 15 16
row 1
a[1][0] a[1][1] a[1][1]
17 18 19
row 2
a[2][0] a[2][1] a[2][2]
(In memory Representation of array)
(2) Run time:
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf(" %d ", &a[i][j] );
}
}
Example: Program to read 3X3 matrix (2D array elements) and print it.
#include<stdio.h>
void main()
{
int i,j,a[3][3];
printf("Enter elements of 3X3 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The elements of 3X3 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
Introduction to a String:
String:
A string is a sequence of zero or more characters followed by a NULL '\0' character.
String is always terminating with NULL '\0' character.
Syntax:
char stringname[size];
where size is the number of characters.
Example:
char city[10] ;
Initialization of string variable:
(1) Compile time:
char city[9]= " NEW YORK";
(2) Run time:
char city[9];
scanf("%s", city);
OR
char city[9];
gets(ciy);
puts() :
This function prints a string on the console screen. It is also defined in the <stdio.h> header file of C.
Example: Program to read and print a string using gets() and puts().
void main()
{
char day[10];
printf("Enter day: \n");
gets(day);
Pointer
Introduction to Pointer:
Pointer is a variable which store the address of another variable.
Advantage of pointer:
Pointer reduces the code and improves the performance.
We can return multiple values from a function using the pointer.
It makes you able to access any memory location in the computer's memory.
Characteristics of Pointers:
Example:
int *p;
Initialization of pointer:
Syntax:
Pointer variable= &variable-name;
Example:
int price;
int *p;
p=&price;
Example:
#include<stdio.h>
Void main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the number
printf("Value of p variable is %d \n",*p); // * is used to dereference a pointer therefore *p means get the
value stored at the address contained by p.
}
Types of pointer : void and null
void pointer
Syntax:
void *variable name
include<stdio.h>
void main()
{
int a = 10;
void *p;
p = &a;
printf(“%d”, *(int *)p);
}
NULL Pointer:
A pointer that is not assigned any value but NULL is known as the NULL pointer.
If you don't have any address to be specified in the pointer at the time of declaration, you can
assign NULL value. It will provide a better approach.
int *p=NULL;
pointer to pointer:
Pointer variable may pointes to another pointer variable.
It is also known as double pointer.
The first pointer is used to store the address of a variable whereas the second pointer is used to
store the address of the first pointer.
Syntax:
Data type **pointer variable name;
Example:
int **p; // pointer to a pointer which is pointing to an integer.
Example:
int n,*p1,**p2;
n=10;
p1=&n;
p2=&p1;
Example:
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **p2;
p = &a; // pointer p is pointing to the address of a
p2 = &p; // pointer p2 is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p); // Address of a will be printed
printf("address of p: %x\n",p2); // Address of p will be printed
printf("value stored at p: %d\n",*p); // value stoted at the address contained by p i.e. 10 will be
printed
printf("value stored at p2: %d\n",**pp); // value stored at the address contained by pointer
stored at p2
}
UNIT: 5
Functions
Introduction to Functions:
Definition of Function: Function is a group of statement to perform a specific task.
Types of functions:
1. Library function
2. User-defined Function
1. By using functions, we can avoid rewriting same logic/code again and again in a program.
2. We can call C functions any number of times in a program and from any place in a program.
3. We can track a large C program easily when it is divided into multiple functions.
User-defined Function:
The function which is develop by user itself is known as User-defined function.
Example: main()
User-defined function is created by the user at the time of writing of program.
Example:
int sum(int x, int y)
{
int total;
total = x + y;
return total;
}
When a function is called using the value of variables, then it is known as call by value.
The value of variables, which are to be passed, will be copied from the variables of the calling functions
to the variables of the called functions.
All the process done on the duplicate variables rather then actual variables.
Example:
#include <stdio.h>
void swap(int , int); // function prototype
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b); // function call
printf("After swapping values in main a = %d, b = %d\n",a,b); //actual parameters a=10, b=20
}
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a=20, b=10
}
When a function is called using the address of variables, then it is known as call by reference.
Instead of passing the value of variables from calling function to the called function, addresses of the
variables are passed.
All the process done on the actual variables.
Example:
#include <stdio.h>
void swap(int *, int *); // function prototype
void main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
Recursion
Advantages of Recursion:
The recursion is very flexible in data structure like stacks.
Using recursion, the length of the program can be reduced.
Disadvantages of Recursion:
It requires extra storage space.
The recursion function is not efficient in execution speed and time.
Proper termination is required; otherwise it leads to infinite loop.
Built-in Functions:
C Maths Function:
The <math.h> header file contains various methods for performing mathematical operations such as
sqrt(), pow(), ceil(), floor() etc.
ceil(number)
Rounds up the given number.
It returns the integer value which is greater than or equal to given number.
Ex: ceil(3.6)
It returns 4.
floor(number)
Rounds down the given number. It returns the integer value which is less than or equal to given number.
Ex: floor(3.6)
It returns 3.
sqrt(number)
Returns the square root of given number.
Ex: sqrt(16)
It returns 4.
pow(base, exponent)
Returns the power of given number.
Ex pow(2,4)
It returns 16.
abs(number)
Returns the absolute value of given number.
Ex: abs(-16)
It returns 16.
C String Functions:
There are many important string functions defined in "string.h" library.
3) strcat(first_string, second_string) concats or joins first string with second string. The
result of the string is stored in first string.
Storage Classes in C:
Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a
variable. There are four types of storage classes in C
Automatic
External
Static
Register
extern RAM Zero Global Till the end of the main program Maybe declared
anywhere in the program
static RAM Zero Local Till the end of the main program, Retains value
between multiple functions call
UNIT: 6
Structure, Union and Files
Structure
Introduction to Structures and Declaration, Initialization and accessing of
Structures:
Structure is a collection of logically related data items with different data type.
Structure must be defined first, that may be used later to declare structure.
Syntax:
struct structure_name
{
datatype member1;
datatype member2;
---
datatype memberN;
};
struct is a keyword, it declares structure to hold data.
Structure_Name is the name of structure (structure tag).
member1, member2,… are members of structure.
Example:
struct student
{
char name[20];
int roll_no;
float per;
}s1;
Here s1 is a structure variable.
Structure Initialization:
Example:
struct student
{
char name[20];
int roll_no;
float per;
}s1={"Arav",20,85.5};
Example:
#include<stdio.h>
struct student
{
char name[20];
int roll_no;
float per;
}s1;
void main()
{
struct student s1={"Arav",20,85.5};
printf("\nName : %s",s1.name);
printf("\nRollNo : %d",s1.roll_no);
printf("\nPercent : %f",s1.per);
}
Example:
union student
{
char name[20];
int roll_no;
float per;
};
Example:
union student
{
char name[20];
int roll_no;
float per;
}u1;
void main()
{
u1.roll_no=65;
printf("\nRollNo : %d",u11.roll_no);
}
2. Any member can be accessed at any time. Only one member can be accessed at a time.
Array Structure
An array is a collection of variables of same A structure is a collection of variables of
1
data type known by same name. different data type known by same name.
For array we have to declare an array For structure we have to design and declare a
3. variable and use it. data structure before the variables of that type
are declared and used.
Example: Example:
int a[5]; struct data
{
4. int a;
float b;
char c;
};
For Example:
#include<stdio.h>
enum week {Mon=1, Tue, Wed, Thur, Fri, Sat, Sun};
void main()
{
enum week day;
day = Wed;
printf("%d",day);
getch();
}
Output:2
typedef :
The typedef is a keyword that is used in C programming to provide existing data types with a new name.
typedef keyword is used to redefine the name already the existing name.
When names of datatypes become difficult to use in programs, typedef is used with user-defined datatypes.
Syntax:
typedef <existing_name> <alias_name>
Example:
typedef long mylong
Prepared By: Department of Computer Engineering Page 4
Subject Name: Basic Computer Programming Unit No: 06 Subject Code: 4310702
Files
Introduction to text Files
File:
A file is a group of related data which is stored in a disk.
File operations:
(1) Naming a file
(2) Opening a file
(3) Reading data from a file
(4) Writing data to a file
(5) Closing a file
fp=fopen("E:\\abc.txt", "r");
---------------------
fclose();
char name[10]=”abc”;
int id=1;
FILE *fp;
Fp= fopen(“ E:\\ abc.txt”, “w”);
fprintf(fp,”%s %d”, name,id);
fclose(fp);
getch();
}
fscanf() : Read all data written in file and store in variables.
Syntax: fscanf(fp , “Control String “, List);
Where fp is a file pointer.
List may include variable, constants and string.
Example:
void main()
{
char name[10];
int id;
FILE *fp;
Fp= fopen(“ E:\\ abc.txt”, “r”);
fscanf(fp,”%s %d”, name, &id);
printf(“Name : %s and id : %d”, name, id);
fclose(fp);
getch();
}
Example of reading data from a file and writing to it(in text mode):
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
fp = fopen("hello.txt", "w");
printf("Enter data");
while( (ch = getchar()) != EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("hello.txt", "r");
fclose(fp);
}
BCP (4310702)
Assignment: 1
Assignment: 2
Assignment-4
Assignment-5
1) What is user defined(UDF) and library function? Explain with two example of each.
2) Explain call by value with example.
3) Explain call by reference with example.
4) Explain recursion with example.
5) Explain following string function with example.
(1) strcpy() (2) strcat() (3) strcmp() (4) strlen()
6) List out math in-built functions.
7) List available storage class in C.
Assignment-6
5. Explain keyword.
Keywords are those words whose meaning is predefined.
The programmer cannot change the meaning of keywords.
There are 32 keywords available in c.
All the keywords must be written in lowercase.
Following is the list of keywords:
Int char float double
Long short signed unsigned
If else do while
For break continue switch
Case default consulting goto
Struct union void return
Static extern volatile register
Auto enum sizeof typedef
6. Explain identifier.
Identifier is are the words which are defined by the programmer in a program.
Identifier refers to the name of variable, array, function etc.
It consists of letters, digits and underscore.
First character must be an alphabet or underscore.
Keywords cannot be use as identifier.
Identifier name are case sensitive.
The name of identifier should be meaningful.
2
7. Write the Basic Structure of C Program.
Documentation section:
Documentation consists of a set of comment line(/* */) giving the name of the
program.
Header File(Link Section):
Link section provides instruction to the compiler to link function from the system
library(Header Files like #include<stdio.h>).
Constant Declaration:
Definition section defines all symbolic constants(e.g.-#define pi 3.14).
Global declaration section:
There are some variable that are used more than one function. Such Variables are called global
variable and that is outside all the function. This section also declares the entire user define
functions.
Main () function section:
Every c program must have one main () function section. This section contains
twoparts. Declaration part and executable part. Declaration part declares all the
variables used in the executable part. These two parts must be appearing
between opening and closing braces.
3
There are three types of integer.
Decimal: A decimal integer constant consist of any combination of digits from 0to 9.Example
123,-345,001.
Octal: An octal integer constant consists of any combination of digits from 0 to 7with a leading
0.Example:01234, 0564.
2. Real constant: Real constant is a sequence of digits with decimal points. Such aconstant are
used to represent weight, height etc.
3. Character constant: Character constant is a single character enclosed in a singlequotation
mark.
Example: ‘a’
Back slash constants are the special type of character constants which actuallyconsists of
two characters. These are known as escape sequences.
The combination of backslash and a character is known as escape sequence.
Escape sequence start with back slash ‘\’ character.
Escape Sequence Meaning
‘\0’ End of string
‘\n’ End of line
‘\t’ Horizontal tab
4. String Constant: String constant is a sequence enclosed in a double quotationmark.
Example: ‘’Hello’’
Constant can be declared in a program using count keyword.For
Example: const PIE = 3.14;
The variable name must begin with a letter. Some system permit underscore as a first
character.
Example: int 1NO; is not valid
The length should not be more than eight characters.
Variable name should not be a keyword.
Example: int case is not valid
A white space is not allowed in the name of variable.
Example: int NO 1; is not valid.
Variable is case sensitive. Uppercase and lowercase are different.
Example: int MAX;int max;
Here both MAX and max are treated as different variable.
12. Define symbolic constants & list out Rules of defining symbolic constant.
#define is a preprocessor directive which is used to defined symbolic constant.
Symbolic name is a name of the constant, which we want to define.
Value of the constant represents the value, which we want to assign to the constant.
Rules of defining symbolic constant
The rules for naming symbolic constant are same as the rules for naming variables. Symbolic
names are written in capital letters to distinguish them from the normal variable.
There should be no blank space between the pound sign ‘#’ and the word defines.
‘#’ must be the first character in the line.
1. A blank space is required between #define and symbolic name and between the symbolic
name and the value.
2. #define statement must not end with a semicolon.
3. After defining the symbolic constant they should not be assigned any other valuein the
program.
4. There is no need to declare data type for the symbolic constant.
+ Addition
- Subtraction
* Multiplication
/ Division
% modulo
5
Ex: a+b, a-b, a*b, a%b, a/b
where a & b variables & known as Operand (variable).
Example:
int a,b,sum,sub,div,mul,mod;a =
15;
b = 3;
sum = a+b; // sum = 18
sub = a-b; // sub = 12
mul = a*b; // mul = 45
div = a / b; // div = 5
mod = a%b; // mod = 0
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
== Is equal to
!= Is not equal to
Relational operators are used in decision statements.
Such as while & if to decide the course of action of a running program.
Example:
int a = 15, b = 12;
Expression Result
a>b True
a<b False
a>=b True
a<=b False
a==b False
a!=b True
6
15. Explain Increment & Decrement Operators in detail.
Increment Operator ++
Decrement operator --
The increment operator ++ adds 1 to the operand, while decrement operator --subtracts 1.
We use the increment and decrement statements in for and while loops.
A prefix operator first adds 1 to the operand and then the result is assigned to thevariable on
the left.
A postfix operator first assigns the value to the variable on the left and then increment
the operand.
Postfix Increment Operator
Example:
int a,b;
a=10;
b = a++;
printf(“a = %d”,a);
printf(“b = %d”,b);
Output:
a=11
b=10
Prefix Increment Operator
Example:
int a,b;
a=10;
b = ++a;
printf(“a = %d”,a);
printf(“b = %d”,b);
Output:
a=11
b=11
Flowchart of if….else :
8
Example:
if(n%2==0)
{
}
else
{
}
18. Explain the else if ladder with example.
Used when we need multipath decisions.
General Form:
if(condition1)
statement1;
else if(condition2)
statement2;
else if(condition3)
statement3;
else
default-statement;
statement-x;
General Form
switch (expression)
{
case value1:
block-1;
break;
casevalue2:
block-2;
break;
default:
default-block;
break;
}
statement-x;
Where, the expression is an integer expression or characters.
Value1, value2….. Are constants or constant expression and are known as case
labels.
Case labels end with a colon(:)
When the switch is executed, the value of the expression is successfully comparedagainst the
values. If a case is found whose value matches with the value of the expression, then the block
of statement that follows the case are executed.
10
Example
switch( op)
{
case +:
ans = a + b;
break;
case -:
ans = a - b;
break;
default:
printf(“ wrong choice ”);
break;
}
printf(“ Answer is : %d”, ans);
{} is known as body of the loop. If body contain one statement then it is notnecessary to
enclose body of the loop in the pair of brace {}.
In while loop, the condition is evaluated first and if it is true then the statement inthe body of
the loop is executed.
After executing body , the condition is evaluated again and if it is true body isexecuted
again. This process is repeated as long as the condition is true. The control move out once
the condition is false.
Flowchart:
11
Example: Print 1 to 10 numbers using while loop.
i =1;
while (i < = 10)
{
printf( “% d \n ” , i );i =
i+1;
}
In do..while loop the body is executed and then the condition is checked.
If the condition is true the body is executed again and again, as long as thecondition is
true.
The control moves out of loop once, the condition become false.
The body of the loop is executed at least once even if the condition is false firsttime.
Flowchart:
12
22. Give differences between While & Do-while loop.
While Do--while
It is entry control loop, means first condition is It is exit control loop, first body is executedand
checked, and if it is true thenbody of the loop then condition is checked and if the condition is
executes, otherwise nextstatement after the loop
true, then again body of the loop will be executed,
will be executed.
otherwise next statement after the loop will be
executed
At first time, condition is false, thenbody At first time, condition is false, at least oncethe
is not executed. body is executed.
Syntax: Syntax:
while (condition) Do
{ {
Body of the loop; Body of the loop
} }while(condition);
Example: Example:
i =1; i =1;
do do
{ {
printf( “% d \n ” , i );i = printf( “% d \n ” , i );i =
i+1; i+1;
} while (i < = 10); } while (i < = 10);
13
Example: Print 1 to 10 numbers using for loop.
15
Introduction to a String:
String:
A string is a sequence of zero or more characters followed by a NULL '\0'
character. String is always terminating with NULL '\0' character.
Syntax:
char stringname[size];
where size is the number of characters.
Example:
char city[10] ;
Initialization of string variable:
(1) Compile time:
char city[9]= " NEW YORK";
(2) Run time:
char city[9];
scanf("%s", city);
OR
char city[9];
gets(y);
Pointer
Introduction to Pointer:
Pointer is a variable which store the address of another variable.
Advantage of pointer:
Pointer reduces the code and improves the performance.
We can return multiple values from a function using the pointer.
It makes you able to access any memory location in the computer's memory.
Characteristics of Pointers:
16
Types of pointer :
void pointer
Syntax:
void *variable name
include<stdio.h
>void main()
{
int a = 10;
void *p;
p = &a;
printf(“%d”, *(int *)p);
}
NULL Pointer:
A pointer that is not assigned any value but NULL is known as the NULL pointer.
If you don't have any address to be specified in the pointer at the time of declaration, you
canassign NULL value. It will provide a better approach.
int *p=NULL;
Introduction to Functions:
Definition of Function: Function is a group of statement to perform a specific task.
Types of functions:
1. Library function
2. User-defined Function
17
User-defined Function:
The function which is develop by user itself is known as User-defined function.
Example: main()
User-defined function is created by the user at the time of writing of program.
When a function is called using the value of variables, then it is known as call by
value.
The value of variables, which are to be passed, will be copied from the variables of the calling functions
to the variables of the called functions.
All the process done on the duplicate variables rather then actual variables.
Example:
#include <stdio.h>
void swap(int , int); // function prototype
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b); // function call
printf("After swapping values in main a = %d, b = %d\n",a,b); //actual parameters a=10, b=20
}
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a=20, b=10
}
When a function is called using the address of variables, then it is known as call by reference.
Instead of passing the value of variables from calling function to the called function, addresses of the
variables are passed.
18
All the process done on the actual variables.
Example:
#include <stdio.h>
void swap(int *, int *); // function prototype
void main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b); // function call
printf("After swapping values in main a = %d, b = %d\n",a,b); // actual parameters a = 20, b = 10
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters a=20, b=10
}
Recursion
}
Example :
19
void main()
{
function1();
}
function1()
{
function1();
Advantages of Recursion:
The recursion is very flexible in data structure like stacks.
Using recursion, the length of the program can be reduced.
Disadvantages of Recursion:
It requires extra storage space.
The recursion function is not efficient in execution speed and time.
Proper termination is required; otherwise it leads to infinite loop.
Storage Classes in C:
Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of
avariable. There are four types of storage classes in C
Automatic
External
Static
Register
extern RAM Zero Global Till the end of the main program Maybe
declaredanywhere in the program
20
static RAM Zero Local Till the end of the main program, Retains
value between multiple functions call
Example:
Struct student
{
Char name[20];
int roll_no;
float per;
}s1;
Here s1 is a structure variable.
Structure
initialization:
Example:
struct student
{
char name[20];
int roll_no;
float per;
}s1={"Arav",20,85.5};
21
User-defined Datatypes: enum, typedef
Fo rExample:
#include<stdio.h>
enum week{Mon=1,Tue,Wed,Thur,Fri,Sat,Sun};
void main()
{
enum week day;
day = Wed;
printf("%d",day);
getch();
}
Output:2
Typedef:
The typedef is a keyword that is used in C programming to provide existing data types with a new
name. typedef keyword is used to redefine the name already the existing name.
When names of data types become difficult to use in programs, typedef is used with user-defined data
types.
Syntax:
typedef<existing_name><alias_name>
Example:
typede flong mylong
Files
Introduction to text Files
File:
A file is a group of related data which is stored in a
disk. File operations:
(1) Naming a file
(2) Opening a file
22
(3) Reading data from a file
(4) Writing data to a file
(5) Closing a file
fclose();
23
Seat No.: ________ Enrolment No.______________
Q.1 Answer any seven out of ten. દશમ ાંથી કોઇપણ સ તન જવ બ આપો. 14
1. State TRUE or FALSE
a) C is a low level language.
b) # symbol is used to denote preprocessor statement.
૧. સ ચાં છે કે ખોટાં તે જણ વો.
a) C એક નીચ લેવલની ભ ષ છે .
b) # નનશ ની નિિોસેસર ને બત વવ વપર ય છે .
2. Define algorithm. Write advantages of algorithm. (write at least two
advantages)
૨. અલગોરીધમ ની વ્ય ખ્ય આપો. તેન ફ યદ લખો. (ઓછ મ ાં ઓછ બે ફ યદ લખવ )
3. State TRUE or FALSE
a) main( ) is inbuilt function in C.
b) global variable automatically initialized by 0.
૩. સ ચાં છે કે ખોટાં તે જણ વો.
a) main( ) એ C મ ાં રહેલ ઇનનબલ્ટ ફાંક્શન છે .
b) ગ્લોબલ વેરીએબલ આપમેળે 0 થી િ રાંનભત થ ય છે .
4. Define data type. List different derived data types.
૪. ડેટ ટ ઈપને વ્ય ખ્ય ઇત કરો. ત રવેલ ડેટ ટ ઈપની ય દી લખો.
5. Fill in the blanks by selecting proper option.
a) Array name represents the address of __________ element.
i. last ii. first
iii. middle iv. highest
Q.3 (a) Give the differences between BREAK and CONTINUE. (write at least three 03
differences)
િશ્ન. 3 (અ) BREAK અને CONTINUE વચ્ચેનો તફ વત લખો. (ઓછ મ ાં ઓછ ત્રણ તફ વત લખવ ) ૦૩
OR
(a) Differentiate between structure and union. (write minimum three points) 03
(અ) structure અને union વચ્ચેનો તફ વત લખો. (ઓછ મ ાં ઓછ ત્રણ તફ વત લખવ ) ૦૩
(b) Write a short note on flow chart. 03
(બ) ફ્લો ચ ટણ પર ટૂાંકનોંધ લખો. ૦૩
OR
(b) Explain entry controlled loop with an example. 03
(બ) entry controlled loop ઉદ હરણ સ થે સમજાવો. ૦૩
(c) Explain call by value with example. 04
2/4
(ક) call by value ઉદ હરણ સ થે સમજાવો. ૦૪
OR
(c) Explain call by reference with example. 04
(ક) call by reference ઉદ હરણ સ થે સમજાવો. ૦૪
(d) Write a C program using loop to print the following output pattern. 04
1
21
321
4321
(ડ) લૂપ નો ઉપયોગ કરી નીચેની આઉટપૂટ પેટનણ છ પવ મ ટેનો C િોગ્ર મ લખો. ૦૪
1
21
321
4321
OR
(d) Write a C program using loop to print the following output pattern. 04
1
23
456
7 8 9 10
(ડ) લૂપ નો ઉપયોગ કરી નીચેની આઉટપૂટ પેટનણ છ પવ મ ટેનો C િોગ્ર મ લખો. ૦૪
1
23
456
7 8 9 10
Q.4 (a) Write a C program to copy the content of a string STR1 into another string 03
STR2. (Note: take two string variables as STR1 and STR2. STR1 = ‘AABU’
િશ્ન. ૪ (અ) એક સ્ટ્રીંગ STR1 મ ાંન ઘટકોને બીજી સ્ટ્રીંગ STR2 મ ાં નકલ કરવ મ ટેનો C િોગ્ર મ લખો. ૦૩
(નોંધ: બે સ્ટ્રીંગ વેરીએબલ STR1 તથ STR2 લેવ . તેમજ STR1 = ‘AABU’ લેવ.ાં )
OR
(a) Write a C program to check whether the entered string is palindrome or not. 03
(અ) દ ખલ કરેલ સ્ટ્રીંગ પેલીન્રોમ (મરજબાંધ) છે કે નહીં તે તપ સતો C િોગ્ર મ લખો. ૦૩
(b) Explain 1-D array with example. 04
(બ) 1-D એરે ઉદ હરણ સ થે સમજાવો. ૦૪
OR
(b) Explain UDF with example. 04
(બ) UDF ઉદ હરણ સ થે સમજાવો. ૦૪
(c) Define pointer. 07
Write advantages and disadvantages of pointer.
Write a C program to swap the values of two integer variables using pointer.
(ક) પોઈન્ટરની વ્ય ખ્ય આપો. ૦૭
પોઈન્ટરન ફ યદ અને ગેરફ યદ લખો.
બે ઇન્ટીજર વેરીએબલ ને અદલ બદલ કરવ મ ટેનો C િોગ્ર મ પોઇન્ટર નો ઉપયોગ કરી લખો.
Q.5 (a) Write a C program to find the sum of all the numbers entered in array. (Note: 04
take single dimensional array of size 10)
િશ્ન. ૫ (અ) એરેમ ાં દ ખલ કરેલ તમ મ અાંકોનો સરવ ળો કરવ નો C િોગ્ર મ લખો. (નોંધ: 10 ન મ પનો એક ૦૪
પ કરમ નણક એરે લેવો)
(b) Explain fprintf( ) with example. 04
3/4
(બ) fprintf( ) ઉદ હરણ સ થે સમજાવો. ૦૪
(c) Write a C program to print the numbers in reverse manner on the screen using 03
for loop. (Note: 20 will be the first number and 0 will be the last number to
print in the screen)
(ક) લૂપની મદદથી અાંકોને ઉલટ ક્રમમ ાં સ્ટ્ક્રીન પર છ પવ નો લખવ નો C િોગ્ર મ લખો. (નોંધ: સ્ટ્ક્રીન ૦૩
પર િથમ અાંક 20 તથ છે લ્લો અાંક 0 છ પવો)
(d) Differentiate between array and structure. (write at least three points) 03
(ડ) એરે અને સ્ટ્રક્ચર ને જદ ત રવો. (ઓછ મ ાં ઓછ ત્રણ મદ્દ લખવ ) ૦૩
************
4/4
Seat No.: ________ Enrolment No.______________
1
(બ) Enumerated data type ઉદાહરણ આપી િમજાવો. ૦૪
(c) Draw flowchart and explain for loop with example. 07
(ક)for લૂપ નો ફલોચાર્ટ ડ્રો કરો અને ઉદાહરણ આપી િમજાવો. ૦૭
Q.4 (a) Define Array. Explain initialization of two dimensional array. 03
(અ) Array ની વ્યાખ્યા આપી. Two dimensional array નુાં ૦૩
initializationસમજાિો.
(b) State the difference between structure and union. 04
(બ) Structure and union નોતફાવત િમજાવો. ૦૪
(c) Explain recursion with example. 07
(ક) Recursion ઉદાહરણ આપી િમજાવો. ૦૭
OR
Q.4 (a) Explain following string function with example. (1) strcpy (2) strcat 03
(3) strcmp (4) strlen
(અ) ઉપર ના ફાંક્શન ઉદાહરણ આપી િમજાવો ૦૩
(1) strcpy (2)strcat (3) strcmp (4)strlen
(b) Explain gets( ) and puts( ) functions. 04
(બ) gets( ) and puts( ) ફાંક્શન િમજાવો. ૦૪
(c) What is user defined and library function? Explain with two 07
example of each.
(ક) User defined and library ફાંક્શન શુાં છે .બે ઉદાહરણ આપી િમજાવો. ૦૭
Q.5 (a) Explain ?: operator with example. 03
(અ) ?: ઓપેરેર્ર ઉદાહરણ આપી િમજાવો. ૦૩
(b) Define pointer. Explain how pointers are declared and initialized. 04
(બ) પોઈન્ર્ર શુાં છે . પોઇન્ર્રને declared and initialized કરી ને િમજાવો. ૦૪
(c) Write a program to find average of 1 to 10. 07
(ક) ૧ થી ૧૦ િુધીની િાંખ્યાનો એવરેજ શોધવાનો પ્રોગ્રામ લખો. ૦૭
OR
Q.5 (a) Explain fscanf() & fprintf () functions with examples. 03
(અ) fscanf() & fprintf() ફાંક્શન ઉદાહરણ આપી િમજાવો. ૦૩
(b) Explain one dimensional array with example. 04
(બ) સિાંગલ ડાઇમેન્શન array ઉદાહરણ આપી િમજાવો. ૦૪
(c) Write a program to insert and display element in 2*2 matrixes 07
(ક) 2*2 મેરરક્ષ માાં એલીમેન્ર્ ઇન્િર્ટ અને રડસ્ત્્લેકરવાનો પ્રોગ્રામ લખો. ૦૭