imp_bcp
imp_bcp
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