Sie sind auf Seite 1von 39

SECTION B

1. Describe the basic datatypes with example?

 C data types are defined as the data storage format that a variable can store
a data to perform a specific operation.
 Data types are used to define a variable before to use in a program.
 Size of variable, constant and array are determined by data types.
BASIC DATA TYPES IN C LANGUAGE:
1.1. INTEGER DATA TYPE:
 Integer data type allows a variable to store numeric values.
 “int” keyword is used to refer integer data type.
 The storage size of int data type is 2 or 4 or 8 byte.
 It varies depend upon the processor in the CPU that we use. If we are using 16
bit processor, 2 byte (16 bit) of memory will be allocated for int data type.
 Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of
memory for 64 bit processor is allocated for int datatype.
 int (2 byte) can store values from -32,768 to +32,767
 int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
 If you want to use the integer value that crosses the above limit, you can go for
“long int” and “long long int” for which the limits are very high.
1.2. CHARACTER DATA TYPE:
 Character data type allows a variable to store only one character.
 Storage size of character data type is 1. We can store only one character using
character data type.
 “char” keyword is used to refer character data type.
 For example, ‘A’ can be stored using char datatype. You can’t store more than
one character using char data type.
 Please refer C – Strings topic to know how to store more than one characters in
a variable.
1.3. FLOATING POINT DATA TYPE:
Floating point data type consists of 2 types. They are

1. float
2. double
1. FLOAT:
 Float data type allows a variable to store decimal values.
 Storage size of float data type is 4. This also varies depend upon the processor
in the CPU as “int” data type.
 We can use up-to 6 digits after decimal using float data type.
 For example, 10.456789 can be stored in a variable using float data type.
2. DOUBLE:
 Double data type is also same as float data type which allows up-to 10 digits
after decimal.
 The range for double datatype is from 1E–37 to 1E+37.

Example:

#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}

OUTPUT:

Storage size for int data type:4


Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

2. Explain the arithmetic and relational operator available in c with


examples?

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like


addition, subtraction, multiplication and division. There are five arithmetic
operators available in C (+, -, *, /, %). All arithmetic operators are binary
operators, ie; they operate on two operands to produce the result.
EXAMPLE:

#include <stdio.h>

int main()

int a = 25, b = 10;

printf("Sum: %d\n", a + b);

printf("Difference: %d\n", a - b);

printf("Product: %d\n", a * b);

printf("Quotient: %d\n", a / b);


printf("Remainder: %d\n", a % b);

return 0;

Output:

Sum: 35

Difference: 15

Product: 250

Quotient: 2

Remainder: 5

Relational operator

Relational operators are binary operators(operates on two operands) and are used
to relate or compare two operands. There are four relational operators in C (i.e <,
<=, >, >=). If the relationship between the operands is correct, it will return 1 and
returns 0 otherwise.

Apart from four relational operators, C has two equality operator (== and !=) as
well for comparing operands. Now let's take a look at different relational and
equality operators and how they operate on the operands.

Operator Name Description Example


Checks if the first operand is less 10 < 5 returns 0
'Less than'
< than second operand and returns 1 and 5 <
operator
if it's true, else returns 0 10 returns 1

Checks if the first operand is less


'Less than or 10 <= 10 returns
than or equals to second operand
<= equals to' 1 and 10 <=
and returns 1 if it's true, else
operator 5 returns 0
returns 0

Checks if the first operand is 10 > 5 returns 1


'Greater than'
> greater than second operand and and 5 >
operator
returns 1 if it's true, else returns 0 10 returns 0

Checks if the first operand is


'Greater than 10 >= 10 returns
greater than or equals to second
>= or equals to' 1 and 5 >=
operand and returns 1 if it's true,
operator 10 returns 0
else returns 0

Checks if the two operands are 10 == 10 returns


Equality
== equal are returns 1 if it's true, else 1 and 10 ==
operator
returns 0 1 returns 0

Checks if the two operands are 10 != 10 returns


