003 - Estruturas básicas
Arquivos necessários
Baixe todos os programas aqui.
Configuração do VSCode
tasks.json
{
"tasks": [
{
"type": "shell",
"label": "buildactivefile",
"windows": {
"command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-W",
"-Wall",
"-Werror",
"-Wextra",
"-pedantic",
"-std=c2x",
"-o",
"${fileDirname}\\..\\bin\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\msys64\\ucrt64\\bin"
}
},
"linux": {
"command": "gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-W",
"-Wall",
"-Werror",
"-Wextra",
"-pedantic",
"-std=c2x",
"-o",
"${fileDirname}/../bin/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
}
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "buildactivefile",
"windows": {
"program": "${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
"cwd": "${workspaceFolder}\\bin",
"miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe"
},
"linux": {
"program": "${workspaceFolder}/bin/${fileBasenameNoExtension}",
"cwd": "${workspaceFolder}/bin",
"miDebuggerPath": "/usr/bin/gdb"
},
"args": [],
"stopAtEntry": false,
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
Python
terceiro.py
def soma(a, b):
return a + b
n1 = 5
n2 = 10
print("Terceiro programa")
print("Soma de %d + %d = %d" % (n1, n2, soma(n1, n2)))
leitor.py
def soma(a, b):
return a + b
def le_int():
return int(input())
print("Leitor de inteiros")
n1 = le_int()
n2 = le_int()
print("Soma de %d + %d = %d\n" % (n1, n2, soma(n1, n2)))
leitor2.py
def soma(a, b):
return a + b
def le_int(mensagem):
while True:
try:
return int(input(mensagem + "\n"))
except ValueError:
print("Erro lendo número. Redigite.")
print("Leitor de inteiros")
n1 = le_int("N1")
n2 = le_int("N2")
print("Soma de %d + %d = %d\n" % (n1, n2, soma(n1, n2)))
C
segundo.c
#include <stdio.h>
int main(void)
{
char c = 'A';
short s = 6;
int i = 10;
long l = 0L;
long long ll = 0LL;
float f = 0.0f;
double d = 0.0;
long double g = 0.0L;
unsigned int z = 4u;
unsigned long x = 6UL;
unsigned long long y = 9ULL;
unsigned short t = 10u;
printf("Tamanho de tipo char: %zu\n", sizeof(c));
printf("Tamanho de tipo short: %zu\n", sizeof(s));
printf("Tamanho de tipo int: %zu\n", sizeof(i));
printf("Tamanho de tipo long: %zu\n", sizeof(l));
printf("Tamanho de tipo long long: %zu\n", sizeof(ll));
printf("Tamanho de tipo float: %zu\n", sizeof(f));
printf("Tamanho de tipo double: %zu\n", sizeof(d));
printf("Tamanho de tipo long double: %zu\n", sizeof(g));
printf("\nTamanho de tipo unsigned short: %zu\n", sizeof(t));
printf("Tamanho de tipo unsigned int: %zu\n", sizeof(z));
printf("Tamanho de tipo unsigned long: %zu\n", sizeof(x));
printf("Tamanho de tipo unsigned long long: %zu\n", sizeof(y));
printf("\nchar: %c\n", c);
printf("short: %d\n", s);
printf("int: %d\n", i);
printf("long: %ld\n", l);
printf("long long: %lld\n", ll);
printf("float: %f\n", f);
printf("double: %lf\n", d);
printf("long double: %Lf\n", g);
printf("\nunsigned short: %u\n", t);
printf("unsigned int: %u\n", z);
printf("unsigned long: %lu\n", x);
printf("unsigned long long: %llu\n", y);
}
terceiro.c
#include <stdio.h>
int soma(int a, int b)
{
return a + b;
}
int main(void)
{
int n1 = 5;
int n2 = 10;
puts("Terceiro programa\n");
printf("Soma de %d + %d = %d\n", n1, n2, soma(n1, n2));
}
leitor.c
#include <stdio.h>
int soma(int a, int b)
{
return a + b;
}
int le_int()
{
int a = 0;
scanf("%d", &a);
return a;
}
int main(void)
{
puts("Leitor de inteiros");
int n1 = le_int();
int n2 = le_int();
printf("Soma de %d + %d = %d\n", n1, n2, soma(n1, n2));
}
leitor_2.c
#include <stdio.h>
#include <stdbool.h>
int soma(int a, int b)
{
return a + b;
}
int le_int(char mensagem[])
{
int a = 0;
do
{
puts(mensagem);
if (scanf("%d", &a)){
return a;
}
puts("Erro lendo número. Redigite.");
int lido = 0;
do
{
lido = getchar();
} while (lido != '\n' && lido != EOF);
} while (true);
}
int main(void)
{
puts("Leitor de inteiros 2");
int n1 = le_int("Primeiro numero:");
int n2 = le_int("Segundo numero:");
printf("Soma de %d + %d = %d\n", n1, n2, soma(n1, n2));
}
Video
Linguagem C para quem já programa em Python - Aula 3