Variables, types, and arithmetic · Variáveis, tipos e aritmética
Every value has a type
- In C, every variable has a type that says what kind of value it holds.
- The three you use most:
int(a whole number),double(a number with a decimal point), andchar(a single character). - You must give the type when you create the variable:
int n = 5;ordouble pi = 3.14;.
Todo valor tem um tipo
- Em C, toda variável tem um tipo que indica qual tipo de valor ela armazena.
- Os três que você usa mais:
int(um número inteiro),double(um número com ponto decimal) echar(um único caractere). - Você deve dar o tipo ao criar a variável:
int n = 5;oudouble pi = 3.14;.
Doing arithmetic
- C does math with
+,-,*, and/, just like a calculator. *and/happen before+and-. Use brackets( )to change the order.- The
%operator gives the remainder of a division:17 % 5is2.
Fazendo aritmética
- C faz matemática com
+,-,*e/, assim como uma calculadora. *e/acontecem antes de+e-. Use parênteses( )para alterar a ordem.- O operador
%dá o restante de uma divisão:17 % 5é2.
Integers divide differently
- When you divide two
ints, C throws away the fraction:7 / 2is3, not3.5. - To keep the fraction, at least one side must be a
double.7 / 2.0is3.5. - You can cast an
intto adoublewith(double):(double)sum / count.
Inteiros dividem de forma diferente
- Quando você divide dois
ints, C descarta a fração:7 / 2é3, não3.5. - Para manter a fração, pelo menos um lado deve ser um
double.7 / 2.0é3.5. - Você pode converter um
intem umdoubleusando(double):(double)sum / count.
Reading input
scanfreads typed input into a variable.scanf("%d", &n);reads oneintinton.- Note the
&beforen:scanfneeds the address of the variable so it can fill it in. - In this playground, Run first asks you for the input, one value per line; Check answer feeds the example input for you.
Lendo entrada
scanflê a entrada digitada em uma variável.scanf("%d", &n);lê umintemn.- Note o
&antes den:scanfprecisa do endereço da variável para preenchê-la. - Neste playground, Run primeiro pede sua entrada, um valor por linha; Check answer fornece a entrada de exemplo para você.
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("%d\n", a / b); // 3 (integer division)
printf("%.1f\n", a / 2.0); // 3.5 (one side is a double)
printf("%d\n", a % b); // 1 (remainder)
return 0;
}
Common mistakes
- Declare a variable with its type:
int n;. - Integer division truncates:
7 / 2is3, not3.5.
Erros comuns
- Declare uma variável com seu tipo:
int n;. - A divisão inteira truncada:
7 / 2é3, não3.5.
Now you try
- Use
/and%for whole-number math, and adouble(or a cast) when you need a fraction. - For the function tasks, do not write a
main— the checker provides one. - Press Run to compile and run, then Check answer.
Agora você tenta
- Use
/e%para matemática de números inteiros, e umdouble(ou conversão) quando precisar de uma fração. - Para as tarefas de função, não escreva um
main— o verificador fornece um. - Pressione Run para compilar e executar, depois Check answer.
Variables hold a value · Variáveis armazenam um valor
A variable is a named box of a fixed type; assigning replaces it. · Uma variável é uma caixa nomeada de tipo fixo; atribuir substitui o conteúdo.
Read one integer n with scanf. A box holds 6 items. Print two lines exactly: Full boxes: then the value of n / 6, and Left over: then the value of n % 6. · Leia um inteiro n com scanf. Uma caixa contém 6 itens. Imprima duas linhas exatamente: Full boxes: seguido do valor de n / 6, e Left over: seguido do valor de n % 6.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Complete double average3(int a, int b, int c) so it returns the average of the three as a double. Watch out for integer division — divide by 3.0, not 3. Do not · não write a main. · Complete double average3(int a, int b, int c) para retornar a média dos três como um double. Cuidado com divisão inteira — divida por 3.0, não por 3. Não escreva um main.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write double hyp(double a, double b) that returns the hypotenuse length: the square root of a*a + b*b. Use sqrt from <math.h>. Do not · não write a main. · Escreva double hyp(double a, double b) que retorne o comprimento da hipotenusa: a raiz quadrada de a*a + b*b. Use sqrt de <math.h>. Não escreva um main.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.