Blogs

Computer Programming

Posted 9/28/2016

Computer programming is the act of writing computer programs, which are a sequence of instructions written using a Computer Programming Language to perform a specified task by the computer.

Computer Programming is fun and easy to learn provided you adopt a proper approach. This tutorial attempts to cover the basics of computer programming using a simple and practical approach for the benefit of novice learners.

Audience
This tutorial has been prepared for the beginners who are willing to learn computer programming but they are unable to learn it due to lack of proper guidance. We are confident that after completing this tutorial, you will be at a level where you can code in C Programming language and will have a basic understanding of Java and Python programming languages as well from where you can continue further.

If you are completely new to Computer Programming, then we recommend you to read this tutorial twice or even thrice. First reading will not give you much idea, but during your second reading, you will start grasping most of the concepts and you will enjoy writing computer programs.

Prerequisites
We do not expect much from you as prerequisites, however, we assume that you have some amount of exposure to computers and its peripherals like keyboard, mouse, screen, printer, etc.

Read the rest of this entry »

Learn "C - Type Casting"

Posted 9/28/2016

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows −


(type_name) expression


Consider the following example where the cast operator causes the division of one integer variable by another to be performed as a floating-point operation −


#include <stdio.h>

main() {

int sum = 17, count = 5;
double mean;

mean = (double) sum / count;
printf("Value of mean : %f\n", mean );

}


When the above code is compiled and executed, it produces the following result −

Value of mean : 3.400000

 

 

 It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.

Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator. It is considered good programming practice to use the cast operator whenever type conversions are necessary.


Integer Promotion
Integer promotion is the process by which values of integer type "smaller" than int or unsigned int are converted either to int or unsigned int. Consider an example of adding a character with an integer −


 

#include <stdio.h>

 

main() {

 

int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;

 

sum = i + c;
printf("Value of sum : %d\n", sum );

 

}


When the above code is compiled and executed, it produces the following result −


Value of sum : 116


 

Here, the value of sum is 116 because the compiler is doing integer promotion and converting the value of 'c' to ASCII before performing the actual addition operation.

 

Usual Arithmetic Conversion
The usual arithmetic conversions are implicitly performed to cast their values to a common type. The compiler first performs integer promotion; if the operands still have different types, then they are converted to the type that appears highest in the following hierarchy −



The usual arithmetic conversions are not performed for the assignment operators, nor for the logical operators && and ||. Let us take the following example to understand the concept −

#include <stdio.h>

main() {

int i = 17;
char c = 'c'; /* ascii value is 99 */
float sum;

sum = i + c;
printf("Value of sum : %f\n", sum );

}


 

  

Read the rest of this entry »