Warning: session_start(): open(/tmp/sess_90e06cca21fe6d7cdfe6930198d97bf9, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
C Tutorials - LearnShareIT

C Tutorials

Learning C programming is the most basic step for you to approach embedded programming, or it can also be a stepping stone to learn later other languages such as C#, JAVA, Python, JS…

In this tutorial we will give you the knowledge about the C programming language that learners need to be firmly rooted in.

What is C 

C is considered a powerful and popular programming language around the world. C has an independent structure and is applied on many different programs or operating systems such as Windows, Git, …

In addition, the C programming language is considered the basis and foundation for other languages, and if the programmer learns C programming well, other languages such as C++, Java can be easily conquered.

The structure of the program C

A C program consists of the following parts:

  • Preprocessor commands:
  • Functions
  • Variables
  • Commands and expressions
  • Comments

General structure:

#include <stdio.h>
int main(void){
   //Processing statements
   return 0;
}

Rules for writing programs

  • End the statement with an exclamation point ;
  • Use indents (spaces at the beginning of statements) as well as line breaks to make the program structure easier to understand.
  • Use the // character for single-line comments, and /* and */ for multi-line comments.
  • Comments have the effect of making it easier for people to read C source code. 
  • Including the main() function, programs in C are made up of functions.
  • The function starts with { and ends with }.

Variable in C

In the C programming language, a variable is seen as a place to store data and is given a specific name. Each variable will be assigned a certain data type or scope and it can be changed. The name of the variable is named according to the rules so that the variable can be easily identified.

Type of  variables

TypeDescription
charIs an integer variable, 1 byte in size.
intis the type for the natural number.
floatSingle-precision floating-point value.
doubleDouble-precision floating-point value.
voidRepresents a type without a type.

Declare variable

type variableName = value;

  • type: is the Type of variable like int, ..
  • variableName: name of variable according to variable naming rules in C

For example:

int myNum = 22;

After declaring a variable in C, we can assign a value to the variable using the ‘=’ operator. A variable can only be assigned after it has been declared.

If statement in C

The if statement in C consists of the if keyword, a condition, and a block of statements described in that block. The block in if is enclosed between curly braces {} to indicate the start and end of the block. The statements described in the block are processed only when the specified conditional expression is True.

We use the if statement in C with the following syntax:

if (condition) {
   // If the condition is true the block of code is executed
}

If the condition is True, the statements described in the block will be executed in order.

For example:

if (22 > 15) {
   printf("22 is greater than 15");
}
// the output is "22 is greater than 15"

if…. else statement

In case you want to handle the if statement in C when the conditional expression is False, use the if… else statement in C with the following syntax:

if (condition) {
   // If the condition is true the block of code is executed
} else {
   // If the condition is false the block of code is executed
}

For example:

int time = 24;
if (time < 18) {
   printf("Have a nice day!");
} else {
   printf("Good evening.");
}
// Output "Good evening."

For loop in C

‘For’ loop in C is used when you know the exact number of times to loop.

Syntax:

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Explain:

Statement 1 is the first expression to be executed when the for statement is executed. It is mainly used to initialize counter variables used in conditional expressions.

Statement 2  is an expression used to decide whether to execute the statements described inside the for block. If the conditional expression evaluates to true, then the statements described inside the for block enclosed between {} will be executed in top-down order. And if false, the iteration is terminated.

Statement 3 is an expression that transforms the value of a counter variable. After each time the loop is executed, the counter variable will be changed through the statement 3 resulting in the statement 2 result also changing. And the statement 2  will be re-evaluated, to decide whether to continue or terminate the loop.

For example:

#include <stdio.h>

int main(void){
    for (int i = 1; i < 5; i = i + 2) {
        printf("test %d\n", i);
    }
    printf("Done");
    return 0;
}

Output:

test 1
test 3
Done

Explain:

Statement 1: int i = 1 has the effect of creating a variable i to count the loop. In this example we specify the initial value of the variable i to be 1.

Statement 2: i < 5 has the effect of specifying the condition to execute the loop. If the conditional expression is TRUE then the loop is run and if FALSE the loop is terminated.

Statement 3: i = i + 2 has the effect of changing the variable i after each iteration is performed. Here i = i + 2 means an increase of 2 units into the variable i.

Function in C

A function in C is a set of operations to perform a specific function in the program. Functions allow you to combine different handlers into one and give it a name. After creating and naming a function, we just need to call the function name every time we need to use it in the program.

The function structure consists of 3 main components: parameters, arguments and return values.

Syntax to initialize a Function in C:

void myFunction() { // declaration
   // the body of the function (definition)
}

For example:

#include <stdio.h>
// Create a function
void myFunction() {
   printf("Hello This is LearnShareIT");
}
int main() {
   myFunction(); // call the function
   return 0;
}

Output:

Hello This is LearnShareIT

Tutorials on C

Summary

In the above tutorial, we have outlined the important knowledge when you start learning about C programming language. Mastering the basics will help you quickly understand that programming language. If you are interested in other programming languages, visit LearnShareIT‘s website to learn them. Thanks for reading!

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *