Skip to main content

C programming Variables.

What is variables? 

C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.The value of the C variable may get change in the program.C variable might be belonging to any of the data type like int, float, char etc.



Prog-languages



TypeDescription
charTypically a single octet(one byte). This is an integer type.
intThe most natural size of integer for the machine.
floatA single-precision floating point value.
doubleA double-precision floating point value.
voidRepresents the absence of type.

Variable Definition in C 

A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows −
type variable_list;
Here, type must be a valid C data type including char, w_char, int, float, double, bool, or any user-defined object; and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here −
int    i, j, k;
char   c, ch;
float  f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k; which instruct the compiler to create variables named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows −
type variable_name = value;
Some examples are −
extern int d = 3, f = 5;    // declaration of d and f. 
int d = 3, f = 5;           // definition and initializing d and f. 
byte z = 22;                // definition and initializes z. 
char x = 'x';               // the variable x has the value 'x'.
For definition without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables are undefined.

 

 

 Example code

 

#include <stdio.h>

// Variable declaration:
extern int a, b;
extern int c;
extern float f;

int main () {

   /* variable definition: */
   int a, b;
   int c;
   float f;
 
   /* actual initialization */
   a = 10;
   b = 20;
  
   c = a + b;
   printf("value of c : %d \n", c);

   f = 70.0/3.0;
   printf("value of f : %f \n", f);
 
   return 0;
}

 
























Comments

Popular posts from this blog

GCD and LCM easy calculation.

C Program to Find LCM of two Numbers Examples on different ways to calculate the LCM (Lowest Common Multiple) of two integers using loops and decision making statements. To understand this example, you should have the knowledge of following  C programming topics: C Programming Operators C if...else Statement C Programming while and do...while Loop The LCM of two integers  n1  and  n2  is the smallest positive integer that is perfectly divisible by both  n1  and  n2  (without a remainder). For example: the LCM of 72 and 120 is 360. Example #1: LCM using while Loop and if Statement #include <stdio.h> int main () { int n1 , n2 , minMultiple ; printf ( "Enter two positive integers: " ); scanf ( "%d %d" , & n1 , & n2 ); // maximum number between n1 and n2 is stored in minMultiple minMultiple = ( n1 > n2 ) ? n1 : n2 ; // Always true while ( 1 ) ...

Difference Between Waterfall and Agile Model.

Waterfall methodology is a software development methodology that is based on sequential-linear approach of software development. It reinforces the notion of "define before design" and "design before code". Whereas agile is based on increamental-iterative approach where requirements are expected to change frequently. # Waterfall Agile 1. Waterfall methodology is sequential and linear. Agile methodology is increamental and iterative. 2. Requirements have to be freezed at the beginning of SDLC. Requirements are expected to change and changes are incorporated at any point. 3. Working model of software is delivered at the later phases of SDLC. Working model is delivered during initial phases and successive iteration of the model are delivered to the client for feedback. 4. It is difficult to scale-up projects based on waterfall methodology. Scaling up of products is easy because of the iterative approach. 5. Customers or end user doesn't have a...