popads

Friday, November 28, 2014

Variables and constants in c programming

Variables

If you declare a variable in C (later on we talk about how to do this), you ask the operating system for a piece of memory. This piece of memory you give a name and you can store something in that piece of memory (for later use). There are two basic kinds of variables in C which are numeric and character.

Numeric variables

Numeric variables can either be of the type integer (int) or of the type real (float). Integer (int) values are whole numbers (like 10 or -10). Real (float) values can have a decimal point in them. (Like 1.23 or -20.123).

Character variables

Character variables are letters of the alphabet, ASCII characters or numbers 0-9. If you declare a character variable you must always put the character between single quotes (like so ‘A’ ). So remember a number without single quotes is not the same as a character with single quotes.

Constants

The difference between variables and constants is that variables can change their value at any time but constants can never change their value. (The constants value is lockedfor the duration of the program). Constants can be very useful, Pi for instance is a good example to declare as a constant.

Data Types

So you now know that there are three types of variables: numeric – integer, numeric-real and character. A variable has a type-name, a type and a range (minimum / maximum). In the following table you can see the type-name, type and range:
Type-name Type Range
int Numeric – Integer -32 768 to 32 767
short Numeric – Integer -32 768 to 32 767
long Numeric – Integer -2 147 483 648 to 2 147 483 647
float Numeric – Real 1.2 X 10-38 to 3.4 X 1038
double Numeric – Real 2.2 X 10-308 to 1.8 X 10308
char Character All ASCII characters

Declaring

So we now know different
type-names and types of variables, but how do we declare them. Declaring a variable is very easy. First you have to declare the type-name. After the type-name you place the name of the variable. The name of a variable can be anything you like as long it includes only letters, underscores or numbers (However you cannot start the name with a number). But remember choose the names wisely. It is easier if a variable name reflects the use of that variable. (For instance: if you name a float PI, you always know what it means).
Now let’s declare some variables, a variable MyIntegerVariable and MyCharacterVariable:

 int main()
 {
  int MyIntegerVariable;
  int MyCharacterVariable;
  return 0;
 }
 
It is possible to declare more than one variable at the same time:

 int main()
 {
  int Variable1, Variable2, Variable3;
  int abc, def, ghi;
  return 0;
 }
 
To declare a constant is not much different then declaring a variable. The only difference is that you have the word const in front of it:

 int main()
 {
  const float PI = 3.14;
  char = 'A';
  return 0;
 }
 
Note: As you can see, you can assign a value with the equal sign during declaration.

Signed and unsigned variables

The difference between signed and unsigned variables is that signed variables can be either negative or
positive but unsigned variables can only be positive. By using an unsigned variable you can increase the maximum positive range. When you declare a variable in the normal way it is automatically a signed variable. To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are.

 int main()
 {
  unsigned int MyOnlyPositiveVar;
  signed int MyNegativeAndPositiveVar;
  int MyNegativeAndPositiveVar;
 }
 

Calculations and variables

There are different operators that can be used for calculations which are listed in the following table:
Operator
Operation
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus(Remainder of integer
division)
Now that we know the different operators, let’s calculate something:

int main()
{
 int a, b;
 a = 1;
 b = a + 1;
 a = b - 1;
 return 0;
}

Reading and printing

Calculating something without reading input or printing something on the screen is not much fun. To read input from the keyboard we will use the command scanf. (How to print something to the screen we all ready
know).
So let’s make a program that can do all these things:

#include<stdio.h>

int main()
{
 int inputvalue;

 scanf("%d", &inputvalue);
 inputvalue = inputvalue * 10;
 printf("Ten times the input equals %d\n",inputvalue);
 return 0;
}

Note: The input must be a whole number (integer).
The & sign will be explained in a later tutorial. The %d is for reading or printing a decimal integer value (It is also possible to use %i). In the table below you can find the commands for other types:
%i or %d int
%c char
%f float
%lf double
%s string
That was all for now. In the next tutorial we take a look at the “if statement” and “switch statement”.

Sunday, November 9, 2014

First C progtam run in Hello world

After talking about the history and compilers it is time to make our first program. We like to make C programs under Linux, so we will use a text editor and the gnu compiler. But all programs we make in these tutorials will also work under Windows. Remember, the examples included in the C and C++ tutorials are all console programs. That means they use text to communicate. All compilers support the compilation of console programs. Check the user’s manual of your compiler for more info on how to compile them. (It is not doable for us to write this down for every compiler).
Open a text editor (vi, emacs, notepad) or make a console project in Visual Studio Express and type the following lines of code (Don’t use cut/paste, type them. This is better for learning purposes):


 #include<stdio.h>
 int main()

 {

  printf(“Hello World\n”);

  return 0;

 }

 
Save the program with the name: hello.c

#include<stdio.h>

With this line of code we include a file called stdio.h. (Standard Input/Output header file). This file lets us use certain commands for input or output which we can use in our program. (Look at it as lines of code commands) that have been written for us by someone else). For instance it has commands for input like reading from the keyboard and output commands like printing things on the screen.

int main()

The int is what is called the return value (in this case of the type integer). Where it used for will be explained further down. Every program must have a main(). It is the starting point of every program. The round brackets are there for a
reason, in a later tutorial it will be explained, but for now it is enough to know that they have to be there.

{}

