Docsity

Beginners Cheatsheet for C Language, Cheat Sheet of C programming

Toggle sidebar
bg1
Title : - C Language Cheat Sheet
The C Language Cheat Sheet is an easy-to-use reference manual for
key grammar and ideas in the C programming language. Beginning with
a discussion of the fundamental syntax of a C programme, it gradually
moves on to topics such as data types, variables, operators, control flow,
arrays, functions, pointers, strings, input/output, structures, memory
management, preprocessor directives, file handling, and common library
functions.
The cheat sheet presents each topic with clear syntax examples, making
it simple to comprehend and use in practise. The cheat sheet focuses on
the core concepts of C, such as declaring variables, carrying out
operations with operators, directing the flow of programmes with
conditional statements and loops, working with arrays and strings,
utilising functions and recursion, manipulating memory with pointers,
managing files, and utilising various other programming languages.
You may rapidly refer to and review important C programming principles
by utilising this cheat sheet. It is a helpful tool for both new and
seasoned programmers who need to locate syntax examples and
recollect crucial information when writing in C.
This cheat sheet will provide you the crucial knowledge you need to
create successful and efficient C programmes, whether you just need a
fast refresher or a thorough understanding of the C language.
**1. Basic Syntax:**
```
#include <stdio.h>
int main() {
// Code goes here
return 0;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Beginners Cheatsheet for C Language and more Cheat Sheet C programming in PDF only on Docsity!

Title : - C Language Cheat Sheet

The C Language Cheat Sheet is an easy-to-use reference manual for key grammar and ideas in the C programming language. Beginning with a discussion of the fundamental syntax of a C programme, it gradually moves on to topics such as data types, variables, operators, control flow, arrays, functions, pointers, strings, input/output, structures, memory management, preprocessor directives, file handling, and common library functions. The cheat sheet presents each topic with clear syntax examples, making it simple to comprehend and use in practise. The cheat sheet focuses on the core concepts of C, such as declaring variables, carrying out operations with operators, directing the flow of programmes with conditional statements and loops, working with arrays and strings, utilising functions and recursion, manipulating memory with pointers, managing files, and utilising various other programming languages. You may rapidly refer to and review important C programming principles by utilising this cheat sheet. It is a helpful tool for both new and seasoned programmers who need to locate syntax examples and recollect crucial information when writing in C. This cheat sheet will provide you the crucial knowledge you need to create successful and efficient C programmes, whether you just need a fast refresher or a thorough understanding of the C language.

1. Basic Syntax:

```

#include <stdio.h> int main() { // Code goes here return 0;

```

2. Data Types:

- int: Used for integers.

```

int age;

```

- float: Used for floating-point numbers.

```

float pi;

 ## - char : Used for characters. ### ``` char grade; 

- pointers: Used to store memory addresses.

```

int *ptr;

 x += 5; 
  • Logical operators: && (and), || (or),! (not)
if (a && b) { ... } 
  • Relational operators: ==, !=, >, <, >=, <=
if (a > b) { ... } 
  • Bitwise operators: &, |, ^, ~, <<, >>
int result = a & b; 

****5. Control Flow:****

  • if-else statements:
if (condition) { // code to execute if the condition is true } else { // code to execute if the condition is false } 
  • switch statements:
switch (expression) { case constant1: // code to execute if expression equals constant break; case constant2: // code to execute if expression equals constant break; default: // code to execute if expression doesn't match any constant } 
  • Loops:
    • for loop:
      for (initialization; condition; increment/decrement) { // code to execute repeatedly 

```

  • continue statement:
for (int i = 0; i < 10; i++) { if (i == 5) { continue; // Skip the iteration when i is 5 } } 

****6. Arrays:****

  • Declare an array with a data type followed by the array name and size.
int numbers[5]; 
  • Initialize array elements individually or using an initializer list.
int numbers[5] = {1, 2, 3, 4, 5}; 
  • Access array elements using their index (starting from 0).
 int firstNumber = numbers[0]; 
  • Multi-dimensional arrays are arrays of arrays.
int matrix[3][3]; 

****7. Functions:****

  • Define a function with a return type, function name, parameters, and body.
int add(int a, int b) { return a + b; } 
  • Call a function by using its name followed by parentheses.
int result = add(5, 3); 
  • Pass arguments to a function using parameters.
void greet(char name[]) { - Declare a pointer with a data type followed by an asterisk (*) and the pointer name. ```c int *ptr; 
  • Assign a memory address to a pointer using the & operator.
int number = 10; int *ptr = &number; 
  • Access the value at a memory address using the dereference operator (*) or arrow operator (->).
int value = *ptr; 
  • Perform pointer arithmetic using + and - operators.
int numbers[5] = {1, 2, 3, 4, 5}; int *ptr = numbers; int thirdNumber = *(ptr + 2); 

9. Strings:

  • Declare a string as an array of characters.
char name[10]; 
  • Initialize a string with double quotes.
char greeting[] = "Hello"; 
  • Access individual characters using their index.
char firstChar = name[0]; 
  • Manipulate strings using library functions like strlen(), strcpy(), strcat(), strcmp(), etc.
char destination[20]; char source[] = "World"; strcpy(destination, source); 

10. Input/Output:

  • Read input from the user using scanf() or fgets().

struct Person p; strcpy(p.name, "John"); p.age = 25;

 ## **12. Memory Management:** - Dynamic memory allocation functions: - malloc(size): Allocates a block of memory of the given size in bytes. ```c int *ptr = (int*)malloc (sizeof(int)); ``` - calloc(num, size): Allocates an array of num elements, each of size bytes, and initializes them to 0. ```c int *ptr = (int*)calloc(5, sizeof(int)); ``` - realloc(ptr, size): Resizes the previously allocated block of memory to the given size. ```c int *newPtr = (int*)realloc(ptr, 10 * sizeof(int)); ``` - free(ptr): Releases the memory block previously allocated. ```c free(ptr); ``` ## **13. Preprocessor Directives:** - #include: Includes header files in the program. ```c #include <stdio.h> 
  • #define: Defines a macro or a constant.
#define PI 3. 

14. File Handling:

  • Read from a file using functions like fopen(), fclose(), fgets(), fscanf(), etc.
FILE *file = fopen("data.txt", "r"); char line[100]; fgets(line, sizeof(line), file);