Introduction to c part -1
-
Upload
baabtracom-mentoring-partner-first-programming-school-in-india -
Category
Technology
-
view
285 -
download
1
description
Transcript of Introduction to c part -1
![Page 1: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/1.jpg)
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?
![Page 2: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/2.jpg)
Introduction to CWeek 2
![Page 3: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/3.jpg)
How does human perform simple task; for Eg: add 456 and 44
Add 456 and 44 500
![Page 4: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/4.jpg)
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
5003 We say the answer through our output senses
![Page 5: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/5.jpg)
1 Computer use keyboard to receive inputs
2 Computer store the numbers 456 and 44 in Ram
456
44
456+44
3Computer calculate the result in CPU (ALU within CPU) and stores result back in ram
5004 Computer use monitor to display outputs
How does computer perform simple task; Eg: add 456 and 44
![Page 6: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/6.jpg)
– Start
– Get two numbers and store them
– Add them and store the result
– Print the answer
– End
Algorithm
![Page 7: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/7.jpg)
– 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
50044
456ba
c
![Page 8: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/8.jpg)
main(){
int a=456, b=44,c;
c= a+b;
printf (“%d”,c); // So who defined this function?? Where is it located?
}
![Page 9: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/9.jpg)
#include < stdio.h >main(){
int a=456, b=44,c;
c= a+b;
printf (“%d”,c);
}
........Printf(..){........}Scanf (..){.....}
Stdio . h
![Page 10: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/10.jpg)
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
50044
456ba
c
![Page 11: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/11.jpg)
–%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()
![Page 12: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/12.jpg)
Complete Program
#include < stdio.h >main(){
int a,b,c;Scanf(“%d %d”,&a,&b);c= a+b;
printf (“%d”,c);
}
![Page 13: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/13.jpg)
Elements of C
• Variables• Operators• Control structures
Decision Loops
• functions
![Page 14: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/14.jpg)
Variables in C
![Page 15: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/15.jpg)
Variables
int a;Data type variable name
![Page 16: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/16.jpg)
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 it can contain numeric values Eg: int _hai123, char h12h; int 123Hai // ERROR .variable name should not begin with numeric values
![Page 17: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/17.jpg)
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– lowerLimit lower_limit– incomeTax income_tax
![Page 18: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/18.jpg)
Decisions in C
![Page 19: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/19.jpg)
N Y
Start
i=1
If i<100
Print iStop
Print all the numbers up to 100
i=i+1
![Page 20: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/20.jpg)
Decisions
• Eg: if(i=100){ printf(“You are a low performer”);
}else{printf(“You are a top performer”);
}
![Page 21: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/21.jpg)
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){…}
![Page 22: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/22.jpg)
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
![Page 23: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/23.jpg)
switchSwitch(i){Case 0:
printf(“poor performer”);break;
Case 100: printf(“Good performer”);break;
Case default: printf(“performer”);break;
}
![Page 24: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/24.jpg)
switchSwitch(i){Case 0:
printf(“poor performer”);break;
Case 100: printf(“Good performer”);break;
Case default: printf(“performer”);break;
}
if(i==0){printf(“Poor Performer”);}else if(i==100){printf(“Good performer”);}Else {Printf(“performer”);}
![Page 25: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/25.jpg)
Loops in c
![Page 26: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/26.jpg)
N Y
Start
i=1
If i<100
Print iStop
Print all the numbers up to 100
i=i+1
![Page 27: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/27.jpg)
Loops in C
• For loop• While Loop• Do While Loop
![Page 28: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/28.jpg)
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++Step 5 : repeat
from step 2Step 6 : Stop
![Page 29: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/29.jpg)
While Loop
i=0;While(i<50){printf(“%d ”, i); i++;
} // {braces} are not necessary if there is only one statement inside loop
Step 1 : i=0 : initialization
Step 2 : i<50 : if true step 3
or else step 6
Step 3 : { executes }
Step 4 : i++Step 5 : repeat
from step 2Step 6 : Stop
![Page 30: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/30.jpg)
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 else step 6
Step 5 : repeat from step 2
Step 6 : Stop
![Page 31: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/31.jpg)
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 any code in between.
![Page 32: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/32.jpg)
Exampleint a = 10; while( a < 20 ) {
printf("value of a: %d \n", a); a++; if( a > 15) {break;}
}
![Page 33: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/33.jpg)
Example
int a = 10; while( a < 20 ) {
printf("value of a: %d \n", a); a++; if( a > 15) {break;
}}
Outputvalue of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
![Page 34: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/34.jpg)
Exampleint a = 10; do {
If( a == 15) {
a = a + 1; continue;
} printf("value of a: %d\n", a);
a++; } while( a < 20 );
![Page 35: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/35.jpg)
Exampleint a = 10; do {
If( a == 15) {
a = a + 1; continue;
} printf("value of a: %d\n", a);
a++; } while( a < 20 );
Outputvalue 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
![Page 36: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/36.jpg)
Operators• Arithmetic Operators
+, - , *, / and the modulus operator %.
• Relational operators<, <=, > >=, ==, !=
• Logical operators&&, ||, ! eg : If (a<10
&& b>9)• Assignment Operators
=, += ,-= eg: a+=10//same as a=a+10
• Increment and decrement operators
--,++
![Page 37: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/37.jpg)
Difference between i++ and ++i
• ++i Increments i by one, then returns i.
• i++ Returns i, then increments i by one.
i=10,j=20Z=++i;W=j++;Printf(“%d %d”, z,w); // w=20; z=11
![Page 38: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/38.jpg)
Questions?“A good question deserve a
good grade…”
![Page 39: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/39.jpg)
Self Check !!
![Page 40: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/40.jpg)
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 must occur first.
![Page 41: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/41.jpg)
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 must occur first.
![Page 42: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/42.jpg)
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 Times3.0 times4.10 times
![Page 43: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/43.jpg)
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 Times3.0 times4.10 times
![Page 44: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/44.jpg)
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=114.None of
above
![Page 45: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/45.jpg)
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=114.None of
above
![Page 46: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/46.jpg)
Self-Check
What is the output of the following program? void main(){ int i=1,j=1; while (++i < 10) printf("%d ",i); printf("\n"); while (j++ < 10) printf("%d ",j);}
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 92 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10
![Page 47: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/47.jpg)
Self-Check
What is the output of the following program? void main(){ int i=1,j=1; while (++i < 10) printf("%d ",i); printf("\n"); while (j++ < 10) printf("%d ",j);}
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 92 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10
![Page 48: Introduction to c part -1](https://reader033.fdocuments.in/reader033/viewer/2022061114/5461deb6af7959f84e8b4c49/html5/thumbnails/48.jpg)
End of Day 1