Sunday, 24 April 2011

CHAPTER 2: C programing




C programing is done using software’s like notepad, turbo c++, gc++, dev c++.
You may use any of it.
Now c programs go like this


And when you execute this it looks like this.

Now the program that was written and executed is called the source code..
#include

#include
int main()
{
printf("hello world");
getch();
return 0;
}
Let us understand this step by step.
Execution of every c program starts with main().
Main is a pre declared inbuilt c function that gets executed first. // we will elaborate this later
Now before the main function is executed c goes to all statements with # in front of it.
# is called pre-processor-directive. Which means pre (before) processor (u start processing main) directive (perform this).
# will tell the computer to include first then include
Now stdio.h and conio.h are header files or simply files similar to txt files doc files of your programs cpp files. //cpp stands for c++
These are called header files and hence have an extention “.h” in the end of the file name.
Note: every file has a name as .
The programs that we are going write will have names as .c or .cpp
These header files are necessary as they include the code to several in built functions.
Let’s elaborate what I just said.
Let’s say you have a watching machine at home. And when I ask you its functions you would say. It can rinse, spin, perform water level adjustment etc.
All these are functions.
Now c also has inbuilt functions which is stored in different header files like printf, scanf, clrscr, etc.
Printf is used to print certain things or display certain things on the output window (the black screen shown above)
Similarly scanf is used to scan user input values or take input from user and store them in variables.
All these functions are stored in different header files because they are very large in number and including all of those in a single program without the necessity will lead to large and uselessly excess size of the program.
So we can chose only to include those header files that we need for our program and reject those that we don’t need.
int main()
{
getch();
return 0;
}
These are necessary part of c programs.
Getch is an inbuilt function of stdio.h and is used to take a character input from user (whatever the character the user inputs is now shown to the user on the output window) and until the user does not provide the input the program execution remains paused. Hence it is used to temporarily pause the execution of programs sometimes by programmers.
printf("hello world");
It is used to display “hello world” on the output window.
int main()
{
}
As we discussed earlier that int main() is a function and every function has a limited zone of active region which is bounded or marked by the braces
Zone starts here -“ { “
Zone ends here – “ } ”
Now what we write within this braces are the instructions or tasks the main function is going to perform.
Like printf(“hello world”);
Now in c every statement that is written ends with a “;” which signifies that the statement ends here.
It is similar to the full stop you put at the end of each line in English language.
Now open up your c++ IDE and lets program..
Q1. Write a program to display your name and age and class.
Soln:-
First we will need these 2 lines..
#include
#include
Then we are going to need the main function so let’s put it here
int main()
Now every function has a zone so we need these {}.
{
}
Now inside this zone we are going to need some special fix codes as shown
{
getch();
return 0;
}
Now to display your name we are defiantly going to need printf so..
{
Prinf(“”);
getch();
return 0;
}
Inside printf you may write your name and age
Printf(“Hi this is Vikash singh age 21 :) ”);
Now to execute it first you need to compile it.
In English compile means to “Get or gather together (a large number of something)”
In computer language compile means “Use a computer program to translate source code written in a particular programming language into computer-readable machine code that can be executed”
In simple terms.
Compilation (compile) is a process that programs are passed through to verify that the code you have written are right and check for any errors in the program and finally prepare and executable file for your program.
Now since computer does not understand printf, scanf as computer at hardware level only understands 0 and 1 (binary) values. So at the end of compilation a similar to binary files is prepared that is almost equivalent to your program and which the computer can understand and run on it.
To perform the compilation you have to first compile it by pressing ctrl + f9.
And the exe is produced by the IDE you are using (if it’s not a notpad hi hi!!).
Then to run the program press ctrl + f10.
In the above 2 statements ctrl + means ctrl pressed and simultaneously you also press f9 or f10.
Q2. Take 1 user input numbers and display it.
Sol:
We begin the program as every other program
#include
#include
int main()
{
}
Next step we have to take a user input and display it..
For this we are going to need a variable and since user input is going to be a integer so we will declare an int variable as...
{
int a;
}
Now lets take the user input.
{
int a;
printf("enter number: ");
scanf(“%d”,&a);
}
Now this is how we take user input. “Scanf” then brackets ( )
Then in inverted cotes put (“%d”,)
Here %d as we will be taking integer from user.
If we ware to take character we would have used (“%c”,)
If we ware to take float we would have used (“%f”,)
[Float represents fractional values like 1.2, 1.000, 23.676, etc.]
Then we have put &a as scanf(“%d”,&a);
Let’s say for now this is how input from user is taken, as this topic of & is related to pointers which is not in the scope of discussion right now.
So…
If we ware to take character we would have used scanf(“%c”,&var);
If we ware to take float we would have used scanf(“%f”,&var);
Now we already have the number so let’s continue with the program..
Now we have to display the number to the user.
Which we will do with printf as…
printf(“num is :%d”, a);
}
[NOTE] here we haven’t put &sum but just sum.
Remember this for future reference do display the value of a variable we don’t use “&value”, we simply use “value”.
But to take user input and store it in a variable we use “&value”
Now we end the program with
printf(“num is :%d”, a);
getch();
return 0;
}
So the program is:
#include
#include
int main()
{
int a;
printf("enter number: ");
scanf(“%d”,&a);
printf(“num is :%d”, a);
getch();
return 0;
}
And when we execute it with ctrl+f9 and then ctrl+f10 we get the following output.

No comments:

Post a Comment