C ( 1 ~ 6)

Hyun·2022년 11월 3일

1.Basic codes

  1. #include <stdio.h>: This line is needed to run the line of code that starts with printf.

  2. int main(){ }: This is the starting point of the code. All the code inside the curly braces {} runs first.

  3. // output a line: This is a comment. It is not a line of code but a message we can add to code to tell ourselves or others what the code does. When the code is run this line will be ignored.

  4. printf("Hello World!");: This line of code prints, or outputs, the text “Hello World!” to the console. Printing text to the console is one way for a program to communicate with the user. The text inside is called string.

2.Compile

  1. gcc is how we run the compiler application.

  2. helloWorld.c is the filename of our code to be compiled.

  3. -o helloWorld is an optional but common addition to the command. It tells gcc to output the program executable under the name helloWorld. If this is left out, the executable file will be called a.out.

3. Data types

TypeDescriptionValues
inta whole number-2,147,483,648 to 2,147,483,647
floata number with possible decimals6 decimal places
doublea number with possible decimals15 decimal places
charstores one character (letter or number)a single character

4. Basic Format

printf("string to display", [list of optional parameters]).

symboltype
%d or %iint
%fdouble or float
%cchar
symboleffect
\nnewline
\rcarriage return
\ttab

If you use const, it means you can't change your variable type.

5. Operators

Assigning values to variables: =, +=, -=, *=, /=, %=
Performing basic comparisons between values and variables: ==, !=, <, <=, >, >=
Using logical operators in C: &&, ||, !.

6. if Statement

if (condition) {
// Statement(s)
}

if (condition) {
// Statement1 — do something
} else {
// Statement2 — do something else
}

if (condition) {
// Statement1 — do something
} else {
// Statement2 — do something else
}

if (condition) {
// Some code
} else if (condition) {
// Some code
} else {
// Some code
}

switch (grade) {
case 9:
printf("Freshman\n");
break;
case 10:
printf("Sophomore\n");
break;
case 11:
printf("Junior\n");
break;
case 12:
printf("Senior\n");
break;
default:
printf("Invalid\n");
break;
}

6.Ternary Operator

condition ? do something : do something else;

same as

if (condition) {
// Do something
} else {
// Do something else
}

0개의 댓글