1 / 16

Lecture 2: Introduction to C Programming

Lecture 2: Introduction to C Programming. OBJECTIVES. In this lecture you will learn: To use simple input and output statements. The fundamental data types. Computer memory concepts. To use arithmetic operators. The precedence of arithmetic operators.

lecea
Download Presentation

Lecture 2: Introduction to C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2: Introduction to C Programming

  2. OBJECTIVES • In this lecture you will learn: • To use simple input and output statements. • The fundamental data types. • Computer memory concepts. • To use arithmetic operators. • The precedence of arithmetic operators. • To write simple decision-making statements.

  3. Example • Problem: Determine if a user-entered number is odd. • Questions: • How to enter a number? • Where to store the number? • Under what condition is a number is odd?

  4. /* Determine if a user-entered number is odd. */ #include <stdio.h> /* function main begins program execution */ int main( void ) { int integer1; /* the number to be input by user */ printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ /* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 ); return0; /* indicate that program ended successfully */ } /* end function main */ Definition of variable scanf obtains a value from the user and assigns it to integer1 Checked if integer1 is odd

  5. /* Determine if a user-entered number is odd. */ #include <stdio.h> /* function main begins program execution */ int main( void ) { int integer1; /* the number to be input by user */ printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ /* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 ); return0; /* indicate that program ended successfully */ } /* end function main */ • Comments: Used to describe program • #include <stdio.h>: <stdio.h> allows standard input/output operations • int main() • C programs contain one or more functions, exactly one of which must be main • int means that main "returns" an integer value • The execution of any C program starts from main

  6. /* Determine if a user-entered number is odd. */ #include <stdio.h> /* function main begins program execution */ int main( void ) { int integer1; /* the number to be input by user */ printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ /* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 ); return0; /* indicate that program ended successfully */ } /* end function main */ • int integer1; • Definition of a variable: location in memory where a value can be stored. • int means the variable can hold an integer • Variable name (identifier) • integer1 • Identifier: consist of letters, digits (cannot begin with a digit) and underscores( _ ) • Case sensitive • Definitions appear before executable statements

  7. /* Determine if a user-entered number is odd. */ #include <stdio.h> /* function main begins program execution */ int main( void ) { int integer1; /* the number to be input by user */ printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ /* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 ); return0; /* indicate that program ended successfully */ } /* end function main */ • scanf( "%d", &integer1 ); • Obtains a value from the user -- uses standard input (usually keyboard) • Two arguments • %d - conversion specifier: data should be a decimal integer (“%d” is the format control string) • &integer1 - location in memory to store variable • When executing the program the user responds to the scanf statement by typing in a number, then pressing the enter (return) key

  8. /* Determine if a user-entered number is odd. */ #include <stdio.h> /* function main begins program execution */ int main( void ) { int integer1; /* the number to be input by user */ printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ /* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 ); return0; /* indicate that program ended successfully */ } /* end function main */ • if ( integer1 % 2 == 1 ) • printf( "The entered number %d is odd.\n", integer1 ); • Simple version of the if statement, more detail next lecture • 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

  9. /* Determine if a user-entered number is odd. */ #include <stdio.h> /* function main begins program execution */ int main( void ) { int integer1; /* the number to be input by user */ printf( "Enter an integer number:\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ /* determine if the number is odd using the modulus operator */ if ( integer1 % 2 == 1 ) printf( "The entered number %d is odd.\n", integer1 ); return0; /* indicate that program ended successfully */ } /* end function main */ • printf( "The entered number %d is odd.\n", integer1 ); • Similar to scanf • "The entered number %d is odd.\n” - printf format control string. • %d - conversion specifier: means decimal integer will be printed

  10. Memory Concepts • Variable • Variable names correspond to locations in the computer's memory • Every variable has a name, a type, 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 RAM … 2000 integer1 2500 sum … 500 integer2 …

  11. Arithmetic • Arithmetic calculations • Use * for multiplication and / for division • Integer division truncates remainder e.g. 9 / 5 evaluates to 1. • Modulus operator (%) returns the remainder e.g. 9 % 5 evaluates to 4. • Operator precedence • Used to decide which of two operators should be processed first. • Parentheses () • Multiplication/Division/Remainder • Addition/Subtraction • Use parenthesis when needed • E.g.: Find the average of three variables a, b, and c using (a + b + c)/3, not a + b + c/3

  12. Arithmetic • Operator associativity • used to decide which of two operators should be processed when both operators have same precedence. • Multiplication/Division: Left to Right • Addition/Subtraction: Left to Right • Examples: a - b + c = ((a - b) + c) a * b % c = ((a * b) % c) a^b^c = (a^(b^c))

  13. Some Arithmetic Operators

  14. Some Arithmetic Operators

  15. Equality and Relational Operators

  16. Keywords Special words reserved for C Cannot be used as identifiers or variable names

More Related