PDF

Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

Type of Storage class "register"

Leave a Comment
A 'register' storage class is very similar to 'auto' storage class except one most important property. All register variable in C stores in CPU not in memory.

Properties of register storage class

(1) In following declaration:

register in a;

We are only requesting not forcing to compiler to store variable a in CPU.Compiler will decide where to store in the variable 'a'.

(2) A register variable execute faster than other variables because it is stored in CPU so during the execution compiler has no extra burden to bring the variable from memory to CPU.

(3) Since a CPU have limited number of register so it is programmer responsibility which variable should declared as register variable i.e. variable which are using many time should declared as register variable.

(4) We cannot dereference register variable since it has any memory address.

For Example:

(a)

#include<stdio.h>

int main()
{
register int a = 10;
int *p;
p = &a;
printf("%u",p);
}


Output: Compilation Error


(5) Default initial value of register variable is garbage.

(6) Scope and visibility of register variable is block.


Introduction of Storage class

Leave a Comment
In C there are four types of storage class.
they are 


1. auto

2. register

3. static

4. extern


Storage class is modifier or qualifier of data types which decides :

1. In which area of memory a particular variable will be stored?
2. What is scope of variable?
3. What is visibility of variable?



Visibility of a variable in C:


Visibility means accessibility. Up to which part or area of a program, we can access a variable, that area of part is known as visibility of that variable.


Scope of a variable in C :


Meaning of scope is to check either variable is alive or dead. Alive means data of a variable has not destroyed from memory. Up to which part or area of the program a variable is alive, that area or part is known as scope of a variable.

In the above figure scope of variable a represented outer red box i.e. it is dead then variable must not be visible.


There are four type of scope in C :



1. Block scope

2. function scope

3. File scope

4. Program scope


This will be clear when all the topics are covered.

Type of Storage class "auto"

Leave a Comment
Automatic variables or auto variables are default storage class of local variable. An auto variable cannot be declared globally.

Properties of auto storage class


1 Default initial value of auto variable is garbage.

For example :

#include<stdio.h>
int main()
{
int i;
auto char c;
float f;
printf("%d %c %f",i,c,f);
return 0;
}

Output: Garbage Garbage Garbage

2. Visibility of auto variable is within the block where it has declared.

For example:

(a)

#include<stdio.h>
int main()
{
int a = 10;
{
int a = 20;
printf("%d",a);
}
printf("%d",a);
return 0;
}

Output: 20 10

Explanation: 
Visibility of variable a which has declared inside inner has block only within that block.

(b)

#include<stdio.h>
int main()
{
{
int a = 20;
printf("%d",a);
}
printf("%d",a);     // 'a' is not visible here
return 0;
}

Output: Compilation error

Explanation:
Visibility of variable a which has declared inside inner block has only within that block.


3. Scope of 'auto' variable is within the block where it has declared.

For Example:

#include<stdio.h>
int main()
{
int i;
for(i=0;i<4;i++)
{
int a = 20;
printf("%d",a);
a++
}
return 0;
}

Output: 20 20 20 20

Explanation: Variable declared inside the for loop block has only within that block.
After the first iteration variable a becomes dead and it looses it's incremented value. In second iteration variable 'a' is again declared and initialize and so on.


4. From above example it is clear 'auto' variable initialize each time.


5. An 'auto' vriable gets memory at run time.

Data Type Introduction

Leave a Comment

Introduction


Every programming language deals with some data. For example to print any message it requires charter or string type of data. To solve any mathematics expression it requires integral as well as real number (floating type) of data. C is very rich in data type. We can broadly divide all data type in C in three categories : 

  1. Primitive or fundamental data type 
  2. Derived data type
  3. User defined data type

Built-in data types or primitive data types :


  1.  int
  2.  float
  3.  double
  4.  char
  5.  void

int - data type


int is used to define integer numbers.

{
int count;
Count = 5;
}


