You are on page 1of 13

C Programming

 Discuss about C character set.

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.

There are 4 types of characters in C.

Uppercase A-Z
1. Alphabets
Lowercase a-z

2. Digits All digits 0-9

3. Special Characters All Symbols: , . : ; ? ' " ! | \ / ~ _$ % # & ^ * - + < > ( ) {  } [ ]

4. White Space Characters Blank space, Horizintal tab, Carriage return, New line, Form feed

 What are the different constants in C?

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'
'='

 How do you define constants in C?

There are two simple ways in C to define constants:

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;


}

 How do you construct variable name?

Rules for Constructing Variable Names:

 A variable name is any combination of alphabets, digits or underscores. There is no rule on


how long a variable name can be. However, there may be some problems in some compilers
if the variable name is longer than 31 characters.
 The first character in the variable name must be an alphabet or underscore.
 No commas or blanks are allowed within a variable name.
 No special symbol other than an underscore can be used in a variable name.

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.

Syntax of C programming conditional operator:


(condition) ? expression1 : expression2

If the condition is true then expression1 is executed else expression2 is executed.

Example: checking whether a number is odd or even

#include<stdio.h>

main()
{
int num;

printf("Enter the Number : ");


scanf("%d",&num);

(num%2==0)?printf("Even"):printf("Odd");

 What are the different loop control structure in C?

There are 3 types of loop control structures in C language:

1. for
2. while
3. do-while

Loop Name  Syntax  Description


for (exp1; exp2; expr3) In for loop control statement, loop
{ statements;  is executed until condition
} becomes false.
where,
exp1 – variable initialization
for
( Example: i=0, j=2, k=3 )
exp2 – condition checking
( Example: i>5, j<3, k=3 )
exp3 – increment/decrement
( Example: ++i, j–, ++k )
while (condition) In while loop control statement, loop
{ statements;  is executed until condition becomes
while } false.
where,
condition might be a>5, i<10
do  In do-while loop control statement,
{ statements;  while loop is executed irrespective of
} the condition for first time. Then 2nd
do-while
while (condition); time onwards, loop is executed until
where, condition becomes false.
condition might be a>5, i<10
Examples:

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);    
 
}

 What is the difference between while and do-while loop?

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.

 Discuss about the break and continue statement.


The break statement ends the loop immediately when it is encountered. The break statement is
almost always used with if-else statement inside the loop.

How break statement works?

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.

How continue statement works?

 What is Infinite Loop? How do you stop infinite loop?

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.

ii) while 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.

Stopping an infinite loop:

1. One can terminate an infinite loop by pressing Ctrl + C keys.

2. Another way is to use break statement:

Ex.

#include <stdio.h>

main ()
{

while(1)
{
statement;
if(condition)
break;
}
}

Here the while loop is stopped when the condition under if is true.

 What are the different case control structure used in C?

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
}

How does the switch statement work?

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.

switch statement Flowchart:

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.

 Why should goto be avoided?

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.

 Give an example in favour of using goto statement.

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!" ) ;
}

 Discuss about array.

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:

int num[6] = { 2, 4, 12, 5, 45, 5 } ;

It is not mandatory to mention the size of the array during its initialization. Thus the following
declarations work well.

int n[ ] = { 2, 4, 12, 5, 45, 5 } ;

float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;

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 }

};

or, int stud[4][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 } } ;

int arr[2][3] = { 12, 34, 23, 45, 56, 45 } ;

int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ;

are perfectly acceptable.

whereas,
int arr[2][ ] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][ ] = { 12, 34, 23, 45, 56, 45 } ;

would never work.


 What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function.

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);
}

 Discuss about structure in C.

Structure is a user-defined datatype in C language which allows us to combine data of


different types together. Structure helps to construct a complex data type which is more
meaningful.

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.

In structure, data is stored in form of records.

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

When a struct type is declared, no storage or memory is allocated. To allocate memory of a


given structure type and work with it, we need to create variables.

Here's how we create structure variables:

struct Person
{
char name[50];
int citNo;
float salary;
};

int main()
{
struct Person person1, person2, p[20];
return 0;
}

Another way of creating a struct variable is:

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.

Access members of a structure

There are two types of operators used for accessing members of a structure.

1. Dot(.) - Member operator


2. -> - Structure pointer operator

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;
};

struct Patient p1 = { 180.75 , 73, 23 }; //initialization

or,

struct Patient p1;


p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;

Array of Structure

We can also declare an array of structure variables in which each element of the array will
represent a structure variable.

Example : struct employee emp[5];

 What is the difference between i++ and ++i?

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.

But, in this case

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

 How is an array name interpreted? How it is passed to function?


The array name is actually a pointer pointed to the first memory location of allocated fixed-size
contagious memory-locations for an array. The compiler automatically converts a[i] to *(a+i)
when accessing the (i+1)th value of a.
Like other variables an array can also be passed to a function.
But, it must be remembered that passing an array to a function is always call by reference.
So, modifying the element of the array within the function will actually modify the actual
array.
It has either of the following general form:
return-type function-name(type arrayName[]){
//…body
}
or, return-type function-name(type *arrayName){
//…body
}

 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.

You might also like