C Notes
C Notes
C Notes
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.
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) { }
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
Factorial: 24
Q.4) C program to reverse a number entered through keyboard.
#include <stdio.h>
int main()
{
int number, temp, remainder, reverse = 0;
temp = number;
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.
When you need to execute a block of code several number of times then you need to use looping concept
in C language.
Types of Loops.
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.
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:
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];
strrev(arr);
return 0;
}
Q. Functions in C.
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.
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.
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.
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.
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)
The strcst function joins two strings together. It takes the following form:
strcat(string1, string2);
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);
The strcpy function works almost like a string-assignment operator. It takes the form:
Strcpy(sting1,string2);
n=strlen(string);
Q.) diff. Between Continue and break statement.
COMPARISON
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
Other uses 'break' can be used with 'continue' cannot be executed with
COMPARISON
Basic Which statement will be executed Which statement will be executed is decided
Expression if-else statement uses multiple switch statement uses single expression for
Testing if-else statement test for equality as switch statement test only for equality.
Sequence of Execution Either if statement will be executed or switch statement execute one case after
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
Editing It is difficult to edit the if-else It is easy to edit switch cases as, they are
statement is used.