C basics: main, printf, and functions · Fundamentos de C: main, printf y funciones
C is a compiled language
- C is one of the oldest and most important languages. Linux, databases, and even Python are built with C.
- Unlike Python, C is compiled: the computer first turns your code into a program, then runs it.
- C is strict: every statement ends with a semicolon
;, and every variable needs a type.
C es un lenguaje compilado
- C es uno de los lenguajes más antiguos e importantes. Linux, las bases de datos y hasta Python están construidos con C.
- A diferencia de Python, C es compilado: la computadora primero convierte tu código en un programa y luego lo ejecuta.
- C es estricto: cada sentencia termina con un punto y coma
;, y cada variable necesita un tipo.
The C skeleton
- Every C program starts in a function called main. Your code goes inside main.
#include <stdio.h>gives youprintf, which prints text.return 0;at the end ofmainmeans "the program finished without an error".
La estructura básica de C
- Todo programa de C comienza en una función llamada main. Tu código va dentro de main.
#include <stdio.h>te proporcionaprintf, que imprime texto.return 0;al final demainsignifica "el programa terminó sin errores".
#include <stdio.h>
int main(void) {
printf("C runs from inside main.\n");
int year = 11;
printf("Grade %d\n", year);
return 0;
}
printf and types
printfprints text. The\ninside the text means "start a new line".- To print a value, use a placeholder:
%dfor anint,%ffor adouble,%cfor one character. printf("Grade %d\n", year);puts the value ofyearwhere the%dis.- Every variable needs a type first:
int year = 11;ordouble pi = 3.14;.
printf y tipos
printfimprime texto. El\ndentro del texto significa "iniciar una nueva línea".- Para imprimir un valor, usa un marcador de posición:
%dpara unint,%fpara undouble,%cpara un solo carácter. printf("Grade %d\n", year);coloca el valor deyeardonde está el%d.- Cada variable necesita un tipo primero:
int year = 11;odouble pi = 3.14;.
Common mistakes
- Every statement ends with
;, andmainreturnsint(return 0;). printfneeds the right format (%d,%f) and\nfor a new line.
Errores comunes
- Cada sentencia termina con
;, ymainretornaint(return 0;). printfnecesita el formato correcto (%d,%f) y\npara una nueva línea.
Now you try
- Each task pre-fills the code you need. Write inside
main, or complete the function shown. - Some tasks ask for a function only — do not write your own
main; the checker adds one. - Press Run to compile and run, then Check answer. Your code compiles and runs on the server.
Ahora tú intentas
- Cada tarea prellena el código que necesitas. Escribe dentro de
main, o completa la función mostrada. - Algunas tareas piden solo una función; no escribas tu propia
main; el verificador añadirá una. - Presiona Run (Ejecutar) para compilar y ejecutar, luego Check answer (Comprobar respuesta). Tu código se compila y ejecuta en el servidor.
Compile then run · Compilar y luego ejecutar
C source is compiled to a program, which you then run. · El código fuente en C se compila a un programa, que luego ejecutas.
Inside · Dentro main, use printf(...) to print exactly Hello, C! on its own line. · Dentro de main, usa printf(...) para imprimir exactamente Hello, C! en su propia línea.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Complete the function int square(int n) so it returns n * n. The checker calls it with several values. Do not · no write a main — the checker provides one. · Completa la función int square(int n) para que devuelva n * n. El verificador la llama con varios valores. No escribas un main — el verificador proporciona uno.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Inside · Dentro main, declare int grade = 11;. Then use printf to print two lines: first Hello!, then Grade 11 — print the number with a %d placeholder, not by typing 11. · Dentro de main, declara int grade = 11;. Luego usa printf para imprimir dos líneas: primero Hello!, luego Grade 11 — imprime el número usando el marcador de posición %d, no escribiendo 11 directamente.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.