Non-equality
!= equal are returns 1 if they are not 0 and 10 !=
operator
equal, else returns 0 1 returns 1
Examples of Relational Operator

#include <stdio.h>

int main()

int a = 25, b = 10;

printf("%d < %d : %d\n", a, b, a < b);

printf("%d <= %d: %d\n", a, b, a <= b);

printf("%d > %d : %d\n", a, b, a > b);

printf("%d >= %d: %d\n", a, b, a >= b);

printf("%d == %d: %d\n", a, b, a == b);

printf("%d != %d: %d\n", a, b, a != b);

return 0;

OUTPUT:

25 < 10 : 0

25 <= 10: 0

25 > 10 : 1
25 >= 10: 1

25 == 10: 0

25 != 10: 1

3. Explain the action of switch statement in c?

A switch statement tests the value of a variable and compares it with multiple
cases. Once the case match is found, a block of statements associated with that
particular case is executed.

Each case in a block of a switch has a different name/number which is referred to


as an identifier. The value provided by the user is compared with all the cases
inside the switch block until the match is found.

If a case match is found, then the default statement is executed, and the control
goes out of the switch block.
SYNTAX:

switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x;

EXAMPLE:

include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
RESULT:

Value is 8

4. write a c program to find area of a circle ?

CODE:

#include <stdio.h>
#include <math.h>
#define PI 3.142

void main()
{
float radius, area;

printf("Enter the radius of a circle \n");


scanf("%f", &radius);
area = PI * pow(radius, 2);
printf("Area of a circle = %5.2f\n", area);
}

OUTPUT:

Enter the radius of a circle


30
Area of a circle = 2827.80
5.Explain one dimensional array with example?
A one-dimensional array (or single dimension array) is a type of linear array.
Accessing its elements involves a single subscript which can either represent a row
or column index.

An array can be of any type, For example: int , float , char etc. If an array is of
type int then it's elements must be of type int only. ... An array of one
dimension is known as a one-dimensional array or 1-D array

SYNTAX:

datatype anArrayname[sizeofArray];

EXAMPLE:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], n;
printf("How many element you want to store in the array ? ");
scanf("%d",&n);
printf("Enter %d element to store in the array : ",n);
for(int i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("The Elements in the Array is : \n");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
getch();
}

6. write a note on nesting function in c? Exlain with example

Some programmer thinks that defining a function inside an another function is


known as “nested function”. But the reality is that it is not a nested function, it is
treated as lexical scoping. Lexical scoping is not valid in C because the compiler
cant reach/find the correct memory location of the inner function.

Nested function is not supported by C because we cannot define a function within


another function in C. We can declare a function inside a function, but it’s not a
nested function.
Because nested functions definitions can not access local variables of the
surrounding blocks, they can access only global variables of the containing
module. This is done so that lookup of global variables doesn’t have to go through
the directory. As in C, there are two nested scopes: local and global (and beyond
this, built-ins). Therefore, nested functions have only a limited use. If we try to
approach nested function in C, then we will get compile time error.
EXAMPLE:

// C program of nested function

// with the help of gcc extension

#include <stdio.h>

int main(void)

auto int view(); // declare function with auto keyword

view(); // calling function

printf("Main\n");

int view()

printf("View\n");

return 1;

}
printf("GEEKS");

return 0;

OUTPUT :

view

Main

GEEKS

7. Explain putchar () and puts function ?


puts ():

puts() function is a file handling function in C programming language which is


used to write a line to the output screen.

SYNTAX:

int puts(const char *string)

EXAMPLE:

#include <stdio.h>
#include <string.h>

int main()
{
char string[40];
strcpy(str, "This is a test string");
puts(string);
return 0;
}
RESULT:
This is a test string

Putchar():

The putchar(int char) method in C is used to write a character, of unsigned char


type, to stdout. This character is passed as the parameter to this method.

SYNTAX:

int putchar(int char)

Parameters: This method accepts a mandatory parameter char which is the


character to be written to stdout.
Return Value: This function returns the character written on the stdout as an
unsigned char. It also returns EOF when some error occurs.
EXAMPLE:

#include <stdio.h>

int main()
{

// Get the character to be written


char ch = 'K';

// Write the Character to stdout


putchar(ch);

return (0);
}
RESULT:

8. Explain Else if ladder with example ?

 When we have multiple options available or we need to take multiple


decisions based on available condition, we can use another form of if
statement called else…if ladder.
 In else…if ladder each else is associated with another if statement.
 Evaluation of condition starts from top to down.
 If condition becomes true then the associated block with if statement is
executed and rest of conditions are skipped.
 If the condition becomes false then it will check for next condition in a
sequential manner.
 It repeats until all conditions are cheeked or a true condition is found.
 If all condition available in else…if ladder evaluated to false then default
else block will be executed.
 Else…if ladder provides multi way decision making when any one
alternative is to be select among the available option.

SYNTAX:

if (condition 1)
{

// block 1

else if (codition 2)

// block 2

else if(codition 3)

// block 3

else

// default block

EXAMPLE:

#include<stdio.h>
#include<conio.h>

void main()

int x, y, z, ch;

clrscr();

printf("\n1.Addition\n2.Subtraction\n3.Multiplication\4.Division\n");

printf("\nEnter your choice :");

scanf("%d", &ch);

printf("\nEnter X and Y :");

scanf("%d %d", &x, &y);

if (ch == 1)

z = x + y;

else if (ch == 2)

z = x - y;
}

else if(ch == 3)

z = x * y;

else if(c == 4)

z = x/y ;

else

printf("\n Invalid choice! Please try again!");

printf("\n Answer is %.2f", z);

getch();

9. Explain basic structure of c program ?


Documentation Consists of comments, some description of the program, programmer name
and any other useful points that can be referenced later.

Link Provides instruction to the compiler to link function from the library function

Definition Consists of symbolic constants.

Global Consists of function declaration and global variables.


declaration

