C TUTORIAL

 

What is a programme?

A program tells a computer what to do using instructions in a programming language. There are two types of languages: low-level and high-level. Low-level ones are harder to understand, while high-level ones are easier. Programs have to be changed into machine code for the computer to follow them. We use programming languages for applications, games, and websites, which are important for how computers work and our digital world.

What is compilation?

When you write a program using a language computers don’t understand, you need to translate it. This is called compilation. The compiler does a few things: it reads your code and turns it into something the computer can read, checks your code for mistakes, and generates code the computer can run. The process happens in these steps: first, the compiler breaks your code into small parts, called tokens. Then, it organizes those tokens into a structure. Next, it makes sure your code is following the rules of the language and flags any problems. After that, it turns the code into instructions the computer can use. Finally, if everything is working, the computer reads those instructions and runs the program smoothly and without errors.

What is data type?

In programming, data types refer to the classification or categorization of data items based on their nature and characteristics. These data types are essential to maintain the integrity of the code and ensure that the program runs correctly.

The most common data types include:

1. Integer: for whole numbers (positive or negative)
2. Float: for decimal numbers (positive or negative)
3. Boolean: for true/false values
4. Character: for letters or symbols
5. String: for a sequence of characters
6. Array: for a collection of data items of the same type
7. Object: for a collection of related variables and functions
8. Null: for a value that is empty or non-existent.

The selection of data types depends on the purpose of the program and the specific needs of the application or system. Proper use and manipulation of data types are crucial in programming to avoid errors and unexpected results.

Identifier

Identifiers in programming are words or names that represent variables, functions, classes, or other programming entities. They are used to define and refer to objects within a program and give developers a way to describe and manipulate these objects quickly and efficiently.

In most programming languages, identifiers must follow certain rules. For example, they cannot start with a number, contain spaces or special characters, and cannot be the same as reserved words or keywords. Identifiers are case-sensitive meaning that lowercase and uppercase letters are treated differently, so “myVariable” and “myvariable” would be considered different identifiers.

Variables and Arrays

Variables are containers for storing values in a computer program, such as numbers or words. They can be changed or set during the program. Arrays are a type of variable that can store lots of values of the same kind, like a list. They can be useful for sorting or finding information in the data. To use them well is an important skill for programming.

Examples of variables include integers (whole numbers), floating-point numbers (decimal numbers), and strings (sequences of characters). For instance, a program that calculates the area of a rectangle might use variables to store the length and width of the rectangle. For example, an array could be used to store the scores of a class of students. Each element in the array would represent the score of a particular student, and the entire array could be sorted, filtered, or averaged as needed.

Constants

In programming, a constant refers to a value that cannot be modified once it is defined. This means that once a constant is assigned a value, it cannot be changed later on in the program. Constants are typically used to store values that do not need to be modified during the execution of a program, such as mathematical constants like pi, or constants used to represent fixed values such as maximum file size or a specific color value.

Constants are usually defined at the beginning of a program or within a specific block of code, and are often given descriptive names to make the code more readable and maintainable. By using constants, developers can ensure that certain values remain consistent throughout the program, reducing the likelihood of errors and making the code easier to maintain over time.

Definition of C programming language

The C programming language, invented by Dennis Ritchie in 1972 at AT&T (then called Bell Laboratory), is a general-purpose procedural language that allows for low-level access to the system memory. It supports structured programming and was first implemented in the UNIX system. 

Syntax

if Statement
if (test expression) 
{   
}
if…else Statement
if (test expression) {   
   } else {    
 }
 for loop Statement
for (test expression)
{     

}

 

Relational Operators

Relational operators are symbols used in programming languages to compare two values and determine whether they are equal, greater than, less than, or not equal. These operators are commonly used in conditionals and loops to make decisions or carry out tasks based on certain conditions. The commonly used relational operators are:

– == (equals to) – used to compare whether two values are equal
– != (not equals to) – used to compare whether two values are not equal
– > (greater than) – used to compare whether one value is greater than the other
– < (less than) – used to compare whether one value is less than the other
– >= (greater than or equal to) – used to compare whether one value is greater than or equal to the other
– <= (less than or equal to) – used to compare whether one value is less than or equal to the other

Relational operators are an essential part of programming and are used extensively in various applications.

Do While statement

To make a do-while loop, start with “do” and then add the code you want to repeat. Then, add “while” and the condition you want to check. If the condition is true, it will run the code again. If not, the loop stops. The do-while loop is helpful when you want to make sure a code block runs once, or when you need to repeat a block until a specific condition is met, even if it takes more than one run.

 

Nested Loop Statement

A nested loop is when a loop is placed inside another loop. The outer loop controls how many times the inner loop runs. This is useful when dealing with repeating tasks that are organized in a hierarchy or when you need to go through multiple data structures. Nested loops can also be used to go through arrays or matrices that have multiple dimensions. However, nested loops can use up a lot of resources and be slow when working with large data sets. So, it’s important to design and optimize nested loops carefully to avoid any problems with how they run.

A nested loop in C is a loop within another loop. These loops can be used to perform repetitive actions on multidimensional arrays, for example. In order to better understand the concept, consider the following example:

#include <stdio.h>

int main() {

    int i, j;

  for(i = 1; i <= 3; i++) {

        for(j = 1; j <= 5; j++) {

            printf(“i = %d, j = %d\n”, i, j);

        }

   }

  return 0;

}

In this example, we have two loops – an outer `for` loop that runs three times, and an inner `for` loop that runs five times for each iteration of the outer loop. As a result, the `printf()` statement inside the inner loop is executed a total of 15 times. This is because the inner loop will execute its entire sequence of events before the outer loop progresses. In this way, nested loops can be used to accomplish a wide variety of tasks in C programming.

