C Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Q.1) Program.

A collection of functions which when compiled, assembled, linked, loaded, and executed performs some
task. C is a computer programming language. That means that you can use C to create lists of instructions
for a computer to follow. C is one of thousands of programming languages currently in use. C has been
around for several decades and has won widespread acceptance because it gives programmers maximum
control and efficiency. C is an easy language to learn. It is a bit more cryptic in its style than some other
languages, but you get beyond that fairly quickly.

A program (noun) is executable software that runs on a computer. It is similar to a script, but is often much
larger in size and does not require a scripting engine to run. Instead, a program consists of compiled code
that can run directly from the computer's operating system.

Q.2) Function Overloading in C++

Function refers to a segment that groups code to perform a specific task.

In C++ programming, two functions can have same name if number and/or type of arguments passed are
different.

These functions having different number or type (or both) of parameters are known as overloaded functions.
For example:

int test() { }

int test(int a) { }

float test(double a) { }

int test(int a, double b) { }

Here, all 4 functions are overloaded functions because argument(s) passed to these functions are different.

Notice that, the return type of all these 4 functions are not same. Overloaded functions may or may not have
different return type but it should have different argument(s).
You can have multiple definitions for the same function name in the same scope. The definition of the
function must differ from each other by the types and/or the number of arguments in the argument list. You
cannot overload function declarations that differ only by return type.

Following is the example where same function print() is being used to print different data types −
Live Demo
Q.3) write program in C++ to calculate the factorial of any integer number entered through keyboard.

(Recursion)

#include<iostream.h>
#include<conio.h>

void main()
{
int i, no, fact=1;
clrscr();
cout<<"Enter the any no. : "<<endl;
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial: "<<fact;
getch();
}

Output

Enter the any no. : 4

Factorial: 24
Q.4) C program to reverse a number entered through keyboard.

#include <stdio.h>

int main()
{
int number, temp, remainder, reverse = 0;

printf("Enter a positive integer :");


scanf("%d", &number);

temp = number;

while (temp > 0)


{
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp /= 10;
}

printf("The reverse of %d is %d.", number, reverse);

return 0;
}

Output
Enter a positive integer :12345
The reverse of 12345 is 54321.
Q.5) Looping Statement in C.
Looping statement are the statements execute one or more statement repeatedly several number of times.
In C programming language there are three types of loops; while, for and do-while.

Why use loop ?

When you need to execute a block of code several number of times then you need to use looping concept
in C language.

Advantage with looping statement

 Reduce length of Code

 Take less memory space.

 Burden on the developer is reducing.

 Time consuming process to execute the program is reduced.

Types of Loops.

There are three type of Loops available in 'C' programming language.

 while loop

 for loop

 do..while

 While loop: In while loop First check the condition if condition is true then control goes inside the
loop body other wise goes outside the body. while loop will be repeats in clock wise direction.

For loop: for loop is a statement which allows code to be repeatedly executed. For loop contains 3
parts Initialization, Condition and Increment or Decrements.

do-while: A do-while loop is similar to a while loop, except that a do-while loop is execute at least one
time. A do while loop is a control flow statement that executes a block of code at least once, and then
repeatedly executes the block, or not, depending on a given condition at the end of the block (in while).

Nested loop: In Nested loop one loop is place within another loop body. When we need to repeated
loop body itself n number of times use nested loops. Nested loops can be design upto 255 blocks.
Q.5) Friend Function in C++

Private members are accessed only within the class they are declared. Friend function is used to access the private
and protected members of different classes. It works as bridge between classes.

 Friend function must be declared with friend keyword.


 Friend function must be declare in all the classes from which we need to access private or protected
members.
 Friend function will be defined outside the class without specifying the class name.
 Friend function will be invoked like normal function, without any object.
 Following are some important points about friend functions and classes:
1) Friends should be used only for limited purpose. too many functions or external classes are
declared as friends of a class with protected or private data, it lessens the value of encapsulation of
separate classes in object-oriented programming.
 2) Friendship is not mutual. If a class A is friend of B, then B doesn’t become friend of A
automatically.
 3) Friendship is not inherited (See this for more details)
 4) The concept of friends is not there in Java.
Q) Recursion in C.

When a called function in turn calls another function , a process of ‘chaining’ occurs. Recursion is a special
case of this process, where a function calls itself.
The process of calling a function by itself is called recursion and the function which calls itself is called
recursive function.
Or
In programming languages, if a program allows you to call a function inside the same function, then it is
called a recursive call of the function.

But while using recursion, programmers need to be careful to define an exit condition from the function,
otherwise it will go into an infinite loop.

Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial
of a number, generating Fibonacci series, etc.

EXAMPLE:
void recursion() {
recursion(); /* function calls itself */
}

int main() {
recursion();
}
Q. Arrays in C.

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.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one
array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and
the highest address to the last element.
Few key notes:

 Arrays have 0 as the first index not 1. In this example, mark[0]


 If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4]
 Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be 2124d,
address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.
Q. Data types in C.

1. Fundamental Data Types


o Integer types
o Floating type
o Character type
2. Derived Data Types
o Arrays
o Pointers
o Structures
o Enumeration

Int - Integer data types


Integers are whole numbers that can have both positive and negative values but no decimal values. Example:
0, -5, 10
In C programming, keyword int is used for declaring integer variable. For example:
int id;
Here, id is a variable of type integer.
You can declare multiple variable at once in C programming. For example:
int id, age;