main( ) Every C program must have a main() function which is the starting point of
{ the program execution.

Subprograms User defined functions.

10. Explain getchar() and gets function?

gets():

Reads characters from the standard input (stdin) and stores them as a C string into
str until a newline character or the end-of-file is reached.

SYNTAX:
char * gets ( char * str );

EXAMPLE:

#include <stdio.h>
#define MAX 15

int main()
{
char buf[MAX];

printf("Enter a string: ");


gets(buf);
printf("string is: %s\n", buf);

return 0;
}

RESULT:

INPUT: HELLO

OUTPUT: HELLO

getchar() :

getchar is a function in C programming language that reads a single character


from the standard input stream stdin, regardless of what it is, and returns it to the
program. It is specified in ANSI-C and is the most basic input function in C. It is
included in the stdio.h header file.

SYNTAX:

int getchar(void);

EXAMPLES:

include <stdio.h>
int main()
{
printf("%c", getchar());
return 0;
}

RESULT:

INPUT : HELLO

OUTPUT:HELLO

SECTION C

1. What are the different loop used in c language?

Depending upon the position of a control statement in a program, a loop is


classified into two types:

1. Entry controlled loop


2. Exit controlled loop

In an entry controlled loop, a condition is checked before executing the body of a


loop. It is also called as a pre-checking loop.

In an exit controlled loop, a condition is checked after executing the body of a


loop. It is also called as a post-checking loop.

A while loop is the most straightforward looping structure. The basic format of
while loop is as follows:

while (condition) {
statements;
}
It is an entry-controlled loop. In while loop, a condition is evaluated before
processing a body of the loop. If a condition is true then and only then the body of
a loop is executed. After the body of a loop is executed then control again goes
back at the beginning, and the condition is checked if it is true, the same process is
executed until the condition becomes false. Once the condition becomes false, the
control goes out of the loop.

After exiting the loop, the control goes to the statements which are immediately
after the loop. The body of a loop can contain more than one statement. If it
contains only one statement, then the curly braces are not compulsory. It is a good
practice though to use the curly braces even we have a single statement in the
body.

In while loop, if the condition is not true, then the body of a loop will not be
executed, not even once. It is different in do while loop which we will see shortly.

CODE:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

Do-While loop

A do-while loop is similar to the while loop except that the condition is always
executed after the body of a loop. It is also called an exit-controlled loop.

The basic format of while loop is as follows:

do {
statements
} while (expression);
As we saw in a while loop, the body is executed if and only if the condition is true.
In some cases, we have to execute a body of the loop at least once even if the
condition is false. This type of operation can be achieved by using a do-while loop.

In the do-while loop, the body of a loop is always executed at least once. After the
body is executed, then it checks the condition. If the condition is true, then it will
again execute the body of a loop otherwise control is transferred out of the loop.

Similar to the while loop, once the control goes out of the loop the statements
which are immediately after the loop is executed.

The critical difference between the while and do-while loop is that in while loop
the while is written at the beginning. In do-while loop, the while condition is
written at the end and terminates with a semi-colon (;)

CODE:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:

2
4
6
8
10
12
14
16
18
20

A for loop is a more efficient loop structure in 'C' programming. The general
structure of for loop is as follows:

for (initial value; condition; incrementation or decrementation )


{
statements;
}

 The initial value of the for loop is performed only once.


 The condition is a Boolean expression that tests and compares the counter to
a fixed value after each iteration, stopping the for loop when false is
returned.
 The incrementation/decrementation increases (or decreases) the counter by a
set value.

CODE:
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

2. Explain two dimensional array with examples and write a program?

TWO DIMENSIONAL ARRAY:


The Two Dimensional Array in C language is nothing but an Array of Arrays. If
the data is linear we can use the One Dimensional Array but to work with multi-
level data we have to use Multi-Dimensional Array.

Implementing a database of information as a collection of arrays can be


inconvenient when we have to pass many arrays to utility functions to process the
database. It would be nice to have a single data structure which can hold all the
information, and pass it all at once.

2-dimensional arrays provide most of this capability. Like a 1D array, a 2D array


is a collection of data cells, all of the same type, which can be given a single name.
However, a 2D array is organized as a matrix with a number of rows and columns.

SYNTAX:

Data_Type Array_Name[Row_Size][Column_Size]

 Data_type: This will decide the type of elements will accept by two
dimensional array in C. For example, If we want to store integer values then
we declare the Data Type as int, If we want to store Float values then we
declare the Data Type as float etc
 Array_Name: This is the name you want to give it to this C two dimensional
array. For example students, age, marks, employees etc
 Row_Size: Number of Row elements an array can store. For example,
Row_Size =10 then array will have 10 rows.
 Column_Size: Number of Column elements an array can store. For example,
Column_Size = 8 then array will have 8 Columns.

EXAMPLE:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}

RESULT:

Enter value for disp[0][0]:1


Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
123
456

3. What are c token ? Explain its types

Each and every small unit in c program is known as tokens. We can also say that
tokens are the basic buildings blocks in C language which are constructed together
to write a C program. Tokens are of 6 types.

1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators
Keywords are pre-defined words in a C compiler.Each keyword is meant to
perform a specific function in a C program.Since keywords are referred names for
compiler, they can’t be used as variable name. Eg: int,void,long.
Identifiers are the name give to each program element in program.Names given to
identify Variables, functions and arrays are examples for identifiers.For example
int x; here int is the keyword and x is the identifier.

Constants are variables whose values can not be modified by the program once
they are defined. Constants have a fixed value and they are also known as
literals.Keyword const is used before datatype .

Syntax: const data_type variable_name; Eg: const int a ;

Strings are array of characters terminated by a null character. For example a string
‘toy’ has got 4 characters in it ‘t’ , ‘o’ , ‘y’ & the null character.

Symbols other than the Alphabets and Digits and white-spaces are called Special
symbols. Eg: * , & ,^ ,@, etc.

An operator is a symbol which operates on a value or a variable. For example: +is


an operator to perform addition.C programming has wide range of operators to
perform various operations. For better understanding of operators, these operators
can be classified as:

