C Language: Step 1: Creating A Source Code
C Language: Step 1: Creating A Source Code
C was developed and written by Dennis M. Ritchie in the year 1972, and hence he
is known as the founder of C.
Running a c program :
The computer cannot understand the high-level language. It can understand only
low-level language. So, the program written in the high-level language needs to be
converted into the low-level language to make it understandable for the computer.
This conversion is performed using either Interpreter or Compiler.
Some of them are an integer, floating point, character, etc. Usually, programming
languages specify the range values for given data-type.
VARIABLE: Variable is used to name the memory location to store the data.
Variable declaration:
Example:
int width, height=5;
char letter='A';
float age, area;
double d;
variable rules:
A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-
9, and the underscore character.
The first character must be a letter or underscore.
Blank spaces cannot be used in variable names.
Special characters like #, $ are not allowed.
C keywords cannot be used as variable names.
Variable names are case sensitive.
Values of the variables can be numeric or alphabetic.
Variable type can be char, int, float, double, or void.
Example:
#include<stdio.h>
void main()
{
/* c program to print value of a variable */ int age = 33;
printf("I am %d years old.\n", age);
}
Operators: Operators are the symbols or few words. Operators can be used to
operate on the data.
- sub
* mul
/ div (Quotient)
% modulus (remainder)
Ex:x=10
X+=4(x=x+410+4)
X=14
2.a=5
a-=2(a=a-25-2=3)
>,<,<=,>=,==,!=.
5.Logical Operators: logical operators can be used to check the multiple conditions
at a time.they are 3 types
1.Logical AND (&&) =returs 1 whenever all conditions are true return 0 atleat one
condition if false.
3.Logical NOT(!)=inverse.
OPERATORS
Operators are the symbols or few words, operators can be used to operate on the
data.
1.Arithematic Operators:
+,-,*,/,%
2.Assignment Operators:
Compound assignment.
= Simple assignment operator. Assigns values from right side operands to C=A+B
left side operand will assign
the value
of A + B
to C
+= Add AND assignment operator. It adds the right operand to the left C += A is
operand and assign the result to the left operand. equivalent
to C = C +
A
-= Subtract AND assignment operator. It subtracts the right operand from the C -= A is
left operand and assigns the result to the left operand. equivalent
to C = C -
A
*= Multiply AND assignment operator. It multiplies the right operand with the C *= A is
equivalent
left operand and assigns the result to the left operand. to C = C *
A
/= Divide AND assignment operator. It divides the left operand with the right C /= A is
operand and assigns the result to the left operand. equivalent
to C = C /
A
3.Relational Operators:
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples
== Checks if the values of two operands are equal or not. If yes, then the condition (A == B)
becomes true. is not
true.
!= Checks if the values of two operands are equal or not. If the values are not equal, (A != B)
then the condition becomes true. is true.
> Checks if the value of left operand is greater than the value of right operand. If yes, (A > B)
then the condition becomes true. is not
true.
< Checks if the value of left operand is less than the value of right operand. If yes, (A < B)
then the condition becomes true. is true.
>= Checks if the value of left operand is greater than or equal to the value of right (A >= B)
operand. If yes, then the condition becomes true. is not
true.
<= Checks if the value of left operand is less than or equal to the value of right (A <= B)
operand. If yes, then the condition becomes true. is true.
4.Logical operators:
A holds 1 and variable B holds 0, then −
Show Examples
&& Called Logical AND operator. If both the operands are non-zero, then the condition (A && B)
becomes true. is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the (A || B) is
condition becomes true. true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If !(A &&
a condition is true, then Logical NOT operator will make it false. B) is
true.
5.Bitwise Operators:
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |,
and ^ is as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
A = 0011 1100=60
B = 0000 1101=13
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A'
holds 60 and variable 'B' holds 13, then −
Show Examples
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) =
12, i.e.,
0000
1100
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) =
49, i.e.,
0011
0001
~ (~A ) =
~(60),
Binary One's Complement Operator is unary and has the effect of 'flipping' bits.
i.e,. -
0111101
<< Binary Left Shift Operator. The left operands value is moved left by the number of A << 2 =
bits specified by the right operand. 240 i.e.,
1111
0000
>> Binary Right Shift Operator. The left operands value is moved right by the number A >> 2 =
of bits specified by the right operand. 15 i.e.,
0000
1111
6.Conditional Operator:
? :; Conditional Expression
7.Special Operator:
8.Unary Operators:
++ Increment -- Decrement
#include <stdio.h>
//stdio.h is a header file used for input.output purpose.
void main()
{
//set a and b both equal to 5.
int a=5, b=5;
printf("\n%d %d",a--,b--);
printf("\n%d %d",--a,--b);
printf("\n%d %d",a++,b++);
printf("\n%d %d",++a,++b);
}
Conditional statements
Conditional Statements in C programming are used to make decisions based on
the conditions.
1. If statement
2. If-Else statement
3. Nested If-else statement
4. If-Else If ladder
5. Switch statement
if statement:
The statements inside the body of “if” only execute if the given condition returns
true. If the condition returns false then the statements inside “if” are skipped.
Syntax:
if (condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}
Program:
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
if else:
If condition returns true then the statements inside the body of “if” are executed
and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped
and the statements in “else” are executed.
Syntax:
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
Program:
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
/* This statement will only execute if the
* above condition (age>=18) returns true
*/
printf("You are eligible for voting");
}
else
{
/* This statement will only execute if the
* condition specified in the "if" returns false.
*/
printf("You are not eligible for voting");
}
return 0;
}