C Programming Language

C Programming Languages

C programming is a middle-level structured programming language close to low-level and high-level languages. The C language was developed in 1970’s at Bell laboratories by the Dennis Ritchie.
Three features of C programming are:
• It is a structured programming language.
• It has a rich and powerful set of operators.
• It allows manipulation of internal processor registers.

Advantages of C Language:

  • It is compact and efficient to use.
  • It is easy to interact with hardware.
  • Adding a new feature is easier and faster.
  • Debugging in a program is easy.

Disadvantage of C Language:

  • As the program extends, it is very difficult to fix the bugs.
  • It does not support Object-oriented Programming features.
  • It does not contain runtime checking.
  • The program takes more time to design and implement the software.

The important elements of C programming are:

Preprocessor directive
The line that starts with # is the preprocessor directive. It filters the statement before it is compiled. It contains the header files or library, which includes a declaration for operation in c programming.

Functions
An independent section of program code that performs a certain task and has been assigned a name is called a function. There are two types of functions in c programming. Library functions are part of the C compiler package, whereas user-defined functions are created by a programmer.

Statements
Statements are lines of code enclosed with a semicolon (;). The collection of statements forms a program that can use to solve a problem.

Comments
The code written between (/*——–*/) are comment. Comments are usually written to document the program and make it easier for further use.

C tokens
Tokens are the text in source code that the compiler doesn’t break down into component elements. Some of them are:

1. Identifier
The identifier is the name issued to various elements like functions, variables, constants, etc to identify them uniquely.

2. Keywords
C keywords are the words that have special meaning for the compiler. Examples are auto, const, break, case, char, int, etc.

3. Variable and Constant
A variable is a named location in the memory that is used to store a value during program execution. Variable changes in a program but constant doesn’t change during the program’s execution.
A constant is a quantity that doesn’t change during program execution.
The different types of constants in C programming are:
• Integer constant
• Real constant
• Character constant
• String constant

4. Operators
Operators are the symbol that instructs C to perform some operation on operands. For example:- 4+5 is an expression where 4 & 5 are operands and + is the operator.

Arithmetic Operators
Arithmetic operators are used to manipulate arithmetic data and perform arithmetic operations.
Arithmetic operators used in C programming are addition, subtraction, multiplication, division, and modulus.

Unary operators
The unary operators operate on one operand-variable or constant. Increment (++) and decrement (–) are two types of unary operators used in C programming.

Relational operator
Relational operator is used to evaluate and compare two values of the same type, either both numeric or both string. Greater than(<) and smaller than (>) are two relational operators.

logical operator
Logical operator connects two or more relational expressions into one or reverses the logic of an expression. Logical operators return TRUE if the expression evaluates to nonzero or FALSE if the expression evaluates to zero.

Input/Output functions
Input/output operations
The operations that deal with a console(screen/keyboard) related operations for getting input from the keyboard and displaying output in the console. A library function is supplied to perform these operations.

printf(): printf() is used to display the output data in a console of different types.

Scanf() : scanf() is used to take input from keyboard or user.

Data types in C
Datatype is an instruction that is used to declare the type of variable being used in the program. Variables must be declared before using in C programming.
Some basic datatype in C are:

Int: It represents the integer numbers(both signed and unsigned). Some of the derived datatypes of int are short int, long int.

Float: Float represents decimal numbers with 4 bytes of memory space. Derived data types from float are double, long double.

Char: Char represents the single alphabet or symbol, whereas a collection of alphabets or symbols is represented by a string.

Void: Void represents a function that doesn’t return any values.

Data Type Modifiers
Data type modifiers are used to change the meaning of the basic data type according to the requirement of the program.
Data type modifiers with their memory consumptions are:
• Short int (2 bytes),
• Long int (4 bytes),
• Unsigned short (2 bytes),
• Unsigned long int (4 bytes).

Looping
Looping is the repetition of a set of instructions for a certain number of times while its condition remains true.
For loop
For loop is the most commonly used loop statement. In for loop condition is initialized and repeated until the condition is satisfied.
Differences between While and do-while
The Differences between while and do-while are:

While loop do-while loop
1. The while loop evaluates the condition first and then executes the statements. 1. The do-while loop executes the statements first before evaluating the condition.
2. There is no semicolon at the end of while loop. 2. There is a compulsion of the semicolon at the end of do-while loop.

Differences between selection statement and looping statement
The Differences between the selection statement and looping statement are:

Selection statement Looping statement
1. Selection statement select among a set of statements depending on the value of a controlling expression. 1. Looping statement allows a set of instructions to be repeated a certain number of times.
2. If and switch statements are selection statements. 2. For loop, while loop and do-while loops are looping statements.

Differences between Break and continue statement
The Differences between break and continue statements are:

Break statement Continue statement
1. Break statement is used to terminate the loops. 1. Continue statement is used to continue the next iterations of loop.
2. Break statement is used in loop and switch case. 2. It is used in a loop only.

C program to display Hello World !:

#include < stdio.h >  
#include < conio.h >  
int main()  
{  
printf("Hello World!");  
getch ();  
} 

Output:
Hello World!

C program to display area of rectangle:

#include < stdio.h >  
#include < conio.h >  
int main()  
{  
int length, breadth, area;  
printf("\nEnter the Length of Rectangle : ");  
scanf("%d", &length);  
printf("\nEnter the Breadth of Rectangle : ");  
scanf("%d", &breadth);  
area = length \* breadth;  
printf("\nArea of Rectangle : %d", area);  
getch ();  
return 0;  
} 

