SlideShare a Scribd company logo
Chapter 2 - Introduction to C Programming Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision Making: Equality and Relational Operators
2.1 Introduction C programming language Structured and disciplined approach to program design Structured programming Introduced in chapters 3 and 4 Used throughout the remainder of the book
2.2 A Simple C Program: Printing a Line of Text Comments Text surrounded by  /*  and  */  is ignored by computer Used to describe program #include <stdio.h> Preprocessor directive Tells computer to load contents of a certain file <stdio.h>  allows standard input/output operations Welcome to C!  1 /* Fig. 2.1: fig02_01.c 2   A first program in C */ 3 #include  <stdio.h> 4 5 int  main() 6 { 7   printf( &quot;Welcome to C!\n&quot; ); 8 9   return  0; 10 }
2.2 A Simple C Program: Printing a Line of Text int main() C++ programs contain one or more functions, exactly one of which must be  main Parenthesis used to indicate a function int  means that  main  &quot;returns&quot; an integer value Braces ( {  and  } ) indicate a block The bodies of all functions must be contained in braces
2.2 A Simple C Program: Printing a Line of Text printf(   &quot;Welcome to C!\n&quot;   ); Instructs computer to perform an action Specifically, prints the string of characters within quotes ( “   ” ) Entire line called a statement All statements must end with a semicolon ( ; ) Escape character ( \ ) Indicates that printf should do something out of the ordinary \n  is the newline character
2.2 A Simple C Program: Printing a Line of Text return   0; A way to exit a function return   0 , in this case, means that the program terminated normally Right brace  } Indicates end of  main  has been reached Linker When a function is called, linker locates it in the library Inserts it into object program If function name is misspelled, the linker will produce an error because it will not be able to find function in the library
1. Initialize variables 2. Input 2.1 Sum 3. Print Program Output Enter first integer 45 Enter second integer 72 Sum is 117   1 /* Fig. 2.5: fig02_05.c 2   Addition program */ 3 #include  <stdio.h> 4 5 int  main() 6 { 7   int  integer1, integer2, sum;  /* declaration */ 8 9   printf( &quot;Enter first integer\n&quot; );  /* prompt */ 10   scanf( &quot;%d&quot;, &integer1 );  /* read an integer */ 11   printf( &quot;Enter second integer\n&quot; );  /* prompt */ 12   scanf( &quot;%d&quot;, &integer2 );  /* read an integer */ 13   sum = integer1 + integer2;  /* assignment of sum */ 14   printf( &quot;Sum is %d\n&quot;, sum );  /* print sum */ 15 16   return  0;  /* indicate that program ended successfully */ 17 }
2.3 Another Simple C Program: Adding Two Integers As before Comments,  #include <stdio.h>  and  main int   integer1,   integer2,   sum; Declaration of variables Variables: locations in memory where a value can be stored int  means the variables can hold integers ( -1 ,  3 ,  0 ,  47 ) Variable names (identifiers) integer1 ,  integer2 ,  sum   Identifiers: consist of letters, digits (cannot begin with a digit) and underscores(  _  ) Case sensitive Declarations appear before executable statements If an executable statement references and undeclared variable it will produce a syntax (compiler) error
2.3 Another Simple C Program: Adding Two Integers scanf( &quot;%d&quot;, &integer1 ); Obtains a value from the user scanf  uses standard input (usually keyboard) This  scanf  statement has two arguments %d  - indicates data should be a decimal integer &integer1  - location in memory to store variable &  is confusing in beginning – for now, just remember to include it with the variable name in  scanf  statements When executing the program the user responds to the  scanf  statement by typing in a number, then pressing the  enter  (return) key
2.3 Another Simple C Program: Adding Two Integers =  (assignment operator) Assigns a value to a variable Is a binary operator (has two operands) sum = variable1 + variable2; sum gets variable1 + variable2; Variable receiving value on left printf(   &quot;Sum is %d\n&quot;,   sum   ); Similar to  scanf %d  means decimal integer will be printed sum  specifies what integer will be printed Calculations can be performed inside  printf  statements printf( &quot;Sum is %d\n&quot;, integer1 + integer2 );
2.4 Memory Concepts Variables  Variable names correspond to locations in the computer's memory Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable (through  scanf , for example), it replaces (and destroys) the previous value Reading variables from memory does not change them A visual representation integer1 45
2.5  Arithmetic Arithmetic calculations Use  *  for multiplication and  /  for division Integer division truncates remainder 7   /   5  evaluates to  1 Modulus operator( % ) returns the remainder  7   %   5  evaluates to  2 Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed Example: Find the average of three variables  a ,  b  and  c Do not use:  a + b + c / 3  Use:  (a + b + c ) / 3
2.5  Arithmetic Arithmetic operators: Rules of operator precedence:
2.6 Decision Making: Equality and Relational Operators Executable statements Perform actions (calculations, input/output of data) Perform decisions May want to print  &quot;pass&quot;  or  &quot;fail&quot;  given the value of a test grade if  control structure Simple version in this section, more detail later If a condition is  true , then the body of the  if  statement executed 0  is  false , non-zero is  true Control always resumes after the  if  structure Keywords Special words reserved for C Cannot be used as identifiers or variable names
2.6 Decision Making: Equality and Relational Operators
2.6 Decision Making: Equality and Relational Operators
1. Declare variables 2. Input 2.1  if  statements 3. Print 1 /* Fig. 2.13: fig02_13.c 2   Using if statements, relational   3   operators, and equality operators */ 4 #include  <stdio.h> 5 6 int  main() 7 { 8   int  num1, num2; 9   10   printf( &quot;Enter two integers, and I will tell you\n&quot; ); 11   printf( &quot;the relationships they satisfy: &quot; ); 12   scanf( &quot;%d%d&quot;, &num1, &num2  );  /* read two integers */ 13   14   if  ( num1 == num2 )  15   printf( &quot;%d is equal to %d\n&quot;, num1, num2 ); 16 17   if  ( num1 != num2 )  18   printf( &quot;%d is not equal to %d\n&quot;, num1, num2 ); 19 20   if  ( num1 < num2 )  21   printf( &quot;%d is less than %d\n&quot;, num1, num2 ); 22 23   if  ( num1 > num2 )  24   printf( &quot;%d is greater than %d\n&quot;, num1, num2 ); 25 26   if  ( num1 <= num2 )  27   printf( &quot;%d is less than or equal to %d\n&quot;,  28   num1, num2 );
3.1 Exit  main Program Output Enter two integers, and I will tell you  the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7   Enter two integers, and I will tell you  the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12   29 30   if  ( num1 >= num2 )  31   printf( &quot;%d is greater than or equal to %d\n&quot;,  32   num1, num2 ); 33 34   return  0;  /* indicate program ended successfully */ 35 }

