Fundamentals of C and C
Fundamentals of C and C
ASSIGNMENT-I
“FUNDAMENTAL OF C PROGRAMMING”
STRUCTURE OF A C PROGRAM
C IDENTIFIERS
characters.
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.
DATA TYPES IN C
A data type specifies the type of data that a variable can store
such as integer, floating, character, etc.
1. int a;
2. float b;
3. char c;
Here, a, b, c are variables. The int, float, char are the data types.
CONSTANTS IN C
A constant is a value or variable that can't be changed in the
program, for example: 10, 20, 'a', 3.4, "c programming" etc.
LIST OF CONSTANTS
C OPERATORS
o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Ternary or Conditional Operators
o Assignment Operator
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.
Syntax :
if(expression){
//code to be executed
Example:
PROGRAM TO FIND THE LARGEST NUMBER OF THE
THREE.
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
simultaneously. Using if-else statement is always preferable
since it always invokes an otherwise case with every if
condition.
Syntax :
if(expression){
}else{
}
Example :
PROGRAM TO CHECK WHETHER A PERSON IS
ELIGIBLE TO VOTE OR NOT.
IF ELSE-IF LADDER STATEMENT
The if-else-if ladder statement is an extension to the if-else
statement. It is used in the scenario where there are multiple
cases to be performed for different conditions. In if-else-if
ladder statement, if a condition is true then the statements
defined in the if block will be executed, otherwise if some other
condition is true then the statements defined in the else-if
block will be executed, at the last if none of the condition is
true then the statements defined in the else block will be
executed. There are multiple else-if blocks possible. It is similar
to the switch case statement where the default is executed
instead of else block if none of the cases is matched.
Syntax:
if(condition1){
}else if(condition2){
else if(condition3){
//code to be executed if condition3 is true
...
else{
Example:
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
Example:
C GOTO STATEMENT
The goto statement is known as jump statement in C. As the
name suggests, goto is used to transfer the program control to
a predefined label. The goto statement can be used to repeat
some part of the code for a particular condition. It can also be
used to break the multiple loops which can't be done by using a
single break statement. However, using goto is avoided these
days since it makes the program less readable and complicated.
Syntax:
label:
goto label;
Example:
C LOOPS
The looping can be defined as repeating the same process
multiple times until a specific condition satisfies. There are
three types of loops used in the C language. In this part of the
tutorial, we are going to learn all the aspects of C loops.
ADVANTAGE OF LOOPS IN C
TYPES OF C LOOPS
1. do while
2. while
3. for
DO-WHILE LOOP IN C
The do-while loop continues until a given condition satisfies. It
is also called post tested loop. It is used when it is necessary to
execute the loop at least once (mostly menu driven programs).
Syntax :
do{
//code to be executed
}while(condition);
Example:
WHILE LOOP IN C
The while loop in c is to be used in the scenario where we don't
know the number of iterations in advance. The block of
statements is executed in the while loop until the condition
specified in the while loop is satisfied.
Syntax :
while(condition){
//code to be executed
}
Example:
FOR LOOP IN C
The for loop is used in the case where we need to execute
some part of the code until the given condition is satisfied. The
for loop is also called as a per-tested loop. It is better to use for
loop if the number of iteration is known in advance.
Syntax :
for(initialization;condition;incr/decr){
//code to be executed
}
Example:
C BREAK STATEMENT
The break is a keyword in C which is used to bring the program
control out of the loop. The break statement is used inside
loops or switch statement. The break statement breaks the
loop one by one, i.e., in the case of nested loops, it breaks the
inner loop first and then proceeds to outer loops. The break
statement in C can be used in the following two scenarios:
2. With loop
Syntax:
break;
C CONTINUE STATEMENT
The continue statement in C language is used to bring the
program control to the beginning of the loop. The continue
statement skips some lines of code inside the loop and
continues with the next iteration. It is mainly used for a
condition so that we can skip some code for a particular
condition.
Syntax:
//loop statements
continue;