-
Notifications
You must be signed in to change notification settings - Fork 14
01. Getting started
Download your favorite compiler. This page has a list of compilers.
By convention C files use .c
extension. Make a file named main.c
and save it. Run the following command from your terminal
- Using the clang compiler (If you are using gcc compiler, replace
clang
withgcc
)
clang main.c -o main
- Using the msvc compiler (If need to run the downloaded developer console for the execution of following command)
cl main.c
Note: The -o main is the flag that tells the compiler to output the executable named main
(the default name is a
). By default msvc compiler uses the name of the input file i.e. main. If you are using clang/gcc in windows type -o main.exe
This should output the following:
- Clang
LINK : fatal error LNK1561: entry point must be defined
clang: error: linker command failed with exit code 1561 (use -v to see invocation)
- Msvc
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29111 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
main.c
Microsoft (R) Incremental Linker Version 14.27.29111.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
LINK : fatal error LNK1561: entry point must be defined
Let's focus on the error entry point must be defined
.
The entry point
is the start of the program. The C Language uses a Main Function as the entry point. Type the following in your main.c
file and compile the file.
int main(void) {
return 0;
}
This should output a executable. You can run the executable but it doesn't do anything interesting right now. To understand what functions are, let us visit the pure mathematical functions. The example of some mathematical functions are as follows:
- f(x)=sin(x2+1)
- f(a, b, t)=(1-t)*a+t*b
- f(x, y)=x2+y2
Mathematical functions take some input (x, y, etc), do some operation on them and return some result. C functions are the same. Let us visit the Main Function again.
int main(void);
Here:
-
int
is the return type i.e. what kind of value the function will be returning -
main
is the name of the function i.e. function identifier -
void
means that the function doesn't take any input
- Return type : The type the function will be returning (void if the function doesn't return)
- Function Name : Name that identifies the function i.e. function identifier
- Parameters : The input to the function (void if the function doesn't take any input)
- Function Body : The body of the function, they are enclosed in between curly braces
int foo(void) {
return 6;
}
int seven(void) {
return 7;
}
void i_dont_return(void) {
}
int add(int a, int b) {
return a+b;
}
int main(void) {
return 0;
}
Note: Pure functions are the functions that doesn't modify it's input and return a value, if the function doesn't return anything or modify their inputs, they are usually called procedure or impure functions
Function declaration are the function without their bodies. Function declaration are used to tell the compiler that the functions exist (more on this later). Functions with functions body are called Function Definitions Example of function declarations:
int foo(void);
int seven(void);
void i_dont_return(void);
int add(int a, int b);
int main(void);
A function can be called, or executed by the identifier (i.e the name of the function). The required parameters should be passed to the function, for example:
// the `main` function automatically gets called by the compiler
int main(void) {
int a = foo(); // calling `foo` and storing the result in `a`
int b = seven(); // calling `seven` and storing the result in `b`
i_dont_return(); // calling i_dont_return, since the funtion doesn't return, no results need to be stored
int sum = add(5, 6); // calling `add` with `5` and `6` as parameters and the result is stored in `sum`
return 0;
}
Use reference book, links or videos here for in-depth knowledge on C Programming
The different topics for covering C Programming are as follows. You are expected to read this after you are able to write basic code in C.
- Basic Concepts
- Keyword
- Preprocessor
- Expression
- Declaration
- Initialization
- Statements
- Variables
- Flow Control
- Functions
- Advance Data Types
- Unions
- Type Punning
- Discriminated Union
- Arrays and Strings
- Pointer and Arrays
- Dynamic Memory Allocation
- Visibility and Lifetime
- Translation Phases
- Multifile compilation & Translation Units
- C Runtime Library
- Static Linking
- Dynamic Libraries Windows
- Shared Libraries Linux
- Dynamic Libraries Linux
- Structure Alignment and Packing
- Particularities of C
- Memory