Monday, 25 May 2020

Introduction to C Programming by Mubi

  • C-Programmjng language by Mubidon 



In Summary this tutorial helps you get started with C language:

History 
Features 
Application 


What is C language
By definition, C is a general-purpose and procedural programming language.

General-purpose – it means C language is designed for developing software that applies in a wide range of application domains.
Procedural – it means a C program is a set of functions. Each function performs a specific task. In the C program, functions are called in sequence to make the program works as designed.

It is the outcome of two programming languages B and BCPL . All three programming languages were developed at bell laboratory, USA. It is written by Dennis Ritchie was known as the father of C language Together with his colleague, Ken Thompson, they developed the UNIX operating system. Ritchie was a computer scientist who helped shape the digital era in 1970s.

C language key features
The main key features of C language are listed as follows:

Capable of operating very closed to the machine. In C you can manipulate data at a very low-level such as direct memory address.
Efficiency. C programs are smaller and run faster than programs written in other languages such as C++, Java or C#.
Portable source code. If you have a compiler for C in a specific platform, C program will operate properly on that platform. You can find compilers for almost platforms. In addition, the design of C language makes it easy to port existing programs to a new platform.
Recently many new languages have been invented such as C++, Java, C#, etc. However, C language is still a good choice among developers for system programming, especially for embedded software. The efficiency of C also allows programmers to develop robust and powerful software including database management systems and graphics software.
 C  is both
(1) System program and
(2) Application program development tool. 
It lies between high and lower level languages so it is also called as middle level language. The c character set consists of alphabets, digits ,and special symbols. There are 91 character in C. The C tokens are the basic  units of C Program and they are categorized into keywords , Identifies, constants, strings, operators and special Symbols. All the keywords  in C have one or more fixed meaning and they can not be changed . The keywords must be written in lowercase letters. The identifiers can not be changed. The identifiers are the names gives to the program elements such as variables, arrays and functions. All the variables must be declared for their types before they are used in the program. A constant is a quantity that does not change execution of a program , Each instruction end with character semicolon":" and comments can be placed anywhere. All the words in program line must be separated from each other by at least one space or tab, or punctuation. Every C  program requires a man ( ) function and its place is  where the program execution begins. The execution of a function begins at the opening brace of the function and ends at the corresponding closing brace. we must make sure to include header files using # include directive when the program refers to special names and functions that it does not  define.

Compiler Directive 
Compiler directive such as define and include are special instruction to the compiler to help it to complier program. They do not end with semicolon . The sign # of compiler directives must appear in the first column of the line. when the braces are used to group statements , make sure that the opening brace has a corresponding closing brace. To read data from the input device input statements are used and to print the processed data onto the output device outputs statements are used. These input/output (I/O) statements can be formatted or unformatted. In unformatted I/O statements no specification of data types and the manner in which they are read in or written out is mentioned. But formatted I/O statements specify what type of data is being input or displayed and in what way it should be. scanf ( ) and print ( ) are the two in - built functions use to read and print the data. They are used for all types of data like int , char, float, double , etc. In general, it is not possible to read multi- word string with scanf ( ) function. Using escape sequences , we can format the output as per our requirements.



Data type is a set of values along with set of permissible operations. Simple data types consists of integers ( short , int , long), real number ( float , double ) and characters ( char). Every data item to be used in program needs to be declared/ initialized. The backslash (\) character constant performs a specific task. They begin with a backslash (\) Both the backslash and an associated character are enclosed within single quotes.

(The size and range of each data type is given in the table below )



There are four data types' modifiers like signed, unsigned ,  short , long . These are used to provide the accuracy and precision to the integer and character type data . 
Note : % * C specification is used to read the new line (\n), which is obtained by pressing the enter key and enter key is used to enter the character to reach at the new line.




C Operators are basically classified into unary, binary and ternary operators . There are five unary operators, four types of binary operators and one ternary operators. Where unary, !, ++, - and- binary operators. The +,-,*,/ are basic arithmetic operators. Modulus operators is another arithmetic operator which results in the remainder after an integer division. The <, <=,>,=>.= = . ! = are relational operators. These are used to define the relation between constants and variables. 