float - data type


float is used to define floating point numbers.

{
float Miles;
Miles = 4.6;
}


double - data type


double is used to define BIG floating point numbers.
It reserves twice the storage for the number. On PCs this is likely to be 8 bytes;

{
double Atoms;
Atoms = 25000000;
}


char - data type 


char defines characters.

{
char Letter;
Letter = 'a';
}

Derived data types available are 


  1.  array
  2.  pointers
  3.  functions


User defined data types


  1.  structure 
  2.  union
  3.  enumerations


Derived data types and User defined data types will be explained in next sessions.

Data Type Modifiers

Leave a Comment

Modifiers 


The data types explained previously have the following modifiers : 

  1.  short 
  2.  long
  3.  signed
  4.  unsigned


The modifiers define the amount if storage allocated to variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

short int <= int <= long int

float <= double <= long double


What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.

What this means in the real word is:

Bytes:


2 short int
2 unsigned short int
4 unsigned int
4 int
4 long int
1 signed char
1 unsigned char
4 float
8 double
12 long double


Ranges


short int             -32,768 to +32,767

unsigned short int    0 to +65,535

unsigned int         0 to +65,535

int                  -32,768 to +32,767

long int             -2,147,483,648 to + 2,147,483,647

signed char          -128 to +127

unsigned char        0 to +255

float                3.4*10^-38 to 3.4*10^38

double               1.7*10^-308 to +1.7*10^308


Note: One simple way to remember the limits is :
Every 1 byte = 8 bits
so range is form (2^(n-1))  to  (2^(n-1))-1
where 'n' is the no. of. bits

e.g.
for int takes 2 bytes
2 bytes = 16 bits
so n = 16
so range is from (2^15) to (2^15)-1


Qualifiers 


A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:

The value of a variable can be changed. The value of variable must always be read from memory rather than from a register.

Standard C language recognizes the following two qualifiers:

1. const :


The const qualifiers is used to tell C that the variable value can not changed after initialisation.

const float pi = 3.24259;

Now pi cannot be changed at a later time within the program. Another way to define constants is with the # define preprocessor which has the advantage that it does not use any storage.

2. volatile :


The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler ( such as a variable updates by the system clock or by another program).
This prevents the compiler from optimizing code referring to the object by storing the objects value in a registered re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in 'C'. So for now just proceed.

Data Type Example

Leave a Comment
Note : As you know size of data types is compiler depended in C. Answer of all question is based upon that compilers whose word sixe is two byte. Just to know you, size of data type in 2 bytes compilers is as follows:

char  : 1 byte
int   : 2 byte
float : 4 byte
double: 8 byte

Please adjust the answers according to size of data types in your compiler.

1. What will be output when you will execute following C code?


#include<stdio.h>
int main()
{
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));
return 0
}

Output : 

Turbo C++ 3.0 = 8 4 2
Turbo C++ 4.5 = 8 4 2
Linux GCC     = 8 4 4
Visual C ++   = 8 4 4

Explanation : 

By default data type of numeric constants is :
6.5 = double
90000 = long int
'A' = char

In C size of data type varies from compiler to compiler.
In TURBO C 3.0 (16 bit compilers) size of : 
double is 8 byte
long int is 4 byte
character constant is 2 byte (size of char data type is one byte)

In TURBO C $.5 or Linux GCC compilers (32 bit compilers ) size of :
double is 8 byte
long int is 8 byte 
character constant is 2 byte


2. What will be output when you will execute following C code ?

#include<stdio.h>
int main()
{
double num = 5.2;
int var = 5;
printf("%d\t",sizeof(!num));
printf("%d\t",sizeof(var = 15/2));
printf("%d",var);

return 0;
}


Output :

Turbo C++ 3.0 = 2 2 5
Turbo C++ 4.5 = 2 2 5
Linux GCC     = 4 4 5
Visual C ++   = 4 4 5

