C Programming Language

 

What is Programming?

Programming is, quite literally, all around us. From the take-out we order, to the movies we stream, code enables everyday actions in our lives. Tech companies are no longer recognizable as just software companies — instead, they bring food to our door, help us get a taxi.

In other words

Programming is giving a set of instructions to a computer to execute. If you’ve ever cooked using a recipe before, you can think of yourself as the computer and the recipe’s author as a programmer. The recipe author provides you with a set of instructions that you read and then follow. The more complex the instructions, the more complex the result!

Hardware Component

 

A computer's hardware consists of its physical parts, including its internal pieces and connected external devices. Hardware components perform a computer's tasks like calculating data, storing information, processing input, and providing output. Any part of a computer that you can physically touch is hardware.

Software

Software is a catch-all term for the programs and applications that run on a computer. Software programs act as the instructions that tell a computer's physical components, known as its hardware, what to do. It is made by software developers, who write the underlying source code in one of many programming languages before compiling software into executable files that a computer can understand.

There are Three main categories of software

01: System Software -> like Operating system

02: Application Software -> Vscode ,WhatsApp etc

03: Utility software -> Billing software,tracking software etc.

 

Happy Coding😊

History of C


C is a general-purpose compiled programming language. It was first developed by

Dennis Ritchie at AT & T Bell Laboratories in USA in the late ‘60s and early ‘70s. The C language is widely used for all

kinds of programming: everything from general-purpose applications, programming

language tools and compilers – even operating systems. The C language is also widely used for programming hardware devices.


                                                                                  A C compiler (and an associated tool called a ‘linker’) is the program that translates your source code (the text you write in an editor) into machine code that is capable of being run by your operating system. C compilers are available for all major operating systems including Windows,Mac,and Linux.

Characters

Alphabet

  • UPPERCASE

A-Z

  • LOWERCASE

a-z

Digits

0-9

Special symbols

@ # <> / \ { } [ ] etc.

Assembly Language

Use mnemonics and symbolic representation

 

 

 

 

Machine Language

Consists of binary code, Using 0s and 1s

 

Interpreter and Compiler

Q 1: Where we can write our C code
Text Editor/IDE (Integrated development environment)
Notepad
Notepad++
Sublime
Atom
etc.

VsCode
TurboC
DevC++

Q 2: Where we can execute/Run C code
CMD
PowerShell
Terminal

How to create a C File

 

01: Create a specific folder

02: Open that folder on Text Editor/IDE

03: Create a File like Filename.c (.c-> extension)

 

 

Comment in C

Comment are use to improve code readability or its make code readable
Inside the comment all expressions are ignored by compiler or interpreter

There are two ways to represent comment in C
01: Single line comment
syntax:
// comment here......
02: Multi-line comment
syntax:
/*.......................
............comment here....
..............................*/

 

 

Token in C

 

Tokens are the smallest unit/Main component of any programming language

Constant
identifier
keywords/reserved word
operators
string
character

 

Input & output methods

For output -> printf("format specifier with string",variablename);

For input ->  scanf("format-specifier",&variable);

 

 

Variable in C

 

Variable is just a memory allocation/address where we are assign/store the different types of data

Syntax to declare a variable

datatype variableName = value;

Rules to write a variable name

01: We cannot start with number or special symbol to write a variable name instead of underscore_ dollar sign $ and alphabet

02: We cannot use special symbols or white-space in between the variable name instead of number underscore alphabet

03: We can Not use reserved/keywords to write a variable name

04: We can write a variable name as camelCase or snack_case

 

Data-type in C

 

Data Types or Types are attributes that are predefined or can be created by the user so that the program can easily detect the different types of information. This is useful because Computers can understand only Binary Language i.e. 0’s and 1’s. To perform other basic and complex math operations, you need Data Types that will be understandable in computer language

 

Data-types          Size Format-specifier
int 4 bytes %d %i
float 4 bytes %f
char 1 bytes %c
double 8 bytes %lf

Operator in C


An operator in C can be defined as the symbol that helps us to perform some specific mathematical, relational, bitwise, conditional, or logical computations on values and variables

