C programming language

  • Learning any other popular programming language such as Python or C++ becomes a cakewalk already if you know C.
  • C is a flexible language and that gets proven by the fact that it can be used in a variety of applications as well as technologies.
  • C is very fast when compared to other programming languages be it Java or Python.

HTML

Since the late 19th century, C has been a popular programming language for general-purpose use.⁣
Its applications are very diverse. It ranges from developing operating systems to databases and all.⁣
Even if it’s old, it is still a very popular programming language.⁣
As the whole UNIX operating system was written in C, it has a strong association with the operating system⁣ C has also been used widely while creating iOS and Android kernels.⁣
MySQL database is written using C.⁣
Ruby and Pearl are mostly written using C.⁣
Most part of Apache and NGINX is written using C.⁣
Embedded Systems are created using C⁣

  1. starter template
    Basic Structure & Syntax

    Programming in C involves following a basic structure throughout. Heres what it can be broken down to.

    • Pre-processor commands
    • Functions
    • Variables
    • Statements
    • Expressions
    • Comments

    Pre-processor commands are commands which tell our program that before its execution, it must include the file name mentioned in it because we are using some of the commands or codes from this file.
    #include<filename.h> is how we include them into our programs.
    They add functionalities to a program.

    • Keywords
    • Identifiers
    • Constants
    • String Literal
    • Symbols

    Keywords are reserved words that can not be used elsewhere in the program for naming a variable or a function. They have a specific function or task and they are solely used for that. Their functionalities are pre-defined.
    One such example of a keyword could be return which is used to build return statements for functions. Other examples are auto, if, default, etc.

    Identifiers are names given to variables or functions to differentiate them from one another. Their definitions are solely based on our choice but there are a few rules that we have to follow while naming identifiers. One such rule says that the name can not contain special symbols such as @, -, *, <, etc.
    C is a case-sensitive language so an identifier containing a capital letter and another one containing a small letter in the same place will be different. For example, the three words: Code, code, and cOde can be used as three different identifiers.

    Constants are very similar to a variable and they can also be of any data type. The only difference between a constant and a variable is that a constant’s value never changes.

    String literals or string constants are a sequence of characters enclosed in double quotation marks. For example, “This is a string literal!” is a string literal. C method printf() utilizes the same to format the output.

    Symbols are special characters reserved to perform certain actions. Using them lets the compiler know what specific tasks should be performed on the given data. Several examples of symbols are arithmetical operators such as +, *, or bitwise operators such as ^, &.
                               
                        #include <stdio.h>
                        int main()
                        {
                            return 0;
                        }
                
  2. C Comments
    C Comments
    Comments can be used to insert any informative piece which a programmer does not wish to be executed. It could be either to explain a piece of code or to make it more readable. In addition, it can be used to prevent the execution of alternative code when the process of debugging is done.
    Comments can be singled-lined or multi-lined.

    Single-line comments start with two forward slashes (//).
    Any information after the slashes // lying on the same line would be ignored (will not be executed).

    A multi-line comment starts with /* and ends with */.
    Any information between /* and */ will be ignored by the compiler.

    Single Line Comments
                      //This is a single line comment
                

    Multi-line Comments
                  /* This is a
        multi-line
        comment */
            
  3. C Variables
    C Variables
    Variables are containers for storing data values.
    In C, there are different types of variables. For example
    an integer variable defined with the keyword int stores integers (whole numbers), without decimals, such as 91 or -13.
    a floating point variable defined with keyword float stores floating point numbers, with decimals, such as 99.98 or -1.23.
    a character variable defined with the keyword char stores single characters, such as 'A' or 'z'. Char values are bound to be surrounded by single quotes.
    Comments can be singled-lined or multi-lined.

    We cannot declare a variable without specifying its data type. The data type of a variable depends on what we want to store in the variable and how much space we want it to hold. The syntax for declaring a variable is simple:
    data_type variable_name;
    OR
    data_type variable_name = value;

    There is no limit to what we can call a variable. Yet there are specific rules we must follow while naming a variable:
    • A variable name can only contain alphabets, digits, and underscores(_).
    • A variable cannot start with a digit.
    • A variable cannot include any white space in its name.
    • The name should not be a reserved keyword or any special character.

    A variable, as its name is defined, can be altered, or its value can be changed, but the same is not true for its type. If a variable is of integer type, then it will only store an integer value through a program. We cannot assign a character type value to an integer variable. We can not even store a decimal value into an integer variable.
  4. C Data Types & Constants
    C Data Types & Constants
    As explained previously, a variable in C must be a specified data type, and you must use a format specifier inside the printf function to display it.
    The data type specifies the size and type of information the variable will store.

    Data Type

    Size

    Description

    Format Specifier

    int

    2 or 4 bytes

    Stores whole numbers, without decimals

    %d or %i

    float

    4 bytes

    Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits

    %f

    double

    8 bytes

    Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits

    %lf

    char

    1 byte

    Stores a single character/letter/number, or ASCII values

    %c

    • When you don't want the variables you declare to get modified intentionally or mistakenly in the later part of your program by you or others, you use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only).
    • You should always declare the variable as constant when you have values that are unlikely to change, like any mathematical constant as PI.
    • When you declare a constant variable, it must be assigned a value.

    A variable, as its name is defined, can be altered, or its value can be changed, but the same is not true for its type. If a variable is of integer type, then it will only store an integer value through a program. We cannot assign a character type value to an integer variable. We can not even store a decimal value into an integer variable.

    Data types
                      // Creating variables having different data types
                        int integer = 26;
                        float floating = 39.32;
                        char character = 'A';
                        // Printing variables with the help of their respective format specifiers
                        printf("%d\n", integer);
                        printf("%f\n", floating);
                        printf("%c\n", character);
                

    Constants
                 const int MOD = 10000007;
                   
            
  5. C Operators
    C Operators
    Special symbols that are used to perform actions or operations are known as operators. They could be both unary or binary.
    For example, the symbol asterisk (*) is used to perform multiplication in C so it is an operator and it is a binary operator.
    This section covers all types of operators

    Arithmetic operators are used to perform mathematical operations such as addition, subtraction, etc. A few of the simple arithmetic operators are

    Operator

    Description

    +

    Addition

    -

    Subtraction

    *

    Multiplication

    /

    Division

    %

    Modulus

    Relational operators are used for the comparison between two or more numbers or even expressions in cases. Same as Java, C also has six relational operators and their return value is of a Boolean type that is, either True or False (1 or 0).

    Operator

    Description

    >

    Greater than

    <

    Less than

    >=

    Greater than or equal to

    <=

    Less than or equal to

    ==

    Is equal to

    !=

    Is not equal to

    There are three logical operators i.e. AND, OR, and NOT. They can be used to compare Boolean values but are mostly used to compare conditions to see whether they are satisfying or not.
    • AND: it returns true when both operators are true or 1.
    • OR: it returns true when either operator is true or 1.
    • NOT: it is used to reverse the logical state of the operand.

    Operator

    Description

    &&

    AND Operator

    ||

    OR Operator

    !

    NOT Operator

    A bitwise operator is used to performing operations at the bit level. To obtain the results, they convert our input values into binary format and then process them using whatever operator they are being used with.

    Operator

    Description

    &

    Bitwise AND

    |

    Bitwise OR

    ^

    Bitwise XOR

    ~

    Bitwise Complement

    >>

    Shift Right Operator

    <<

    Shift Left Operator

    Assignment operators are used to assign values. We will use them in almost every program we develop.
    int a = 0;
    Equal to (=) is the assignment operator here. It is assigning 0 to a and 1 to b in the above example.

    Operator

    Description

    =

    It assigns the right side operand value to the left side operand.

    +=

    It adds the right operand to the left operand and assigns the result to the left operand.

    -=

    It subtracts the right operand from the left operand and assigns the result to the left operand.

    *=

    It multiplies the right operand with the left operand and assigns the result to the left operand.

    /=

    It divides the left operand with the right operand and assigns the result to the left operand.