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
C_chap02.ppt Introduction to C Programming Language
PPT
C Intro.ppt
PPT
CHTP5e_02.ppt
PDF
Chap 2 Introduction to C ProgrammingFile.pdf
PPT
CHTP5e_02.ppt jkkkkkkk jjj
PPTX
basics of c programming for naiver.pptx
PPT
c-programming
PDF
C language concept with code apna college.pdf
PPT
Ch2 introduction to c
PPTX
C programming language tutorial
PPT
Introduction to Basic C programming 01
PPT
Unit i intro-operators
ODP
Programming basics
PPT
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
PPTX
Each n Every topic of C Programming.pptx
PPTX
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
PPT
Variable< Arithmetic Expressions and Input
PDF
UNIT1 PPS of C language for first year first semester
PDF
First c program
C_chap02.ppt Introduction to C Programming Language
C Intro.ppt
CHTP5e_02.ppt
Chap 2 Introduction to C ProgrammingFile.pdf
CHTP5e_02.ppt jkkkkkkk jjj
basics of c programming for naiver.pptx
c-programming
C language concept with code apna college.pdf
Ch2 introduction to c
C programming language tutorial
Introduction to Basic C programming 01
Unit i intro-operators
Programming basics
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
Each n Every topic of C Programming.pptx
Chapter 1_C Fundamentals_HS_Tech Yourself C.pptx
Variable< Arithmetic Expressions and Input
UNIT1 PPS of C language for first year first semester
First c program
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
Microsoft Excel 365/2024 Beginner's training
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
Statistics on Ai - sourced from AIPRM.pdf
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
Architecture types and enterprise applications.pdf
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
CloudStack 4.21: First Look Webinar slides
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
DOCX
search engine optimization ppt fir known well about this
PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
PPTX
Configure Apache Mutual Authentication
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PPT
What is a Computer? Input Devices /output devices
PPTX
Benefits of Physical activity for teenagers.pptx
Microsoft Excel 365/2024 Beginner's training
A review of recent deep learning applications in wood surface defect identifi...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Basics of Cloud Computing - Cloud Ecosystem
Statistics on Ai - sourced from AIPRM.pdf
A contest of sentiment analysis: k-nearest neighbor versus neural network
Custom Battery Pack Design Considerations for Performance and Safety
Architecture types and enterprise applications.pdf
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
CloudStack 4.21: First Look Webinar slides
NewMind AI Weekly Chronicles – August ’25 Week III
Convolutional neural network based encoder-decoder for efficient real-time ob...
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
search engine optimization ppt fir known well about this
Improvisation in detection of pomegranate leaf disease using transfer learni...
Configure Apache Mutual Authentication
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
What is a Computer? Input Devices /output devices
Benefits of Physical activity for teenagers.pptx

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 }