0% found this document useful (0 votes)
19 views

C Notes

Uploaded by

kiway68577
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C Notes

Uploaded by

kiway68577
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Notes on Principles of programming

BCSC 201

Q1. Discuss about different data types in C.


Data types in C programming language enables the programmers to appropriately select the data as
per requirements of the program and the associated operations of handling it.
Data types in c language can be broadly classified as:
1. Primitive Data Types , for example int, char, float etc.
2. User Defined Data Types, for example, enum, structure, union
3. Derived Data Types, for example, array, pointers
In this tutorial we will only focus on primitive data types, user defined and derived data types will
be discussed separately.

Primitive Data Types


The primitive data types in c language are the inbuilt data types provided by the c language itself.
Thus, all c compilers provide support for these data types.
The following primitive data types in c are available:

Integer Data Type, int


Integer data type is used to declare a variable that can store numbers without a decimal. The
keyword used to declare a variable of integer type is “int”. Thus, to declare integer data type
following syntax should be followed:
int variable_name;

Float data Type, float


Float data type declares a variable that can store numbers containing a decimal number.
Syntax
float variable_name;

Double Data Type, double


Double data type also declares variable that can store floating point numbers but gives precision
double than that provided by float data type. Thus, double data type are also referred to as double
precision data type.
Syntax
double variable_name;

Character Data Type, char


Character data type declares a variable that can store a character constant. Thus, the variables
declared as char data type can only store one single character.
Notes on Principles of programming
BCSC 201

Syntax
char variable_name;

Void Data Type, void


Unlike other primitive data types in c, void data type does not create any variable but returns an
empty set of values. Thus, we can say that it stores null.
Syntax
void variable_name;
Q2. Discuss about iterative statements in C.
You may encounter situations, when a block of code needs to be executed several number of times.
In general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. Given
below is the general form of a loop statement in most of the programming languages.

C programming language provides the following types of loops to handle looping requirements.

1 while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition
before executing the loop body.

Syntax:

while(condition) {

statement(s);
Notes on Principles of programming
BCSC 201

for loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop
variable.

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

statement(s);
}

do...while loop

It is more like a while statement, except that it tests the condition at the end of the loop body.

Syntax:
3
do {

statement(s);
} while( condition );

Q3. Discuss about conditional statements in C.


Decision making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the condition is
determined to be false.
Show below is the general form of a typical decision making structure found in most of the
programming languages −
Notes on Principles of programming
BCSC 201

S.N. Statement & Description


if statement

An if statement consists of a boolean expression followed by one or more statements.

1 if(expression) {

/* statement(s) will execute if the expression is true */


}

if...else statement

An if statement can be followed by an optional else statement, which executes when the
Boolean expression is false.

if(expression) {
2
/* statement(s) will execute if the expression is true */
}
else {
/* statement(s) will execute if the expression is false */
}

nested if statements

You can use one if or else if statement inside another if or else if statement(s).

if( expression 1) {

3
/* Executes when the expression 1 is true */
if(expression 2) {
/* Executes when the expression 2 is true */
}
}

4 switch statement

A switch statement allows a variable to be tested for equality against a list of values.

switch(expression) {
Notes on Principles of programming
BCSC 201

case constant-expression :

statement(s);
break; /* optional */

case constant-expression :
statement(s);
break; /* optional */

/* you can have any number of case statements */


default : /* Optional */
statement(s);
}

nested switch statements

You can use one switch statement inside another switch statement(s).

switch(ch1) {

case 'A':
printf("This A is part of outer switch" );
5 switch(ch2) {
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}

break;
case 'B': /* case code */
}

Q4. Write short note on 1-D array.


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.

To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows −
type arrayName [ arraySize ];
Notes on Principles of programming
BCSC 201

This is called a single-dimensional array. The arraySize must be an integer constant greater than
zero and type can be any valid C data type. For example, to declare a 10-element array called
balance of type double, use this statement −
double balance[10];

Here balance is a variable array which is sufficient to hold up to 10 double numbers.


An element is accessed by indexing the array name. Index of array starts from 0. This is done by
placing the index of the element within square brackets after the name of the array. For example,
balance[0]=200.0;

will set the value 200.0 as the first element of the array.

Q5. Write short note on 2-D array.

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array
is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size
[x][y], you would write something as follows −

type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier. A two-
dimensional array can be considered as a table which will have x number of rows and y number of
columns. A two-dimensional array a, which contains three rows and four columns can be shown as
follows −

Thus, every element in the array a is identified by an element name of the form a[ i ][ j ], where 'a'
is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in 'a'.
However, in memory, elements of 2-D array are stored in sequential way. Following figure
illustrating the actual memory representation of 2-D array. Such allocation is called row-major
format. Note that, in memory, first row is presented first, then the second and so on. Another
representation can be a column-major form. In general row-major form is supported by most of the
compilers.
Notes on Principles of programming
BCSC 201

Note:
Practice some programs also for theory examinations.
1. String reversal.
2. Number reversal.
3. Palindrome check.
4. Matrix addition, multiplication. (Important)
5. Linear search from array.

You might also like