C Programming 101: A Beginner's Cheat Sheet to Get You Started
Welcome to your C programming cheat sheet! This handy guide is designed to help you quickly and easily reference some of the most commonly used concepts and commands in C programming. C is a powerful and versatile programming language that has a rich history and has been widely used in a wide range of applications, including operating systems, embedded systems, and high-performance applications.
C was first developed in the early 1970s by Dennis Ritchie at Bell Labs, as a successor to the B programming language. It quickly gained popularity and became the standard language for system programming. It was used to develop the Unix operating system, which is still in use today.
One of the things that makes C so popular is its flexibility and control over the hardware. C allows you to directly interact with memory, allowing you to have full control over the performance of your program. This makes C a great choice for systems that require high performance, such as video games and operating systems.
This cheat sheet will cover the essential concepts and commands of C programming, including arithmetic operators, comparison operators, logical operators, bitwise operators, and more. Whether you’re just starting out on your coding journey or looking for a quick refresher, this cheat sheet has got you covered. So, let’s dive in and start exploring the world of C programming together!
Data Types
int
: Integer typefloat
: Floating-point typedouble
: Double-precision floating-point typechar
: Character typevoid
: Specifies no value
Variables
- To declare a variable, use the data type followed by the variable name (e.g.
int x;
) - To initialize a variable, assign a value to it (e.g.
x = 5;
)
Operators
Arithmetic operators
+ : addition
- : subtraction
* : multiplication
/ : division
% : modulus (remainder)
++ : increment by 1
-- : decrement by 1
Comparison operators
== : equal to
!= : not equal to
< : less than
> : greater than
<= : less than or equal to
>= : greater than or equal to
Logical operators
&& : logical and
|| : logical or
! : logical negation
Bitwise operators
& : bitwise and
| : bitwise or
^ : bitwise xor
~ : bitwise inversion
<< : left shift
>> : right shift
Assignment operators
= : assigns values
+= : adds and assigns
-= : subtracts and assigns
*= : multiplies and assigns
/= : divides and assigns
%= : takes modulus and assigns
Conditional operator
? : : this operator is also known as ternary operator it is used to return
a value based on a condition
Comments
// single line comment
/*
multi-line
comment
*/
Arrays
int marks[5] = {90, 85, 75, 70, 80};
Strings
char name[10] = "John Doe";
Loops
while (age < 25) {
printf("You are still young.");
age++;
}
do {
printf("This will execute at least once.");
} while (age < 25);
for (int i = 0; i < 5; i++) {
printf("Iteration %d", i);
}
Conditional Statements
if (age > 18) {
printf("You are eligible to vote.");
}
if (grade == 'A') {
printf("Excellent work!");
} else {
printf("Keep working hard.");
}
switch (grade) {
case 'A':
printf("Excellent work!");
break;
case 'B':
printf("Good work!");
break;
default:
printf("Keep working hard.");
}
Functions
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(2, 3);
printf("%d", sum);
}
Input/Output
printf("Enter a number: "); // output
scanf("%d", &number); // input (int)
fgets(variable, sizeof variable, stdin); // input (string)
Pointers
- To declare a pointer, use an asterisk (*) before the variable name (e.g.
int *p;
) - To access the value at a memory address, use the indirection operator (
*
) before the pointer name (e.g.*p = 5;
) - To get the address of a variable, use the address-of operator (&) before the variable name (e.g.
p = &x;
)
Importing Libraries
In C, libraries are typically imported using the #include
preprocessor directive. The library to be imported is included in the program using the <
and >
characters, indicating that the library is a system library, or using double quotes " "
, indicating that the library is a user-defined library.
For example, to import the standard input/output library (stdio.h), you would use the following code:
#include <stdio.h>
To import a user-defined library, you would use the following code:
#include "library.h"
It’s also worth noting that some libraries may also require additional linking when building the final executable program. This linking is usually done through a build tool such as GCC or through the IDE.
It’s important to note that not all libraries are compatible with C. Some libraries are built for C++ or other languages, so you should make sure you are using the correct libraries for your project and that they are compatible with the version of C you are using.
A few more things to note
- C supports the usual arithmetic operators (+, -, *, /, %)
- C has a ternary operator, which can be used as a shorthand for an if-else statement:
(condition) ? true : false
. - C supports the increment operator (++) and the decrement operator (–)
- C supports the usual logical operators (&&, ||, !)
- C has a sizeof operator that can be used to determine the size of a variable or data type in bytes
- A function should always return a value if it’s of non-void type, otherwise it will return an undefined value.
- To initialize an array without providing values for all of the elements, you can use {} and leave out the remaining values, for example:
int marks[5] = {90, 85};
. - To access an element of an array, use the array name and an index in square brackets, for example:
marks[2] = 80;
And there you have it, a comprehensive overview of some of the most important concepts and commands in C programming. This cheat sheet is an invaluable tool to have on hand as you continue to learn and grow in the world of C programming. Keep it handy and refer back to it as needed. Happy coding and best of luck on your C programming journey!
Tools I use for this site
- I buy all my domain names on Namecheap, as thetrendycoder.com
- The hosting of this website is made on Bluehost.
- The website is created with WordPress.org (and not WordPress.com).
- I use the page builder Elementor because it makes it easy to create modern pages with drag and drop.
- I have multiple websites, and on most of them, I use themes from wpKoi. I love their design, they are very original and work well with Elementor.
- All the designs and images are created using canvas.
- I use Grammarly and languagetool to correct all my spelling and grammar mistakes.
- SEO is a big thing on a website, I use a WordPress plugin called YoastSEO to help me with the basic analysis. I also use a tool called Keysearch for choosing the right keywords.
- To handle affiliate links, I use two platforms: impact and ShareASale.
You want to write on TheTrendyCoder ?
If you are interested in publishing guest articles on this website, sharing your experience or coding tutorials, apply through this form.
NO EXPERIENCE needed!
NO PERFECT English needed!
NO DEGREE needed!
NO AGE limits!
No matter at what stage we are in our tech journey, we all have learned things and experienced things. Sharing them can help others and even help us. So, if you are a student, a professional, or a self-taught coder, feel at home and share some of your knowledge with the community.