The two curly brackets (one in the beginning and one at the end) are used to group all commands together. In this case all the commands between the two curly brackets belong to main(). The curly brackets are often used in the C language to group commands together. (To mark the beginning and end of a
group or function.).

printf(“Hello World\n”);

The printf is used for printing things on the screen, in this case the words: Hello World. As you can see the data that is to be printed is put inside round brackets. The words Hello World are inside inverted ommas, because they are what is called a string. (A single letter is called a character and a series of characters is called a string). Strings must always be put between inverted commas. The \n is called an escape sequence. In this case it represents a newline character. After printing something to the screen you usually want to print something on the next line. If there is no \n then a next printf command will print the string on the same line.
Commonly used escape sequences are:
  • \n (newline)
  • \t (tab)
  • \v (vertical tab)
  • \f (new page)
  • \b (backspace)
  • \r (carriage return)
After the last round bracket there must be a semi-colon. The semi-colon shows that it is the end of the command. (So in the future, don’t forget to put a semi-colon if a command ended).

return 0;

When we wrote the first line “int main()”, we declared that main must return an integer int main(). (int is short for integer which is another word for number). With the command return 0; we can return the value null to the operating system. When you return with a zero you tell the operating system that there were no errors while running the program. (Take a look at the C tutorial – variables and constants for more information on integer variables).

Compile

If you have typed everything correct there should be no problem to compile your program. After compilation you now should have a hello.exe (Windows) or hello binary (UNIX/Linux). You can now run this program by typing hello.exe (Windows) or ./hello (UNIX/Linux).
If everything was done correct, you should now see the words “Hello World” printed on the screen.
Congratulations!!!!!
You have just made your first program in C.

Comments in your program

The Hello World program is a small program that is easy to understand. But a program can contain thousands of lines of code and can be so complex that it is hard for us to understand. To make our lives easier it is possible to write an explanation or comment in a program. This makes it much easier to understand the code. (Even if you did not look at the code for years).These comments will be ignored by the compiler at compilation time.
Comments have to be put after // or be placed between /* */ .
Here is an example of how to comment the Hello World source code :


 /* Description : Print Hello World on the screen

  Author  : Your name

  Date  : 01/01/2007    */
 #include<stdio.h>


 int main()

 {

  //Print something and then newline

  printf(“Hello World\n”);

  return 0;

 }

 

Indentation

As you can see the printf and return statements have been indented or moved to the right side. This is
done to make the code more readable. In a program as Hello World, it seems a stupid thing to do. But as the programs become more complex, you will see that it makes the code more readable.
So, always use indentations and comments to make the code more readable. It will make your life much easier if the code becomes more complex.

copy from :  www.codingunit.com

Saturday, November 8, 2014

The History of The c Language

The C programming language was devised in the early 1970s by Dennis M. Ritchie an employee from Bell Labs (AT&T).
In the 1960s Ritchie worked, with several other employees of Bell Labs (AT&T), on a project called Multics. The goal of the project was to develop an operating system for a large computer that could be used by a thousand users. In 1969 AT&T (Bell Labs) withdrew from the project, because the project could not produce an economically useful system. So the employees of Bell Labs (AT&T) had to search for another project to work on (mainly Dennis M. Ritchie and Ken Thompson).

Ken Thompson began to work on the development of a new file system. He wrote, a version of the new file system for the DEC PDP-7, in assembler. (The new file system was also used for the game Space Travel). Soon they began to make improvements and add expansions. (They used there knowledge from the Multics project to add improvements). After a while a complete system was born. Brian W. Kernighan called the system UNIX, a sarcastic reference to Multics. The whole system was still written in assembly code.
Besides assembler and Fortran, UNIX also had an interpreter for the programming language B. ( The B language is derived directly from Martin Richards BCPL). The language B was developed in 1969-70 by Ken Thompson. In the early days computer code was written in assembly code. To perform a specific task, you had to write many pages of code. A high-level language like B made it possible to write the same task in just a few lines of code. The language B was used for further development of the UNIX system. Because of the high-level of the B language, code could be produced much faster, then in assembly.
A drawback of the B language was that it did not know data-types. (Everything was expressed in machine words). Another functionality that the B language did not provide was the use of “structures”. The lag of these things formed the reason for Dennis M. Ritchie to develop the programming language C. So in 1971-73 Dennis M. Ritchie turned the B language into the C language, keeping most of the language B syntax while adding data-types and many other changes. The C language had a powerful mix of high-level functionality and the detailed features required to program an operating system. Therefore many of the UNIX components were eventually rewritten in C (the Unix kernel itself was rewritten in 1973 on a DEC PDP-11).
The programming language C was written down, by Kernighan and Ritchie, in a now classic book called “The C Programming Language, 1st edition”. (Kernighan has said that he had no part in the design of the C language: “It’s entirely Dennis Ritchie’s work”. But he is the author of the famous “Hello, World” program and many other UNIX programs).
For years the book “The C Programming Language, 1st edition” was the standard on the language C. In 1983 a committee was formed by the American National Standards Institute (ANSI)
to develop a modern definition for the programming language C (ANSI X3J11). In 1988 they delivered the final standard definition ANSI C. (The standard was based on the book from K&R 1st ed.).
The standard ANSI C made little changes on the original design of the C language. (They had to make sure that old programs still worked with the new standard). Later on, the ANSI C standard was adopted by the International Standards Organization (ISO). The correct term should there fore be ISO C, but everybody still calls it ANSI C.


copy from :  www.codingunit.com