Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Estrutura do Programa C | Introdução
Fundamentos de C

book
Estrutura do Programa C

Programas escritos na linguagem C são estruturados em blocos, muitas vezes referidos como "blocos de construção". Aqui está um programa básico que exibe a mensagem "Hello, c<>definity!" na tela:

c

Main

copy
#include <stdio.h> // preprocessor directive

int main() // the main function
{
printf("Hello, c<>definity!\n"); // print text

return 0; // exit
}
12345678
#include <stdio.h> // preprocessor directive int main() // the main function { printf("Hello, c<>definity!\n"); // print text return 0; // exit }

The double forward slashes // indicate a comment. Comments don't influence the behavior of your program. They're meant for human readers, not the computer. There are two types of comments in C:

python
// This is a single-line comment

/*
This
is
a
multi-line
comment
*/
h

comments

copy
// This is a single-line comment

/*
This is a
multi-line
comment
*/
1234567
// This is a single-line comment /* This is a multi-line comment */

As duas barras // indicam um comentário. Comentários não influenciam o comportamento do seu programa. Eles são destinados aos leitores humanos, não ao computador. Existem dois tipos de comentários em C:

python
// This is a single-line comment

/*
This
is
a
multi-line
comment
*/

Diretiva Include

O #include é uma diretiva de pré-processador que incorpora o arquivo "stdio.h" ao nosso programa. Esta diretiva deve ser colocada no início, antes do início do programa principal (main).

Arquivo de Cabeçalho Stdio

O arquivo "stdio.h" contém a função printf(). Ao incluí-lo, estamos simplesmente adicionando a capacidade de exibir texto na tela ao nosso programa. Muitos programas em C não têm acesso inerente a funções de E/S (entrada/saída) ou outras da biblioteca "stdio.h". É por isso que precisamos trazê-lo explicitamente usando a diretiva #include.

Nota

Um princípio orientador em C é manter seu programa enxuto, evitando a inclusão de funções desnecessárias.

Brackets and Scope

You'll encounter plenty of curly braces { } in C and other C-derived languages. It's a hallmark of the language.

These braces define blocks of code, much like bricks make up a wall. Here's a way to enhance our sample program:

c

Main

copy
#include <stdio.h>

int main()
{
// first block
{
printf("First block\n");
}

// second block
{
printf("Second block\n");
}

// third block
{
printf("Third block\n");
}

return 0;
}
123456789101112131415161718192021
#include <stdio.h> int main() { // first block { printf("First block\n"); } // second block { printf("Second block\n"); } // third block { printf("Third block\n"); } return 0; }
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

We use cookies to make your experience better!
some-alt