## Relational Operators in C Programming

C Language has a following relational operators. For example: Assume variable A holds 100 and variable B holds 200 then −

Operator Description Example
== Equals to (A == B) is not true.
!= Not equal (A != B) is true.
Greater than (A > B) is not true.
Less than (A < B) is true.
>= Greater than or equals to (A >= B) is not true.
<= Less than or equals to (A <= B) is true.
Example: Write a C Program to find the largest of two numbers.

Output

## Arithmetic Operators in C Programming

C language have a following arithmetic operators. Assume variable i holds 100 and variable j holds 200 then –

Operator Description Example
+ Adds two operands. i + j = 300
Subtracts second operand from the first. i − j = -100
* Multiplies two operands. i * j = 20000
/ Divide. j / i = 2
% Modulus Operator returns the remainder of integer division. j % i = 0
++ Increment operator increases the value of variable by one. i++ = 101
Decrement operator decreases the value of variable by one. i– = 99

Example : Write a program to demonstrate the various arithmetic operators.

OUTPUT

Increment and Decrement Operator

1. Increment operator      ++
2. Decrement operator    —

In a C language, Increment operator (++)  increases the value of variable by one and Decrement operator  (–) decreases the value of variable by one.

There are two form of increment  and decrement operator:

1. Prefix form
2. Postfix form

Prefix Form : In the prefix expression operator appears in the expression before the operands.

Example : ++A

In the prefix form first the value of operand is increment or decrement than the value of operand is used in expression.

Example

Postfix Form: In the postfix expression operator appears in the expression after the operands.

Example : A++

In the postfix form first the value of operand is used in expression than value of operand is incremented or decremented.

Example: Write a program to demonstrate the increment and decrement operator.

OUTPUT

## Operators in a C Programming

An operator is a symbol. It is used to tells the compiler to perform specific mathematical or logical functions. C language have a following types of built-in operators:−

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators

## Tokens

In a C language, token is either a keyword, a symbol ,an identifier, a constant or a string literal.

A C program consists of various tokens for example, the following C program consists of five tokens −

The individual tokens are −

Comments are the text in C program and they are ignored by the compiler during compilation and execution.

They start with /* and terminate with the characters */ as shown below −

Anything written within Comment will never execute. You can’t write comments within comments.

## Identifiers

In a C language, identifier is a name given to any variable, function, or any other user-defined item.

C language identifier starts with a letter A to Z, a to z, digits (0 to 9) or an underscore ‘_’ followed by zero.

C does not allow punctuation characters such as @, \$, and % within identifiers. Here are some examples of acceptable identifiers −

## Keywords

In a C language keywords are the reserved words whose meaning already explained in a compiler.

These reserved words can’t be used as a variables or identifier names. There are 32 Keywords in C

 auto else long switch break enum register typedef case extern return union char float short unsigned const for signed void continue goto sizeof volatile default if static while do int struct double

## Format Specifiers in C Programming

In C programming there are number of data types and format specifiers is used to defines the type of data to be printed.

Whether to print output or to take input in both case we required format specifiers. Format specifiers are also called as format string.

list of format specifiers used in C language.

Format specifier Description Supported data types
%c Character char
unsigned char
%d Signed Integer short
unsigned short
int
long
%e or %E Scientific notation of float values float
double
%f Floating point float
%g or %G Similar as %e or %E float
double
%hi Signed Integer(Short) short
%hu Unsigned Integer(Short) unsigned short
%i Signed Integer short
unsigned short
int
long
%l or %ld or %li Signed Integer long
%lf Floating point double
%Lf Floating point long double
%lu Unsigned integer unsigned int
unsigned long
%s String char *
%u Unsigned Integer unsigned int
unsigned long

## How to print integer character float and double using format string

Print integer : we can print integer value by using %d. for example:

Result

Here whatever written within double code “  ” print as it is.  %d is used to tell compiler print integer value. And value of  “ i  (integer value) ” will be print.

Print multiple integer : suppose we want to print two integer value then we need to write two times  %d. for example:

Output

Here whatever written within double code “  ” print as it is.  %d%d is used to tell compiler print two integer value. And value of  “ i & j  (integer value) ” will be print.

Print Character and float value : suppose we want to print two character and float value then we need two format string  %c  and %f  respectively. for example:

Here whatever written within double code “  ” print as it is.  %d%d is used to tell compiler print character and float value. And value of  “ c & f  ” will be print.

## How to take input from user : scanf()

scanf() function is used to read character, string, numeric data from keyboard. The scanf() function allows you to accept input from keyboard.

Take  integer input from user : we can take input integer value by user using %d format string. for example:

above line will read an integer value that the user enters on the keyboard. %d is used to tell compiler to read integers value from keyboard and store in a integer variable i.

Take  multiple integer input from user: we can take two integer value as s input  by user using  two times %d  format string. for example:

above line will read two integer value that the user enters on the keyboard. Two times %d is used to tell compiler to read two integers value from keyboard and store in a integer variable i & j

Note: similarly to take char, float value from user %c & %f formatted string used in a scanf() function.

## Variables in C Programming

Variables is a name given to memory location.

int x =10;

here, x is a integer variable.

Integer variable x is a name given to memory location and where we stored integer value10.

Initialize multiple variable : we can also initialize multipleby comma separated list.

int  a,b,c;

Here, we have initialized three integer variable a,b & c. variable  ‘a’  stores 10, variable ‘b’ stores 20 and variable ‘c’ stores 30.

Initialize multiple variable : we can also initialize multipleby comma separated list.

## Data Type

There are four types of data type in C language.  They are as follows:

Types Data Types
Basic data types int, char, float, double
Enumeration data type Enum
Derived data type pointer, array, structure, union
Void data type void

## Basic Data Types In C Language

### Integer Data Type

Integer data ( example 1,2,3,4,5,6, ….. etc. ) is stored in int ,short and long data type.

Type Storage size Value range
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Output

Note: Here %i is a format specifier which is used to print integer value.

### Character Data Type

Character data ( example ‘a’ , ‘b’ , ‘f’, ….. etc. ) is stored in “char” data type.

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127

Example: Declare and initialize integer variable.

Output

### Float Data Type

Floating point value (example  11.23, 333.3330 etc. ) can be stored in data type float and double. Small floating value can be stored in float and large floating stored in double.

Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

Example: Declare and initialize float & double variable.

Output:

## A Simple C Program

A simple C program basically consists of the following parts −

• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions

Basic structure of C program

#include

Above shown the basic structure of C program. For every program we have to write above mention basic structure.

How to write or print

We can write or print in C language by using printf() function. printf() function is a predefined function. for example: code to write “hello” is as follows:

printtf(“hello”);

output:   hello

Note: any thing written within double code in a printf() function will be write.

A simple C program to print “Hello”

Description

• First line  #include <stdio.h> is a preprocessor, which instruct to C compiler to include stdio.h file. stdio.h (slandered input output ) is used for printf function.
• int main() is the main function from where the program execution begins.
•  /*………..*/  is a “comments” in the program. This comments will be ignored by the compiler during execution of program
•  printf() is a predifned function in C which is used to print message.
•  line return 0; terminates the main() function and returns the value 0.return 0 tells the compiler that everything is ok and the execution of main() function ends. Because 0 is the code for successful execution, while values greater than 0 (integers) is an indication that something went wrong.

## Compile and Execute C Program in Unix

How to save and compile the source code and run it:

• Write the above-mentioned code on a text editor.
• Save the file as hello.c
• Open a command prompt and go to the directory where you have saved the file.
• To compile your code: type gcc hello.c and press enter.
• If there are no errors in your code, the command prompt will take you to the next line and generate a.out executable file.
• To execute your program, type  a.out  and press enter.
• Output “Hello” will print on the screen.

COMPILATION

OUTPUT

## GCC installation on linux unix

In a Linux or UNIX, to check whether GCC is installed on your system  enter the following command in the command line −

If GCC compiler installed on your system, then it should print a message as follows −

If GCC is not installed, then you will have to install it yourself

On ubantu to install GCC write the following command in terminal

## History of C Programming

C is computer programming language developed in 1972 by Dennis M. Ritchie.

C programming language developed in  Bell Laboratories to develop the UNIX operating system.

C is a  high-level language which can be easily read and written by any one.

C is a case-sensitive programming language. Thus, “HELLO” and “Hello” two different identifiers in C.