C Programing: Applications of C Programming
C Programing: Applications of C Programming
C Programing: Applications of C Programming
Applications of C Programming
C was initially used for system development work, particularly the programs that make-up the
operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as the code written in assembly language. Some examples of the use
of C are:
Operating Systems
Language Compilers
Assemblers
Text Editors
Network Drivers
Browsers (Mozzila)
Databases (Oracle RDBMS)
GUI
Gaming Applications
Language Interpreters
Utilities
Variables in C
A variable is a name of the memory location. A variable is a container (storage area) to hold
data. To indicate the storage area, each variable should be given a unique name (identifier).
Its value can be changed and it can be reused many times. It is a way to represent memory
location through symbol so that it can be easily identified.
The example of declaring the variable is given below:
1. int a;
2. float b;
3. char c;
Here a, b, c are variables. The int, float, char are the data types. We can also provide values
while declaring the variables as given below:
int a=10,b=20; //declaring 2 variable of integer type
float f=20.8;
char c='A';
C is a strongly typed language. This means that the variable type cannot be changed once it is
declared. For example:
int number = 5; // integer variable
number = 5.5; // error
float number; //error
C Identifiers
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifiers must be unique. They are created to give a unique name to an entity to identify it
during the execution of the program. For example:
int money;
Here, money is identifiers. Also remember, identifier names must be different from
keywords. You cannot use int as an identifier because int is a keyword.
Keywords in C
A keyword is a reserved word. You cannot use it as a variable name, constant name, etc. There
are only 32 reserved words (keywords) in the C language. A list of 32 keywords in the c
language is given below:
auto break case char const continue defaultdo
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
Literals
Literals are data used for representing fixed values. They can be used directly in the code. For
example: 1, 2.5, 'c' etc. Here, 1, 2.5 and 'c' are literals.
Data Types
1. Integers: An integer is a numeric literal (associated with numbers) without any
fractional part. Can have both zero, positive and negative values but no decimal values.
2. Floating-point Literals: A floating-point literal is a numeric literal that has either a
fractional form.
3. Characters: A character literal is created by enclosing a single character inside single
quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.
4. String Literals: A string literal is a sequence of characters enclosed in double-quote
marks. For example: "good", "Earth is round\n", "x" etc.
5. void is an incomplete type. It means "nothing" or "no type". You can think of void as
absent. For example, if a function is not returning anything, its return type should be
void. Note that, you cannot create variables of void type.
6. Escape Sequences: Sometimes, it is necessary to use characters that cannot be typed
or has special meaning in C programming. For example: newline(enter), tab, question
mark etc. In order to use these characters, escape sequences are used.
Escape Sequences
Escape Sequences Character
\b Backspace
\n Newline
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
For example: \n is used for a newline. The backslash \ causes escape from the normal way
the characters are handled by the compiler.
Example 1: C Output
#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}
How does this program work?
All valid C programs must contain the main() function. The code execution begins from
the start of the main() function.
The printf() is a library function to send formatted output to the screen. The function
prints the string inside quotations. The format string can be %d (integer), %c
(character), %s (string), %f (float) etc.
Syntax: printf("format string",argument_list)
To use printf() in our program, we need to include stdio.h header file using the
#include <stdio.h> statement.
The return 0; statement inside the main() function is the "Exit status" of the program.
It's optional.
C Input
In C programming, scanf() is one of the commonly used function to take input from the user.
The scanf() function reads formatted input from the standard input such as keyboards.
Example 5: Integer Input/Output
#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an integer: ");
scanf("%d", &testInteger);
printf("Number = %d",testInteger);
return 0;
}
Notice, that we have used &testInteger inside scanf(). It is because &testInteger gets the
address of testInteger and the value entered by the user is stored in that address.
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
C Increment and Decrement Operators
C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement
-- decreases the value by 1. These two operators are unary operators, meaning they only
operate on a single operand.
return 0;
}
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
Example 3: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
C Relational Operators
A relational operator checks the relationship between two operands (Operands are
expressions or values on which an operator acts or works). If the relation is true, it returns 1;
if the relation is false, it returns value 0. Relational operators are used in decision making and
loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
return 0;
}
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results false or true. Logical operators are commonly used in decision making in C
programming.
Operator Meaning of Operator Example
&& Logical AND. True only if all If c = 5 and d = 2 then, expression
operands are true ((c==5) && (d>5)) equals to 0.
|| Logical OR. True only if either one If c = 5 and d = 2 then, expression
operand is true ((c==5) || (d>5)) equals to 1.
! Logical NOT. True only if the If c = 5 then, expression !(c==5) equals
operand is 0 to 0.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a != b);
printf("(a != b) is %d \n", result);
return 0;
}
Other Operators
Comma Operator: Comma operators are used to link related expressions together. For
example: int a, c = 5, d;