Using datatypes and variables 0 comments

Let us write a simple program for using data types, which were explained in the previous few posts. Have a look at the simple program to add 2 numbers.

/* Declaration of Variables (Input) */
int iSum;
int iNum1=23;
int iNum2=33;

/* Processing Input */
iSum = iNum1 + iNum2;

// Display Output
printf("Sum of %d and %d is %d\n",iNum1,iNum2,iSum);

/* Changing inputs */
iNum1 = 10;
iNum2 = 23;

/* Process Again */
iSum = iNum1 + iNum2;

// Display Output Again
printf("Sum of %d and %d is %d",iNum1,iNum2,iSum);

The above program introduces many new things to you. First thing is Comments. To make the program look more understandable, we can use comments to describe certain blocks of code. So we use comments. Comment Lines are ignored by compiler while compiling. There are two types of comments - Single line comment and Multiline comment. Single Line comment can be made by starting the line with // and multiline comment can be made by starting the comment with /* and ending the comment with */

Next thing that is introduced here is variables and data types. Recall the third paragraph in this Post. We can store data/values in memories and can give those memory locations as input to the add function. So if we need to change the value for which we need to find sum, we simply change the value in those memory location and call the sum function with the same address location.

But as explained earlier, C program makes things easy for us. We don't need to specify where to store our data. It simply finds a free memory location and puts our value in it. So how do we know where our value is stored ? How can we change the value in that memory location ? That is where Variables comes into picture. We just name the memory location (whatever it might be). We use that variable name to identify the memory location. Internally C will map the variables names to the memory location using a Table maintained by it.

Thus when we DECLARE a variable(int iSum;), a new memory location is allocated and the address is mapped to the variable name iSum. So even without knowing the memory location, we would be able to change the values in that memory location. Hence the name "Variables". Now when we INITIALIZE a variable(int iNum1=23;), inaddition to declaration, the program also stores the value 33 in the memory allocated.

Now I know you will have a question. What will be present in the Variable which is not initialized? At any point of time, a memory location will have random combination of 0s and 1s or some value which was previously stored during execution of someother program. So it will contain some random number which we can't guess. We call those values GARBAGE VALUES.

Ok I have said it will allocate memory for the variable. But how many bytes of memory will it allocate ? Now comes datatype into picture. As explained here, Int means integer and the memory size for it is 4 bytes (depending on the compiler/system). So when we specify "int a", 4 bytes of memory is allocated. (Now I have a question again, have a look at the next post for what it is)

Now the statement for processing the input(iSum = iNum1 + iNum2;), It is something like 2E [2000] [2004] (as explained here). Instead of giving the memory address [2000],[2004] we specify the variable names. Instead of specifying 2E, we specify the operator +. Now we can also specify where to store the result through iSum =

Now the output will be something like this

Sum of 23 and 33 is 56
Sum of 10 and 23 is 33

Let us see about the statement Printf in a new Post.

More about Data Types 0 comments

For the reasons stated here, there should be some data type range that every system should have. Depending on the sytem/compiler you are working on, these range of data types can vary. So don't worry if you see different results in your computer (I will give and explain a program later to see what is the range of data type that your computer can store).

Data TypeSizeRange
int4 bytes–2,147,483,648 to 2,147,483,647
char1 byte–128 to 127
float4 bytes3.4 e-38 to 3.4 e+38 (7 digits precision)
double4 bytes1.7e-308 to 1.7e+308 (15 digits precision)

There are variations of primitive data types such as signed and unsigned char, signed and unsigned int, short and long int, signed and unsigned long int,
Note : 1 byte = 8 bits

Internal Storage of Data 0 comments

Digital Computers store datas in zeroes and ones. Any data we want to represent are converted to binary equivalent (zeroes and ones) and stored in the memory. Let us say we have 33, the binary equivalent of 33 is 100001. 33 will be actually stored as 100001 internally. Similarly if we have a character 'A', the ASCII equivalent of that character is taken and it is then converted to the binary equivalent and stored.

But had a look at the variations in the number of digits in the binary equivalent ? 33 takes 6 characters (100001), but 23 takes 5 (10111), similarly the number of bits (binary digits) differ for each number. So to create uniformity and to make the system understand where a number/character starts and end, they have introduced something called padding of zeroes. We will have fixed number of bits, say 8. So every number will be stored as 8 bit binary number. That is 33 will be stored as 00100001 and 23 will be stored as 00010111. This makes the compiler easy to understand where it should start reading a number.

Ok, What can we do for storing characters whose ASCII equivalent is also a number and is stored as binary the same way as a number. Wont there be a contracdiction whether it is a character or number? How can we differentiate it is a number or a character ? This is the reason we have data types. We tell the compiler whatever stored in this memory location is a charcter, and whatever we store here is a number through data types.

Ok, But 8 bits can store only from 0 to 255 (from 00000000 to 11111111). What can we do for higher numbers? Ok we can increase the number of bits from 8 to 16 or 32 or 64 etc. But the problem of extending the number of bits required to store one number is, we have to append more and more zeroes thus wasting the memory. This is one of the reasons why we have variations in Primary Data types. Also we need to have something called, range of values each data types can store. If we use the correct data types, we can save memory.

Next post will explain the range of values that can be used by a data type.

Data Types in C 0 comments

When we are reading something, we know that it is a alphabet, it is a number, it is a decimal number etc. For the compiler to know what type of data we are dealing with, we must specify the data type. Following are the primary data types that can be used in C.

ExampleData Type NameData Type
123, -23Integersint
12.5, 3.4E-38Floating Pointfloat
1.7E-308Double Precision Floatdouble

When I say primary, there are some secondary/derived data types also. We will look into those data types later. Also there are some variations of primary data types. To know more about, those data types, we need to know what is actually happening internally. My next post will explain how computers store data.

Basic C Program 0 comments

Enough of basics, we will quickly move into programming. I will start with a very simple program and start explaining to you on how to write a C Program and compile it using Dev CPP. Whatever may be the compiler, the steps will be almost similar. Have a look at the following program. It simply displays "Welcome to".

printf("Welcome to");
As I have already described in this post, there are many inbuilt functions and they are grouped to form library files. These files can be included in the program and the functions in those library can be used. The first statement includes the standard input/output library. Extension of the file .h stands for Header file, it contains the definition (let us see what they are later) for input/output functions. This is called a Pre-processor directive (Processed before compilation phase)

main() - Is a user defined function. Every program starts execution from this point. The opening brace { and closing brace } tells the compiler where the function starts and ends.

printf - As I have said, it is a inbuilt function. Its job is to display whatever is given as input to it. Here the input is a string (group of characters).

Now what to do with this piece of code ? Open your C compiler, and open a new source file in it. Type the above code and save it as Welcome.c , where .c is the file extension.

If you are using Dev C++, go to Build -> Compile & Run (F9).
If you are using TurboC, go to Run -> Run (Ctrl + F9)

Ok, but what happened ? Nothing seemed to come up? Yes, because the output window has already came and closed in Dev C++. In TurboC, Press Alt+F5, to get to the outscreen.

In DevC++, the window has already closed. So we need a little work around to see the output.

printf("Welcome to");
So what have we added extra ? getch(); What it does ? It is again a inbuilt function, and it is used for accepting a Single Character from our Keyboard, so until we press any key, the cursor stays there, Hence we will see the output. So usually I used to put this getch(); as the last line in a program.

Did you note something ? we have a semi colon at the end of every statement. This is how we tell the compiler that its an end of a line/statement.

Another point to be noted here is that, the above program still runs fine in Dev C++, without errors even if the stdio.h header file is not included. Possible reason is that, it is automatically included by DevC++ compiler when compiling & executing the program. In TurboC, it will show error.

What you need ? 0 comments

You should have an operating system installed in your machine :d. And you will require one C Programming complier like Turbo C, Borland C etc for your operating system. Just google for the names you will find the place to download them. Install that compiler into your system.

I use Dev CPP. It is a C++ compiler, but it can compile C source also. I prefer using that because I feel it is easy for me to edit and compile the programs. Syntax highlighting also helps me a lot. Also if there is any run time errors, or infinite loops it will be easy for me to quit the program, without damaging the source code files that are open.

More About C Programming Language 0 comments

Let us think of a scenario. I need to find answer for 2 to the power 5. My algorithm will be something like this. I place 5 in a memory location 1. Place 2 in a memory location 2. Multiply value in memory location 2 with 2, and everytime I multiply, I will decrement the value in memory location 1 until it reaches 0. We don't need to specify where we need to store the values. Those jobs are taken care by the compiler. It reads the programs, separates them into smaller parts called tokens and checks them against predefined syntax. It then converts the program into codes that machines can understand. More like a conversion from Human readable form to Machine Readable form.

Now, since we will be requiring to find Power of certain numbers many times in a program, we can puts these program into a block called functions. In order to find the power, we just need to call these functions and pass our arguments/inputs (say 2 and 5). Similar to this power function, there will be many other function that are predefined when we install compilers like Turbo C, Borland C etc. These are standards that are set and every compiler for C Programming will follow those standards.

In order to make it more organised, those Predefined functions called INBUILT functions are grouped and put into a package called library files. So all math related functions will be put into one package. All string related functions will be put under one package etc. When we need to make use of these inbuilt functions, we simply include those files into our program.

What is C Programming Language ? 0 comments

Before one should learn something, they must know what they are learning. Let us say, we have a Computer. In the simplest form a computer consists of a Input device (like our keyboard), a Output device (like our monitor), a CPU (for processing the input we give), a memory device (like RAM, Hard Disk etc for storing the programs and datas we supply).

Now, how does computer function with all these ? Computer doesn't have intelligence. It simply does what we ask it do through Programs. Every CPU has it own set of operations that it can perform like Addition, Subtraction, Multiplication, Division, comparision etc. We can make use of these operations to occamplish complicated tasks, Say finding the n'th power of a number. For that we need to specify a series of instructions (Program) to the CPU. The data is present here, and the program is present here. The CPU then starts executing the program and sends the output to the output device as we have requested.

Earlier days, we used to write Machine Codes. Machine codes are codes which corresponds to each basic CPU operation. Say we have a code 2E for Addition. (2E - Hexadecimal number - Just an example). So to add two numbers we need to send something like 2E 34 23 to the CPU. Now the CPU understands that it needs to perform addition operation on 34 and 23. As the requirement for dynamic data increased, we place the data in the memory location and send the instruction like 2E [2000] [2004]. Now the CPU knows that It needs to take data from memory locations (address given to memory) 2000 and 2004 and add them.

As it was very difficult for us to remember each and every instruction code, we started developing something call Mnemonics. Where we simply say Add 34,23 and there will be a compiler which will understand Add 34,23 and sends the ouput as 2E,34,23 to the CPU. CPU then processes this. This compiler itself is a program written with the instruction codes. Still mnemonics are very hard, because we still need to specify the address where the data should be stored and where the CPU needs to put the output. Also it is not easy to handle loops, control statements etc without actually knowing the instruction sets of CPU.

The result of all our expectations for faster and efficient programming is C Programming Language. C is a general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. Though there are many languages that were developed prior to C, it is one of the basic languages for understanding many other Programming langugages. Knowing C, we can learn other programming languages very easily.

I am quite sure that if you understand how C Programming works, you will enjoy programming.

About this blog 0 comments

This blog is all about Basics in C Programming. Though there are lots of newer, advanced and much efficient languages developed these days, I believe that Language C is one of the root which one should know to easily understand other programming languages. Similar to HTML, which is very much required for web designing, I believe that one should know C. So I am going to explain to you what I have understood from my graduation and other programming expertise. The contents posted here are from my understandings, and if you see any contradiction from what you have learnt, I will be glad to learn from you. Suggestions, Comments, Feedbacks are all welcome. You can drop a mail to talktotrix[at]ymail[dot]com or can comment here.
Template designed using TrixTG