Thursday, 18 January 2018

First C programe



First C program to print Hello World

#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.

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. 
=========================================================

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-nameTypeRange
intNumeric – Integer-32 768 to 32 767
shortNumeric – Integer-32 768 to 32 767
longNumeric – Integer-2 147 483 648 to 2 147 483 647
floatNumeric – Real1.2 X 10-38 to 3.4 X 1038
doubleNumeric – Real2.2 X 10-308 to 1.8 X 10308
charCharacterAll 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;                                                                  
 }


No comments:

Post a Comment