C Programming Cheat Sheet

C Programming Cheat Sheet

C Programming Cheat Sheet will guide you to work on C language with basics and advanced topics. Cheat Sheet for students, engineers, and professionals.

Introduction

C language is a high-level language developed by Dennis M. Ritchie. It has been used to develop operating systems, databases, editors, etc. C language code is generally written in a text file with the extension .c

Features

  • Structured Language
  • Execution Speed
  • Code Reusability
  • C language extends itself

Variables

Variable names in C Language are the names given to locations in memory. These locations can be an integer, characters, or real constants.

Variables in C Programming

Variable Scope

The existence of a variable in a region is called its scope, or the accessibility of a variable defines its scope:

  • Local Variables: Whenever a variable is defined in a function or a block, it can be used only in that function or block. The value of that variable cannot be accessed from outside the function.
  • Global Variables: A variable has Global Scope, if it is defined outside a function i.e., the code body, and is accessible from anywhere

User Input

To get user input in C language, use the scanf(). It is used to read the data input by the user on the console. Let us print an integer entered by the user:

Output

Command Line Arguments

In C language, you can pass values from the command line. The arguments are placed after the program name at the time of execution. For command line arguments, set the main() function in C Language to the following, wherein argc is used to count the arguments and argv has all the arguments:

Output

The output when we have set the command line arguments as A, B, C, D:

Tokens

C Tokens are the individual words and punctuation, which is the smallest element of a program, understandable by the compiler:

  • Identifiers: Used for naming variables, functions, and arrays. Do not use keywords as identifiers.
  • Keywords: These are reserved words used in programming languages. For example, continue, if, else, double, sizeof, struct, union, etc.
  • Whitespace: White space in C language includes vertical tab, horizontal tab, blank, comments, form feed, end-of-line, etc.

Constants

Constants in C language are the values that cannot be modified once they are defined:

Constants in C

Example:

Typecasting

Type casting in C Language converts one type to another. In C language, we use the cast operator for type casting:

Example:

  • int to float:
  • int to double

Operators

Operators perform operations by taking one or more values, to give another value. These operations are performed on variables and values. The following are the operators in C Programming:

  • Arithmetic Operators
  • Assignment Operators
  • Arithmetic Assignment Operators
  • Relational/ Comparison Operators
  • Logical Operators
  • Unary Operators
  • Conditional Operators

Decision Making Statements

Decision-making is needed when you want a set of instructions to be executed in a condition and other sets of instructions in another condition.

  • if statement

  • if-else statement

  • if-else if-else statement

  • switch statement

Loops

Loops are used when you need to repeat a part of the program. Here are the loops:

  • while loop: Used when you want to repeat a part of the program a fixed number of times.

  • do-while loop: The do-while executes the statement first and then the condition is tested.

  • for loop: Executes a specified number of times. Initialize, test, and increment loop counter in a single line:

Arrays

An array is a collection of elements, of fixed size and type, for example, a collection of 5 elements of type int:

  • Declare an array:
  • Initialize an array:
  • Two-Dimensional Arrays:

    Two Dimensional Arrays Rows and Columns Structure

Strings

An array of characters in C Language is called Strings.  The null character also has a role in C language. A string is terminated by a null character i.e. \0:

  • Create a String: We haven’t added the null character above, since the compiler adds it on its own. In addition, we haven’t added size, since it is optional.
  • Create a String with size in square brackets:


    Array of characters with index in C Programming

Functions

A function in C Language is an organized block of reusable code that avoids repeating the tasks again and again:

  • Create a Function:

  • Call a Function:

  • Function Parameters:

  • Multiple Parameters:

C – Recursion

When a function calls itself, it is called Recursion in C Language. In another sense, with Recursion, a defined function can call itself. Recursion is a programming approach, that makes code efficient and reduces LOC.

The following figure demonstrates how recursion works when we calculate Factorial in C with Recursion:

Recursion in C Language

Structures

Structures in C allow you to store data of different types. You can easily store variables of types, such as int, float, char, etc:

  • Define a Structure:

  • Declare a Structure while defining it:

  • Declare a Structure with struct:


    Declare e1e2, and e3 of type emp:

  • Access Structure Members:

Unions

Structures in C language allow you to store data of different types, whereas Unions allow storing different data types but in the same memory location:

  • Create a Union:

  • Access Union Members: Use the member access operator (.):

Pointers

The C Pointer is a variable that is used to store the memory address as its value. However, to get the memory address of a variable, use the & operator:

  • The Address Operator: Access the address of a variable. Defined by &, the ampersand sign.
  • The Indirection Operator: Access the value of an address. Defined by *, the asterisk sign.

Create and Declare a Pointer:

Example:

Output

Enums

Enums, stand for enumerations i.e., a group of constants. Use enums to name constants. In C, we use the enum keyword to create an enum:

  • Syntax:

  • Create an Enum:

  • Create an Enum Variable:

  • Assign a value to the enum variable:


    The output depends on the following:
    Enums in C

What next?

After completing C Programming, follow the below tutorials:


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


C - File Handling
C Tutorial | Learn C Programming
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment








Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue Reading