Unit-1 Part2, Unit-2 Upto Loops
Unit-1 Part2, Unit-2 Upto Loops
Introduction
C Identifiers
In C programming language, programmers can
specify their name to a variable, array, pointer,
function, etc... An identifier is a collection of
characters which acts as the name of variable,
function, array, pointer, structure, etc... In
other words, an identifier can be defined as the
user-defined name to identify an entity
uniquely in the c programming language that
name may be of the variable name, function
name, array name, pointer name, structure
name or a label.
The identifier is a user-defined name of an
entity to identify it uniquely during the
program execution
Example
int marks;
char studentName[30];
C Tokens
In a C program, a collection of all the
keywords, identifiers, operators, special
symbols, constants, strings, and data values
are called tokens.
Tokens are used to construct c programs and
they are said to the basic building blocks of a c
program.
In a c program tokens may contain the
following...
1. Keywords
2. Identifiers
3. Operators
4. Special Symbols
5. Constants
6. Strings
7. Data values
8. C Keywords
C Output Functions
printf() function
The printf() function is used to print string or
data values or a combination of string and data
values
on the output screen (User screen). The
printf() function is built-in function defined in
a header file called "stdio.h". When we want
to use printf() function in our program we
need to include the respective header file
(stdio.h) using the #include statement.
Example:
printf("message to be display!!!");
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
C Input Functions
scanf() function
The scanf() function is used to read multiple
data values of different data types from the
keyboard. The scanf() function is built-in
function defined in a header file called
"stdio.h". When we want to use scanf()
function in our program, we need to include
the respective header file (stdio.h)
using #include statement.
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i;
printf("\nEnter any integer value: ");
scanf("%d",&i);
printf("\nYou have entered %d number",i);
}
C Operators
An operator is a symbol used to perform
arithmetic and logical operations in a program.
That means an operator is a special symbol
that tells the compiler to perform
mathematical or logical operations. C
programming language supports a rich set of
operators that are classified as follows.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment & Decrement Operators
5. Assignment Operators
6. Bitwise Operators
7. Conditional Operator
8. Special Operators
1.Arithmetic Operators (+, -, *, /, %)
+ Addition 10
- Subtraction 10
* Multiplication 10
/ Division 10
Operator Meaning Exa
j = i++; // Post-Increment
}
Output:
int main() {
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Operato
r Meaning
Output
Output = 8
Bitwise OR Operator |
The output of bitwise OR is 1 if at least
one corresponding bit of two operands
is 1. In C Programming, bitwise OR
operator is denoted by |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
#include <stdio.h>
int main() {
int main() {
Output
Output = 21
Bitwise Complement Operator ~
Bitwise complement operator is a unary
operator (works on only one operand). It
changes 1 to 0 and 0 to 1. It is denoted
by ~.
35 = 00100011 (In Binary)
int main() {
Output
Output = -36
Output = 11
Shift Operators in C programming
There are two shift operators in C
programming:
Right shift operator
Left shift operator.
Right Shift Operator
Operato
r Meaning
int main() {
int num=212, i;
}
printf("\n");
return 0;
}
Run Code
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53
Operato
r Meaning
1 () function call L
[] array reference
-> structure member access
. structure member access
2 ! negation R
~ 1's complement
+ Unary plus
- Unary minus
++ increment operator
-- decrement operator
Precedence Operator Operator Meaning A
3 * multiplication L
/ division
% remainder
4 + addition L
- subtraction
7 == equal to L
!= not equal to
Precedence Operator Operator Meaning A
9 ^ bitwise EXCLUSIVE OR L
10 | bitwise OR L
12 || logical OR L
13 ?: conditional operator L
14 = assignment R
*= assign multiplication
/= assign division
%= assign remainder
+= assign addition
-= assign subtraction
&= assign bitwise AND
^= assign bitwise XOR
|= assign bitwise OR
<<= assign left shift
>>= assign right shift
Precedence Operator Operator Meaning A
15 , separator L
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 15;
int result;
// Left to right associativity example
result = a - b - c;
printf("Result of a - b - c: %d\n", result);
// Right to left associativity example
result = a = b = c;
printf("Result of a = b = c: %d\n",
result);
printf("Values after assignment: a = %d,
b = %d, c = %d\n", a, b, c);
return 0;
}
1. Left to Right Associativity:
o In the expression a - b - c, the
subtraction operators have left-to-
right associativity. So, it is evaluated
as (a - b) - c.
2. Right to Left Associativity:
o In the expression a = b = c, the
assignment operators have right-to-
left associativity. So,
o it is evaluated as a = (b = c).
Type Conversion:
Type conversion in C is the process of
converting one data type to another. The type
conversion is only performed to those data
types where conversion is possible. Type
conversion is performed by a compiler.
For example, if you try to divide two
integers, 5 by 2, you would expect the result to
be 2.5. But since we are working with integers
(and not floating-point values), the following
example we just got output 2:
Example
int x = 5;
int y = 2;
int sum = 5 / 2;
printf("%d", sum); // Output 2
To get the right result, you need to know
how type conversion works.
There are two types of conversion in C:
Implicit Conversion (automatically)
Explicit Conversion (manually)
#include <stdio.h> As you can see, the
compiler automatically converts the int
value 9 to a float value of 9.000000.
This can be risky, as you might lose control
over specific values in certain situations.
Especially if it was the other way around - the
following example automatically converts the
float value 9.99 to an int value of 9:
#include <stdio.h>
int main() {
// Automatic conversion: int to float
float a = 9;
printf("%f", a);
return 0;
}
#include <stdio.h>
int main() {
// Automatic conversion: float to int
int a = 9.99;
printf("%d", a);
return 0;
}
O/P: 9
What happened to .99?
As another example, if you divide two
integers: 5 by 2, you know that the sum is 2.5.
And as you know from the beginning of this
page, if you store the sum as an integer, the
result will only display the number 2.
Therefore, it would be better to store the sum
as a float or a double,.
#include <stdio.h>
int main() {
float sum = 5 / 2;
printf("%f", sum); // 2.000000
return 0;
}
O/P:
2.000000
Why is the result 2.00000 and not 2.5? Well, it
is because 5 and 2 are still integers in the
division. In this case, you need to manually
convert the integer values to floating-point
values. That’s why explicit type conversion
has introduced.
Explicit Conversion
Explicit conversion is done manually by
placing the type in parentheses () in front of
the value.
Considering our problem from the example
above, we can now get the right result:
#include <stdio.h>
int main() {
// Manual conversion: int to float
float sum = (float) 5 / 2;
printf("%f", sum);
return 0;
}
O/P:
2.500000
You can also place the type in front of a
variable:
#include <stdio.h>
int main() {
int num1 = 5;
int num2 = 2;
float sum = (float) num1 / num2;
printf("%f", sum);
return 0;
}
O/P:
2.500000
#include <stdio.h>
int main ()
{
// declare integer variables
int n, temp, rev = 0, digit, sum_of_digi
ts = 0;
printf (" Enter a Number: \n");
scanf (" %d", &n); // get the number
temp = n; // assign the number to tem
p variable
// use while loop to calculate the sum
of digits
while ( temp > 0)
{
// extract digit one by one and stor
e into the sum_of_digits
sum_of_digits = sum_of_digits + te
mp % 10; /* use modulus symbol to get t
he remainder of each iteration by temp
% 10 */
temp = temp / 10;
}
}
Output:
Enter a number
1729
The sum of the digits = 19
The reverse of the digits = 91
The product of 19 * 91 = 1729
1729 is a Magic Number.
'switch' statement in C
Consider a situation in which we have many
options out of which we need to select only
one option that is to be executed. Such kind of
problems can be solved using nested
if statement. But as the number of options
increases, the complexity of the program also
gets increased. This type of problem can be
solved very easily using a switch statement.
Using the switch statement, one can select
only one option from more number of options
very easily.
#include<stdio.h>
void main(){
int n ;
printf("Enter any digit: ") ;
scanf("%d", &n) ;
switch( n )
{
case 0: printf("ZERO") ;
break ;
case 1: printf("ONE") ;
break ;
case 2: printf("TWO") ;
break ;
case 3: printf("THREE") ;
break ;
case 4: printf("FOUR") ;
break ;
case 5: printf("FIVE") ;
break ;
case 6: printf("SIX") ;
break ;
case 7: printf("SEVEN") ;
break ;
case 8: printf("EIGHT") ;
break ;
case 9: printf("NINE") ;
break ;
default: printf("Not a Digit") ;
}
}
For loop:
A for loop in C is used to repeat a block of
code a specific number of times. It consists of
three parts: initialization, condition, and
update.
Syntax
for (initialization; condition; update) {
// code block to be executed
}
#include<stdio.h>
void main()
{
int n, i=1, sum=0;
printf("\n Enter a number: ");
scanf("%d", &n);
/* Loop to calculate the sum of positive
divisors */
while(i<n)
{
if(n%i==0)
{
sum=sum+i;
}
i++;
}
Do-while condition:
At first, the single statement or block of
statements which are defined
in do block are executed.
After the execution of the do block, the
given condition gets evaluated. If the
condition is evaluated to TRUE, the
single statement or block of statements
of do block are executed again. Once the
execution gets completed again the
condition is evaluated.
If it is TRUE, again the same
statements are executed.
The same process is repeated until the
condition is evaluated to FALSE.
Whenever the condition is evaluated to
FALSE, the execution control moves out
of the while block.
It is a form of an exit-controlled or post-
tested loop where the test condition is
checked after executing the body of the loop.
Syntax of do…while Loop in C
do {
} while (condition);
Example1: Program to display even
numbers upto 10.
#include<stdio.h>
void main(){
int n = 0;
clrscr() ;
printf("Even numbers upto 10\n");
do
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}while( n <= 10 ) ;
}
Example 2:
Program code for Armstrong Number or Not
in C:
It calculates the sum of the cubes of its digits.
#include<stdio.h>
void main()
{
int n,num,r,ans=0;
printf("Enter a positive integer: ");
scanf("%d", &num);
n=num;
Break :
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
// Output: 0 1 2 3 4
In this example, when i becomes equal to 5,
the break statement is executed, causing an
immediate exit from the loop, and thus
numbers from 0 to 4 are printed
continue statement
The continue statement is used to move the
program execution control to the beginning of
the looping statement. When
the continue statement is encountered in a
looping statement, the execution control skips
the rest of the statements in the looping block
and directly jumps to the beginning of the
loop. The continue statement can be used with
looping statements like while, do-while and
for.
When we use continue statement
with while and do-while statements the
execution control directly jumps to the
condition. When we use continue statement
with for statement the execution control
directly jumps to the modification portion
(increment/decrement/any modification) of the
for loop. The continue statement execution is
as shown in the following figure.
Example Program to illustrate continue
statement.
#include<stdio.h>
void main(){
int number ;
while( 1 )
{
printf("Enter any integer number: ") ;
scanf("%d", &number) ;
if(number%2 == 0)
{
printf("Entered number is EVEN!!! Try
another number!!!\n") ;
continue ;
}
else
{
printf("You have entered ODD
number!!! Bye!!!") ;
break;
}
}
goto statement
The goto statement is used to jump from one
line to another line in the program.
Using goto statement we can jump from top to
bottom or bottom to top. To jump from one
line to another line, the goto statement
requires a label. Label is a name given to the
instruction or line in the program. When we
use a goto statement in the program, the
execution control directly jumps to the line
with the specified label.
Example Program for goto statement.
#include<stdio.h>
void main(){
printf("We are at first printf statement!!!\
n") ;
goto last ;
printf("We are at second printf statement!!!\
n") ;
printf("We are at third printf statement!!!\n")
;
last: printf("We are at last printf
statement!!!\n") ;
Output:
continue statement
The continue statement is used to move the
program execution control to the beginning of
the looping statement. When
the continue statement is encountered in a
looping statement, the execution control skips
the rest of the statements in the looping block
and directly jumps to the beginning of the
loop. The continue statement can be used with
looping statements like while, do-while and
for.
When we use continue statement
with while and do-while statements the
execution control directly jumps to the
condition. When we use continue statement
with for statement the execution control
directly jumps to the modification portion
(increment/decrement/any modification) of the
for loop.
#include<stdio.h>
#include<conio.h>
void main(){
int number ;
while( 1 )
{
printf("Enter any integer number: ") ;
scanf("%d", &number) ;
if(number%2 == 0)
{
printf("Entered number is EVEN!!! Try
another number!!!\n") ;
continue ;
}
else
{
printf("You have entered ODD
number!!! Bye!!!") ;
exit(0) ;
}
}
}