Posts

Showing posts from May, 2018

Data types and Variable Declarations in C Language

Image
Data types in C Language Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities. C language supports 2 different type of data types: Primary data types : These are fundamental data types in C namely integer( int ), floating point( float ), character( char ) and  void . Derived data types : Derived data types are nothing but primary datatypes but a little twisted or grouped together like  array ,  stucture ,  union  and  pointer . These are discussed in details later. Data type determines the type of data a variable will hold. If a variable  x  is declared as  int . it means x can hold only integer values. Every variable which is used in the program must be declared as what data-type it is. Integer type Integers are used to store whole numbers. Size and range of Integ

Arithmetic Operators in C

Arithmetic Operators in C The following table shows all the arithmetic operators supported by the C language. Assume variable  A  holds 10 and variable  B  holds 20, then  − Operator Description Example + Adds two operands. A + B = 30 − Subtracts second operand from the first. A − B = -10 * Multiplies both operands. A * B = 200 / Divides numerator by de-numerator. B / A = 2 % Modulus Operator and remainder of after an integer division. B % A = 0 ++ Increment operator increases the integer value by one. A++ = 11 -- Decrement operator decreases the integer value by one. A-- = 9 Example Try the following example to understand all the arithmetic operators available in C − #include <stdio.h main () { int a = 21 ; int b = 10 ; int c ; c = a + b ; printf ( "Line 1 - Value of c is %d\n" , c ); c = a - b ; printf ( "Line 2 - Value of c is %d\n" , c ); c = a * b ; printf ( "Line 3 - Value

Logical Operators in C

Logical Operators in C Following table shows all the logical operators supported by C language. Assume variable  A  holds 1 and variable  B  holds 0, then − Operator Description Example && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true. ! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true. Example Try the following example to understand all the logical operators available in C − #include <stdio.h> main () { int a = 5 ; int b = 20 ; int c ; if ( a && b ) { printf ( "Line 1 - Condition is true\n" ); } if ( a || b ) { printf ( "Line 2 - Condition i

Relational Operators in C

Relational Operators in C The following table shows all the relational operators supported by C language. Assume variable  A  holds 10 and variable  B  holds 20 then − Operator Description Example == Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to t

C Programming break and continue Statement

Image
C Programming break and continue Statement In this tutorial, you will learn how to use break and continue statements to alter the program flow of loops. It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases,  break  and  continue  statements are used. break Statement The break statement terminates the loop ( for ,  while and do...while loop ) immediately when it is encountered. The break statement is used with decision making statement such as  if...else . Syntax of break statement break; The simple code above is the syntax for break statement. Flowchart of break statement How break statement works? Example #1: break statement // Program to calculate the sum of maximum of 10 numbers // Calculates sum until user enters positive number # include <stdio.h> int main () { int i ; double number , sum = 0.0 ; for (