PDF

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