Archive

Posts Tagged ‘c++ program’

[C Tutorial 3] The First Program

July 1st, 2011 No comments

A program to display "Hello Sweety!" as the output. All the explanations are in the form of comments. And everything between /* and */ is a comment and everything else is a part of the code. To prevent confusion, the program is first written without the comments.

The Program (without comments/explanation) :

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

int main()
{
printf("Hello Sweety!");
getch();
return 0;
}

The Program (with comments for explanation) :

#include <stdio.h> /* a header file which has the function
printf used to display text as output */

#include <conio.h> /* another header file that has the function
used below */

int main() /* main - the main function,
int - function returns an integer type,
() - for arguments, no arguments in this case */

{ /* main function begins */

printf("Hello Sweety!"); /* shows Hello Sweety! on the screen */
getch(); /* waits for a character before closing program */
return 0; /* since main function should return integer */

} /*main function ends */

It may not look like it but this is one of the most important programs you will ever learn. The syntax used in the program can be generalized as following :

#include <filename.h> is the general syntax you'll use to include any header file. You'll need to include many header files as you deal with more complex problems. stdio.h contains the printf function that we've used to display output.

return-type function-name (arguments) {...} is used to define a function. In this case the function is called main and the return type is integer with no arguments.

printf("..."); a function that shows whatever is inside the double inverts on the output. This is your first C statement. All C statements end with a semi-colon (;)

getch(); this function waits for a character input from the user and then exits the program. Try running the code without it and with it and you'll know what i mean. This function is actually part of a header file conio.h but will work without it on most compilers - but no harm adding the conio.h header

return value; Since the main function must return an integer value, we return 0. However, your program will run without it, but you should make it a practice since you'll need it in the long run.

By the way, All the code we write goes in a new source file on your compiler. (File > New > Source File) Once you type in the above code, you need to compiler & run your program. It should give the following output :

first_program_output

[Note - we added the header file conio.h for getch();]

[C Tutorial 2] The Building Blocks of C

July 1st, 2011 No comments

If you look at any program in C - you'll find that it's actually made up of various "functions". A function is a basic building block of every C program. All the code that we write is a part of a C function.

The "main function" in C is called the "main function". It is the first function your compiler is gonna look for. Apart from the main function, we can define and use our own functions.

Also, Some functions are used very commonly and cannot be defined again & again in every program - like functions to get input and show output - these are already defined in a standard program and can be used in our own program by "including" their source file. These files of code included in our program for ready-made functions are called header files.

c_building_blocks

Now, all the code & the syntax goes inside these functions. We begin coding in the next tutorial. Get ready to write your first C program.

[C Tutorial 1] Introduction to Programming

June 30th, 2011 No comments

In these tutorials, we're not really getting into the whos & whens - our one and only aim is to write efficient programs in C. For everything else - there's Google.

Now, you could be here either to pass an exam - get a degree or you could be here for the love of code - to make something new. Make sure your's is the second reason. If not, make it so.

What is a Program? Why do we need to program at all?

Some people wonder(believe me, they do) - what's with this code stuff.. why do we really need to write a program at all? The answer is simple - even simpler if you know how a computer works. A computer or any electronic system for that matter, is made of a large number of switches or gates - When a certain combination of gates is created - an operation is performed. This combination is made by opening and closing appropriate switches.

Since a computer doesn't have a mind of its own - we need to give instructions to the computer in order to close and open the switches. This set of instructions is called a program. A program is essentially a blueprint for this combination.

What is a Compiler? Which compiler should be used for C?

Since our computer only understands binary - we need a "program" to translate the code written by us in a programming language of our choice - to binary. This program is called the compiler. The programs written by us are a "higher-level" (user-friendly or rather human-friendly) than what the computer understands.

You can download any of the C compilers available for free. Try Dev C++ (Download Link) or Code::Blocks (Download Link) to start with - and you can later switch to a compiler of your choice. A list of all the C compilers is available here.

[C++ Tutorial 3] Basic Program Structure

July 10th, 2010 1 comment

So here's our very first program in C++. We're gonna take a look at it and then see what each line of code means.

#include<iostream>
using namespace std;

int main(
{
cout<<"Hello Jolly!";
system("pause");
return 0;
}

#include<iostream>

Here, we're using the code already there in the file iostream in own program. So the compiler is told to get the code from iostream and compile it before moving on to compile the rest of the code. The "<" and ">" are used when the file is located in the default compiler bin and not in an external location. You can try making your own iostream file. Copy all code from iostream to any file, say phoenix.txt and use #include<phoenix.txt> and it'll work.

using namespace std;

Before anything else, you need to know that all statements end with a semi-colon. So, this is your first C++ statement. This just means that you're using a namespace called the standard library. Now, this is done to avoid name collisions. For instance, consider a university which has a student with roll number 70. Another university may also have a student with the same roll number. Now, these are two namespaces, and by specifying  which namespace we're using, we can differentiate between the two students.

int main(){...}

All he shit that we do before this statement is just getting things ready and set for the main function. Did i say function? Yes, this is your first C++ function. Like any function, it must return some data. We use "int" here to show that the returned data would be an integer. Every function has pretty much the same format.

It goes like this-> return-type func-name (parameters){statements}

cout<<"Hello Jolly!";

Now this is another C++ statement. If you have seen the output of the program using your compiler-which is what you should have done. You'll only see Hello Jolly! printed on a stupid blank black screen. Well, there you go-the cout is your standard output function. Anything in the double inverts with the output operator "<<" goes as output to the screen. This function, along with the standard input function(cin) is stored in the "std" file which we talked about in the namespace statement.

system("pause");

A program like this one would usually run in a matter of nanoseconds and the window would close in a flash, unless mi friend-you use the above to simply, pause the system-causing it to wait until you exit.

return 0;

Did I say the main function in C++ would return an integer? So, here we return 0-nada-nothing-goose egg.

Again, the most important thing is to go and try it out for yourself.

[Go to Tutorial 4]