Explanation:
sizeof(Expr) operator always returns the integer value which represents the size of the final value of the expression expr.

Consider on the following expression: 
!num
=!5.2
=0

0 is int type integer constant and it size is 2 by in TURBO C 3.0 compiler and 4 in the TURBO C 4.5 and Linux GCC compilers.

Consider on the following expression:
var = 15/2
= var = 7
= 7

7 is int type integer constant.

Any expression which is evaluated inside the sizeof operator. So value of variable 'var' will remain 5 in the printf statement.


3. What will be output when you will execute following C code?

#include<stdio.h>
int main()
{
signed x;
unsigned y;
x = 10 +-10u + 10u +-10;
y = x;
if(x==y)
printf("%d %d",x,y);
else if(x!=y)
printf("%u %u",x,y);

return 0;
}


Output :

Turbo C++ 3.0 = 0 0
Turbo C++ 4.5 = 0 0
Linux GCC     = 0 0
Visual C ++   = 0 0

Explanation : 
Consider on the expression:
x = 10 +- 10u + 10u +- 10;

10 = it is signed integer constant
10u = it is unsigned integer constant
X = it is signed integer variable.

In nay binary operation of dissimilar data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
As we know operators enjoy higher precedence than binary operators. So our expression is:

x = 10 + (-10u) + 10u + (-10);
  = 10 + -10 + 10 + (-10);
  =0

Note : Signed is higher data type than unsigned int. So, Corresponding signed value of unsigned 10u is +10


4. What will be output when you will execute following C code?

#include<stdio.h>
int main()
{
volatile int a = 11;
printf("%d",a);
return 0;
}


Output : Connot predict

Explanation:
We cannot predict the value of volatile vriavle because it's value can be changed by nay microprocessor interrupt.

Variables

Leave a Comment

Introduction


A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and it's type or class, before you actually try to do anything with it.

The Programming language C has Two main Variable Types as following:

Local Variables


Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block.
When a local variable is defined it is not initialised by the system, you must initialise it yourself. When execution of the block starts the variable is available and when the block ends the variable 'dies'.


Global Variables


Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it. Global variables are initialised automatically by the system when you define them !
Every variable in C has it's own name. A variable without any name is not possible in C. Most important properties of variables name are it's unique names. The rules for writing variable name is same as that of identifiers.

Not two variables in C can have same name with same visibility.

For Example:

#include<stdio.h>
int main()
{
int a = 5;    //Visibility is within main block
int a = 10;   //Visibility is within main block

/*  Two variables of same name  */

printf("%d",a);
return 0;
}

Output:  Compilation error


#include<stdio.h>
int a = 5;    // Visibility is in whole program
int main()
{
int a = 10;    //Visibility is within main block

/*  Two variables of ame name  */

printf("%d",a);
return 0;
}

Output:  10

But it is possible that two variable with same name but different visibility.

In this case variable name can access only that variable which is more local.

In C there is not any way to access global variable if any local variable is present of same name.

For Example:


#include<stdio.h>
int main()
{
int a=10;    //Visibility within main block
{
    a= a+5;       //Accessing outer local variable 'a'
    int a=20;     //Visibility within inner block
    a= a+10;     //Accessing inner local variable 'a'

    printf("%d",a);    //Accessing inner local variable 'a'
}
printf("%d",a);    //Accessing outer local variable 'a'
return 0;
}

Output:  30  15

Address of a Variable in C

Leave a Comment
Location in a memory where a variable stores it's data or value is known as address of variable.

To know address of any variable C has provided a special unary operator & which is known as deference operator or address operator.

It operator is only used with variables not with the constant.

For Example:


#include<stdio.h>
int main()
{
int a = 5;
printf("Address of variable 'a' is :",&a);
return 0;
}


We cannot write:  &&a   because:
&&a = &(&a) = & (address of variable 'a') = & (a constant number)
And we cannot use address operator with constant.