More Related Content

PPT
Cinfo
PPT
intro to c
PDF
Cp module 2
PPT
05 control structures 2
PDF
[ITP - Lecture 07] Comments in C/C++
PPT
Computer programming:Know How to Flowchart
PDF
cp Module4(1)
PPTX
COM1407: Program Control Structures – Decision Making & Branching
Cinfo
intro to c
Cp module 2
05 control structures 2
[ITP - Lecture 07] Comments in C/C++
Computer programming:Know How to Flowchart
cp Module4(1)
COM1407: Program Control Structures – Decision Making & Branching

What's hot (19)

PPT
Ch1 principles of software development
PPT
Ch3 repetition
PPTX
Overview of C Mrs Sowmya Jyothi
PDF
Casa lab manual
PDF
Chap 3 c++
DOCX
Basics of c++
PPTX
Flowcharts
PPTX
C Programming Unit-1
PPTX
Introduction to flowchart
PPT
C Introduction
PPT
Pseudocode algorithim flowchart
PPT
algo
PPT
Unit 3 Foc
PDF
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
PPT
PPT
keyword
PDF
Learning the C Language
PPSX
C programming tokens & error types
PPTX
C Programming Unit-2
Ch1 principles of software development
Ch3 repetition
Overview of C Mrs Sowmya Jyothi
Casa lab manual
Chap 3 c++
Basics of c++
Flowcharts
C Programming Unit-1
Introduction to flowchart
C Introduction
Pseudocode algorithim flowchart
algo
Unit 3 Foc
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
keyword
Learning the C Language
C programming tokens & error types
C Programming Unit-2
Ad

Similar to C chap02 (20)

