Professional Documents
Culture Documents
CSE (2nd & 3rd )
CSE (2nd & 3rd )
2nd Chapter
First C Program
Before starting the abcd of C language, you need to learn how to write, compile and run the first c
program.
To write the first c program, open the C console and write the following code:
1. #include <stdio.h>
2. int main(){
3. printf("Hello C Language");
4. return 0;
5. }
#include <stdio.h> includes the standard input output library functions. The printf() function is defined
in stdio.h .
int main() The main() function is the entry point of every program in c language.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for successful
execution and 1 for unsuccessful execution.
There are 2 ways to compile and run the c program, by menu and by shortcut.
By menu
Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.
By shortcut
Or, press ctrl+f9 keys compile and run the program directly.
printf() function
The printf() function is used for output. It prints the given statement to the console.
1. printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the console.
1. scanf("format string",argument_list);
1. #include<stdio.h>
2. int main(){
3. int number;
4. printf("enter a number:");
5. scanf("%d",&number);
7. return 0;
8. }
Output
enter a number:5
The scanf("%d",&number) statement reads integer number from the console and stores the given value
in number variable.
The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on
the console.
Let's see a simple example of input and output in C language that prints addition of 2 numbers.
1. #include<stdio.h>
2. int main(){
3. int x=0,y=0,result=0;
4.
6. scanf("%d",&x);
8. scanf("%d",&y);
9.
10. result=x+y;
12.
13. return 0;
14. }
Output
sum of 2 numbers:18
Variables in C
A variable is a name of the memory location. It is used to store data. 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.
1. type variable_list;
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:
2. float f=20.8;
3. char c='A';
o A variable name can start with the alphabet, and underscore only. It can't start with a digit.
o A variable name must not be any reserved word or keyword, e.g. int, float, etc.
1. int a;
2. int _ab;
3. int a30;
1. int 2;
2. int a b;
3. int long;
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Local Variable
A variable that is declared inside the function or block is called a local variable.
1. void function1(){
3. }
A variable that is declared outside the function or block is called a global variable. Any function can
change the value of the global variable. It is available to all the functions.
2. void function1(){
4. }
Static Variable
A variable that is declared with the static keyword is called static variable.
1. void function1(){
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
If you call this function many times, the local variable will print the same value for each function call,
e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call,
e.g. 11, 12, 13 and so on.
Automatic Variable
All variables in C that are declared inside the block, are automatic variables by default. We can explicitly
declare an automatic variable using auto keyword.
1. void main(){
External Variable
We can share a variable in multiple C source files by using an external variable. To declare an external
variable, you need to use extern keyword.
myfile.h
program1.c
#include "myfile.h"
#include <stdio.h>
void printValue(){
}
Comment in C
Comments in C language are used to provide information about lines of code. It is widely used for
documenting code. There are 2 types of comments in the C language.
2. Multi-Line Comments
Single line comments are represented by double slash \\. Let's see an example of a single line comment
in C.
1. #include<stdio.h>
2. int main(){
3. //printing information
4. printf("Hello C");
5. return 0;
6. }
Output:
Hello C
Even you can place the comment after the statement. For example:
Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many lines of code, but it
can't be nested. Syntax:
1. /*
2. code
3. to be commented
4. */
1. #include<stdio.h>
2. int main(){
3. /*printing information
4. Multi-Line Comment*/
5. printf("Hello C");
6. return 0;
7. }
Output:
Hello C
Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
There are the following data types in C language.
float 4 byte
double 8 byte
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.
C Identifiers
C identifiers represent the name in the C program, for example, variables, functions, arrays, structures,
unions, labels, etc. An identifier can be composed of letters such as uppercase, lowercase letters,
underscore, digits, but the starting letter should be either an alphabet or an underscore. If the identifier
is not used in the external linkage, then it is called as an internal identifier. If the identifier is used in the
external linkage, then it is called as an external identifier.
We can say that an identifier is a collection of alphanumeric characters that begins either with an
alphabetical character or an underscore, which are used to represent various programming elements
such as variables, functions, arrays, structures, unions, labels, etc. There are 52 alphabetical characters
(uppercase and lowercase), underscore character, and ten numerical digits (0-9) that represent the
identifiers. There is a total of 63 alphanumerical characters that represent the identifiers.
o The first character of an identifier should be either an alphabet or an underscore, and then it
can be followed by any of the character, digit, or underscore.
o In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that
identifiers are case sensitive.
o Commas or blank spaces cannot be specified within an identifier.
o Identifiers should be written in such a way that it is meaningful, short, and easy to read.
Types of identifiers
o Internal identifier
o External identifier
Internal Identifier
If the identifier is not used in the external linkage, then it is known as an internal identifier. The internal
identifiers can be local variables.
External Identifier
If the identifier is used in the external linkage, then it is known as an external identifier. The external
identifiers can be function names, global variables.
Keyword Identifier
1. int main()
2. {
3. int a=10;
4. int A=20;
5. printf("Value of a is : %d",a);
6. printf("\nValue of A is :%d",A);
7. return 0;
8. }
Output
Value of a is : 10
Value of A is :20
The above output shows that the values of both the variables, 'a' and 'A' are different. Therefore, we
conclude that the identifiers are case sensitive.
C-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
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
We will, in this chapter, look into the way each operator works.
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples
Show Examples
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then −
Show Examples
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
B = 0000 1101
-----------------
~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
~ (~A ) =
Binary One's Complement Operator is unary ~(60),
and has the effect of 'flipping' bits. i.e,. -
0111101
Show Examples
3rd Chapter
Statements
Statements are fragments of the C program that are executed in sequence. The body of any function is a
compound statement, which, in turn is a sequence of statements and declarations:
int main(void)
1) compound statements
2) expression statements
3) selection statements
4) iteration statements
5) jump statements
Labels
Any statement can be labeled, by providing a name followed by a colon before the statement itself.
Any statement (but not a declaration) may be preceded by any number of labels, each of which
declares identifier to be a label name, which must be unique within the enclosing function (in other
words, label names have function scope).
Label declaration has no effect on its own, does not alter the flow of control, or modify the behavior of
the statement that follows in any way.
Compound statements
(until
{ statement | declaration...(optional) }
C23)
The compound statement allows a set of declarations and statements to be grouped into one unit that
can be used anywhere a single statement is expected (for example, in an if statement or an iteration
statement):
{ // start of block
int n = 1; // declaration
The initializers of the variables with automatic storage duration declared inside a block and the VLA
declarators are executed when flow of control passes over these declarations in order, as if they were
statements:
int main(void)
{ // start of block
{ // start of block
Expression statements
expression(optional) ; (1)
Most statements in a typical C program are expression statements, such as assignments or function
calls.
An expression statement without an expression is called a null statement. It is often used to provide an
empty body to a for or while loop. It can also be used to carry a label in the end of a compound
statement or before a declaration:
char *s;
; // null statement
An attr-spec-seq followed by ; does not form an expression statement. It forms an attribute (since C23)
declaration instead.
Selection statements
The selection statements choose between one of several statements depending on the value of an
expression.
1) if statement
3) switch statement
Iteration statements
1) while loop
2) do-while loop
3) for loop
Jump statements
2) continue statement
4) goto statement
C if else Statement
The if-else statement in C is used to perform the operations based on some specific condition. The
operations specified in if block are executed if and only if the given condition is true.
o If statement
o If-else statement
o If else-if ladder
o Nested if
If Statement
The if statement is used to check some given condition and perform some operations depending upon
the correctness of that condition. It is mostly used in the scenario where we need to perform the
different operations for the different conditions. The syntax of the if statement is given below.
1. if(expression){
2. //code to be executed
3. }
Flowchart of if statement in C
Let's see a simple example of C language if statement.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("Enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
8. }
9. return 0;
10. }
Output
Enter a number:4
4 is even number
enter a number:5
1. #include <stdio.h>
2. int main()
3. {
4. int a, b, c;
6. scanf("%d %d %d",&a,&b,&c);
8. {
9. printf("%d is largest",a);
10. }
12. {
14. }
16. {
18. }
19. if(a == b && a == c)
20. {
22. }
23. }
Output
12 23 34
34 is largest
If-else Statement
The if-else statement is used to perform two operations for a single condition. The if-else statement is
an extension to the if statement using which, we can perform two different operations, i.e., one is for
the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must
notice that if and else block cannot be executed simiulteneously. Using if-else statement is always
preferable since it always invokes an otherwise case with every if condition. The syntax of the if-else
statement is given below.
1. if(expression){
3. }else{
5. }
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
8. }
9. else{
12. return 0;
13. }
Output
enter a number:4
4 is even number
enter a number:5
5 is odd number
1. #include <stdio.h>
2. int main()
3. {
4. int age;
6. scanf("%d",&age);
7. if(age>=18)
8. {
10. }
11. else
12. {
14. }
15. }
Output
Enter your age?18
1. if(condition1){
3. }else if(condition2){
5. }
6. else if(condition3){
8. }
9. ...
10. else{
12. }
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number==10){
8. }
9. else if(number==50){
10. printf("number is equal to 50");
11. }
14. }
15. else{
17. }
18. return 0;
19. }
Output
enter a number:4
enter a number:50
number is equal to 50
Program to calculate the grade of the student according to the specified marks.
1. #include <stdio.h>
2. int main()
3. {
4. int marks;
6. scanf("%d",&marks);
8. {
12. {
14. }
16. {
18. }
20. {
22. }
23. else
24. {
26. }
27. }
Output
It is always legal in C programming to nest if-else statements, which means you can use one if or else
if statement inside another if or else if statement(s).
Syntax
if( boolean_expression 1) {
if(boolean_expression 2) {
You can nest else if...else in the similar way as you have nested if statements.
Example
Live Demo
#include <stdio.h>
int main () {
int a = 100;
int b = 200;
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
return 0;
When the above code is compiled and executed, it produces the following result −
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute
multiple operations for the different possibles values of a single variable called switch variable. Here, We
can define various statements in the multiple cases for the different values of a single variable.
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
12. }
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement found in
the case, all the cases will be executed present after the matched case. It is known as fall through the
state of C switch statement.
Let's try to understand it by the examples. We are assuming that there are following variables.
1. int x,y,z;
2. char a,b;
3. float f;
First, the integer expression specified in the switch statement is evaluated. This value is then matched
one by one with the constant values given in the different cases. If a match is found, then all the
statements specified in that case are executed along with the all the cases present after that case
including the default statement. No two cases can have similar values. If the matched case contains a
break statement, then all the cases present after that will be skipped, and the control comes out of the
switch. Otherwise, all the cases following the matched case will be executed.
Let's see a simple example of c language switch statement.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. switch(number){
7. case 10:
9. break;
12. break;
15. break;
16. default:
18. }
19. return 0;
20. }
Output
enter a number:4
enter a number:50
number is equal to 50
1. #include <stdio.h>
2. int main()
3. {
4. int x = 10, y = 5;
6. {
7. case 1:
8. printf("hi");
9. break;
10. case 0:
11. printf("bye");
12. break;
13. default:
15. }
16.
17. }
Output
hi
Let's try to understand the fall through state of switch statement by the example given below.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4.
5. printf("enter a number:");
6. scanf("%d",&number);
7.
8. switch(number){
9. case 10:
15. default:
17. }
18. return 0;
19. }
Output
enter a number:10
number is equal to 10
number is equal to 50
1. #include <stdio.h>
2. int main () {
3.
4. int i = 10;
5. int j = 20;
6.
7. switch(i) {
8.
9. case 10:
12. switch(j) {
15. }
16. }
17.
20.
21. return 0;
22. }
Output
the value of i evaluated in outer switch: 10
Exact value of i is : 10
Exact value of j is : 20
While loop in C
A while loop in C programming repeatedly executes a target statement as long as a given condition is
true.
Syntax
while(condition) {
statement(s);
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following the
loop.
Flow Diagram
Here, the key point to note is that a while loop might not execute at all. When the condition is tested
and the result is false, the loop body will be skipped and the first statement after the while loop will be
executed.
Example
Live Demo
#include <stdio.h>
int main () {
int a = 10;
while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
return 0;
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
For loop in C
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
Syntax
statement(s);
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the
body of the loop does not execute and the flow of control jumps to the next statement just after
the 'for' loop.
After the body of the 'for' loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control variables. This
statement can be left blank, as long as a semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes and the process repeats
itself (body of loop, then increment step, and then again condition). After the condition
becomes false, the 'for' loop terminates.
Flow Diagram
Example
Live Demo
#include <stdio.h>
int main () {
int a;
return 0;
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
do….while loop in C
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C
programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one
time.
Syntax
do {
statement(s);
} while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop
executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop
executes again. This process repeats until the given condition becomes false.
Flow Diagram
Example
Live Demo
#include <stdio.h>
int main () {
int a = 10;
/* do loop execution */
do {
a = a + 1;
}while( a < 20 );
return 0;
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Nested loop
C programming allows to use one loop inside another loop. The following section shows a few examples
to illustrate the concept.
Syntax
statement(s);
statement(s);
The syntax for a nested while loop statement in C programming language is as follows −
while(condition) {
while(condition) {
statement(s);
statement(s);
The syntax for a nested do...while loop statement in C programming language is as follows −
do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition );
A final note on loop nesting is that you can put any type of loop inside any other type of loop. For
example, a 'for' loop can be inside a 'while' loop or vice versa.
Example
The following program uses a nested for loop to find the prime numbers from 2 to 100 −
Live Demo
#include <stdio.h>
int main () {
int i, j;
return 0;
}
When the above code is compiled and executed, it produces the following result −
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
#include <stdio.h>
int main () {
for( ; ; ) {
return 0;
When the conditional expression is absent, it is assumed to be true. You may have an initialization and
increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite
loop.