Monday, October 14, 2019

c-variable-declaration


Variable Declaration Rules in C

To Declare any variable in C language you need to follow rules and regulation of C Language, which is given below;
  • Every variable name should start with alphabets or underscore (_).
  • No spaces are allowed in variable declaration.
  • Except underscore (_) no other special symbol are allowed in the middle of the variable declaration (not allowed -> roll-no, allowed -> roll_no).
  • Maximum length of variable is 8 characters depend on compiler and operation system.
  • Every variable name always should exist in the left hand side of assignment operator (invalid -> 10=a; valid -> a=10;).
  • No keyword should access variable name (int for <- invalid because for is keyword).
Note: In a c program variable name always can be used to identify the input or output data.

Variable declarations

This is the process of allocating sufficient memory space for the data in term of variable.

Syntax

Datatype  variable_name;
int  a;



Variables in C

variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:

  1. type variable_list;  

The example of declaring the variable is given below:

  1. int a;  
  2. float b;  
  3. char c;  

Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:

  1. int a=10,b=20;//declaring 2 variable of integer type  
  2. float f=20.8;  
  3. char c='A';  

Rules for defining variables

  • A variable can have alphabets, digits, and underscore.
  • A variable name can start with the alphabet, and underscore only. It can't start with a digit.
  • No whitespace is allowed within the variable name.
  • A variable name must not be any reserved word or keyword, e.g. int, float, etc.
Valid variable names:

  1. int a;  
  2. int _ab;  
  3. int a30;  

Invalid variable names:

  1. int 2;  
  2. int a b;  
  3. int long;  

Types of Variables in C

There are many types of variables in c:
  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

Local Variable

A variable that is declared inside the function or block is called a local variable.
It must be declared at the start of the block.

  1. void function1(){  
  2. int x=10;//local variable  
  3. }  

You must have to initialize the local variable before it is used.


Global Variable

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions.
It must be declared at the start of the block.

  1. int value=20;//global variable  
  2. void function1(){  
  3. int x=10;//local variable  
  4. }  

Static Variable

A variable that is declared with the static keyword is called static variable.
It retains its value between multiple function calls.

  1. void function1(){  
  2. int x=10;//local variable  
  3. static int y=10;//static variable  
  4. x=x+1;  
  5. y=y+1;  
  6. printf("%d,%d",x,y);  
  7. }  

If you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.


Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

  1. void main(){  
  2. int x=10;//local variable (also automatic)  
  3. auto int y=20;//automatic variable  
  4. }  

External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.
myfile.h


  1. extern int x=10;//external variable (also global)  

program1.c


  1. #include "myfile.h"  
  2. #include <stdio.h>  
  3. void printValue(){  
  4.     printf("Global variable: %d", global_variable);  
  5. }  



c-variable

Variable in C Language

Variable is an identifier which holds data or another one variable. It is an identifier whose value can be changed at the execution time of program. It is used to identify input data in a program.
Syntax:

Syntax

Variable_name = value;

Rules to declare a Variable

To Declare any variable in C language you need to follow rules and regulation of C Language, which is given below;
  • Every variable name should start with alphabets or underscore (_).
  • No spaces are allowed in variable declaration.
  • Except underscore (_) no other special symbol are allowed in the middle of the variable declaration (not allowed -> roll-no, allowed -> roll_no).
  • Maximum length of variable is 8 characters depend on compiler and operation system.
  • Every variable name always should exist in the left hand side of assignment operator (invalid -> 10=a; valid -> a=10;).
  • No keyword should access variable name (int for <- invalid because for is keyword).
Note: In a c program variable name always can be used to identify the input or output data.

Variable declarations

This is the process of allocating sufficient memory space for the data in term of variable.

Syntax

Datatype variable_name; int a;
If no input values are assigned by the user than system will gives a default value called garbage value.

Garbage value

Garbage value can be any value given by system and that is no way related to correct programs. This is a disadvantage of C programming language and in C programming it can overcome using variable initialization.

Variable initialization

It is the process of allocating sufficient memory space with user defined values.

Syntax

Datatype nariable_name=value;

Example

int  b = 30;

Variable assignment

It is a process of assigning a value to a variable.

Syntax

Variable_Name = value

Example

int  a= 20;
int b;

Example

b = 25; // --> direct assigned variable
b = a;  // --> assigned value in term of variable
b = a+15;  // --> assigned value as term of expression


c-constant

Constant in C


Types of Constant in C

It is an identifier whose value can not be changed at the execution time of program. In general constant can be used to represent as fixed values in a C program. Constants are classified into following types.
If any single character (alphabet or numeric or special symbol) is enclosed between single cotes ' ' known as single character constant.
If set of characters are enclosed between double cotes " " known as string character constant.

Declare constant

const keyword are used for declare a constant.

Syntax

const int height = 100;

Example

#include<stdio.h>
#include<conio.h>

void main()
{
const int a=10;
printf("%d",a);
a=20; // gives error you can't modify const
getch();
}

c-keywords

Keywords in C

Keyword is a predefined or reserved word in C library with a fixed meaning and used to perform an internal operation. C Language supports 32 keywords.
Every Keyword exists in lower case latter like auto, break, case, const, continue, int etc.

32 Keywords in C Language

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile

c-comments

Comments in C


Generally Comments are used to provide the description about the Logic written in program. Comments are not display on output screen.
When we are used the comments, then that specific part will be ignored by compiler.
In 'C' language two types of comments are possible
  • Single line comments
  • Multiple line comments

Single line comments

Single line comments can be provided by using / /....................

Multiple line comments

Multiple line comments can be provided by using /*......................*/
Note: When we are working with the multiple line comments then nested comments are not possible.

Rules for Writing Comments

1. Program contains any number of comments at any place.

Example

// header files
#include<stdio.h>
#include<conio.h>

void main()
{
// variable declaration
int a,b,c;
a=10;
b=20;
c=a+b;
printf("Sum= %d",c);
getch();
}
2. Nested Comments are not possible, that means comments within comments.

Example

void main()
{
/*
/*      comments   */
*/
}
3. Comments can be splits over more than one line.

Example

void main()
{
/* main   
   function
   body part
*/
}
4. Comments are not case sensitive.

Example

void main()
{

/*  MAIN Function BODY   */

}
5. Single line comments start with "//"

Example

void main()
{

// Single line comment

}



Area-of-Circle


C Program to find Area of a Circle

In geometry, the area enclosed by a circle of radius r is πr2. Here the Greek letter π represents a constant, approximately equal to 3.14159, which is equal to the ratio of the circumference of any circle to its diameter.






#include<stdio.h>
#include<conio.h>

void main()
{
    float radius,area,pi=3.14;
    clrscr();
 
    printf("Enter the Radius of a Circle : ");
    scanf("%f",&radius);
 
    area = pi*radius*radius;
 
    printf("Area of Circle is: %f",area);
    getch();
}
 OUTPUT:
Enter the Radius of a Circle : 2
Area of Circle is: 12.560000