From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Introduction to c part -1
1. Recall
• What is a variable?
• How doe the CPU Execution flow occurs?
Random or sequential?
• What are the options to control the normal
flow of executions?
• What is a function? When do we use
functions?
3. How does human perform simple task; for
Eg: add 456 and 44
Add 456
and 44 500
4. How does human perform simple task;
Eg: add 456 and 44
1 We Hear it through our input senses
2 We store the numbers 456 and 44 in our memory
456
44
456+44 3 We calculate the result in our brain and store it in
memory
500
3 We say the answer through our output senses
5. 1 Computer use keyboard to receive inputs
2 Computer store the numbers 456 and 44 in Ram
456
44
456+44
3
Computer calculate the result in CPU (ALU within
CPU) and stores result back in ram
500
4 Computer use monitor to display outputs
How does computer perform simple
task; Eg: add 456 and 44
6. – Start
– Get two numbers and
store them
– Add them and store
the result
– Print the answer
– End
Algorithm
7. – Start
– Get two numbers and
store them
– Add them and store
the result
– Print the answer
– End
Main
{
int a,b,c;
a=456,b=44;
c= a+b;
// something to print the value
from c, will discuss soon
}
Algorithm Vs
Program
500
44
456
b
a
c
10. Printf()
• Printf(“%d”,c)
– %d refers to Format specifier which is used
to specify the type and format of the data to
be taken to the stream and to be printed on
screen
• %f -> for float data type
• %c for Char data type
• %s for string data type
–c refers to the value of location named
c
500
44
456
b
a
c
11. –%d refers to Format specifies
which is used to specify the type
and format of the data to be
retrieved from the stream and
stored into the locations pointed by
&a.
–&a refers to the memory address of
location named a
scanf()
16. Variables
Data type
• Data type is the type of data we are going to store
in the reserved Ram location.
• We need to specify the data type so that size to be
allocated will be done automatically.
Int -> reserves 2 bytes of memory
Char -> reserves 1 byte of memory
float -> reserves 4 bytes of memory
Variable Name
• Variable is the name we give to access the value
from the memory space we allocate.
• Variable name should begin with characters or _ ;But
17. Variables
• Naming a Variable
– Must be a valid identifier.
– Must not be a keyword
– Names are case sensitive.
– Variables are identified by only first 32
characters.
– Library commonly uses names beginning with
_.
– Naming Styles: Uppercase style and
Underscore style
21. Nested if
• Eg: if(i==0)
{
printf(“You are a low performer”);
}
else if(i==200)
{
printf(“You are a top performer”);
}
Else if (i==300)
{
…
}
22. What is “=“ and “==“
=
– As discussed earlier = is used to assign a
value into a location we reserved already/or to
assign value in to a variable
– Eg: a=10
==
– Is used to check whether the value of a
variable is equal to the value provided in the
other side of operand
27. Loops in C
• For loop
• While Loop
• Do While Loop
28. For Loop
for
(i=0;i<50;i++)
{
printf(“%d ”, i);
} // {braces} are not
necessary if there is only
one statement inside for
loop
Step 1 : i=0 :
initialization
Step 2 : i<50 : if
true step 3
or else step 6
Step 3 : {
executes }
Step 4 : i++
29. While Loop
i=0;
While(i<50)
{
printf(“%d ”, i);
i++;
} // {braces} are not
necessary if there is only
Step 1 : i=0 :
initialization
Step 2 : i<50 : if
true step 3
or else step 6
Step 3 : {
executes }
Step 4 : i++
30. Do while Loop
i=0;
Do
{
printf(“%d ”, i);
i++;
} While(i<50)
Step 1 : i=0 :
initialization
Step 3 : {
executes }
Step 4 : i++
Step 2 : i<50 : if
true step 3 or
31. Other Control statements
• Break Statements
– The break statement terminates the
execution of the nearest
enclosing do, for, switch, or while statement
in which it appears.
• Continue statements
– The continue statement works like
the break statement. Instead of forcing
termination, however, continue forces the next
iteration of the loop to take place, skipping
32. Example
int a = 10;
while( a < 20 )
{
printf("value of a: %d n", a);
a++;
if( a > 15) {
break;
}
}
33. Example
int a = 10;
while( a < 20 )
{
printf("value of a: %d n", a);
a++;
if( a > 15) {
break;
}
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
34. Example
int a = 10;
do
{
If( a == 15)
{
a = a + 1;
continue;
}
printf("value of a: %dn", a);
a++;
} while( a < 20 );
35. Example
int a = 10;
do
{
If( a == 15)
{
a = a + 1;
continue;
}
printf("value of a: %dn", a);
a++;
} while( a < 20 );
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
37. Difference between i++ and ++i
• ++i Increments i by one, then returns i.
• i++ Returns i, then increments i by one.
i=10,j=20
Z=++i;
W=j++;
Printf(“%d %d”, z,w); // w=20; z=11
40. Self-Check
• What is a difference between a declaration and a
definition of a variable?
– Both can occur multiple times, but a
declaration must occur first.
– There is no difference between them.
– A definition occurs once, but a declaration
may occur many times.
– A declaration occurs once, but a definition
may occur many times.
– Both can occur multiple times, but a definition
41. Self-Check
• What is a difference between a declaration and a
definition of a variable?
– Both can occur multiple times, but a
declaration must occur first.
– There is no difference between them.
– A definition occurs once, but a declaration
may occur many times.
– A declaration occurs once, but a definition
may occur many times.
– Both can occur multiple times, but a definition
42. Self-Check
• How many times “baabtra“ get printed?
main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
printf(“baabtra");
}
}
1.Infinite
times
2.11 Times
3.0 times
4.10 times
43. Self-Check
• How many times “baabtra“ get printed?
main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
printf(“baabtra");
}
}
1.Infinite
times
2.11 Times
3.0 times
4.10 times
44. Self-Check
What is the output of the following program?
void main()
{
int i=10;
switch(i)
{
case 1: printf(" i=1");
break;
case 10: printf(" i=10");
case 11: printf(" i=11");
break;
case 12: printf(" i=12");
}
}
1. i=10 i=11
i=12
2. i=1 i=10
i=11 i=12
3. i=10 i=11
4. None of
above
45. Self-Check
What is the output of the following program?
void main()
{
int i=10;
switch(i)
{
case 1: printf(" i=1");
break;
case 10: printf(" i=10");
case 11: printf(" i=11");
break;
case 12: printf(" i=12");
}
}
1. i=10 i=11
i=12
2. i=1 i=10
i=11 i=12
3. i=10 i=11
4. None of
above