SlideShare a Scribd company logo
www.acropolis.in
Foundation of
Programming
By: Ajay Khatri & Narendra Pal Singh Rathore
Overview – Topics to be Covered
2 December 2020 www.ajaykhatri.in 3
Variable
Data Types
Comments
Input / Output
Operators
Control Statements
Variables
2 December 2020 www.ajaykhatri.in 4
Symbolic names given to memory location which is used to store
values.
Each variable must have a name and a type.
15
a
Variable value
Variable Name
Memory Location
Rules to Construct Variables
2 December 2020 www.ajaykhatri.in 5
Should start with letter or underscore.
May contain alphabets, digits & a symbol underscore( _).
Should not contain any special symbol.
blank space not allowed.
Should not more than 8 characters (may be optional).
Keyword can not used as variable.
Data Types
2 December 2020 www.ajaykhatri.in 6
Determines the type and size of data associated with variables.
int myVar;
Basic Data Type
2 December 2020 www.ajaykhatri.in 7
Data Types in C
2 December 2020 www.ajaykhatri.in 8
Data Type Shorthand Range Bytes Format
signed char char -128 to +127 1 %c
unsigned char unsigned char 0 to 255 1 %c
short signed int int -32768 to +32767 2 %d
short unsigned int unsigned 0 to 65535 2 %u
long signed int long -2147483648 to +2147483647 4 %ld
long unsigned int long unsigned 0 to 4294967295 4 %lu
float float -3.4e38 to +3.4e38 4 %f
double double -1.7e308 to +1.7e308 8 %lf
long double long double -1.7e4932 to +1.7e4932 10 %Lf
Data Types Rules
2 December 2020 www.ajaykhatri.in 9
All data types are keyword and should be written in lower case.
The data types cannot be used as variable names.
All declarations should be done before the first executable
statement.
All declarations must end with a semicolon.
Keywords - 32
2 December 2020 www.ajaykhatri.in 10
Note: main is not a keyword
Auto Double Int Struct
Break Else Long Switch
Case Enum Register Typeof
Char Extern Return Union
Const Float Short Unsigned
Continue For Signed Void
Default Goto Sizeof Volatile
Do If Static While
The Output Function
2 December 2020 www.ajaykhatri.in 11
The printf()
Displays the message on the console.
The message has no effect on the program
It returns the number of characters that are printed. If there is some error
then it returns a negative value.
The Input Function
2 December 2020 www.ajaykhatri.in 12
The scanf()
Accepts values from the console
It uses the format specifiers.
Code Format
% c Character
% d, % I Signed decimal integers
% f Decimal floating point
% s String of characters
% u Unsigned decimal integers
% x Unsigned hexa decimal
% p Displays a pointer
%% Prints a % sign
Comments
2 December 2020 www.ajaykhatri.in 13
Having comments is good programming practice.
Two types of comments
Single Line (indicated by //)
Multi Line (indicated by /* & */)
 Comments begin with /* and end with */ anything between the delimiters is ignored
Operators in C
2 December 2020 www.ajaykhatri.in 14
Operators
Arithmetic Relational Logical Bitwise
Unary BinaryUnaryBinaryUnaryBinaryBinary
assignment
+
-
++
--
+
-
*
/
%
<
>
<=
>=
==
!=
!
&&
||
^
~
&
|
<<
>>
Binary
=
+=
-=
*=
/=
%=
Arithmetic Operators
2 December 2020 www.ajaykhatri.in 15
Arithmetic operators are used to perform arithmetic operations.
Short-hand operators (++, --)
The Control Statements
Sequence Control Structure
It is an uncomplicated method
The statement are executed in a serial manner
The complier executes the first line initially and then moves on to the
next.
The Decision Control Structure
Checks for conditions
If true then executes the
statement
Checks for conditions
If true then executes the statement
If false executes the next statement
Decision Controls- Variations
Loop Control Structure
The for Statement
Initializing a variable
Stays in the loop till a conditions becomes true.
Increment the loop until the condition is met.
Representations:
For(initialization; condition; increment)
Example:
main() /* main function */
{
int i; /* declaration */
for(i=0; i<=10; i++) /* for statement */
{
printf("%d", i); /* output statement */
}
}
The while Statement
The condition statement
The executable statement
Incrementing the loop
Example
void main() /* main function */
{
int i = 1; /* declaration */
while(i<=20) /* condition*/
{
printf("%d", i); /* executable statements */
i++; /*incrementing*/
}
}
The “switch” statement
/* Equivalent switch for if
else ladder */
Switch (a) {
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;
Default: printf(“grater than
five”);
}
/* if else ladder */
if (a = = 1) {
printf(“one”); }
else if (a = = 2) {
printf(“two”); }
else if (a = = 3) {
printf(“three”); }
else if (a = = 4) {
printf(“four”); }
else if (a = = 5) {
printf(“five”); }
else {
printf(“grater than
five”);
}
The Ternary Operators
It is also called as the conditional operators
It is similar to the if else statement
It has two expressions after the condition to be checked
If the condition is true the first expression is executed
If the condition is false the second is executed.
Example:
main()
{
int a, b;
printf(“enter a value for a:”);
scanf(“%d”, &a); /*accepts data from user */
b = (a > 6 ? 2 : 3); /* conditional operator */
printf(“the value of b is:%d”, b);
}
Practice I
2 December 2020 www.ajaykhatri.in 25
Comparing Two Variables
Pseudo Code
Take two variable a,b
Assign values in a and b
Compare the variable if a > b then
True: Print a is greater
False: Print b is greater.
Practice -II
2 December 2020 www.ajaykhatri.in 26
Given number is even or odd.
Algorithm
Step 1 → Take variable A
Step 2 → Assign value to the variable
Step 3 → Perform A modulo 2 and check result if output is 0
Step 4 → If true print A is even
Step 5 → If false print A is odd
Practice -II
2 December 2020 www.ajaykhatri.in 27
Given number is even or odd.
Pseudo Code
Take input and assign into number variable.
IF (number modulo 2) equals to 0
 PRINT number is even
ELSE
 PRINT number is odd
END IF
Flow Chart
2 December 2020 www.ajaykhatri.in 28
Practice - IV
2 December 2020 www.ajaykhatri.in 29
Largest in three numbers.
Algorithm
Step 1 → Take three variables, say A, B & C
Step 2 → Assign values to variables
Step 3 → If A is greater than B & C, Display A is largest value
Step 4 → If B is greater than A & C, Display B is largest value
Step 5 → If C is greater than A & B, Display A is largest value
Step 6 → Otherwise, Display A, B & C are not unique values
Practice - IV
2 December 2020 www.ajaykhatri.in 30
Largest in three numbers.
Pseudo Code
IF A is greater than B AND A is greater than C
 DISPLAY "A is the largest."
ELSE IF B is greater than A AND B is greater than C
 DISPLAY "B is the largest."
ELSE IF C is greater than A AND C is greater than B
 DISPLAY "C is the largest."
ELSE
 DISPLAY "not unique values."
END IF
Flow Chart
2 December 2020 www.ajaykhatri.in 31
Practice -V
2 December 2020 www.ajaykhatri.in 32
Print all number between two numbers
Algorithm
Step 1 → take two variable s and e.
Step 2 → take input and assign values in s and e.
Step 3 → check if s is equal to e
Step 4 → true: print numbers equal and go to step 6
Step 5 →false: print s, add 1 in s and go to step 3.
Step 6 →end
Practice -V
2 December 2020 www.ajaykhatri.in 33
Pseudo Code
FOR value = START to END
 DO DISPLAY value
 Increment value by 1
END FOR
Flow Chart
2 December 2020 www.ajaykhatri.in 34
Important Link
2 December 2020 www.ajaykhatri.in 35
Android App to Learn C Programming
https://play.google.com/store/apps/details?id=in.ajaykhatri.ctutorial
Online IDE
https://ideone.com/
Download ppt
https://www2.slideshare.net/ajaykhatri
2 December 2020 36www.ajaykhatri.in
Acknowledgement
2 December 2020 www.ajaykhatri.in 37
This presentation has been prepared by summarizing the content
taken from different lecture notes, presentations, books and
websites available on the web. It is really hard to recall from where
these slides exactly come from. However, I don’t want to miss the
opportunity to thank individual contributors who created such a
good piece of work. Without their contribution, this presentation
even can’t be imagined.
This presentation is solely used for education purpose. Still, if you
have copyright on any material and want to get it removed from this
presentation, please let us know, we will remove it immediately.
2 December 2020 38www.ajaykhatri.in

More Related Content

PPT
Unit i intro-operators
PPTX
Variable declaration
PPT
Variable declaration
PPTX
Programming presentation
PDF
Type Conversion in C++ and C# Arithmetic Expressions
PDF
C++11 and 64-bit Issues
PDF
Assignment
Unit i intro-operators
Variable declaration
Variable declaration
Programming presentation
Type Conversion in C++ and C# Arithmetic Expressions
C++11 and 64-bit Issues
Assignment

Similar to Basics of C programming - day 2 (20)

PPTX
Fundamentals of computer programming by Dr. A. Charan Kumari
PPTX
comp 122 Chapter 2.pptx,language semantics
PDF
learn basic to advance C Programming Notes
PPT
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
PPTX
1 introduction to c program
PPT
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
PPTX
UNIT 1.pptx Programming for Problem Solving
PPTX
Programming Fundamentals
PPT
Introduction to Basic C programming 01
PPTX
Chapter 2_ProgramControlStatements_HS_Tech Yourself C.pptx
PPTX
What is c
PDF
Programming For Problem Solving Lecture Notes
PPTX
dinoC_ppt.pptx
PDF
2 EPT 162 Lecture 2
PDF
C language concept with code apna college.pdf
PPTX
Dr Mrs A A Miraje C Programming PPT.pptx
PPT
c-programming
PPTX
C Programming Unit-1
PPT
Programming Fundamentals - Lecture 1.ppt
PPTX
C language
Fundamentals of computer programming by Dr. A. Charan Kumari
comp 122 Chapter 2.pptx,language semantics
learn basic to advance C Programming Notes
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
1 introduction to c program
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
UNIT 1.pptx Programming for Problem Solving
Programming Fundamentals
Introduction to Basic C programming 01
Chapter 2_ProgramControlStatements_HS_Tech Yourself C.pptx
What is c
Programming For Problem Solving Lecture Notes
dinoC_ppt.pptx
2 EPT 162 Lecture 2
C language concept with code apna college.pdf
Dr Mrs A A Miraje C Programming PPT.pptx
c-programming
C Programming Unit-1
Programming Fundamentals - Lecture 1.ppt
C language
Ad

More from Ajay Khatri (9)

PDF
Ajay khatri resume august 2021
PPTX
Lecture 1 Introduction C++
PPTX
Competitive Programming Guide
PPTX
Zotero : Personal Research Assistant
PPTX
Introduction to HTML
PPTX
Introduction To MySQL Lecture 1
PPTX
Introduction to PHP Lecture 1
PPTX
Basics of Java Script (JS)
PPTX
CSS Basics (Cascading Style Sheet)
Ajay khatri resume august 2021
Lecture 1 Introduction C++
Competitive Programming Guide
Zotero : Personal Research Assistant
Introduction to HTML
Introduction To MySQL Lecture 1
Introduction to PHP Lecture 1
Basics of Java Script (JS)
CSS Basics (Cascading Style Sheet)
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
web development for engineering and engineering
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
737-MAX_SRG.pdf student reference guides
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Well-logging-methods_new................
PPT
Project quality management in manufacturing
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Artificial Intelligence
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
additive manufacturing of ss316l using mig welding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
web development for engineering and engineering
Foundation to blockchain - A guide to Blockchain Tech
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Fundamentals of safety and accident prevention -final (1).pptx
737-MAX_SRG.pdf student reference guides
CH1 Production IntroductoryConcepts.pptx
Well-logging-methods_new................
Project quality management in manufacturing
Model Code of Practice - Construction Work - 21102022 .pdf
OOP with Java - Java Introduction (Basics)
Artificial Intelligence
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Geodesy 1.pptx...............................................
additive manufacturing of ss316l using mig welding

Basics of C programming - day 2

  • 2. Foundation of Programming By: Ajay Khatri & Narendra Pal Singh Rathore
  • 3. Overview – Topics to be Covered 2 December 2020 www.ajaykhatri.in 3 Variable Data Types Comments Input / Output Operators Control Statements
  • 4. Variables 2 December 2020 www.ajaykhatri.in 4 Symbolic names given to memory location which is used to store values. Each variable must have a name and a type. 15 a Variable value Variable Name Memory Location
  • 5. Rules to Construct Variables 2 December 2020 www.ajaykhatri.in 5 Should start with letter or underscore. May contain alphabets, digits & a symbol underscore( _). Should not contain any special symbol. blank space not allowed. Should not more than 8 characters (may be optional). Keyword can not used as variable.
  • 6. Data Types 2 December 2020 www.ajaykhatri.in 6 Determines the type and size of data associated with variables. int myVar;
  • 7. Basic Data Type 2 December 2020 www.ajaykhatri.in 7
  • 8. Data Types in C 2 December 2020 www.ajaykhatri.in 8 Data Type Shorthand Range Bytes Format signed char char -128 to +127 1 %c unsigned char unsigned char 0 to 255 1 %c short signed int int -32768 to +32767 2 %d short unsigned int unsigned 0 to 65535 2 %u long signed int long -2147483648 to +2147483647 4 %ld long unsigned int long unsigned 0 to 4294967295 4 %lu float float -3.4e38 to +3.4e38 4 %f double double -1.7e308 to +1.7e308 8 %lf long double long double -1.7e4932 to +1.7e4932 10 %Lf
  • 9. Data Types Rules 2 December 2020 www.ajaykhatri.in 9 All data types are keyword and should be written in lower case. The data types cannot be used as variable names. All declarations should be done before the first executable statement. All declarations must end with a semicolon.
  • 10. Keywords - 32 2 December 2020 www.ajaykhatri.in 10 Note: main is not a keyword Auto Double Int Struct Break Else Long Switch Case Enum Register Typeof Char Extern Return Union Const Float Short Unsigned Continue For Signed Void Default Goto Sizeof Volatile Do If Static While
  • 11. The Output Function 2 December 2020 www.ajaykhatri.in 11 The printf() Displays the message on the console. The message has no effect on the program It returns the number of characters that are printed. If there is some error then it returns a negative value.
  • 12. The Input Function 2 December 2020 www.ajaykhatri.in 12 The scanf() Accepts values from the console It uses the format specifiers. Code Format % c Character % d, % I Signed decimal integers % f Decimal floating point % s String of characters % u Unsigned decimal integers % x Unsigned hexa decimal % p Displays a pointer %% Prints a % sign
  • 13. Comments 2 December 2020 www.ajaykhatri.in 13 Having comments is good programming practice. Two types of comments Single Line (indicated by //) Multi Line (indicated by /* & */)  Comments begin with /* and end with */ anything between the delimiters is ignored
  • 14. Operators in C 2 December 2020 www.ajaykhatri.in 14 Operators Arithmetic Relational Logical Bitwise Unary BinaryUnaryBinaryUnaryBinaryBinary assignment + - ++ -- + - * / % < > <= >= == != ! && || ^ ~ & | << >> Binary = += -= *= /= %=
  • 15. Arithmetic Operators 2 December 2020 www.ajaykhatri.in 15 Arithmetic operators are used to perform arithmetic operations. Short-hand operators (++, --)
  • 17. Sequence Control Structure It is an uncomplicated method The statement are executed in a serial manner The complier executes the first line initially and then moves on to the next.
  • 18. The Decision Control Structure Checks for conditions If true then executes the statement Checks for conditions If true then executes the statement If false executes the next statement
  • 21. The for Statement Initializing a variable Stays in the loop till a conditions becomes true. Increment the loop until the condition is met. Representations: For(initialization; condition; increment) Example: main() /* main function */ { int i; /* declaration */ for(i=0; i<=10; i++) /* for statement */ { printf("%d", i); /* output statement */ } }
  • 22. The while Statement The condition statement The executable statement Incrementing the loop Example void main() /* main function */ { int i = 1; /* declaration */ while(i<=20) /* condition*/ { printf("%d", i); /* executable statements */ i++; /*incrementing*/ } }
  • 23. The “switch” statement /* Equivalent switch for if else ladder */ Switch (a) { 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; Default: printf(“grater than five”); } /* if else ladder */ if (a = = 1) { printf(“one”); } else if (a = = 2) { printf(“two”); } else if (a = = 3) { printf(“three”); } else if (a = = 4) { printf(“four”); } else if (a = = 5) { printf(“five”); } else { printf(“grater than five”); }
  • 24. The Ternary Operators It is also called as the conditional operators It is similar to the if else statement It has two expressions after the condition to be checked If the condition is true the first expression is executed If the condition is false the second is executed. Example: main() { int a, b; printf(“enter a value for a:”); scanf(“%d”, &a); /*accepts data from user */ b = (a > 6 ? 2 : 3); /* conditional operator */ printf(“the value of b is:%d”, b); }
  • 25. Practice I 2 December 2020 www.ajaykhatri.in 25 Comparing Two Variables Pseudo Code Take two variable a,b Assign values in a and b Compare the variable if a > b then True: Print a is greater False: Print b is greater.
  • 26. Practice -II 2 December 2020 www.ajaykhatri.in 26 Given number is even or odd. Algorithm Step 1 → Take variable A Step 2 → Assign value to the variable Step 3 → Perform A modulo 2 and check result if output is 0 Step 4 → If true print A is even Step 5 → If false print A is odd
  • 27. Practice -II 2 December 2020 www.ajaykhatri.in 27 Given number is even or odd. Pseudo Code Take input and assign into number variable. IF (number modulo 2) equals to 0  PRINT number is even ELSE  PRINT number is odd END IF
  • 28. Flow Chart 2 December 2020 www.ajaykhatri.in 28
  • 29. Practice - IV 2 December 2020 www.ajaykhatri.in 29 Largest in three numbers. Algorithm Step 1 → Take three variables, say A, B & C Step 2 → Assign values to variables Step 3 → If A is greater than B & C, Display A is largest value Step 4 → If B is greater than A & C, Display B is largest value Step 5 → If C is greater than A & B, Display A is largest value Step 6 → Otherwise, Display A, B & C are not unique values
  • 30. Practice - IV 2 December 2020 www.ajaykhatri.in 30 Largest in three numbers. Pseudo Code IF A is greater than B AND A is greater than C  DISPLAY "A is the largest." ELSE IF B is greater than A AND B is greater than C  DISPLAY "B is the largest." ELSE IF C is greater than A AND C is greater than B  DISPLAY "C is the largest." ELSE  DISPLAY "not unique values." END IF
  • 31. Flow Chart 2 December 2020 www.ajaykhatri.in 31
  • 32. Practice -V 2 December 2020 www.ajaykhatri.in 32 Print all number between two numbers Algorithm Step 1 → take two variable s and e. Step 2 → take input and assign values in s and e. Step 3 → check if s is equal to e Step 4 → true: print numbers equal and go to step 6 Step 5 →false: print s, add 1 in s and go to step 3. Step 6 →end
  • 33. Practice -V 2 December 2020 www.ajaykhatri.in 33 Pseudo Code FOR value = START to END  DO DISPLAY value  Increment value by 1 END FOR
  • 34. Flow Chart 2 December 2020 www.ajaykhatri.in 34
  • 35. Important Link 2 December 2020 www.ajaykhatri.in 35 Android App to Learn C Programming https://play.google.com/store/apps/details?id=in.ajaykhatri.ctutorial Online IDE https://ideone.com/ Download ppt https://www2.slideshare.net/ajaykhatri
  • 36. 2 December 2020 36www.ajaykhatri.in
  • 37. Acknowledgement 2 December 2020 www.ajaykhatri.in 37 This presentation has been prepared by summarizing the content taken from different lecture notes, presentations, books and websites available on the web. It is really hard to recall from where these slides exactly come from. However, I don’t want to miss the opportunity to thank individual contributors who created such a good piece of work. Without their contribution, this presentation even can’t be imagined. This presentation is solely used for education purpose. Still, if you have copyright on any material and want to get it removed from this presentation, please let us know, we will remove it immediately.
  • 38. 2 December 2020 38www.ajaykhatri.in