Tuesday, July 21, 2020

Exploring float data type in C

  • Float is one among the basic data type in C. 
  • Floating point numbers in C has precision up to 6 decimal places. 
  • The header file float.h defines macros that allows you to use values and other binary representation of real numbers in the program.  
  • double data type has precision up to 15 decimal places.
  • By default all the decimal numbers in C are double.
  • long double data type has precision up to 19 decimal places.
  • The following table shows the range of values for each data type,
                            
  • Float data type has the highest order of precedence over all the other data types. So, doing arithmetic operation with float and any other data type will results in float only.
  • Modulo operator (%) does not work on float.
  • If the value of the float variable exceeds the range then roll over happens.
Let us look into some examples for understanding it better,

Examples:

#include<stdio.h> 
int main() 
float a = 4.3; 
if (x == 4.3) 
printf("Inside if"); 
else if (a == 4.3f) 
printf("Inside else if"); 
else
printf("Inside else"); 

The output of the above code is 'Inside else if'  because by default all the decimal numbers in C are considered as double ans so first if condition will be false. Because comparing two different data types will also be false no matter what their values are.

#include<stdio.h> 
int main() 
float a = 1.5; 
if (x == 1.5) 
printf("Inside if"); 
else if (a == 1.5f) 
printf("Inside else if"); 
else
printf("Inside else"); 

In this examples output is 'Inside if' because if we convert 1.5 into its equivalent decimal then we can see that after one place place all the remaining digits will be zero i.e 1.1000... whatever the precision may be so the If condition will be true.

For more examples and quizzes follow our Instagram page @learnclang.




 

Saturday, July 18, 2020

Exploring integer data type in C

Points to be known:
  • An integer data is one among the basic data types in C language. 
  • By default all the integer in C are signed.
  • For using unsigned we must use unsigned int keyword.
  • Size of the integer may be 2 byte or 4 byte, it is purely compile dependent.
  • short is a qualifier which makes the size of integer 2 or 4 bytes.
  • long is also a qualifier which makes the size of the integer data type 8 byte.
  • The following picture depicts what will happen when the value of the integer variable exceeds its range. Roll over happens whenever the value exceeds the range. 
  • In the below diagram a 2 byte signed integer's roll over is shown.
                                      
  • In case of Unsigned int the value ranges from 0 to 65,536 for 4 byte and 0 to 32,767 for 2 byte int.
Examples:
Consider the following example,

#include<stdio.h>
int main()
{
int a = 5;
float b = 10.5;
printf("%f",a+b);
return 0;
}

Here the output will be 15.500000 because float variable has higher precedence than int variable.

Consider the same example

#include<stdio.h>
int main()
{
int a = 5;
float b = 10.5;
printf("%d",a+b);
return 0;
}

If we try to print the integer part of this expression, the output will be 0 here. Because when an int variable is added with float, the resulting value always will be float for the above mentioned reason.

Again consider the same example with slight modification,

#include<stdio.h>
int main()
{
int a = 5;
char b = 'a';
printf("%d",a+b);
return 0;
}

Here the output will be 102, because the ASCII value of the character will be added and %d is the format specifier so it will print the integer value of the expression.

Extras:
  • Both %d and %i can to used as format specifier for integer in C.
  • %5d will print the integer value after 5 spaces before it.
For more examples follow our Instagram page

Learnclang


Tuesday, July 14, 2020

Digging Deeper into switch...case in C

Switch...case is similar to if...else but syntax of switch...case will be much easier compared to if...else. You won't get confused while using switch...case. Nested if...else will be more complex and may not be easier for the reader to decode the program, so switch...case will be the better option. It allow execution of one code block at a time based upon the value of the expression. 

If the expression doesn't match any of the constants in the case statement then default case will be executed. 

Syntax:

switch (expression)
​{
    case constant1:
      // statements
      break;

    case constant2:
      // statements
      break;
    .
    .
    .
    default:
      // default statements
}

Let us look into some examples for better understanding,

Example 1: 

#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
switch(i)
{
case 1:
case 2:
case 3:
case 5:
case 4: printf("A");
default: printf("C");
}
}
return 0;
}

Output:





  • In the above example there is no break in the case block. So in this kind of situation, all the case blocks below the matched case will be executed including default.
  • For executing a particular case block alone it should be terminated with break, otherwise case blocks below that case block will also be executed.
  • The order of the case doesn't matter. 
  • If the switch expression doesn't match with any of the case constants then default block will be executed.
  • default case can be placed anywhere inside the switch block.
Example 2:

float x = 1.5;
switch (x)
{
    case 1.1: printf("A"); 
    ....
}

The above code snippet will throw an error because the switch expression must be an integer type.

Example 3:

int x = 1.5;
switch (x)
{
    case 1printf("A");  break;

    case 1printf("B");  break;
    ....
}

The above code snippet will throw an compiler error because two case labels cannot have same value. We should not duplicate the case labels.

Example 4:

int x = 1.5;
int y;
switch (x)
{
    y = x; //statement inside switch
    case 1printf("A");  break;

    case 1printf("B");  break;
    ....
}

Note: Any statement inside the switch block other than the case statements will not be executed. It will be skipped by the compiler, it won't throw an error.

Monday, July 13, 2020

Storage Class Specifiers in C


Storage class specifiers in C language instructs the compiler where to store a variable, how to store the variable, what is the initial value of the variable and life time of the variable. There are four types of storage class specifiers in C, they are 
  1. Auto
  2. Static
  3. Extern
  4. Register
The following table makes you to understand about them better


Important points to be known:
  • For faster accessing a variable should be declared as a register. Because register variables are stored in CPU registers not in Main Memory so fetching will be faster.
  • Static variable once initialized cannot be changed. It can never be reset after initialization.
  • volatile and const has something to do with storage but they are qualifiers not a storage class.
  • typedef is also a storage class in c as declaring it before a storage class will throw an error "multiple storage class for a variable".
Syntax:

<storage_class_specifier>  <data_type> <variable_name>;

For examples on storage class follow example series in this blog.

Tuesday, July 7, 2020

Best way to Learn C Programming

C is obviously the most powerful general purpose programming language for beginners. It is a procedural language which supports structured programming. To know about the basics and History behind the C Language refer this website https://en.wikipedia.org/wiki/C_(programming_language) 

Popular websites to learn C Programming are
  1. tutorialspoint - https://www.tutorialspoint.com/cprogramming/index.htm
  2. w3schools - https://www.w3schools.in/c-tutorial/
  3. javatpoint - https://www.javatpoint.com/c-programming-language-tutorial
  4. geeksforgeeks - https://www.geeksforgeeks.org/c-programming-language/
  5. fresh2refreh - https://fresh2refresh.com/c-programming/
Some of the best books for C programming are
  1. The C Programming Language by Brian Kernighan and Dennis Ritchie 
  2. C Programming: A Modern Approach by Kim N. King
  3. Expert C Programming by Peter Van der Linden
  4. C, The Complete Reference by Herbert Schildt
  5. C in Depth by Srivastava
  6. C Programming Absolute Beginner's Guide by S. D. Perry                   
Courses in Udemy and Youtube will be more helpful for learning Programming in an interactive way. 

Exploring float data type in C

Float is one among the basic data type in C.  Floating point numbers in C has precision up to 6 decimal places.  T he header file float.h de...