PPT
Chapter2
PPT
C++ Overview
PPT
Ch2 introduction to c
PPT
C_chap02.ppt Introduction to C Programming Language
PPT
C Intro.ppt
PDF
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
PPTX
Unit-IV.pptx
PDF
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
DOCX
What is C Programming LAnguage and Why to Learn.docx
PDF
Lec1_EENG112-Introduction.pdf
PPTX
Basics Of C++.pptx
PDF
2 EPT 162 Lecture 2
PPTX
C programming
PPTX
Overview of C Programming (CSE115/CS1010)
PPT
03 intro to vb programming
PPT
Chapter03 Ppt
PPT
Visual Basic Programming
DOCX
UNIT-II CP DOC.docx
PPT
intro to programming languge c++ for computer department
PPT
CHAPTER-2.ppt
Chapter2
C++ Overview
Ch2 introduction to c
C_chap02.ppt Introduction to C Programming Language
C Intro.ppt
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
Unit-IV.pptx
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
What is C Programming LAnguage and Why to Learn.docx
Lec1_EENG112-Introduction.pdf
Basics Of C++.pptx
2 EPT 162 Lecture 2
C programming
Overview of C Programming (CSE115/CS1010)
03 intro to vb programming
Chapter03 Ppt
Visual Basic Programming
UNIT-II CP DOC.docx
intro to programming languge c++ for computer department
CHAPTER-2.ppt
Ad

More from Khan Rahimeen (8)

PPT
Mysqlppt3510
PPT
PPT
Lab 4 excel basics
PDF
Introductionto excel2007
PPT
C tutorial
PPT
5 intro to networking
PPT
1 introduction-to-computer-networking
Mysqlppt3510
Lab 4 excel basics
Introductionto excel2007
C tutorial
5 intro to networking
1 introduction-to-computer-networking

Recently uploaded (20)

PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
A Presentation on Touch Screen Technology
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
August Patch Tuesday
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Hybrid model detection and classification of lung cancer
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PPTX
A Presentation on Artificial Intelligence
SOPHOS-XG Firewall Administrator PPT.pptx
1 - Historical Antecedents, Social Consideration.pdf
Enhancing emotion recognition model for a student engagement use case through...
A comparative analysis of optical character recognition models for extracting...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
A Presentation on Touch Screen Technology
Hindi spoken digit analysis for native and non-native speakers
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
August Patch Tuesday
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Tartificialntelligence_presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
cloud_computing_Infrastucture_as_cloud_p
Hybrid model detection and classification of lung cancer
WOOl fibre morphology and structure.pdf for textiles
Zenith AI: Advanced Artificial Intelligence
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
OMC Textile Division Presentation 2021.pptx
A Presentation on Artificial Intelligence