Important points about address of variables in C:

(1)

Address of any variable in c is an unsigned integer.
It cannot be a negative number.
So in printf statement we should use  ' %u ' instead of  ' %d ', to print the address of any variable.

Format specifiers:


" %d " : It is used to print signed decimal number.

" %u " : It is used to print unsigned decimal number.

" %x " : It is used to print a number in hexadecimal format using 0 to 9 and a,b,c,d,e,f.

" %X " : It is used to print a number in hexadecimal format using 0 to 9 and A,B,C,D,E,F.


since, if the address of any variable will beyond the range of signed short int it will print a cyclic value.

(2)

To print the address of any variable in hexadecimal number format by printf function we should use  ' %x ' or  ' %X '.

(3)

A programmer cannot know at which address a variable will store the data. It is decided by compiler or operating system.

(4)

Any two variables in C cannot have same physical address.

(5) 

Address of any variable reserve two bytes of memory spaces.

(6)

Address of any variable in C is not integer type so to assign an address to a integral variable we have to type cast the address.


Address arithmetic in C:


(1) 

We can subtract address of any two variables.

For Example:

#include<stdio.h>
int main()
{
int a;
int b;
printf("%d",&b-&a);
return 0;
}

(2) 

we cannot add, multiply, divide two address.

(3) 

We can add or subtract a integer number with address.

(4) 

Other operators which can be used with addresses are :

Negation operator           :  !
Size operator                    :  sizeof
Type casting operator     :  (Type)
Deference operator         :  *
Logical operator               :  && , ||

Examples of Variables

Leave a Comment
Example 1 : what will be output of the following C program?

#include<stdio.h>
int main()
{
int goto = 5;
printf("%d",goto);
return 0;
}

Output:  Compilation error

Explanation:  Invalid variable name 'goto' is keyword in C. Variable name cannot be any of keywords of C language.



Example 2 : What will be output of the following C program?

#include<stdio.h>
int main()
{
int _ = 5;
int __= 10;
int ___;
___ = _ + __;
printf("%d",___);
return 0;
}

Output :  15

Explanation: Variable name can have only underscore.



Example 3 : What will be output of the following C program?

#include<stdio.h>
int main()
{
int abcdefghijklmnopqrstuvwxyz123456789 = 10;
int abcdefghijklmnopqrstuvwxyz123456 = 40;

printf("%d",abcdefghijklmnopqrstuvwxyz123456);
return 0;
}

Output : Compilation error 

Explanation: Only first 32 characters are significant in variable name. So compiler will show error : Multiple declaration of identifier abcdefghijklmnopqrstuvwxyz123456.



Example 4 : What will be output of the following C Program?

#include<stdio.h>
int main()
{
int xyz = 20;
int xyz;
printf("%d",xyz);
return 0;
}

Output : Compilation error.

Explanation : Two local variables cannot have same name in same scope.



Example 5 : What will be output of the following C Program?

#include<stdio.h>
int main()
{
int main = 50;
printf("%d", main);
return 0;
}

Output :  50

Explanation : Variable name can be ' main ' because it is not a keyword.

Keywords

Leave a Comment
keywords are the reserved words used in programming. Each keywords has fixed meaning and that cannot be changed by user.

For Example:

int money;

Here, int is a keyword that indicates 'money' is of type integer.
As, C programming is case sensitive, all keywords must be written in lower-case. Here is the list of all keywords predefined by ASCII C.


keywords in C Language


  1. auto
  2. double
  3. int 
  4. struct
  5. break
  6. else
  7. long
  8. switch
  9. case
  10. enum
  11. register
  12. typeof
  13. char
  14. extern
  15. return
  16. union
  17. continue
  18. for
  19. signed
  20. void
  21. do
  22. if
  23. static
  24. while
  25. default
  26. goto
  27. sizeof
  28. volatile
  29. const
  30. float
  31. short
  32. unsigned