C Article
After All These Years, the World is Still Powered by C Programming
Despite the prevalence of higher-level languages, the C programming language continuesto empower the world. There are plenty of reasons to believe that C programming will remainactive for a long time. Here are some reasons that C is unbeatable, and almost mandatory, for certain applications.
C Programming Language Still Used
There are many programming languages, today, that allow developers to be more productive than with C for different
kinds of projects.
There are higher level languages that provide much larger built-in libraries that simplify working with JSON, XML, UI, web pages, client requests, database connections, media manipulation, and so on.
But despite
that, there are plenty of reasons to believe that C programming will remain active
for a long time.In programming languages one size does not fit all. Here are some reasons that C is unbeatable, and almost mandatory,for certain applications.
Portability and Efficiency
C is almost a portable assembly language. It is as close to the machine as possible while it is almost universally available forexisting processor architectures.There is at least one C compiler for almost every existent architecture.
And
nowadays, because of highly optimized binaries generated by modern compilers, it’s not an easy task to improve on their output with hand written assembly. Such is its portability and efficiency that “compilers, libraries, and interpreters of
other programming languages are often implemented in C”.
Interpreted languages like Python, Ruby, and PHP have their primary implementations written in C. It is even used by compilers for other languages to communicate with the machine. For example, C is the intermediate language underlying Eiffel
and Forth.
This means that, instead of generating machine code for every architecture to be supported, compilers for those languages just generate intermediate C code,and the C compiler handles the machine code generation.
"C is a great language for expressing common ideas in programming in a way that most people are comfortable with.
Moreover, a lot of the principles
used in C – for instance, argc and argv for command line parameters, as well as loop constructs and variable types – will show up in a lot of other languages
you learn so you’ll be able to talk to people even if they don’t know C in a way that’s common to both of you."
Memory Manipulation
Arbitrary memory address access and pointer arithmetic is an important feature that makes C a perfect fit for system programming (operating systems and embedded systems).
At the hardware/software boundary, computer systems and microcontrollers map their peripherals and I/O pins into memory addresses.
System applications must read and write
to those custom memory locations to communicate with the world. So C’s ability to manipulate arbitrary memory addresses is imperative for system programming.
Syntax:
1)const data_type var_name = var_value;
2)static data_type var_name = var_value;
3)extern data_type var_name = var_value;
4)static data_type var_name = var_value
Data Types in C
-
char:
The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. -
int:
As the name suggests, an int variable is used to store an integer. - float:
It is used to store decimal numbers (numbers with floating point value) with single precision. -
double:
It is used to store decimal numbers (numbers with floating point value) with double precision.
Operators in C
Operators are the foundation of any programming language. Thus the functionality of C language is incomplete without the use of operators.Operators allow us to perform different kinds of operations on operands. In C, operators in Can be categorized in followingcategories:
- Arithmetic Operators (+, -, *, /, %, post-increment, pre-increment, post-decrement, pre-decrement)
- Relational Operators (==, !=, >, <,>= & <=) Logical Operators (&&, || and !
- Bitwise Operators (&, |, ^, ~, >> and <<).
- Assignment Operators (=, +=, -=, *=, etc)
- Other Operators (conditional, comma, sizeof, address, redirection).
Local variables in C
The local variable is a variable that is declared within a function, block (within curly braces), or function argument. Consider the below program:-
void test(int x, int y)
{
int a;
}
In the above program, a, x and y are local variables. They only accessible within the test function when control comes out from the function then they will destroy automatically.If you will create a variable within the function and create the same name variable within the block (inside curly braces) in the same function, then both variables would be different. As soon as the block ends (curly braces) the variable which declared inside the block is destroyed. See the below code:
#include stdio.h
int main(int argc, char *argv[])
{
int a = 5;
{
int a = 10;
printf("value of a = %d\n",a);
}
printf("value of a = %d\n",a);
return 0;
}
Output:
Value of a = 10
Value of a = 5
Static Variable in C
A static variable preserves its previous value and it is initialized at compilation time when memory is allocated. If we do not initialize the static variable, then it’s the responsibility of the compiler to initialize it with zero value. Syntax: static Data_type Variable_name; Let see the below program, in which I have created static variables.
#include stdio.h
// Uninitialized global variable stored in BSS
static int data1;
//Initialized static variable stored in DS
static int data2 = 0;
int main(void)
{
// Uninitialized static variable stored in BSS
static int data3;
//Initialized static variable stored in DS
static int data4 = 0;
//Printing the value
printf("data1 = %d\n",data1);
printf("data2 = %d\n",data2);
printf("data3 = %d\n",data3);
printf("data4 = %d\n",data4);
return 0;
}
Output:
data1 = 0
data2 = 0
data3 = 0
data4 = 0
Global Variable in C
Variables which declared outside the function are called global variables. A global variable is not limited to any function or file it can be accessed by any function or outside of the file. If you have not initialized the global variables, then it automatically initialized to 0 at the time of declaration. See the below program, the data variable is a global variable.
#include stdio.h
int data; // global variable
int main()
{
printf("data = %d\n", data);
return 0;
}
Output:
data = 0
Arrays in C
An array in C be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array.They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C/C++ can store derived data types such as the structures, pointers etc. Given below is the picture representation of an array.
Need arrays
We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.
Functions in C
A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can call the function. The general form of a function is: return_type function_name([ arg1_type arg1_name, ... ]) { code }
Pointers in C
Pointers store address of variables or a memory location.Pointer Expressions and Pointer Arithmetic A limited set of arithmetic operations can be performed on pointers. A pointer may be:
incremented ( ++ )
decremented ( — )
an integer may be added to a pointer ( + or += )
an integer may be subtracted from a pointer ( – or -= )
Pointer arithmetic is meaningless unless performed on an array.
Note : Pointers contain addresses. Adding two addresses makes no sense, because there is no idea what it would point to. Subtracting two addresses lets you compute the offset between these two addresses.
FOR MORE INFORMATION "C" LANGUAGE GO TO THE VIDEO SECTION