How%20Create%20a%20C %20Program - PowerPoint PPT Presentation

About This Presentation
Title:

How%20Create%20a%20C %20Program

Description:

... M'; // assign ASCII code. int a = ch; cout 'ASCII code: ' ch ... cout 'ASCII code: ' ch ' is ' a endl; return 0; Output. M is 77. N is 78 ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 26
Provided by: Ara8
Category:
Tags: 20c | 20create | 20program | 20a | ascii

less

Transcript and Presenter's Notes

Title: How%20Create%20a%20C %20Program


1
How Create a C Program
2
  • includeltiostreamgt
  • using namespace std
  • void main()
  • coutltltHello World
  • If your compiler gives error then

3
  • includeltiostream.hgt
  • void main()
  • coutltltHello World
  • coutltltendl // for going to new line
  • coutltlt good bye
  • cout is an object for printing out some thing
  • on the screen.

4
  • includeltiostream.hgt
  • void main()
  • coutltltHello World \n
  • coutltlt good bye
  • we can use \n instead of coutltltendl

5
How to run your program
  • Using Linux or unix
  • g myFile.cpp
  • ./a.out
  • g myFile.cpp myFile.out
  • ./myFile.out

6
Declare Statement Variable
  • Each variable must be declare before use
  • Each variable has a type
  • For example
  • int , char, float.
  • int for Integer type
  • char for character like A
  • float for real number

7
Example
  • int LuckyNumber17
  • float RealNumber
  • char aA

8
Identifiers
  • Identifier name of a variable, function, or
    class
  • Rules for identifiers in C
  • 1 Can be made up of letters, digits, and the
    underscore (_) character
  • 2 Cannot start with a digit
  • 3 Cannot use other symbols such as ? or
  • 4 Spaces are not permitted inside
    identifiers
  • 5 You cannot use reserved words
  • 6 They are case sensitive

9
Self Check
  • 1. What is the type of the values 0 and 0?
  • 2. Which of the following are legal identifiers?
  • Greeting1gvoid101dalmatiansHello,
    Worldltgreetinggt

10
Answer
  • int and char
  • Only the first two are legal identifiers

11
Syntax Variable Definition
  • typeName variableName valueor typeName
    variableName
  • Example
  • int number 12
  • Purpose
  • To define a new variable of a particular type and
    optionally supply an initial value

12
The Assignment Operator
  • Assignment operator
  • Not used as a statement about equality
  • Used to change the value of a variable
  • int number1
  • int number2, number3 number1number2number3
    88

13
  • number2number2-1
  • number3number21

14
How to read a variable
  • includeltiostreamgt
  • using namespace std
  • void main()
  • int number
  • coutltltplease enter a number \n
  • cingtgtnumber
  • numbernumber1
  • coutltltthe number is ltltnumber

15
Integer Types
  • The short, int and long Integer Types
  • A short integer is at least 16 bits
  • An int integer is at least as big as short
  • A long integer is at least 32 bits and at
  • least as big as int .

16
  • E.g. A 16-bit int might run from -32768 to 32767
  • The sizeof operator returns the size (in bytes)

17
  • includeltiostream.hgt
  • int main()
  • int n_int INT_MAX
  • short n_short SHRT_MAX
  • long n_long LONG_MAX
  • cout ltlt int is ltlt sizeof (int) ltlt bytes
    ltlt endl
  • cout ltlt short ltlt n_short ltlt endl
  • cout ltlt long ltlt n_long ltlt endl
  • return 0

18
  • int is 4 bytes
  • Maximum values
  • Short 32767
  • Long 2147483647

19
Characters and small integers
  • includeltiostream.hgt
  • int main()
  • char ch M // assign ASCII code
  • int a ch
  • cout ltlt ASCII code ltlt ch ltlt is ltlt a
    ltlt endl
  • ch ch 1
  • a ch
  • cout ltlt ASCII code ltlt ch ltlt is ltlt a ltlt
    endl
  • return 0

20
Output
  • M is 77
  • N is 78

21
Boolean type
  • bool isReady true
  • int ans true // ans assigned 1
  • int promise false // promise assigned 0
  • bool start -100 // true
  • bool stop 0 // false

22
Floating-point number
  • E.g.
  • 12.34
  • 9300.3
  • 0.02
  • 8.0
  • We have
  • float, double, long double

23
Arithmetic operators
  • Summation
  • Multiplication
  • Division /
  • Subtraction -

24
Operator Precedence
  • int number 3 4 5 // 35 or 23?
  • float logs 120 / 4 5 // 150 or 6??

25
Type Casting
  • Conversion between types
  • (typeName) value // c
  • typeName (value) // c
  • e.g.
  • cout ltlt int(A) ltlt endl // 65
  • float one,
  • int two
  • one 1.9 2.1
  • two (int) 1.9 (int) 2.1
Write a Comment
User Comments (0)
About PowerShow.com