C chap02

  • 1. Chapter 2 - Introduction to C Programming Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision Making: Equality and Relational Operators
  • 2. 2.1 Introduction C programming language Structured and disciplined approach to program design Structured programming Introduced in chapters 3 and 4 Used throughout the remainder of the book
  • 3. 2.2 A Simple C Program: Printing a Line of Text Comments Text surrounded by /* and */ is ignored by computer Used to describe program #include <stdio.h> Preprocessor directive Tells computer to load contents of a certain file <stdio.h> allows standard input/output operations Welcome to C! 1 /* Fig. 2.1: fig02_01.c 2 A first program in C */ 3 #include <stdio.h> 4 5 int main() 6 { 7 printf( &quot;Welcome to C!\n&quot; ); 8 9 return 0; 10 }
  • 4. 2.2 A Simple C Program: Printing a Line of Text int main() C++ programs contain one or more functions, exactly one of which must be main Parenthesis used to indicate a function int means that main &quot;returns&quot; an integer value Braces ( { and } ) indicate a block The bodies of all functions must be contained in braces
  • 5. 2.2 A Simple C Program: Printing a Line of Text printf( &quot;Welcome to C!\n&quot; ); Instructs computer to perform an action Specifically, prints the string of characters within quotes ( “ ” ) Entire line called a statement All statements must end with a semicolon ( ; ) Escape character ( \ ) Indicates that printf should do something out of the ordinary \n is the newline character
  • 6. 2.2 A Simple C Program: Printing a Line of Text return 0; A way to exit a function return 0 , in this case, means that the program terminated normally Right brace } Indicates end of main has been reached Linker When a function is called, linker locates it in the library Inserts it into object program If function name is misspelled, the linker will produce an error because it will not be able to find function in the library
  • 7. 1. Initialize variables 2. Input 2.1 Sum 3. Print Program Output Enter first integer 45 Enter second integer 72 Sum is 117 1 /* Fig. 2.5: fig02_05.c 2 Addition program */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int integer1, integer2, sum; /* declaration */ 8 9 printf( &quot;Enter first integer\n&quot; ); /* prompt */ 10 scanf( &quot;%d&quot;, &integer1 ); /* read an integer */ 11 printf( &quot;Enter second integer\n&quot; ); /* prompt */ 12 scanf( &quot;%d&quot;, &integer2 ); /* read an integer */ 13 sum = integer1 + integer2; /* assignment of sum */ 14 printf( &quot;Sum is %d\n&quot;, sum ); /* print sum */ 15 16 return 0; /* indicate that program ended successfully */ 17 }
  • 8. 2.3 Another Simple C Program: Adding Two Integers As before Comments, #include <stdio.h> and main int integer1, integer2, sum; Declaration of variables Variables: locations in memory where a value can be stored int means the variables can hold integers ( -1 , 3 , 0 , 47 ) Variable names (identifiers) integer1 , integer2 , sum Identifiers: consist of letters, digits (cannot begin with a digit) and underscores( _ ) Case sensitive Declarations appear before executable statements If an executable statement references and undeclared variable it will produce a syntax (compiler) error
  • 9. 2.3 Another Simple C Program: Adding Two Integers scanf( &quot;%d&quot;, &integer1 ); Obtains a value from the user scanf uses standard input (usually keyboard) This scanf statement has two arguments %d - indicates data should be a decimal integer &integer1 - location in memory to store variable & is confusing in beginning – for now, just remember to include it with the variable name in scanf statements When executing the program the user responds to the scanf statement by typing in a number, then pressing the enter (return) key
  • 10. 2.3 Another Simple C Program: Adding Two Integers = (assignment operator) Assigns a value to a variable Is a binary operator (has two operands) sum = variable1 + variable2; sum gets variable1 + variable2; Variable receiving value on left printf( &quot;Sum is %d\n&quot;, sum ); Similar to scanf %d means decimal integer will be printed sum specifies what integer will be printed Calculations can be performed inside printf statements printf( &quot;Sum is %d\n&quot;, integer1 + integer2 );
  • 11. 2.4 Memory Concepts Variables Variable names correspond to locations in the computer's memory Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable (through scanf , for example), it replaces (and destroys) the previous value Reading variables from memory does not change them A visual representation integer1 45
  • 12. 2.5 Arithmetic Arithmetic calculations Use * for multiplication and / for division Integer division truncates remainder 7 / 5 evaluates to 1 Modulus operator( % ) returns the remainder 7 % 5 evaluates to 2 Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed Example: Find the average of three variables a , b and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3
  • 13. 2.5 Arithmetic Arithmetic operators: Rules of operator precedence:
  • 14. 2.6 Decision Making: Equality and Relational Operators Executable statements Perform actions (calculations, input/output of data) Perform decisions May want to print &quot;pass&quot; or &quot;fail&quot; given the value of a test grade if control structure Simple version in this section, more detail later If a condition is true , then the body of the if statement executed 0 is false , non-zero is true Control always resumes after the if structure Keywords Special words reserved for C Cannot be used as identifiers or variable names
  • 15. 2.6 Decision Making: Equality and Relational Operators
  • 16. 2.6 Decision Making: Equality and Relational Operators
  • 17. 1. Declare variables 2. Input 2.1 if statements 3. Print 1 /* Fig. 2.13: fig02_13.c 2 Using if statements, relational 3 operators, and equality operators */ 4 #include <stdio.h> 5 6 int main() 7 { 8 int num1, num2; 9 10 printf( &quot;Enter two integers, and I will tell you\n&quot; ); 11 printf( &quot;the relationships they satisfy: &quot; ); 12 scanf( &quot;%d%d&quot;, &num1, &num2 ); /* read two integers */ 13 14 if ( num1 == num2 ) 15 printf( &quot;%d is equal to %d\n&quot;, num1, num2 ); 16 17 if ( num1 != num2 ) 18 printf( &quot;%d is not equal to %d\n&quot;, num1, num2 ); 19 20 if ( num1 < num2 ) 21 printf( &quot;%d is less than %d\n&quot;, num1, num2 ); 22 23 if ( num1 > num2 ) 24 printf( &quot;%d is greater than %d\n&quot;, num1, num2 ); 25 26 if ( num1 <= num2 ) 27 printf( &quot;%d is less than or equal to %d\n&quot;, 28 num1, num2 );
  • 18. 3.1 Exit main Program Output Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 29 30 if ( num1 >= num2 ) 31 printf( &quot;%d is greater than or equal to %d\n&quot;, 32 num1, num2 ); 33 34 return 0; /* indicate program ended successfully */ 35 }