You are on page 1of 37

Programming Fundamentals

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.

 Using const keyword.

The #define Preprocessor Following is the form to use #define preprocessor to


define a constant

#define identifier value


C Program Structure

 C – Constant:
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;

area = LENGTH * WIDTH;


printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
C Program Structure

 C – Constant:
The const Keyword You can use const prefix to declare constants with a
specific type as follows:

const type variable = value

#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.

Format specifier Description


%d or %i It is used to print the signed integer value where signed
integer means that the variable can hold both positive
and negative values.
%f It is used for printing the decimal floating-point values.
By default, it prints the 6 values after '.'.
%e/%E It is used for scientific notation. It is also known as
Mantissa or Exponent.
%c It is used to print the unsigned character.
%s It is used to print the strings.
C Program Structure

 C Format Specifier:
include <stdio.h>
int main()
{
int b=10;
int c= -10;
float y=3.4;
char char_Var='c';

printf("Value of b is:%u \n", b);


printf("\nValue of c is:%u \n",c);
printf("Floating point value of y is: %f \n", y);
printf("Exponential value of y is: %e \n", y);
printf("Value of a is: %c", char_Var);
return 0;
}
C Program Structure

 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.

It is composed of two or more characters starting with backslash \. For example:


\n represents new line.
C Program Structure

 Escape Sequence in C:

Escape Sequence Meaning


\a Alarm or Beep
\b Backspace
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\' Single Quote
\" Double Quote
\? Question Mark
C Program Structure

 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() and scanf() in C:


The printf() and scanf() functions are used for input and output in C language.
Both functions are inbuilt library functions, defined in stdio.h (header file).

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

 printf() and scanf() in C:

#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

 printf() and scanf() in C:

#include<stdio.h>
int main(){
int x=0,y=0,result=0;

printf("enter first number:");


scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);

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.

printf("The value of x is %d",x);


}
C Program Structure

 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

 Single Line Comments:


Single line comments are represented by double slash \\. Let's see an example of
a single line comment in C.
#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}
C Program Structure

 Backslash Character Constants:


Even you can place the comment after the statement. For example:
printf("Hello C");//printing information  

 Mult Line Comments:


Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy
many lines of code, but it can't be nested. Syntax:
/*
#include<stdio.h>
code
int main(){
to be commented /*printing information
*/. Multi-Line Comment*/
printf("Hello C");
return 0;
}
C Program Structure

 Operators in C:

 An operator in a programming language is a symbol that tells the


compiler or interpreter to perform a specific operation and produce a
final result.

 An operator is simply a symbol that is used to perform operations.

 An Operator is a symbol in C language that performs some operations


on some data.

 An operator is a symbol that tells the compiler to perform specific


manipulations. C language is rich in built-in operators
C Program Structure
 Operators in C:

 The data items on which operators act upon are called Operands.

 Operators form Expressions by joining individual operands which can


be constants, variables, etc. Most operators allow the individual
operands to be expressions.

 C programming language provides several operators to perform different


kind to operations. There are operators for assignment, arithmetic
functions, logical functions and many more. These operators generally
work on many types of variables or constants, though some are
restricted to work on certain data types.

 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.

 Operators form Expressions by joining individual operands which can


be constants, variables, etc. Most operators allow the individual
operands to be expressions.

 C programming language provides several operators to perform different


kind to operations. There are operators for assignment, arithmetic
functions, logical functions and many more. These operators generally
work on many types of variables or constants, though some are
restricted to work on certain data types.

 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

 C Increment and Decrement Operators:


 C programming has two operators increment ++ and decrement -- to change
the value of an operand (constant or variable) by 1.

 Increment ++ increases the value by 1 whereas decrement -- decreases the


value by 1. These two operators are unary operators, meaning they only
operate on a single operand.
C Program Structure

 C Increment and Decrement Operators:


 ++ and -- operator as prefix and postfix

 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.

 The -- operator works in a similar way to the ++ operator except --


decreases the value by 1.
C Program Structure

 C Increment and Decrement Operators:


 ++ and -- operator as prefix and postfix

#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.

 Following table shows all the relational operators supported by C

language. Assume variable A holds 10 and variable B holds 20, then:


C Program Structure

 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.

Assume variable A holds 1 and variable B holds 0, then:

 Operands can be constants or variables containing integer quantities,


floating-point quantities or characters. In addition, the operands can be
relational expressions and/or arithmetic expressions. A logical operator
along with its operand(s) gives rise to Logical Expression. The output of
a logical expression is a Boolean value (true=non-zero or false=0)
decided by the nature of operation.
C Program Structure

 Logical Operators:
 || (Logical OR) operator

 |If one of the operands or expressions is true, it will return 1.

 If all of them are false, it will return 0.

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.

Otherwise, it will return false.


 Note, non-zero value operands are considered as 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

 Logical NOT operator is used to inverse the current decision. Say, if

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);

ret = ( ( a < b) && (a == b ) );


// 5 < 10 ==>true. 5 == 10 ==> false. So, 1 && 0 will return 0;
printf("Return value of above expression is %d\n",ret);

ret = ! ( ( a < b) && (a == b ) );


/*we have used the same expression here.
And its result was 0.
So !0 will be 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

most common assignment operator is =


 There are following assignment operators supported by C language:
C Program Structure

 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 (?:)

 The conditional operator is kind of similar to the if-else statement as it

does follow the same algorithm as of if-else statement but the


conditional operator takes less space and helps to write the if-else
statements in the shortest way possible.
 Syntax: The conditional operator is of the form

 variable = Expression1 ? Expression2 : Expression3


C Program Structure
 Special Operators:
 Conditional or Ternary Operator (?:)

 Since the Conditional Operator ‘?:’ takes three operands to work, hence

they are also called ternary operators.


 Working:

 Here, Expression1 is the condition to be evaluated. If the

condition(Expression1) is True then Expression2 will be executed


and the result will be returned. Otherwise, if the
condition(Expression1) is false then Expression3 will be executed
and the result will be returned.
C Program Structure
 Special Operators:
 Conditional or Ternary Operator (?:)
C Program Structure
 Special Operators:
 Conditional or Ternary Operator (?:)

#include <stdio.h>

int main()
{
int m = 5, n = 4;

(m > n) ? printf("m is greater than n that is %d > %d",


m, n)
: printf("n is greater than m that is %d > %d",
n, m);

return 0;
}

You might also like