Infinite Loop Statement

A An infinite loop is a situation that can happen when a program repeats over and over again without stopping. This might happen if the program doesn’t have a proper way to end, or if there is a mistake in the way it was written. When this happens, the program can get stuck and might cause problems like crashing or freezing. Usually, this is because of mistakes made by the person who wrote the program. Sometimes, it’s intentional, like when a program is supposed to keep watching something. But most of the time, this is not what’s supposed to happen and programmers have to fix it. They might do this by adding some new rules or making changes to the way it works. It’s important to test and fix any problems with a program before it can get stuck in an infinite loop.

Switch Statement

A switch statement is a part of programming that helps execute different parts of the code, based on a certain value or expression. The statement compares the value to a list of options, and then runs the instructions that go with that option. Switch statements are great when there are many different possible outcomes and each one needs different instructions. People often use them together with conditional statements to control programs better. Switch statements are quicker than many if-else statements, particularly when there are several different outcomes.

A switch statement is a control flow statement used in programming languages that allows a program to evaluate an expression and choose from multiple cases, each with a different set of instructions to execute based on the value of the expression. Here’s an example:

int day = 3;

switch (day) {

  case 1:

    printf(“Monday”);

    break;

  case 2:

    printf(“Tuesday”);

    break;

  case 3:

    printf(“Wednesday”);

    break;

  case 4:

    printf(“Thursday”);

    break;

  case 5:

    printf(“Friday”);

    break;

  default:

    printf(“Invalid input”);

}

In this example, the program evaluates the value of the variable `day` and executes the corresponding code block. Since the value of `day` is 3, the program will print “Wednesday” to the console. If `day` was not equal to any of the cases provided, the default case would execute and print “Invalid input”.

What is Function?

A function is like a set of instructions that does one thing. It gets information to work with and gives back a result. It helps break down big tasks into smaller parts, so it’s easier to work with. You can use a function lots of times in a program, which makes it faster and easier to write. It does lots of different things like maths, checking information, or helping with a database.Making and using functions is really important when writing code. It helps keep things running smoothly. Many programming languages come with lots of ready-made functions that you can use, but you can also make your own if you need to do something specific.

C Library Function

A C library function is a pre-written piece of code that can be used in C programming language. These functions offer developers a simple and efficient way to perform common tasks, such as managing memory, performing math calculations, and working with files. Some commonly used C library functions include printf() for outputting information to the console, malloc() and free() for managing memory dynamically, and fopen() and fclose() for managing files.C library functions are usually organized into header files, such as stdio.h, string.h, and math.h, which are included in the source code of a C program. By using pre-written library functions, developers can save time and avoid having to write complex code from scratch. Additionally, C library functions are often highly optimized for efficiency, making them a preferred choice for performance-critical applications.

Write a programme to display”This is my first c programme”.
#include <stdio.h>

int main()
{

   printf(“This is my first c programme”);

   return 0;

 

}

Write a programme to display positive or negative number.
#include <stdio.h>
int main() {
int number;

printf(“Enter an integer: “);
scanf(“%d”, &number);

if (number < 0) {
printf(“You entered %d.\n”, number);
}
else
printf(“This is a positive number”);

 

return 0;
}

Write a programme to reverse a number.

#include <stdio.h>

int main() {

int n, reverse = 0, remainder;  printf(“Enter an integer: “);

  scanf(“%d”, &n);

 while (n != 0) {

    remainder = n % 10;

    reverse = reverse * 10 + remainder;

    n /= 10;

  }

  printf(“Reversed number = %d”, reverse);

  return 0;

}

 

Types of Programming Language

 

Programming languages can be categorized into several types, depending on their functionality, logic, and application.

  •  Low-level languages:

    As you may know, low-level programming languages are quite different compared to high-level ones. They allow developers to have more control over hardware and memory management while optimizing the system’s performance. These languages include assembly language, which is designed to run on a specific processor architecture, and machine language, which consists of binary code that the computer can execute directly.

    Although low-level programming languages are useful in specialized applications, they can be difficult to write, maintain, and debug. Due to their complexity, it’s quite easy to introduce errors into code written in low-level languages. And when errors do occur, the process of finding and fixing them can consume a lot of time. Nonetheless, low-level programming languages are still relevant in scenarios where performance is of utmost importance, such as in device drivers, operating systems, and embedded systems.

  • High Level Language

    High-level languages are programming languages that allow programmers to write code in a more human-readable and understandable form than low-level languages. These languages often include many built-in functions and structures that simplify programming tasks and reduce the amount of code that must be written. Examples of high-level languages include Python, Java, and C++.

    High-level languages are generally easier to learn and use than low-level languages, as they abstract away many of the complexities of programming that are handled at a lower level. This abstraction also means that high-level languages are often slower and less efficient than low-level languages, as they rely on additional layers of interpretation and compilation to produce machine code.

    Despite these drawbacks, high-level languages are widely used in modern software development due to their ease of use, portability across different hardware systems, and ability to handle complex programming tasks with fewer lines of code than low-level languages.

  • Scripting Language: Scripting languages have become popular due to their flexible nature and easy-to-learn syntax. They have several benefits for developers, making them reliable for various applications such as web development, data processing, and system automation. Some popular scripting languages include Python, Ruby, JavaScript, Perl, and PHP. Integrating these languages with other tools expands their functionalities and leads to better workflow for developers. Utilizing scripting languages can boost productivity, making it possible for developers to accomplish development goals in less time.

Scroll to Top