Skip to content

Commit

Permalink
Add C (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
RameshKumarGogoi authored Oct 30, 2022
1 parent e23df71 commit c4469a1
Show file tree
Hide file tree
Showing 16 changed files with 1,754 additions and 0 deletions.
103 changes: 103 additions & 0 deletions c/arrays/arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index.

Index starts from 0 to size-1.

Arrays can be one-dimensional or multi-dimensional in C language. The more popular and frequently used arrays are one-dimensional and two-dimensional arrays.

Arrays store the collection of data sequentially in memory and they must share same data type.

# How to declare an array?

### One dimentional Array:

```c
data-type array-name[size];
```

### Two dimensional array:

```c
data-type array-name[size][size];
```

## Examples

### One dimentional Array:

```c
int a[5];
```

### Two dimentional Array:

```c
int a[2][3];
int b[][3]; // is also valid
```

# How to initialize arrays

### One dimentional Array:

```c
int a[5] = {1,2,3,4,5};
```
### Two dimentional Array:
```c
int a[2][3] = {
{1,2,3},
{4,5,6}
};
```

# How to access array elements

Array elements can be accessed by using indices. Array indices starts from `0` and `Array[n-1]` can be used to access nth element of an array.

## Examples

### One dimentional array:

```c
#include <stdio.h>
int main()
{
int arr[5] = {1,2,3,4,5};
int n = sizeof(arr)/sizeof(arr[0]); // gives length of an array
printf("Length of the array:%d", n);
printf("\nfirst element: %d", arr[0]); // prints first element of the array
printf("\nlast element: %d", arr[n-1]); // prints last element of the array
}
```
### Check result [here](https://onecompiler.com/c/3vku4yuj5)

### Two dimentional array:

```c
#include <stdio.h>
int main()
{
int a[2][3] = {
{1,2,3},
{4,5,6}
};
for(int i=0; i<2; i++) // iterates for each row
{
for(int j=0; j<3; j++) // iterates for each column
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
```
### Check result [here](https://onecompiler.com/c/3vkud9xj4)

## Summary

* Array is a collection of homogeneous data.
* Arrays stores data sequentially in memory.
* Arrays are finite.

87 changes: 87 additions & 0 deletions c/basics/data-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
As the name suggests, data-type specifies the type of the data present in the variable. Variables must be declared with a data-type.

There are four different types of Data types in C.

| Types | Data-type|
|----|----|
|Basic | int, char, float, double|
|Derived | array, pointer, structure, union|
|Enumeration | enum|
|Void | void|


## 1. Basic data types

Basic data types are generally arithmetic types which are based on integer and float data types. They support both signed and unsigned values. Below are some of the oftenly used data types

| Data type | Description | Range | Memory Size| Format specifier|
|----|----|----|----|----|
| int| used to store whole numbers|-32,768 to 32,767|2 bytes| %d|
|short int| used to store whole numbers|-32,768 to 32,767| 2 bytes|%hd|
|long int| used to store whole numbers| -2,147,483,648 to 2,147,483,647| 4 bytes|%li|
|float| used to store fractional numbers|6 to 7 decimal digits| 4 bytes|%f|
|double| used to store fractional numbers|15 decimal digits| 8 bytes|%lf|
|char|used to store a single character|one character|1 bytes|%c|

### Examples

```c
#include <stdio.h>
#include <float.h>

int main()
{
int x = 90;
int y = sizeof(x);
printf("size of x is: %d " , y);

float f = 3.14;
printf("\nsize of float is: %d " , sizeof(f));

double d = 2.25507e-308;
printf("\nsize of double is: %d " , sizeof(d));

char c = 'a';
printf("\nsize of char is: %d " , sizeof(c));
}
```
### Check result [here](https://onecompiler.com/c/3vkf2hsrg)

## 2. Derived Data types

Derived Data types are the ones which are derived from fundamental data types. Arrays, Pointers, Structures, etc. are examples of derived data types. Let's learn more about them in next chapters.

## 3. Enumeration Data types

Enumeration Data type is a user-defined data type in C. `enum` keyword is used to declare a new enumeration types in C.

### Syntax

```c
enum name{constant1, constant2, constant3, ....... };
```
### Example
```c
#include<stdio.h>
enum month{January, February, March, April, May, June, July, August, September, October, November, December};
int main()
{
enum month name;
name = June;
printf("%d",name);
return 0;
}
```
### Check result [here](https://onecompiler.com/c/3vkf3vuuu)

## 4. Void Data types

Void specifies that there is no return value. Generally void is used in the below situations.

* If the funtion has return type mentioned as Void, then it specifies that the function returns no value.
* A function with out any parameters can accept void. For example., char greetings(void)
* A pointer with type specified as void represents the address of an object but not it's type. Let's learn more about pointers in next chapters.

104 changes: 104 additions & 0 deletions c/basics/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system.

It's one of the simple and flexible programming language and infact almost all of us would have started our programming language's learning with C.

## Key Features

* Structured programming
* Popular system programming language
* UNIX, MySQL are completely written in C.
* Supports variety of platforms
* Efficient and also handle low-level activities.
* As fast as assembly language and hence used as system development language.

## Why you should learn C?

* C is considered as the best foundational language as you can understand the fundamental computer theories well.
* Easy to learn and structured programming language.
* You will get opportunities to work on open source projects.
* C is a middle level programming language means you can write system level programs as well as application level programs.
* You will also get used to programming discipline as C is a strongly typed language.

## Why C is popular?

* C is used to write compilers, interpreters, editors, operating systems and embedded programs.
* C is second popular language according to [Tiobe Index](https://www.tiobe.com/tiobe-index/)
* Most of the operating system kernels are written in C
* Many popular compilers and interpreters are written in C. For example, python interpreter is written in C.
* Not only compilers, interpreters, C is also used to write modern browsers, game engines etc.
* C is very good for embedded programming.

# C Basics

## Sample C program

```c
#include <stdio.h>
int main(){
printf("Hello World!!");
return 0;
}
```
### Run your program [here](https://onecompiler.com/c)

Let's look into each line of the above sample program:

* `#include` is a keyword which is used to include the library file `<stdio.h>`.
* `<stdio.h>` library file is used to read the data from terminal and to display the data on terminal. It has several in-built functions like printf(), scanf() etc.
* `main()` function is the entry point of any C program.
* `printf and scanf` are inbuilt library functions which are used for input and output in C language. They are defined in `stdio.h` header file.
* `return 0` is used to terminate the main() function and returns the value 0

## How to compile and run a C program

You can either use the shortcut `ctrl+F9` to compile and run or Go to Compile menu and then click on compile and then run.


## Comments

1. Single line comments

`//` is used to comment a single line

2. Multi line comments

`/**/` is used to comment multiple lines.


## Installation

## 1. on MacOS

* Install [Xcode](https://developer.apple.com/xcode/) from Appstore.
* Once installed, accept the terms and conditions and provide your MAC password.
* Now, components will get installed.

## 2. on Windows

* There are many C compilers available in the market which are free to use.
* Download C compiler from [VS-C-andC++](https://visualstudio.microsoft.com/vs/features/cplusplus/)
* Run the executable file.

## 3. on Linux

* Most Linux operating systems are pre-installed with GCC.

```shell
gcc -version
```
* For Ubuntu and Debian, use the below command

```shell
sudo apt-get update
sudo apt-get install build-essential manpages-dev
```
* For redhat, use the below command

```shell
yum groupinstall 'Development Tools'
```

## 4. Using Onecompiler

* You don't need to install any software or compiler.
* Just goto [OneCompiler](https://onecompiler.com/) and choose the programming language as C and enjoy programming without any installation.
Loading

0 comments on commit c4469a1

Please sign in to comment.