C Fundamentals: 26/4/2016 Refresher Course On Programming Basics Using C and C++ (Recopb'16)
C Fundamentals: 26/4/2016 Refresher Course On Programming Basics Using C and C++ (Recopb'16)
Compound Statements
• A compound statement consists of several individual
statements enclosed within a pair of braces:
• Example:
{ c = 3;
x = y + z;
}
Control Statement
• Example:
while (num<=3)
{ x = x + 1;
num = num + 1;
}
C Instructions: Rules Applicable To
All C Statements
• Blank spaces may be inserted between two words to
improve readability, however no blank spaces are
allowed within a variable, constant, or keyword
• Usually all statements are entered in small case
letters (all keywords are also in small letters)
• C statement always ends with a ; (semicolon)
• Comments are added using /* … …*/ or //
• A block of statement are enclosed within { }
Anatomy of C Program
C Libraries
A Simple C Program
Another C program
C is a ___ language
a) High Level
b) Low Level
c) Middle Level
d) Machine Level
C is a ___ language
a) High Level
b) Low Level
c) Middle Level
d) Machine Level
a A
Operators Used in C
Operators Used in C
• An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations.
• There are eight categories of operators used in C. They are:
– Arithmetic operators
– Unary operators
– Relational operators
– Logical operators
– Assignment operators
– Conditional operator
– Bitwise operators
– Special operators
Arithmetic / Binary Operators
Operators Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% (modulus operator)
Remainder after Integer Division
Operators Example
Unary Minus (-) -a, -3
Increment operator (++) ++a, a++
Decrement operator (--) --a, a--
sizeof operator sizeof(a)
(type) ((int)(i+f) % 4)
Negation operator (!) !(a > b)
Post-Increment & Pre-Increment
Unary Operators
Operators Meaning
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
Equality == Equal to
Operators
!= Not Equal to
Example of Relational Operators
Example: If a = 1, b = 2, c = 3, then,
Operators Meaning
&& Logical AND
|| Logical OR
Note: If a = 3, then, c = (a *= 3) = ?
Multiple Assignments
• Remember
• Equality operator (==) is used to determine if
two expressions have the same value
Conditional / Ternary Operator (?:)
• A conditional expression is written in the form:
expression 1 ? expression 2 : expression 3
Example: a = (b > c) ? b : c;
• When evaluating a conditional expression, expression 1
will be evaluated first. If expression 1 is true, then,
expression 2 is evaluated. However, if expression 1 is
false, expression 3 is evaluated
Operators Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise EXCLUSIVE OR
<< Shift Left
>> Shift Right
Special Operators
• C also supports various special operators such
as:
– comma operator (used in for loop)
– pointer operators (& and *), and,
– member selection operators (. and ->)
Hierarchy of Operators
Operator Category Operators Associativity
Unary operators - ++ -- ! sizeof (type) RL
Arithmetic multiply, divide * / % LR
and remainder
Arithmetic add and subtract + - LR
Relational operators < <= > >= LR
Equality operators == != LR
Logical and && LR
Logical or || LR
Conditional operator ?: RL
Assignment operators = += -= *= /= %= RL
Operator Precedence is related to the hierarchy of operators, i.e., which operator will be
used first
Questions to Solve
• Evaluate the following expressions:
a) x = -3 * -4 % -6 / -5
b) x = -3 + 4 – 7 * 8 / 5 % 10
c) x = 4 % 5 + 6 % 5
d) y = 2 * 3 /4 + 4 / 4 + 8 – 2 + 5 / 8
• Result:
a) x = 0
b) x = 0
c) x = 5
d) y = 8
Quick Answers to Questions
Which operator has the lowest priority ?
a) ++
b) %
c) +
d) ||
What will be the value of the variable a, on the execution of the above
program?
void main() {
int a= 0;
for ( ; a ;);
a++; }
a) -1
b) 0
c) 1
d) None of these
Quick Answers to Questions
What is sizeof In ‘C’ ?
a) Reserved word
b) Operator
c) Both (a) and (b)
d) Function
What will be the value of the variable a, on the execution of the above
program?
void main() {
int a= 0;
for ( ; a ;);
a++; }
a) -1
b) 0
c) 1
d) None of these
Quick Answers to Questions
What will be the output ?
void main ( ) {
int x;
unsigned y;
printf(“\n%d %d”, sizeof(x), sizeof(y) ); }
a) 2 2
b) 2 4
c) 4 4
d) None of these
Quick Answers to Questions
What will be the output ?
void main ( ) {
int x;
unsigned y;
printf(“\n%d %d”, sizeof(x), sizeof(y) ); }
a) 2 2
b) 2 4
c) 4 4
d) None of these
Compilation Process in C
Executing a ‘C’ Program
Syntax Yes
Errors?
No Object Code
System Library Link With System Library
Executable Object Code
Input Data Executable Object Code
#include <stdio.h>
void main()
{
int a = 3, b = 4;
c = a * b;
printf(“%d %d %d”, a, b, c);
}
Format Specifier / Conversion
Character / Control String
Some More Examples of printf() Function
Output:
printf(): Precision
float x = 123.456;
printf(“\n %.3f %.2f %.4f”, x, x, x);
Output:
int a;
float b;
double p;
char x;
scanf(“%d”, &a);
scanf(“%f”, &b);
scanf(“%lf”, &p);
scanf(“%c”, &x);
The getchar() and putchar()
Library Functions
• Reading a single character can be done using
the function getchar().
• Syntax: variable_name = getchar();
if (expression)
{
statement-block;
}
• Here, if expression is true, then statement-block is
executed. If the expression is false, then, statement-block
is skipped (not executed).
• The statement-block may be a single statement or a group
of statements.
An Example of the if Statement
int main()
{
int x;
printf(“\n Enter a Number : “);
scanf(“%d”, &x);
if (x > 0)
{
printf(“\n The number is : %d”, x);
printf(“\n It is Positive”);
}
return 0;
}
The if-else Statement
• The if … else statement can be used as follows:
if(expression)
{
statement-block1;
}
else
{
statement-block2;
}
• Here, if expression is true, then statement-block1 is executed. If the
expression is false, then, statement-block2, after the else, is executed.
An Example of the if-else Statement
int main()
{
int x;
printf(“\n Enter a Number : “);
scanf(“%d”,&x);
if (x >= 0)
printf(“\n The number %d is positive”, x);
else
printf(“\n The number %d is negative”, x);
return 0;
}
Another Example
int main()
{
int x;
printf(“\n Enter a Number : “);
scanf(“%d”,&x);
if (x > 0)
printf(“\n The number %d is positive”, x);
else if(x < 0)
printf(“\n The number %d is negative”, x);
else if(x == 0)
printf(“\n The number is zero”);
return 0;
}
Try doing the following program
• Write C programs for the following problems:
– to take two numbers as input and display which is the
larger number.
– to enter a number and display whether it is odd or even.
– to input a number and display whether the number is
single-digit or more than single-digit number.
– to input a character and display whether the character is
vowel or not.
– to input three numbers and find out the biggest as well as
smallest among them
Nested if-else statements
When a series of decisions are involved, we may have to use more than one if… else
statement in nested form as shown below:
if (condition1)
{
if (condition2)
{
statement-block1;
Outer Inner }
if if else
{
statement-block2;
}
}
else
{
statement-block3;
}
An Example of Nested if-else
#include <stdio.h>
int main()
{ int n;
printf(“Enter a Number : ”);
scanf(“%d”,&n);
if(n > 0)
{
if ((n>=1) && (n<=9))
printf(“It is single-digit positive number ”);
else
printf(“It is more than single-digit positive number ”);
}
else
printf(“It is either zero or a negative number”);
return 0;
} G. Nandi, Dept. of Computer Sc. & IT, DBU
The switch-case Statement
This control statement allows decision to be made from a number
of choices.
1. It is useful if a variable is
compared with different
constants, and in case it is
equal to a constant.
2. The constant in the case
statement can be of char or
int data type only.
int main()
{ int x, y, res, ch;
switch-case: Example 1
printf(“Enter a Number : ”);
scanf(“%d”,&x);
printf(“Enter another Number : ”);
scanf(“%d”,&y);
printf(“\n Enter Choice: 1. Add 2. Subtract 3. Multiply 4. Divide “);
scanf(“%d”,&ch);
switch(ch)
{
case 1: res = x + y;
break;
case 2: res = x – y;
break;
case 3: res = x * y;
break;
case 4: res = x / y;
break;
default: printf(“\n\n Wrong Choice”);
}
printf(“\n Result = %d “,res);
return 0;
}
int main()
{ char v;
switch-case: Example 2
printf(“Enter a Vowel : ”);
scanf(“%d”,&v);
switch(v)
{
case ‘a’:
case ‘A’: printf(“The vowel is A”);
break;
case ‘e’:
case ‘E’: printf(“The vowel is E”);
break;
case ‘i’:
case ‘I’: printf(“The vowel is I”);
break;
case ‘o’:
case ‘O’: printf(“The vowel is O”);
break;
case ‘u’:
case ‘U’: printf(“The vowel is U”);
break;
default: printf(“\n\n It is not a vowel”);
}
return 0;
}
The goto Statement
• A goto statement is used to branch unconditionally from one
point to another in the program.
• The goto requires a label in order to identify the place where
the branch is to be made.
• The use of goto statement is usually avoided in C programming.
• Example:
int main()
{
int x = 0;
if (x == 0) goto a;
printf(“\n The number is not zero”);
exit(1); // to come out of the program
a: printf(“\n Oops… The number is zero”);
return 0;
}
Quick Answer to Question
What is the output of the following program ?
void main() {
int x=40;y=30;z=80;
if(x<y<z)
printf(“\n Hello world”);
else
printf(“\nGood by”);
a) Hello world
b) Good by
c) Compile time error
d) None of these
Quick Answer to Question
What is the output of the following program ?
void main() {
int x=40;y=30;z=80;
if(x<y<z) X<y -> 0
printf(“\n Hello world”);
else 0<z -> 1
printf(“\nGood by”);
a) Hello world
b) Good by
c) Compile time error
d) None of these
Control Structures: Loops
Control Structures
• Decision Making Statements
– if statement
– if-else statement
– Conditional Operator statement
– switch statement
– goto statement
• Loop Constructs
– while loop
– do-while loop
– for loop
Loop Constructs
Loop Constructs: while loop
• The while loop has the general form:
while (conditional expression)
{
statement-block;
}
For the above example, in the first case, the while loop will not be
executed at all.
However, in the second case, the do-while loop will be executed at
least once even if the condition is false.
Loop Constructs: for loop
• This loop is useful when a block of statement is to be
executed a fixed number of times.
• In the syntax given below:
– The initial expression is executed only once
– The test expression is tested once before every iteration. If
it is true the block of statement is executed once else the
loop terminates.
– The update expression is executed once after every
iteration
for (initial expression; test expression; update expression)
{
statement-block;
}
The for Loop: Example 1
//display square of all numbers from 1 to n
#include<stdio.h>
int main()
{
int n, i, sq;
printf(“Enter the limit :”);
scanf(“%d”, &n);
for(i=1; i <= n; i++)
{
sq = i * i;
printf(“\n Square of %d = %d”, i, sq);
}
return 0;
}
Nesting of Loops
• If we are using one loop within another loop that is called as
nested loop. For example:
int main()
{
for(i = 1; i <= 3; i++)
{
for(j = 1; j <= i; j++)
printf(“ * ”);
printf(“\n”);
}
return 0;
}
Valid for Loops
The break and continue Statements
• http://www.programiz.com/c-programming/
• http://www.cprogrammingexpert.com/C/
for your patience…
Refresher Course