Sunday, 25 September 2016

Variables And Keywords In C

Variables and Keywords in C

variable in simple terms is a storage place which has some memory allocated to it. So basically a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations which can be applied to them.
Variable Declaration:
A typical variable declaration is of the form:
  type variable_name;
    or for multiple variables:
  type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number.
Difference b/w variable declaration and definition
Variable declaration refers to the part where a variable is first declared or introduced before its first use. A variable definition is the part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.
See the following C program for better clarification:
#include <stdio.h>
int main()
{
     // declaration and definition of variable 'a123'
    char a123 = 'a';
     // This is also both declaration and definition as 'b' is allocated
     // memory and assigned some garbage value.  
    float b; 
     // multiple declarations and definitions
    int _c, _d45, e;
     // Let us print a variable
    printf("%c \n", a123);
    return 0;
}
Output:
a

Keywords :

Keywords are specifically reserved words in C each of which has a specific feature associated with it. Almost all of the words which help us use the functionality of the C language are included in the list of keywords. So you can imagine that the list of keywords is not going to be a small one!
There is a total of 32 keywords in C:
   auto       break    case     char     const     continue
   default    do       double   else     enum      extern
   float      for      goto     if       int       long
   register   return   short    signed   sizeof    static
   struct     switch   typedef  union    unsigned  void
   volatile   while 
Most of these keywords have already been discussed in the various sub-sections of the C language, like Data Types, Storage Classes, Control Statements, Functions etc.
Let us discuss some of the other keywords which allow us to use the basic functionality of C:

const : 

The const can be used to declare constant variables. Constant variables are variables which when initialized, can’t change their value. Or in other words, the value assigned to them is unmodifiable.
Syntax:
const data_type var_name = var_value; 
Note: Constant variables need to be compulsorily be initialized during their declaration itself. const keyword is also used with pointers.

extern:

 The extern simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can me made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
Syntax:
extern data_type var_name = var_value;

static

 The static keyword is used to declare static variables which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
Syntax:
static data_type var_name = var_value;

void:

 The void is a special data type only. But what makes it so special? void, as it literally means an empty data type. It means it has nothing or it holds no value. For example, when it is used as the return data type for a function, it simply represents that the function returns no value. Similarly, when it is added to a function heading, it represents that the function takes no arguments.
Note: void also has a significant use with pointers. Please refer the void pointer in C for understanding the same.

typedef

 The typedef is used to give a new name to an already existing or even a custom data type (like a structure). It comes in very handy at times, for example in a case when the name of the structure defined by you is very long or you just need a short-hand notation of a pre-existing data type.

Thanks for visiting my blog, Happy Programming :D
// Note : For basic tutorials scroll down the page //

Sunday, 4 September 2016

C Programming

HOW TO START ?

If you are worried about how to start programming and create newly excited programs in C and C++ then don't get so depressed about how to begin. Here is the basic guide for you guys....!

STEP 1: 

     First of all, you have to set the development environment in your system. To do so  you can download an IDE from the internet which is completely free. Oh, now you are thinking what is an IDE? Well, IDE is an Integrated Developer Environment which is a Software developed for different programming languages.For C programming language there is a number of IDEs are available on the net.
I prefer Code Blocks (IDE for C and C++ languages). You can download it from the below link:


You can also use Turbo C, DOS BOX, Dev C++, Visual Studio Community Edition etc.

STEP 2:

   After downloading Code Blocks, install it. To install it follow the following steps:
1. Double-click on the setup file.
2. Press next key.
3. On the 4th step select console option (extreme right of the selection window)
4. Complete the installation carefully.

STEP 3:

   Now open the code blocks by simply double-clicking on its icon. Now, 
1. Simultaneously press CONTROL+SHIFT+N keys. It will open a new tab in editor bar. 
2. Press CONTROL+S key simultaneously  and the save your file to the desired location with ".c" extension. ( " quotes  are for the clarity purposes).
   For example, I want to save a file with a name "main" then I will save it as "main.c" in which "main" is the file name and ".c" is the extension for every c programme file.    

STEP 4: (HELLO WORLD PROGRAMME)

   Now in this editor, you can write your c programme. The basic programme in c language is printing "Hello World" on the console/output screen.

Code for Hello World programme:

#include<stidio.h>
int main(){
   printf("Hello World \n");
return 0;
}

In the above C code,

1. The first line includes a stdio ( Standard input output)  library which is essential for any C programme to accept an input and give an output.Some essential functions and macros are also defined in this library like main(), printf() etc.

2. In the second line, main() function is being called. Anything or any instruction we want to give to the system is written in the main function.

3. printf() prints anything written within the " " quotes inside its braces except for placeholders.

4. Each line in the main is followed by a semicolon ";" .

5. return statement returns  an integer in case of int function. In the above, code returning an integer value ensures that your programme compiled successfully.

STEP 5:

After writing the code you have to compile and run it.

1. To compile and run your programme click on the build & run icon in the toolbar.
    or you can press CONTROL + F9 followed by CONTROL + F10.



On clicking it, a new black window will pop up as shown in below image.


Thanks for visiting this page. Happy programming. :D