Unary Operator
01: Increment
a:) pre-increment ++a
b:) post-increment a++
02: Decrement
a:) pre-decrement --a
b:) post-decrement a--



Binary Operator
Arithmetic
+ - * / %
Assignment
= += -= *= /=
Relational/Comparison
== != < > <= >=

 

Logical Operator


a:) Logical AND (&&)
b:) Logical OR (||)
c:) Logical NOT !(condition)
Ternary Operator
condition? "First expression" : "last expression"

 

01: Simple conditional statement
syntax:

if(condition){
// block of codes
}
else{
//block of codes
}

 

 

 

 

02: Ladders conditional statement
syntax:

if(condition){
//block of code
}
else if(condition){
//block of code
}
else if(condition){
//block of code
}
else{
//block of code
}

Conditional statement

03: Nested conditional  statement

syntax:

if(condition){
                if(condition){

}
else{

}
}
else{

}

Switch case break default

Syntax:

switch(expression){
case matchIndex:
//block of code
break;
case matchIndex:
//block of code
break;
default:
//block of code

}

 

01: for loop
syntax:
for(initialization; condition; operation){
// block of codes
}

02: while loop
syntax:

initialization;
while(condition){
//block of code
operation;
}

 

 

03: do-while loop

syntax:
initialization;
do{
//block of code
operation;
}while(condition);

Control statement (loop/iterators)
There are three types of control statement in C

 

 

    for(int i =1; i<=10; i++){

        if(i==8){

            break;

        }

        else{

            printf("%d",i);

        }

    }

   for(int i=1; i<=10; i++){

        if(i>=5 && i<=8){

             //printf("%d ",i);

                  continue;

        }

        else{

            printf("%d ",i);

        }

    }

Break and continue statement in C

Examples

Function in C

There are two types of Function in C

Inbuild function

like pow(),strcat(),printf(),scanf() etc

User-define function

syntax:

datatype functionName(){
//block of code
}

void main(){
//call the function
functionName()
}

 

Example:

#include <stdio.h>

 

void message();  // defining the function

 

void main(){

    message();

    message();  // calling the function

    message();

}

 

void message(){

    printf("Good morning! \n"); // declare the function

}

 

Syntax to declare an array

 int ages[length] = {data1,data2,data3,data5.......};

How can be access the particular data
syntax:
ages[index]

How can be update the array element
using re-assignment
ages[3] = data4;

How to get the length of an array
int arr[] = {a,b,c,d,e}

int length = sizeof(arr)/sizeof(arr[0])
printf("%d",length);

 


 

How to iterate an Array element

    for(int i=0; i<4; i++){
        printf("%d : %d\n",i,arr[i]);
    }

Get the data from an array in dynamic mode

 int len = sizeof(ages)/sizeof(ages[0]);
    for(int i=0; i<len; i++){
        printf("%d : %d\n",i,ages[i]);
    }

Multi-dimensional array

How to declare multi-dimensional array

syntax:
arrname[length][length] = {{},{}}
example:
   int arr[2][3] = {{23,32,43},{25,65,67}};

How to access an 2-D array data
arr[0][2] // 43 will be accessed

How to iterate an 2-D array Data
Example:
 for(int i =0; i<2; i++){
        for(int j =0; j<3;j++){
            printf("%d\n",arr[i][j]);
        }
        printf("\n");
    }

Pointer in C

In C programming language a pointer is a variable that stores the memory address of another variable

Declaration: Pointers are declared using (*) symbol
syntax: 

int *x;

Assigning Address: You can assign the address of variable to pointer using the address of operator(&)

syntax:
int num =10;
int *ptr = &num;
 

 

Note:  ptr holds the address of a num variable 

Dereferencing: To access the value stored at the address a pointer is pointing to, you use the dereference operation (*)
syntax:
int value = *ptr;
Note: Value now holds the value of num (which is 10)

Types of Pointer:

01: Integer pointer

02: Array Pointer

03: Function Pointer

04: Pointer to pointer

05: Contant pointer

C_Programming

By akash tiwari

C_Programming

  • 159