Show Menu
Cheatography

Comprehensive C Language Cheat Sheet (DRAFT) by

Comprehensive C Programming Language Cheatsheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

C Progra­mming

Mid-Level (features of both low & high level), Structural (ability to break program into parts/­block), Procedural Oriented progra­mming language developed by Dennis Ritchie in 1972.

Hello World Program

#include<stdio.h>
int main()
{
    printf("Hello World");
    return(0);
}

Comments in C

Used for docume­nting code, Compiler ignores the comments in c program. Two Types:
1. Single Line Comments: Starts with double slash (//)
2. Multi Line Comments: Starts with /* & Ends with */

printf Function

Used to show output on the screen­/co­nsole.
printf­(fo­rma­t_s­tring, data_t­o_p­rint);
printf­("%s­", "­Pus­hpender Singh");
Format string can be %d (integer), %c (character)**
%s (string), %f (float), %lf (double)

scanf Function

Used to take command line input from the user. e.g.
Syntax:
scanf(­"­format string­", argume­nt_­list);
scanf(­"­%d", &num); // Take integer & store it in num variable

Data Types in C

A data type specifies the type of data that a variable
There are 4 Types of DataTypes:
1. Basic/­Fun­dam­ental: int, char, float, double
2. Derived: array, pointer, structure, union
3. Enumer­ation Data Type: enum
4. Void Data Type: void
 

Variables in C

Variable is a Name of Memory Location, Used to Store & Access Data
Syntax to declare variable:
DataType Variab­leName = Value;
e.g.
int rollNo = 69;

General Rules for Constr­ucting Variable Name

1. Variable can have alphabets, digits & underscore.
2. Can start with the alphabet & underscore only. It CANNOT start with a digit.
3. No whitespace is allowed within the variable name.
4. CANNOT be any Reserved word or Keyword e.g. int, float, etc.

Declaring Variable of Other DataTypes

// Create variables
int age = 35;            
float percentage = 94.86;   // 7 digit Decimal Precision
double pi_value = 3.14159265359;   // 15 Decimal Precision
char section = 'A';   

// String: Array of Characters
char name[] = "Pushpender Singh"; 
 
// Print variables
printf("Age: %d\n", age);
printf("Percentage: %f\n", percentage);
printf("Percentage (Only 1 Decimal): %.1f\n", percentage);
printf("Value of Pi: %lf\n", pi_value);
printf("Section: %c\n", char); 
printf("Name: %s\n", name);

Data Types

Data Type
Size
Descri­ption
int
2 or 4 bytes
e.g 20, 30, etc
float
4 bytes
Hold Upto 7 Decimal Digit
double
8 bytes
Hold Upto 15 Decimal Digit
char
1 byte
e.g. 'A', 'B', etc

Type Casting

// Implicit Casting (Automatic)
float  lol = 10;  // int to float
printf("%f", lol); // Output: 10.000000

int noice = 95.555  // float to int
printf("%d", noice); // Output: 95

// Explicit Casting (Manual)
float sum = (float) 5 / 2; //  int to float
printf("%f", sum); // 2.500000