3.programstructure: 1. Overview
3.programstructure: 1. Overview
OVERVIEW
What is c ?
C is a general-purpose, high-level language that was originally developed by
Dennis M. Ritchie to develop the UNIX operating system at Bell Labs.
Advantage of C:-
First program in c:
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
3.PROGRAMSTRUCTURE
3. The next line /*...*/ is used to write
comments
4. The next line printf(...) is another function available in C which causes the
message "Hello, World!" to be displayed on the screen.
5.scanf is another function available in c which is use to store variable.
6.Every statement must be terminated by ending with semicolon(;)
7. The next line return 0; terminates the main() function and returns the
Page 1 of 10
value 0.
Chapter 2
Identifiers:-
A C identifier is a name used to identify a variable, function, or any other user
defined items. An identifier starts with a letter A to Z, a to z, or an underscore ‘_’
followed by zero or more letters, underscores, and digits (0 to 9).
Eg. Ram, hari , shyam123, nepal_123 etc
Keywords:-
The following list reserved words in C. These reserved words may not
be used as constants or variables or any other identifier names.
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 , _Packed
,double
Data types:-
Variable:-
A variable is a name given to a storage area that our programs can
manipulate.
int i, j, k;
char c, ch;
float f, salary;
double d;
Page 2 of 10
variable with the given type and name so that the compiler can proceed for
further compilation without requiring the complete detail about the variable.
Character Constants:-
There are certain characters in C that represent special meaning when preceded
by a backslash.
Escape
Sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
#include <stdio.h>
int main()
{
printf("Hello\tWorld\n\n");
return 0;
}
When the above code is compiled and executed, it produces the following result:
Hello World
OPERATORS
An operator is a symbol that tells the compiler to perform specific mathematical
or logical functions. C language is rich in built-in operators and provides the
following types of operators:
Arithmetic Operators
Page 3 of 10
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Arithmetic Operators:-
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;
}
return 0;
}
2.Relational Operators:-
A relational operator checks the relationship between two operands. 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
== Equal to
> Greater than
< Less than
!= Not equal to
>= Greater than or
equal to
<= Less than or equal to
Page 5 of 10
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}
3.Logical Operators:-
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
Page 6 of 10
return 0;
}
Output
(a == b) && (c > b) equals to 1
(a == b) && (c < b) equals to 0
(a == b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
4: Assignment Operators
// C Program to demonstrate the working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("c = %d \n", c);
Page 7 of 10
c -= a; // c = c-a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
return 0;
}
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, addition and
division are converted to bit-level which makes processing faster and saves power.
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
#include <stdio.h>
int main()
{
int a,b,sum
printf("Enter two integers: ");
Page 8 of 10
// Two integers entered by user is stored using scanf() function
scanf("%d %d", &a, &b);
// sum of two numbers in stored in variable sumOfTwoNumbers
sum = a + b;
// Displays sum
printf("%d + %d = %d", a, b, sum);
return 0;
}
Same as
Q. Multiply two no.
Q. Add three number
Q. Multiply three number
Q. find Area of rectangle
Q.Find any formula.
Conditional statement
If ststement:- The if statement evaluates the test expression inside the parenthesis.
Syntax:-
if (testExpression)
{
// statements
}
47
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
Page 9 of 10
return 0;
}
if…else Statement
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false.
Syntax of if...else
if (testExpression) {
// codes inside the body of if
}
else {
// codes inside the body of else
}
Page 10 of 10