1. Computer Science: A Structured Programming Approach Using C 1
Objectives
❏ Compreender a estrutura de um programa em linguagem C.
❏ Escrever seu primeiro programa em C.
❏ Introduzir o comando include pré-processador.
❏ Ser capaz de criar bons identificadores para objetos em um programa.
❏ Ser capaz de listar, descrever e usar os tipos de dados básicos C.
❏ Ser capaz de criar e utilizar variáveis e constantes.
Introdução à linguagem C
2. Computer Science: A Structured Programming Approach Using C 2
2-1 Introdução
C é uma linguagem de programação estruturada. É
considerada uma linguagem de alto nível porque
permite que o programador se concentre no problema
em questão e não se preocupe com a máquina que o
programa estará usando.
3. Computer Science: A Structured Programming Approach Using C 3
2-2 C Programs
Structure of a C Program
Your First C Program
Comments
The Greeting Program
Topics discussed in this section:
4. Computer Science: A Structured Programming Approach Using C 4
FIGURE 2-2 Estrutura de um Programa em C
5. Computer Science: A Structured Programming Approach Using C 5
FIGURE 2-3 Olá Mundo
6. Computer Science: A Structured Programming Approach Using C 6
PROGRAM 2-1 The Greeting Program
// Declarações de Variáveis
// Instruções
7. Computer Science: A Structured Programming Approach Using C 7
FIGURE 2-4 Exemplo de Comentários
// Aqui escrevemos comentários de uma única linha
/*
**Aqui agrupamos comentários que precisam mais de
**Uma linha
*/
8. Computer Science: A Structured Programming Approach Using C 8
2-3 Identifiers
One feature present in all computer languages is the
identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address.
9. Computer Science: A Structured Programming Approach Using C 9
Table 2-1 Rules for Identifiers
10. Computer Science: A Structured Programming Approach Using C 10
An identifier must start with a letter or underscore:
it may not have a space or a hyphen.
Note
11. Computer Science: A Structured Programming Approach Using C 11
C is a case-sensitive language.
Note
12. Computer Science: A Structured Programming Approach Using C 12
Table 2-2 Examples of Valid and Invalid Names
13. Computer Science: A Structured Programming Approach Using C 13
2-4 Types
A type defines a set of values and a set of operations
that can be applied on those values.
Void Type
Integral Type
Floating-Point Types
Topics discussed in this section:
14. Computer Science: A Structured Programming Approach Using C 14
Tabela 2-4 Tipos em C
Tipo Implementação em C
15. Computer Science: A Structured Programming Approach Using C 15
2-5 Variáveis
Variavel é um nome que definimos para armazenar dados
de forma simples.
Variable Declaration
Variable Initialization
Topics discussed in this section:
16. Computer Science: A Structured Programming Approach Using C 16
FIGURE 2-11 Variables
17. Computer Science: A Structured Programming Approach Using C 17
Table 2-5 Examples of Variable Declarations and Definitions
18. Computer Science: A Structured Programming Approach Using C 18
FIGURE 2-12 Variable Initialization
‘B’
19. Computer Science: A Structured Programming Approach Using C 19
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.
Note
20. Computer Science: A Structured Programming Approach Using C 20
PROGRAM 2-2 Print Sum of Three Numbers
21. Computer Science: A Structured Programming Approach Using C 21
PROGRAM 2-2 Print Sum of Three Numbers (continued)
22. Computer Science: A Structured Programming Approach Using C 22
PROGRAM 2-2 Print Sum of Three Numbers (continued)
23. Computer Science: A Structured Programming Approach Using C 23
2-6 Constants
Constants are data values that cannot be changed during
the execution of a program. Like variables, constants
have a type. In this section, we discuss Boolean,
character, integer, real, complex, and string constants.
Constant Representation
Coding Constants
Topics discussed in this section:
24. Computer Science: A Structured Programming Approach Using C 24
A character constant is enclosed in single quotes.
Note
25. Computer Science: A Structured Programming Approach Using C 25
Table 2-6 Symbolic Names for Control Characters
26. Computer Science: A Structured Programming Approach Using C 26
Table 2-7 Examples of Integer Constants
27. Computer Science: A Structured Programming Approach Using C 27
Table 2-8 Examples of Real Constants
28. Computer Science: A Structured Programming Approach Using C 28
FIGURE 2-13 Some Strings
29. Computer Science: A Structured Programming Approach Using C 29
FIGURE 2-14 Null Characters and Null Strings
30. Computer Science: A Structured Programming Approach Using C 30
Use single quotes for character constants.
Use double quotes for string constants.
Note
31. Computer Science: A Structured Programming Approach Using C 31
PROGRAM 2-3 Memory Constants
32. Computer Science: A Structured Programming Approach Using C 32
PROGRAM 2-3 Memory Constants (continued)