Output:
Enter the Length of Rectangle : 2
Enter the Breadth of Rectangle : 3
Area of Rectangle : 6

While statement

\# include < stdio.h >  
\# include <conio.h >  
int main()  
{  
int i=1;  
while (i <= 4 )  
{  
printf("%d\\n",i);  
i++;  
}  
return 0;  
} 

Output:
1
2
3
4

If else loop

#include < stdio.h >  
#include < conio.h >  
int main()  
{  
int age;  
printf("Enter your age:");  
scanf("%d",&age);  
if(age >=18)  
printf("You are eligible for voting");  
else  
printf("You are not eligible for voting");  
getch ();  
return 0;  
}  

Output: Enter your age: 18
You are eligible for voting

If…else if statement

#include <stdio.h >  
#include <conio.h >  
int main()  
{  
int a, b;  
printf("Input the first value:");  
scanf("%d", &a);  
printf("Input the second value:");  
scanf("%d",&b);  
if (a !=b)  
{  
printf("a is not equal to b\n");  
}  
else if (a > b)  
{  
printf("a is greater than b\n");  
}  
else if (b > a)  
{  
printf("b is greater than a\n");  
}  
else  
{  
printf("a is equal to b\n");  
}  
return 0;  
} 

output:
Input the first value: 5
Input the second value: 10
a is not equal to b
b is greater than a

Switch statement

#include < stdio.h >  
#include < conio.h >  
int main ()  
{  
char grade = 'B';  
switch(grade)  
{  
case 'A' :  
printf("Excellent!\\n" );  
break;  
case 'B' :  
printf("Very Very good\\n" );  
break;  
case 'C' :  
printf("Very good\\n" );  
break;  
case 'D' :  
printf("Good\\n" );  
break;  
case 'F' :  
printf("Better\\n" );  
break;  
default :  
printf("Invalid grade\\n" );  
}  
printf("Your grade is %c\\n", grade );  
getch();  
return 0;  
} 

Output:
Your grade is Very Very good

For loop

#include < stdio.h >  
#include < conio.h >  
int main()  
{  
int i;  
for (i = 1; i < 11; i++)  
{  
printf("%d \\n", i);  
}  
getch();  
return 0;  
} 

Output:
1
2
3
4
5
6
7
8
9
10

DO…WHILE statement

#include < stdio.h >  
#include < conio.h >  
int main()  
{  
int i=0;  
do  
{  
printf("Value of variable i is: %d\\n", i);  
i++;  
}while (i<=3);  
getch();  
return 0;  
}

Output:
Value of variable i is: 0
Value of variable i is: 1
Value of variable i is: 2
Value of variable i is: 3

Array
An Array is the collection of data of the same datatypes. Array is used to store data of the same types under the same name.
For example, int digit[10]; here, int is a datatype, digit is an array name, and 10 is a number of data that can be stored in an array.

Single dimensional array

#include < stdio.h >  
#include < conio.h >  
int main()  
{  
int arr\[50\], size, i;  
printf("Number of Elements You want to store in Array ? ");  
scanf("%d", &size);  
printf("\\nEnter %d Elements: ", size);  
for(i=0; i < size; i++)  
scanf("%d", &arr\[i\]);  
printf("\\nElements of this One-dimensional Array is:\\n");  
for(i=0; i < size; i++)  
printf("%d ", arr\[i\]);  
getch();  
return 0;  
} 

Output:
Number of Elements You want to store in Array ?
3
Enter 3 Elements:
1
2
3
Elements of this One-dimensional Array is:
1 2 3

Two dimensional array

#include < stdio.h >  
#include < conio.h >  
int main()  
{  
int arr\[4\]\[2\] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};  
int i, j;  
printf("The Two-dimensional Array is:\\n");  
for(i=0; i < 4; i++)  
{  
for(j=0; j <2; j++)  
printf("%d ", arr\[i\]\[j\]);  
printf("\\n");  
}  
getch();  
return 0;  
} 

Output:
The Two-dimensional Array is:
1 2
3 4
5 6
7 8

Character array(string)
String is the group of characters that can be declared as char a[5]= “abcde”;. While declaring string a[5], it means it can hold up to a 5-character string.

C program to display string:

#include < stdio.h >  
#include < conio.h >  
#include < string.h >  
Void main()  
{  
char text\[30\];  
clrscr();  
printf("Enter a string:");  
scanf("%s", text);  
printf("The string is : %s", text);  
getch();  
return 0;  
}  

Output:
Enter a string : Hello
The string is : Hello

String functions
There are various defined library functions that are used to manipulate strings. some of them are:
strlen(): It is a string function that gives the length of the string.
strcat(): This function is used to combines two strings together to form a whole string.
strcpy(): It is used to copy a string from one source to another source.
Strlwr(): This string function converts all characters of string to lowercase.
strupr(): This function converts all characters of a string to uppercase.

Example:

#include < stdio.h >  
#include < conio.h >  
#include < string.h >  
Void main()  
{  
char text1\[5\] = "abcde", text2 = "fghij";  
char str\[10\], cpy\[10\]; int num; clrscr();  
num = strlen(text1);  
str = strcat(text1,text2);  
strupr(text1);  
strcpy(text2,cpy);  
printf("The number of character in string is : %d", num);  
printf("The concatenated string is %s", str);  
printf("The copied string is %s", cpy);  
printf("The uppercased string is %s", text1);  
getch();  
return(0);  
} 

Output
The number of character in string is : 5
The concatenated string is abcdefghij
The copied string is fghij
The uppercased string is ABCDE