Professional Documents
Culture Documents
Lecture-4
C Program Structure
C – Constant:
C Constants are also like normal variables. But, only difference is, their
values can not be modified by the program once they are defined.
Constants refer to fixed values. They are also called as literals
Constants may be belonging to any of the data type.
Constant Example
Decimal Constant 10, 20, 450 etc.
Real or Floating-point Constant 10.3, 20.2, 450.6 etc.
Character Constant 'a', 'b', 'x' etc.
String Constant "c", "c program" etc.
C Program Structure
C – Constant:
There are two simple ways in C to define constants:
Using #define preprocessor.
C – Constant:
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
C – Constant:
The const Keyword You can use const prefix to declare constants with a
specific type as follows:
#include<stdio.h>
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
C Program Structure
C Format Specifier:
The Format specifier is a string used in the formatted input and output
functions. The format string determines the format of the input and output.
The format string always starts with a '%' character.
C Format Specifier:
include <stdio.h>
int main()
{
int b=10;
int c= -10;
float y=3.4;
char char_Var='c';
Escape Sequence in C:
An escape sequence in C language is a sequence of characters that doesn't
represent itself when used inside string literal or character.
Escape Sequence in C:
Escape Sequence in C:
#include<stdio.h>
int main(){
int number=50;
printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\"");
return 0;
}
C Program Structure
printf() function:
The printf() function is used for output. It prints the given statement to the
console.
The syntax of printf() function is given below:
printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the console.
scanf("format string",argument_list);
C Program Structure
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
C Program Structure
#include<stdio.h>
int main(){
int x=0,y=0,result=0;
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
C Program Structure
C Boolean:
In C, Boolean is a data type that contains two types of values, i.e., 0 and 1.
Basically, the bool type value represents two types of behavior, either true or
false. Here, '0' represents false value, while '1' represents true value.
In C Boolean, '0' is stored as 0, and another integer is stored as 1. In C, we have
to use the header file, i.e., stdbool.h. If we do not use the header file, then
the program will not compile.
bool variable_name;
#include <stdio.h>
#include<stdbool.h>
int main()
{
bool x=false; // variable initialization.
Comments in C:
Comments in C language are used to provide information about lines of code. It
is widely used for documenting code. There are 2 types of comments in the
C language.
Single Line Comments
Multi-Line Comments
Operators in C:
The data items on which operators act upon are called Operands.
Most operators are binary, meaning they take two operands. A few are
unary and only take one operand. There is a single ternary operator
which operates on three operands. Operators can be classified based on
the type of operations they perform
C Program Structure
Operators in C:
The data items on which operators act upon are called Operands.
Most operators are binary, meaning they take two operands. A few are
unary and only take one operand. There is a single ternary operator
which operates on three operands. Operators can be classified based on
the type of operations they perform
C Program Structure
Arithmetic Operators:
These are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus.
Following table shows all the arithmetic operators supported by C language.
Assume variable A holds 10 and variable B holds 20, then:
C Program Structure
Arithmetic Operators:
#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 of c is %d\n", c );
c = a / b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
c = a++;
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", c );
}
C Program Structure
If you use the ++ operator as a prefix like: ++var, the value of var is
incremented by 1; then it returns the value.
If you use the ++ operator as a postfix like: var++, the original value of
var is returned first; then var is incremented by 1.
#include <stdio.h>
int main() {
int var1 = 5, var2 = 5;
// 5 is displayed
// Then, var1 is increased to 6.
printf("%d\n", var1++);
// var2 is increased to 6
// Then, it is displayed.
printf("%d\n", ++var2);
return 0;
}
C Program Structure
Relational Operator:
These operators are used to compare the value of two variables.
Relational Operator:
#include <stdio.h>
void main()
{
int i, j, k;
i = 10;
j = 20;
k = i < j;
printf("i < j is %d\n",k);
k = i <= j;
printf("i <= j is %d\n",k);
k = i == j;
printf("i == j is %d\n",k);
k = i != j;
printf("i != j is %d\n",k);
k = i > j;
printf("i > j is %d\n",k);
k = i >= j;
printf("i >= j is %d\n",k);
}
C Program Structure
Logical Operators:
These operators are used to perform logical operations on the given two
variables.
Following table shows all the logical operators supported by C language.
Logical Operators:
|| (Logical OR) operator
A B A || B Example
0 0 0 (5 > 10) || (5 < 4) Both expressions are false. so, logical OR
output will be 0
0 1 1 10 > 20) || (10 < 20) First expression is false and second one
is true. so, logical OR output will be 1
1 0 1 (10 < 20) || (10 > 100) First expression is true and second one
is false. so, logical OR output will be 1
1 1 1 (10 < 20) || (10 < 100) Both expressions are true. so, logical
OR output will be 1
C Program Structure
Logical Operators:
&& (Logical AND) operator
If both left and right operands or expressions are true, it will return true.
A B A || B Example
0 0 0 (5 > 10) && (5 < 4) Both expressions are false. so, logical
AND output will be 0
0 1 0 (10 > 20) && (10 < 20) First expression is false and second
one is true. so, logical AND output will be 0
1 0 0 (10 < 20) && (10 > 100) First expression is true and second
one is false. so, logical AND output will be 0
1 1 1 (10 < 20) && (10 < 100) Both expressions are true. so,
logical AND output will be 1
C Program Structure
Logical Operators:
! (Logical NOT) operator
current state is true, Logical NOT (!) operator will make it false.
A !A Example
0 1 !(100 < 10) 100 is greater than 10. So, it will return false.
!(false) ==> true
1 0 !(10 < 100) 10 is less than 100. So, it will return true.
!(true) ==> false
C Program Structure
Logical Operators:
#include<stdio.h>
int main()
{
int a = 5, b = 10 ,ret;
ret = ( (a <= b) || (a != b) );
// 5 <= 10 ==> true. 5 != 10 ==> true. So, 1 || 1 will return 1.
printf("Return value of above expression is %d\n",ret);
return 0;
}
C Program Structure
Assignment Operators:
An assignment operator is used for assigning a value to a variable. The
Assignment Operators:
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
C Program Structure
Special Operators:
Conditional or Ternary Operator (?:)
Since the Conditional Operator ‘?:’ takes three operands to work, hence
#include <stdio.h>
int main()
{
int m = 5, n = 4;
return 0;
}