Browsing Category: "Programming"

Simple Example OF Subroutines In C Programming

Posted on: Wednesday, November 5th, 2008 | Written by Wakish

kaleidoscope + subroutines In C Programming

CODE:

//include the required libraries here
void main()
{
int input01, input02, result;

//give some inputs, but you can make it this C program interactive
input01 = 25;
input02 = 10;

result = multiply(input01,input02);    //subroutine call

printf(”Multiplication of %d and %d is %d”,input01,input02,result);
}

/* subroutine definition block */
int multiply(int x, int y)
{
return x*y;
}

OUTPUT:

Multiplication of 25 and 10 is 250

When execution starts and encounters the statement ‘result = multiply(input01,input02);‘, execution goes to the function or subroutine call. We can visualise it as follows:

VISUALISATION:

int multiply(25, 10)
{
return 25*10;   //computes and returns 250
}

Therefore, multiply() returns its result which is stored into the variable ‘result‘. After the value is returned, execution goes back to main and proceeds to the next statement.

star Other Simple C Programming Tutorials:
=> Random Numbers in C programming
=> It’s Too Easy To Make This Simple, But Worse, Logical Error In Programming
=> Format Specifiers Used In C Programming
=> A simple Interactive C Program Using Scanf Function

Share This Article with your friends if you appreciate my effort to write it!

Posted in: Programming | Response(s): No Comments yet - Be the first to respond!

A simple Interactive C Program Using Scanf Function

Posted on: Thursday, October 23rd, 2008 | Written by Wakish

C programming + scanf

star Reading data From Keyboard

scanf(”control string”, &variable01, &variable02, …);

=> The ampersand symbol, &, is very important. It’s an operator specifying the variable name’s address. Omitting it, might result into unexpected results.

star How Does Scanf Behave During Execution

Consider: scanf(”%d”, &number);

=> This statement halts execution and waits for a value to be entered. That value will be stored in a variable ‘number‘ and is of type ‘integer‘ as specified by the format specifier ‘%d
=> The compiler will proceed once a number is typed in and the ‘Return‘ key is pressed.

star The Code At Work

PROGRAM:

//include your libraries here..
void main()
{
int number;

printf(”Enter an integer: \n”);     //prompt message
scanf(”%d”, &number);                 //read message

printf(”The number you entered is: %d”, number);
}

OUTPUT:

Enter an integer:
25
The number you entered is: 25

That’s it, a very simple program to keep things simple for you to understand how interactivity works. Do use the comment form below to add your part..

bulb Other C Programming Articles:
=> Random Numbers in C programming
=> It’s Too Easy To Make This Simple, But Worse, Logical Error In Programming

Share This Article with your friends if you appreciate my effort to write it!

Posted in: Programming | Response(s): No Comments yet - Be the first to respond!

Format Specifiers Used In C Programming

Posted on: Wednesday, October 22nd, 2008 | Written by Wakish

Format Specifiers For Integer Data Type

Integer Data Type Format Specifiers
short signed %d or %I
short unsigned %u
long signed %ld
long unsigned %lu
unsigned hexadecimal %x
unsigned octal %o

Format Specifiers For Real Data Type

Real Data Type Format Specifiers
float %f
double %lf

Format Specifiers For Character/String Data Type

Character Data Type Format Specifiers
signed character %c
unsigned character %c
String %s

Share This Article with your friends if you appreciate my effort to write it!

Posted in: Programming | Response(s): (1) Comment so far - Be the 2nd to respond!

It’s Too Easy To Make This Simple, But Worse, Logical Error In Programming

Posted on: Sunday, February 24th, 2008 | Written by Wakish

- This article is relevant to almost any programming languages, namely C, C++, Java, C#, PHP..etc
- I will be using C++ for example illustrations
padlock
star The Two Operators Which Create Confusion To Beginners

Before we begin with the title, it is important that you understand the following Operators:
1) The assigment operator
Example:

int x = 10; //This statement assigns the value 10 to variable

Meaning, some memory location will be reserved or set aside, it will be named or referenced as the variable “x”. Then, the value “10″ will be stored in that location.

2) The equality operator

Example:
x == 10; //This is an evaluation statement

Meaning, evaluation will be done to verify if really the variable “x” contains the value “10″. If the location named “x” has a value of “10″, then the statement evaluates to TRUE (1), else it is FALSE (0)

I hope this is clear now? If yes, let’s proceed further..

star The Common Programming Error

Consider this piece of C++ Code:

void main()
{ int x=0; //declare a variable x and assign it a value 0 to prevent garbage values

//ask the user to input a value
cout << “Enter an integer: “;
//collect the input
cin >> x;

//an “if” statement to do something*
if (x = 5) //LOGICAL ERROR!
{ cout << “I knew it was 5!” << endl;
}
else
{ cout << “How can I possibly guess what you entered?” << endl;
}
}//end main

OUTPUT:
Enter an integer: 36
I knew it was 5!

As you can see from the output above, the result is wrong. We entered 36, and still the program is reacting as ‘if it was’ actually 5 that was entered!

star What Is Wrong Then?

The “if” statement was there to do something and that something was an “evaluation”.
bulb “if-else” statements always perform one and only one thing: EVALUATION.

Now, I think you are getting the picture; whenever there is an “if-else” statement, there is an “evaluation” and whenever there is an “evaluation”, we use the “equality operator”! (Say this aloud, stick it somewhere on your walls till it becomes natural to your brain) :)

star Further Explanation On the Error

The statement “if (x = 5)”, will always return a TRUE value. This is because an if statement is FALSE, if and only if, that statement evaluates to 0. In our case, the statement says, x=5. Hence, x is not 0. Therefore, if() evaluates to TRUE.
That is why the code body of the “if” statement will still get executed although this is not what we ‘intended‘ the program to do.

exclamation NOTE:
This type of error is called a “logical error” and is one of the worse error in programming since there is no way for the compiler to catch that error for you!
Compile-time errors are caught!
Run-time errors are also caught!
But, logical errors can never be caught!
So be careful! ;)
(I think now after reading the above, you will never ever make such an error) :P

star The Full Correct Code:

void main()
{ int x=0; //declare a variable x and assign it a value 0 to prevent garbage values

//ask the user to input a value
cout << “Enter an integer: “;
//collect the input
cin >> x;

//an “if” statement to do something*
if (x == 5) //GOOD!
{ cout << “I knew it was 5!” << endl;
}
else
{ cout << “How can I possibly guess what you entered?” << endl;
}
}//end main

OUTPUT:
Enter an integer: 36
How can I possibly guess what you entered?

If you liked this article, do subscribe to not miss other exciting tutorials ;)
And I would also love to hear your feedback, point of view or any suggestion.

star Recommended Reading:
=> Random Numbers in C programming

Share This Article with your friends if you appreciate my effort to write it!

Posted in: Programming | Response(s): No Comments yet - Be the first to respond!
Close
E-mail It