Arithmetic Operators : performs mathematical operations such as addition,


subtraction and multiplication on numerical values

Increment and Decrement Operators : C programming has two operators


increment ++ and decrement -- to change the value of an operand by 1. Increment
++ increases the value by 1 whereas decrement -- decreases the value by 1.

Assignment Operators : It is used for assigning a value to a variable. The most


common assignment operator is = .
Relational Operators: it checks the relationship between two operands. If the
relation is true, it returns 1; if the relation is false, it returns value 0. eg: == , > , <
etc.

Logical Operators :An expression containing logical operator returns either 0 or 1


depending upon whether expression results true or false. Logical operators are
commonly used in decision making in program. eg: &&,||,! .

Conditional Operators : it is a ternary operator, that is, it works on 3 operands.


The conditional operator works like this: The first expression conditional
Expression is evaluated first. This expression evaluates to 1 if it's true and
evaluates to 0 if it's false. If conditional Expression is true, expression 1 is
evaluated. If conditional Expression is false, expression 2 is evaluated.

Bit wise Operators : During computation, mathematical operations like: addition,


subtraction, addition and division are converted to bit-level which makes
processing faster and saves power. Bit wise operators are used in C programming
to perform bit-level operations. eg: &,|,^ ,-,>>,<< .

4. Write a c progrom to find biggest of three number?

CODE:

#include <stdio.h>
void main()
{ int num1, num2, num3;

printf("Enter the values of num1, num2 and num3\n");


scanf("%d %d %d", &num1, &num2, &num3);
printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);
if (num1 > num2)
{
if (num1 > num3)
{
printf("num1 is the greatest among three \n");
}
else
{
printf("num3 is the greatest among three \n");
}
}
else if (num2 > num3)
printf("num2 is the greatest among three \n");
else
printf("num3 is the greatest among three \n");
}

OUTPUT:

Enter the values of num1, num2 and num3


6 8 10
num1 = 6 num2 = 8 num3 = 10
num3 is the greatest among three

5. Summarise the rules for passing parameter to function ?


There are different ways in which parameter data can be passed into and out of
methods and functions. Let us assume that a function B() is called from another
function A(). In this case A is called the “caller function” and B is called
the “called function or callee function”. Also, the arguments which A sends
to B are called actual arguments and the parameters of B are called formal
arguments.

 Formal Parameter : A variable and its type as they appear in the prototype
of the function or method.
 Actual Parameter : The variable or expression corresponding to a formal
parameter that appears in the function or method call in the calling
environment.

Modes:
 IN: Passes info from caller to calle.
 OUT: Callee writes values in caller.
 IN/OUT: Caller tells callee value of variable, which may be updated by
callee.
Pass By Value : This method uses in-mode semantics. Changes made to formal
parameter do not get transmitted back to the caller. Any modifications to the
formal parameter variable inside the called function or method affect only the
separate storage location and will not be reflected in the actual parameter in the
calling environment. This method is also called as call by value.

CODE:
#include <stdio.h>

void func(int a, int b)

a += b;

printf("In func, a = %d b = %d\n", a, b);

int main(void)

int x = 5, y = 7;

// Passing parameters

func(x, y);

printf("In main, x = %d y = %d\n", x, y);

return 0;

OUTPUT:
In func, a = 12 b = 7

In main, x = 5 y = 7

ass by reference(aliasing) : This technique uses in/out-mode semantics. Changes


made to formal parameter do get transmitted back to the caller through parameter
passing. Any changes to the formal parameter are reflected in the actual parameter
in the calling environment as formal parameter receives a reference (or pointer) to
the actual data. This method is also called as <em>call by reference. This method
is efficient in both time and space.

CODE:

#include <stdio.h>

void swapnum(int* i, int* j)

int temp = *i;

*i = *j;

*j = temp;

int main(void)

int a = 10, b = 20;


// passing parameters

swapnum(&a, &b);

printf("a is %d and b is %d\n", a, b);

return 0;

EXAMPLE:

a is 20 and b is 10

Das könnte Ihnen auch gefallen