C language supports variety of assignment statements. There is precedence of operators that determines the evaluation of expressions. 



We have seen in this chapter, there are three types of situations that demand the change in the flow of the control. There are :
1) Skipping some segments of the program or going back to execute the same segment again.
2) Executing one segment out of the two or more segments depending on the outcome of the some condition.
3) Executing a segment repeatedly either for a given number of times or till some conditions are met .

For these situation, C has four types of statements: declaration statement, input- output statements, arithmetic, logical statements and control statements.

The control statement defines  the order of execution of the program statement .
There are three types of control statements are if , if else, if- else-if and switch statement.

These are called selective  or decision marking control statements. The goto is unconditional control statement which transfer the control to specified statement in a program



In the above program the if statement checks whether the given number is less than 0 or not. If it is less than zero then the number is negative, therefore the condition becomes true then the statement ," the number is negative" is executed. if the number is not less than zero,the if else construct skips the first statement and prints the second statement prints the second statement declaring that " the number
is positive".
Looping is also called an iterative or repetitive control mechanism, where int set of statements are repeatedly either for a fixed number of times or as long as a certain logical condition is true.

The while, do - while and for loop statements are the three powerful loop control structures available 
in C. The while statement is a pre-test loop statement ( It is executed if only the expression in true) and the do- while is a post- test loop statement ( It is executed at least once. The the expression evaluation for repeating the loop) The " for" loop is used when the user knows the number of iteration to be made . Sometimes, it becomes desirable to exit a loop or skip a part of the loop in an iterative process. To do so, the break and continue statements are used.



Array
An array is a data structure used to store collections of similar values under a given name. They are also called subscripted variables. Elements are accessed by their name of the array followed by the subscript number within brackets.

Like variable, array ( s ) must also be declared before they are used.



Where data type is a valid type ( like int, float...), name is a valid identifier and the elements field ( which is always enclosed in square brackets[ ] ), specifies how many of these elements the array has to contain. 
Entering Data into an Array 
Since array represents a collection of values, so to input data into the individual elements of the array, we make use of loop statements. If the actual size of the array is known in advance, then for loop is preferred to while loop or do - while loop.
Accessing the Values of an Array
In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable. thus being able to both read and modify its value. The format is as simple as ;

Image 


There are two types of arrays:- (1) one dimension and (2) multi dimensional arrays

One pair of square brackets is used for each (1) one dimensional array dimension. For example one dimensional away arrays has only one subscript, and it is viewed as row matrix or column matrix,

But, multidimensional arrays have more than one subscript, hence the name two - dimensional array three dimensional arrays and so on. C dose not support strings data types but strings are handled as an array of characters.

There is rich library of functions for manipulating strings.
Declaration of Two Dimensional Arrays 

In C language, the syntax used to define 2- D or Dim array is ;

Image 


Where the Row - represents the no. of elements processed in the subscript 1
and the column - represents the no. of elements processed in the subscript 2.
Example : Program to sum elements separately with odd & even subscript 

#include<stdio.h>
main( )
{
int x[5][5]={{15,6,7,28,12},{50,5,4,3,65},{34,32,21,29,31},{20,49,18,7,35},{6,4,19,7,6}};
int sum1=0,sum2=0,m,n;
for(m=0;<5;++m)
{
for(n=0;n<5;++n)
{
if(m%2 ==0)
sum1=sum1+x[m][n];
else
sum2=sum2+x[m][n];
}
}
for(m=0;m<5;++m)
{
for(n=0;n<5;++n)
printf("%5d",x[m][n]);
pritnf("\n");
}
printf("Total of elements with even first sub

Is learning C difficult?
The answer is NO. C language is not difficult to learn at all. And once you master C language, you can write so many system software

We hope that you can get the most out of our C tutorials to master C by yourself with ease and fun. Happy programming! 
Thanks for reading my Article by Muhammed Mubarak Yusuf student  from faculty  of Management science Atbu Bauchi. 


No comments:

Post a Comment