C programming an Introduction - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

C programming an Introduction

Description:

C programming an Introduction Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long int a larger ... – PowerPoint PPT presentation

Number of Views:595
Avg rating:5.0/5.0
Slides: 25
Provided by: joh5181
Category:

less

Transcript and Presenter's Notes

Title: C programming an Introduction


1
C programming an Introduction
2
Types
  • There are only a few basic data types in C.
  • char a character
  • int an integer, in the range -32,767 to 32,767
  • long int a larger integer (up to -2,147,483,647)
  • float a floating-point number
  • double a floating-point number, with more
    precision and perhaps greater range than float

3
Declarations
  • A declaration tells the compiler the name and
    type of a variable you'll be using in your
    program. In its simplest form, a declaration
    consists of the type, the name of the variable,
    and a terminating semicolon
  • EXAMPLES
  • char c
  • int i
  • float f

4
Variable names
  • Within limits, you can give your variables and
    functions any names you want
  • Must be alphanumeric
  • Can use underscores etc
  • Case in C is significant the variable names
    variable, Variable, and VARIABLE (as well as
    silly combinations like variAble) are all
    distinct.
  • A final restriction on names is that you may not
    use keywords (the words such as int and for which
    are part of the syntax of the language) as the
    names of variables or functions (or as
    identifiers of any kind).

5
simple output
  • Printf
  • Examples
  • printf("Hello, world!\n")
  • \n moves output to a newline
  • printf("i is d\n", i)
  • In that case, whenever printf printed'' the
    string "i is d", it did not print it verbatim
    it replaced the two characters d with the value
    of the variable i.

6
Format specifiers for printf.
  • There are quite a number of format specifiers for
    printf. Here are the basic ones
  • d print an int argument in decimal
  • ld print a long int argument in decimal
  • c print a character
  • s print a string
  • f print a float or double argument
  • e same as f, but use exponential notation
  • g use e or f, whichever is better
  • o print an int argument in octal (base 8)
  • x print an int argument in hexadecimal (base 16)
  • print a single

7
Cs backslash codes
  • \b backspace
  • \f formfeed
  • \n newline
  • \r carriage return
  • \t horizontal tab
  • \ double quote
  • \0 null
  • \\ backslash

8
Scanf input operator
  • Scanf(formatspecifier,variablename)
  • Examples
  • scanf(d,a) where a is an integer
  • scanf(c,c) where c is a character
  • scanf(f,h) where h is a real

9
Assignment
  • The assignment operator assigns a value to a
    variable. For example,
  • x 1 sets x to 1, and
  • a b sets a to whatever b's value is.
  • The right hand side of an assignment operation
    may be an expression such as
  • a a1

10
Expression Statements
  • Most of the statements in a C program are
    expression statements. An expression statement is
    simply an expression followed by a semicolon. The
    lines
  • i 0 i i 1 and printf("Hello, world!\n")
    are all expression statements

11
Basic Arithmetic Operators
  • Addition
  • Subtraction
  • Multiplication
  • Division /
  • Modulus
  • Increment
  • Decrement --

12
Basic program structure
  • Include ltstdio.hgt
  • main()
  • declarations
  • statements

13
Basic example
  • Include ltstdio.hgt
  • main()
  • int a
  • printf(enter number \n)
  • scanf(d,a)
  • printf(the number you entered is d,a)

14
Basic example
  • Include ltstdio.hgt
  • main()
  • int a,b,c
  • printf(enter first number \n)
  • scanf(d,a)
  • printf(enter second number \n)
  • scanf(d,b )
  • c ab
  • printf(the product of the numbers you entered is
    d,c)

15
If statements
  • The simplest way to modify the control flow of a
    program is with an if statement, which in its
    simplest form looks like this
  • if(x gt max) max x
  • The syntax of an if statement is
  • If ( expression ) statement
  • where expression is any expression and statement
    is any statement

16
Basic example
  • Include ltstdio.hgt
  • main()
  • int a
  • printf(enter number \n)
  • scanf(d,a)
  • if(a gt 10) printf(the number you entered d is
    more than ten,c)

17
A series of statements after an if statement
  • What if you have a series of statements, all of
    which should be executed together or not at all
    depending on whether some condition is true? The
    answer is that you enclose them in braces
  • if( expression )
  • statementltsubgt1lt/subgt
  • statementltsubgt2lt/subgt statementltsubgt3lt/subgt

18
Basic example
  • Include ltstdio.hgt
  • main()
  • int a,b,c
  • printf(enter first number \n)
  • scanf(d,a)
  • printf(enter second number \n)
  • scanf(d,b )
  • c ab
  • If (c gt 100)
  • printf(ok \n)
  • printf(the product of the numbers you entered is
    d,c)

19
Expressions in If statements
  • The expression in an if statement is a Boolean
    expression typically made up of
  • Relational and Boolean operators
  • The statements in an if stamenst are any legal
    statements

20
Relational Operators
  • The relational operators such as lt, lt, gt, and gt
    are in fact operators, just like , -, , and /.
  • The relational operators take two values, test
    them, and return'' a value of 1 or 0 depending
    on whether the tested relation was true or false.
  • The complete set of relational operators in C is
    lt less than
  • lt less than or equal
  • gt greater than
  • gt greater than or equal
  • equal
  • ! not equal For example, 1 lt 2 is 1, 3 gt 4 is
    0, 5 5 is 1, and 6 ! 6 is 0.

21
Boolean operators
  • The three Boolean operators are
  • and
  • or
  • ! not (takes one operand unary'')

22
If else
  • if( expression ) statementltsubgt1lt/subgt else
    statementltsubgt2lt/subgt
  • if (a gt b) printf(a)
  • Else printf(b)

23
If else if
  • We can also have multiple specific alternatives
    using if else if..
  • Suppose we have a variable grade containing a
    student's numeric grade, and we want to print out
    the corresponding letter grade. Here is code that
    would do the job
  • if(grade gt 90) printf("A")
  • else if(grade gt 80) printf("B")
  • else if(grade gt 70) printf("C")
  • else if(grade gt 60) printf("D")
  • else printf("F")

24
Basic example
  • Include ltstdio.hgt
  • main()
  • int a,b,c
  • printf(enter first number \n)
  • scanf(d,a)
  • printf(enter second number \n)
  • scanf(d,b )
  • If (a gt b !! a gt 5)
  • printf(d is greater than d or 5,a,b)
  • Else
  • printf(d is not greater than d and not greater
    than 5,a,b)
Write a Comment
User Comments (0)
About PowerShow.com