Professional Documents
Culture Documents
C character set:
It is the fundamental raw material of any language and they are used to represent information. Like
natural languages, computer language will also have well defined character set, which is useful to
build the programs.
Uppercase A-Z
1. Alphabets
Lowercase a-z
4. White Space Characters Blank space, Horizintal tab, Carriage return, New line, Form feed
C Constants:
Its value is fixed throughout the program that means constants are those variables which
value is not changed throughout the program.
C constants can be divided into two major categories:
Primary Constants
Secondary Constants
Examples:
Constant type Example
Integer 426
+782
-8000
-7605
Real +325.34
426.0
-32.76
-48.5792
+3.2e-5
4.1e8
-0.2e+3
-3.2e-5
Character ‘p’
'A'
'I'
'5'
'='
1. Using #define
e.g.,
#include <stdio.h>
#define value 10
2. Using const keyword
e.g.,
#include <stdio.h>
main()
{
const int value = 10;
…
…
}
Note: You should always try to give meaningful names to variables. For example:
firstName is a better variable name than fn
What is Conditional Operator?
The conditional operator is kind of similar to the if-else statement as it does follow the same
algorithm as of if-else statement but the conditional operator takes less space and helps to
write the if-else statements in the shortest way possible.
#include<stdio.h>
main()
{
int num;
(num%2==0)?printf("Even"):printf("Odd");
1. for
2. while
3. do-while
for loop:
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
printf("%d ",i);
}
}
while loop:
#include <stdio.h>
int main()
{
int i=3;
while(i<10)
{
printf("%d\n",i);
i++;
}
}
do-while loop:
#include <stdio.h>
int main()
{
int i=1;
do
{
printf("Value of i is %d\n",i);
i++;
}while(i<=4 && i>=2);
}
while do while
Loop is executed only Loop is executed for first time irrespective of the
when condition is condition. After executing while loop for first time,
true. then condition is checked.
The continue statement skips the current iteration of the loop and continues with the next
iteration. The continue statement is almost always used with the if-else statement.
A loop becomes an infinite loop if a condition never becomes false. It is done as follows:
i) for loop:
Since none of the three expressions that form the 'for' loop are required, you can make an
endless loop by leaving the conditional expression empty.
#include <stdio.h>
int main () {
for( ; ; )
{
printf("This loop will run forever.\n");
}
When the conditional expression is absent, it is assumed to be true. You may have an
initialization and increment expression, but C programmers more commonly use the for(;;)
construct to signify an infinite loop.
#include <stdio.h>
main () {
while(1) {
printf("This loop will run forever.\n");
}
}
[Any number except zero (0) can be used in a while loop to make it an
infinite loop because the element zero (0)is only false in C.
Ex.
#include <stdio.h>
main ()
{
while(1)
{
statement;
if(condition)
break;
}
}
Here the while loop is stopped when the condition under if is true.
Switch:
The switch statement allows us to execute one code block among many alternatives.
One can do the same thing with the if...else..if ladder. However, the syntax of the
switch statement is much easier to read and write.
Syntax of switch...case
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding statements after the matching label are
executed. For example, if the value of the expression is equal to constant2, statements
after case constant2: are executed until break is encountered.
If there is no match, the default statements are executed.
If we do not use break, all statements after the matching label are executed.
By the way, the default clause inside the switch statement is optional.
goto statement:
The goto statement allows us to transfer control of the program to the specified label.
Syntax of goto Statement
goto label;
... .. ...
... .. ...
label:
statement;
The label is an identifier. When the goto statement is encountered, the control of the program
jumps to label: and starts executing the code.
As C is a highly structured language, one must take care not to use too much of
unconditional goto branching statements which makes the program a poor way of coding.
The goto statement alters the sequential flow of logic that is the characteristic of C
language. Code can get confusing very quickly when it takes arbitrary paths and jumps
from place to place. The use of goto statement may lead to code that is buggy and hard to
follow.
goto can be very useful to break from nested loops. W hen we want to take the control out of the
loop that is contained in several other loops goto can be used. The following program illustrates
this.
main( )
{
int i, j, k ;
for ( i = 1 ; i <= 3 ; i++ )
{
for ( j = 1 ; j <= 3 ; j++ )
{
for ( k = 1 ; k <= 3 ; k++ )
{
if ( i == 3 && j == 3 && k == 3 )
goto out ;
else
printf ( "%d %d %d\n", i, j, k ) ;
}
}
}
out :
printf ( "Out of the loop at last!" ) ;
}
Arrays a kind of data structure that can store a fixed-size sequential collection of elements
of the same type. An array is used to store a collection of data, but it is often more useful
to think of an array as a collection of variables 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.
Declaring Arrays:
One-dimensional array:
type arrayName [ arraySize ];
Two-dimensional array:
type arrayName [ arraySize1 ] [ arraySize2 ];
Array Initialisation:
One-dimensional array:
It is not mandatory to mention the size of the array during its initialization. Thus the following
declarations work well.
Two-dimensional array:
For two dimensional array one has to mention row size and column size or only column size. Thus
int stud[4][2] = {
{ 1234, 56 },
{ 1212, 33 },
{ 1434, 80 },
{ 1312, 78 }
};
whereas,
int arr[2][ ] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][ ] = { 12, 34, 23, 45, 56, 45 } ;
In the recursive program, the solution to the base case is provided and the solution of the bigger
problem is expressed in terms of smaller problems.
The idea is to represent a problem in terms of one or more smaller problems, and add one or more
base conditions that stop the recursion.
For example, we compute factorial n if we know factorial of (n-1). The base case for factorial would
be n = 0. We return 1 when n = 0.
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
It is somewhat similar to an Array, but an array holds data of similar type only. But structure
on the other hand, can store data of any type, which is practical more useful.
For example: If someone have to write a program to store Student information, which will
have Student's name, age, branch, permanent address, father's name etc, which included
string values, integer values etc, then something will be required which can hold data of
different types together.
Syntax of structure
struct structureName
{
dataType member1;
dataType member2;
...
};
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
Here, a derived type struct Person is defined. Now, you can create variables of this type.
Create struct variables
struct Person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct Person person1, person2, p[20];
return 0;
}
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
In both cases, two variables person1, person2, and an array variable p having 20 elements of
type struct Person are created.
There are two types of operators used for accessing members of a structure.
Suppose, you want to access the salary of person2. Here's how you can do it.
person2.salary
Structure Initialization
Like a variable of any other datatype, structure variable can also be initialized at compile
time.
struct Patient
{
float height;
int weight;
int age;
};
or,
Array of Structure
We can also declare an array of structure variables in which each element of the array will
represent a structure variable.
These two operators are called pre-increment (++i) and post-increment -*(i++) operator.
++i will increment the value of i, and then return the incremented value.
Ex.
i = 5;
j = ++i;
(Output: i is 6, j is 6)
i++ will increment the value of i, but return the original value that i held before
being incremented.
Ex.
i = 5;
j = i++;
(Output: i is 6, j is 5)
Reason:
When increment expression is used along with assignment operator, then operator precedence
will come into picture.
i=5;
j=i++;
In this case, precedence of = is higher than postfix ++. So, value of i is assigned to i before
incrementing i. Here j becomes 5 and i becomes 6.
i=5;
j=++i;
precedence of prefix ++ is more than = operator. So i will increment first and the incremented
value is assigned to j Here i and j both become 6.
How can the number of bytes allocated to each basic data type to be determined
for a particular C compiler?
C provides sizeof operator to determine the number of bytes allocated to each basic data type. The
sizeof() operator gives the size of the type passed as argument to it.
e.g.: sizeof(int) gives 2
What is the purpose of a header file? Is the use of a header file absolutely necessary?
The header files are used to store library functions of C. So, when a program needs to use
library functions, it must include a header file that contains the required function.
How can you link multi-file programs in C considering some of the called functions are in a
different file?
Multiple files of C program can be linked together and used in a single program by
including them using #include directives. A C file can be included within another using
one of the following ways:
#include<file-name>
It search the file in the specified
include directory. e.g.:
#include<stdio.h>
#include”file-name”
It search the file in the path preceded the file-name. If no path is given then it searches the
file in the source directory.
e.g.: #include” D:\TC\X.C” - the file X.C is in the TC directory in the D drive.
Define an algorithm.
An algorithm is a precise specification of a sequence of instructions to be carried out in order to
solve a given problem. Each instruction tells what task is to be performed. Actually an algorithm is
a series of mathematical steps written in simple English sentence.