float - Floating types:

Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare a floating point
variable in C by using either float or double keyword. For example:
float accountBalance;
double bookPrice;
Here, both accountBalance and bookPrice are floating type variables.
In C, floating values can be represented in exponential form as well. For example:
float normalizationFactor = 22.442e2;
Difference between float and double
The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float
data type) is 8 bytes. Floating point variables has a precision of 6 digits whereas the precision of double is
14 digits.

Chr-Character types:

A single character can be defined as a character (char) type data. Characters are usually stored in 8 bits(one
byte) of internal storage.
Q. Write a C program to reverse a string added through keyboard.

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

int main()
{
char arr[100];

printf("Enter a string to reverse\n");


gets(arr);

strrev(arr);

printf("Reverse of the string is \n%s\n", arr);

return 0;
}
Q. Functions in C.

Function with No argument and No Return value

In this method, We won’t pass any arguments to the function while defining, declaring or calling the
function. This type of functions will not return any value when we call the function from main() or any sub
function. When we are not expecting any return value but, we need some statements to be printed as output
then, this type of functions are very useful.

Function with no argument and with Return value

In this method, We won’t pass any arguments to the function while defining, declaring or calling the
function. This type of functions will return some value when we call the function from main() or any sub
function. Data Type of the return value will depend upon the return type of function declaration. For
instance, if the return type is int then return value will be int.

Function with argument and No Return value

If you observe the above 2 methods, No matter how many times you executive, it will give the same output.
We don’t have any control over the values of the variables a and b because they are fixed values. In real
time, we mostly deal with dynamic data means we have to allow the user to enter his own values rather than
fixed ones.

This method allows us to pass the arguments to the function while calling the function. But, This type of
functions will not return any value when we call the function from main () or any sub function.

If we want to allow our user to pass his own data to the function arguments but we are not expecting any
return value then, this type of functions are very useful.

Function with argument and Return value

This method allows us to pass the arguments to the function while calling the function. This type of
functions will return some value when we call the function from main () or any sub function. Data Type of
the return value will depend upon the return type of function declaration. For instance, if the return type is
int then return value will be int.

This type of user defined functions are called as fully dynamic function means, it provide maximum control
to the end user.
Q.) Constructor & Destructor.

Constructor:

A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is
automatically called when object(instance of class) create. It is special member function of the class.

Special characteristics of Constructors:

 They should be declared in the public section


 They do not have any return type, not even void
 They get automatically invoked when the objects are created
 They cannot be inherited though derived class can call the base class constructor
 Like other functions, they can have default arguments
 You cannot refer to their address
 Constructors cannot be virtual

Destructor:

As the name implies, destructors are used to destroy the objects that have been created by the constructor
within the C++ program. Destructor names are same as the class name but they are preceded by a tilde (~). It
is a good practice to declare the destructor after the end of using constructor. Here's the basic declaration
procedure of a destructor:

~Cube()
{

The destructor neither takes an argument nor returns any value and the compiler implicitly invokes upon
the exit from the program for cleaning up storage that is no longer accessible.
Q.) Std. Library String Functions. (Strcat, strcpy, Strcmp)

(1) strcat() Function:

The strcst function joins two strings together. It takes the following form:

strcat(string1, string2);

(2) strcmp() Function:

It compares two strings identified by the arguments and has a value 0 if they are equal. If they are not, it
has the numeric difference between the first nonmatching characters in the string. It takes the form:

strcmp(string1,string2);

(3) strcpy() Function:

The strcpy function works almost like a string-assignment operator. It takes the form:

Strcpy(sting1,string2);

(4) strlen() Function:

This function counts and returns the number of characters in a string.

n=strlen(string);
Q.) diff. Between Continue and break statement.

BASIS FOR BREAK CONTINUE

COMPARISON

Task It terminates the execution of It terminates only the current

remaining iteration of the iteration of the loop.

loop.

Control after 'break' resumes the control of 'continue' resumes the control of

break/continue the program to the end of the program to the next iteration of

loop enclosing that 'break'. that loop enclosing 'continue'.

Causes It causes early termination of It causes early execution of the

loop. next iteration.

Continuation 'break' stops the continuation 'continue' do not stops the

of loop. continuation of loop, it only stops

the current iteration.

Other uses 'break' can be used with 'continue' cannot be executed with

'switch', 'label'. 'switch' and 'labels'.


Q.) if..else and Switch case statement.

BASIS FOR IF-ELSE SWITCH

COMPARISON

Basic Which statement will be executed Which statement will be executed is decided

depend upon the output of the by user.

expression inside if statement.

Expression if-else statement uses multiple switch statement uses single expression for

statement for multiple choices. multiple choices.

Testing if-else statement test for equality as switch statement test only for equality.

well as for logical expression.

Evaluation if statement evaluates integer, switch statement evaluates only character or

character, pointer or floating-point integer value.

type or boolean type.

Sequence of Execution Either if statement will be executed or switch statement execute one case after

else statement is executed. another till a break statement is appeared or

the end of switch statement is reached.

Default Execution If the condition inside if statements is If the condition inside switch statements does

false, then by default the else not match with any of cases, for that instance

statement is executed if created. the default statements is executed if created.

Editing It is difficult to edit the if-else It is easy to edit switch cases as, they are

statement, if the nested if-else recognized easily.

statement is used.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy