Archive

Posts Tagged ‘C programming’

Programming Language C : Part 3

April 27, 2009 2 comments

Last day, I’d ended on the basic structure of the C Programming Language. Today, we shall start to do real programming. It is really fun.

Ok. Lets do a small program to print your name:

c1

This program will give you output like: c2

Now, we shall move to DATA Types.

As we know, there are many types of data in our real world. For example, the Integers, the Floating point numbers, Characters etc.

In case of C, there also exist all of them. In case, we want to use them, we need to “INITIALIZE” them to the Compiler. The method is like this:

c3Here we can see, “int a” declares an integer called ‘a’ without any value. But, “int b = 5” declares an integer called ‘b’ with a value 5 assigned in it. Similarly for floating point numbers, ‘float’ is used and for characters, “char” is used. All these ‘int’ or ‘float’ or ‘char’ are called “keywords” in C.

We shall look further more keywords in C:

c4

All these keywords have their own meanings and jobs to do. We shall look them further, while we will need them in future.

Now, there is a question. I know, I can use int as a keyword to mean the integers. But, how long that integer can be? Int x = 1234567891011121314151617…..??? There should be a limit of this.

char     => a single byte, capable of holding one character in the local character set

int        => an integer, typically reflecting the natural size of integers on the host machine

float     => single-precision floating point

double =>  double-precision floating point

In addition, there are a number of qualifiers that can be applied to these basic types. short

and long apply to integers:

short int sh;

long int counter;

The word int can be omitted in such declarations, and typically it is. The intent is that short and long should provide different lengths of integers where practical; int will normally be the natural size for a particular machine. short is often 16 bits long, and int either 16 or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the the restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer than long.  The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers are always positive or zero, and obey the laws of arithmetic modulo 2n, where n is the number of bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values between 0 and 255, while  signed chars have values between -128 and 127 (in a two’s complement machine.) Whether plain  chars are signed or unsigned is machine-dependent, but printable characters are always positive.  The type long double specifies extended-precision floating point. As with integers, the sizes of floating-point objects are implementation-defined; float, double and long double could represent one, two or three distinct sizes.

Advertisements

C Programming: Part 2

April 16, 2009 1 comment

Hello friends! After a long time, I am back with the Programming Language C.

In regard of the last post and the comments posted by our readers, I must insist you trying all of these in Linux / UNIX also. In Linux / UNIX, you don’t need any 3rd party software line ‘Turbo C’ in Windows. In the OS itself, the ‘gcc’ (GNU’s C Compiler) compiler is preloaded. You just need to call the ‘gcc’ and then compile/run the program.

Now, we will go into the C programming!

Before everything, we will write a C program first.

Windows: Open Turbo C è write this code.

Linux / UNIX: Open terminal ècreate ‘hello.c’ in the VI editor è write this code.


#include <stdio.h>

#include <conio.h>

int main()

{

printf(“ Hello World.”);

getch();

return 0;

}

Now, we will look, what are these lines signify:

Line 1 and 2: These are called “Pre-Processor Directives”. We include these “.h” files rather “header files” in our program to use some of their properties. These properties are called “Functions”. Here, in line 5, 6 and 7, you will see some “printf”, “getch” and “return”…these are those Functions. The “printf” and “return” functions are from the header file “stdio.h” (the ‘stdio’ means STANDARD INPUT OUTPUT and ‘conio’ means CONSOLE INPUT OUTPUT) and the “getch” is from the header file “conio.h”. Remember, other than these Functions, we ourselves can also write Functions as we wish.

Line 3: This line indicates the ‘Return type’ and ‘Name’ of a user written function (as I’d said in last paragraph). Now, to know what is ‘return type’ needs some more knowledge in C. So, we will discuss that later. Now, ‘name’. Here as we can see, after the ‘return type’ called ‘int’, there is the name of this function “main”. Now, a question arises, why ‘main’? The reason is, in any C program, there may exist more than 1 function and all of them are controlled by 1 function. Now, to get rid of the question, among those functions, which one is the 1st one, that controls other functions? The answer is: the main function. It’s like the main door of a building. After traversing through it, you can go anywhere in the building. So, here no other function exists, hence, the main door is the ‘main’ function.

Line 4 and 8: You can see these are nothing but, an opening parenthesis and a closing parenthesis. This opening means the starting of any function and closing means the closing of the same.

Line 5, 6 and 7: “printf” is a function, whose job is to print something. Inside the two brackets, there are two upper commas. Inside those ‘upper commas’, if you write some thing, that will be shown to you in the monitor when that program will run. Similarly, “getch” and “return” has some jobs to do. These jobs will be cleared to us when we will have some more knowledges in C.

One more thing, if you have noticed, you can see there is a semicolon (;) after every line inside the main function. This semicolon indicates the ‘End of the Line’.

Now, to compile:

Windows: (in Turbo C) F2 (to save the program) è Alt + F9.

Linux / UNIX: (after saving the program in VI editor) gcc -c -a hello.c

Now, to Run:

Windows: (after compilation) Ctrl + F9.

Linux / UNIX: (after compilation) ./hello.c

The Programming Language – C


Hello and Welcome to all our readers.

This is Jayanta here. I will be posting topics / tutorials on ‘C’ programming Language.

Before everything, at first, I want to share a brief history of C with you.

This ‘C’ was written by Dennis Ritchie for the OS called “Unix“. While, he was developing Unix, he realized that, there should be a good programming language, so, he developed it.

The simple naming convention behind “C” is, prior to C, there was a language called “B”. B actually was the abbr. of ‘BCPL‘. So, as a casual naming convention, after B, it was named C !!

Before Starting C, I want to share some resources about C.

The software, where you want to write , edit ,run C programs (these softwares are generally called IDE – Integrated Development Environment; hence, I’ll also call IDE..), you need an IDE. Here is a link of a good IDE. Use this one only. IDE

[To install the IDE, just copy the downloaded file to your C drive. Double click it. It will take care of itself.]

And now, books. For books I would prefer E. Balaguruswamy or Y. Kanitkar.

So, this much for today.. Have a good day..