C programming notes
C programming notes
1. What is a function?
Function is a collection/group of codes that does a specific
task and is assigned to one name or a group of logical series of activities or
program statements that is assigned to one single name is called function.
Each programming language comes with its built-in function or if there is a
function that is not available in the programming language. You can create one.
The important part is that the function makes the program easier to build and
run. More importantly, they can be used again.
P.S: You can omit the parenthesis as well or write the name of the function
without the parenthesis as well and it will not affect the output.
P.S:
However, this doesn’t mean that this is a zero. The difference between a regular
zero and a terminating zero is that in ASCII(American Standard Character
information interchangeable) table, the zero as in 0 is represented as a
character like this ‘0’ and it’s decimal value is 48 and the null(means empty) is
represented as 0. The string terminator can be typed as \0. It is usually used
to mark the end of a string or used as a pointer.
5. What is a Character Array?
Well, in C programming. You can’t store strings in a variable. C
doesn’t support the storage of string type data. In other words, there is no
string variable. However, to store a string in a variable in the C language. You
will need to create a list of Character variables. Each storing a character from
the string that you want to save in the variable. This doesn’t create a string
variable. However, it will create a list of character variables so that you can
store your string. Creating a list like that is known as Character Array. Please
look below the example how they look like:
Example:
char CharacterVariable[5];
char CharacterVariable[] = “Love Programming”;
Yes. It looks like creating a character variable but adding those brackets “[]”
will help you create a character array. You can define a character array with
the brackets and a number. Inside the bracket, you tell the computer how
many character variables you want. So do remember the length of a string and
don’t forget the string-terminator ‘\0’. C will create exactly the number of
variables that you requested. So be careful.
Also, for the second example. If you are assigning a string to a character
array at the defining stage. Then you don’t need to write the number in the
brackets. C will automatically count the number of characters in your string
and add the number in the bracket. It will also create a variable for the string
terminator as well.
P.S:
Please keep this in mind that once you assign a string to an existing
character array. You cannot assign a new string to that particular array. Like
this:
char NoString[12] = “I love you!”;
NoString = “You love me?”; //This is wrong and it is not allowed in C.
In order to store the new string in an existing array. You will need to either
redefine each element in an array or use the strcpy function to add the new
string to the certain character array.
7. What is an Algorithm?
An Algorithm is a finite step-by-step process to solve a problem.
OR
The process in which logical statements are organized to solve a certain
problem is known as Algorithm.
8. What is Pseudo code?
Pseudo code is an informal and plain term algorithm. Meaning that a
programmer uses Pseudo code to create an algorithm in its own easily
understandable terms for a certain problem.
For example:
( Here is a problem that is solved in pseudo code.)
Problem: Create a program for a robot that when he is sent to buy eggs. He
will only buy 12 eggs if the price of the egg is rs.15 or less then rs.15.
Otherwise, he will purchase 10.
Pseudo code:
if price <=15
get 15 eggs.
else
get 10 eggs.
end if.
This is a Pseudo code. This will not work in a compiler as it is not a proper
algorithm but just an image of a solution to a certain problem. You created an
image of the solution to the problem in your own term. Now you will use the
standard keywords of C to create the algorithm. Pseudo code helps others to
understand the solution to the problem regardless of the programming language
that they are using.
9. What are conditions in programming?
Conditions(often referred as program control, decision, or expression)
control the direction of a program and allow you to make a decision in which
direction the program should go. The directions are set by the programmer and
they can be seen after the program has been executed. The condition checks
for a statement if it is true or false. By True or false in computer, it means
either 1 or 0. The value of True is 1 and the value of False is 0. Condition
checks those values and will let the program execute the section of code which
is the part of that condition statement.
For example:
Like in the Pseudo code example. I created a program for a robot
that when he is sent to buy eggs. He will only buy 12 eggs if the price is rs.15
or less than. Otherwise, the robot will buy 10 eggs.
if price <=15
Please give me 12 eggs.
else
That’s way too expensive. Please give me 10 eggs now.
end if.
These are conditions statements that allow you to make a decision for the
program which section of code should run. Like in the above example. If the
price of the egg is 15 rupees the robot will purchase a dozen eggs. If the price is
greater than 15 rupees. The robot will purchase 10 eggs. That’s how conditions
work.
P.S:
I forgot to mention that conditions check for the true or false of a
statement. Like in the above example. The robot will buy 12 eggs if the price of
a single egg is 15 rupee. If the statement is true, then the robot will buy a
dozen eggs. If it is false, then the robot will buy 10 eggs.