0% found this document useful (0 votes)
10 views28 pages

Fundamentals of C and C

The document provides an overview of the C programming language, including its history, structure, identifiers, keywords, data types, variables, constants, operators, control statements (if-else, switch), loops, and break/continue statements. It explains the rules for constructing identifiers and variables, as well as the syntax for various control structures. Additionally, it highlights the advantages of using loops in C programming for code reusability and simplification of complex problems.

Uploaded by

Navneet Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

Fundamentals of C and C

The document provides an overview of the C programming language, including its history, structure, identifiers, keywords, data types, variables, constants, operators, control statements (if-else, switch), loops, and break/continue statements. It explains the rules for constructing identifiers and variables, as well as the syntax for various control structures. Additionally, it highlights the advantages of using loops in C programming for code reusability and simplification of complex problems.

Uploaded by

Navneet Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Dr.

HARIVANSH RAI BACHCHAN


MAHAVIDYALAYA

ASSIGNMENT-I

“FUNDAMENTAL OF C PROGRAMMING”

Directed by: Submitted by:


INTRODUCTION

History of C language is interesting to know. Here we are going to


discuss a brief history of the c language.
C programming language was developed in 1972 by Dennis Ritchie at
bell laboratories of AT&T (American Telephone & Telegraph), located in
the U.S.A.

Dennis Ritchie is known as the founder of the c language.

It was developed to overcome the problems of previous languages such


as B, BCPL, etc.

STRUCTURE OF A C PROGRAM

The structure of a C program means the specific structure to start the


programming in the C language. Without a proper structure, it becomes
difficult to analyze the problem and the solution. It also gives us a
reference to write more complex programs.

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.

Rules for constructing C 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 Keywords cannot be represented as an identifier.

o The length of the identifiers should not be more than 31

characters.

Example of valid identifiers

total, sum, average, _m _, sum_1,

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.

A list of 32 keywords in the c language is given below:

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.


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.

Syntax : 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.

Rules for defining variables

o A variable can have alphabets, digits, and underscore.

o A variable name can start with the alphabet, and


underscore only. It can't start with a digit.

o No whitespace is allowed within the variable name.

o A variable name must not be any reserved word or


keyword, e.g. int, float, etc.

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

An operator is simply a symbol that is used to perform


operations. There can be many types of operations like
arithmetic, logical, bitwise, etc.

There are following types of operators to perform different


types of operations in C language.

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.

There are the following variants of if statement in C language.

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){

//code to be executed if condition is true

}else{

//code to be executed if condition is false

}
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){

//code to be executed if condition1 is true

}else if(condition2){

//code to be executed if condition2 is true

else if(condition3){
//code to be executed if condition3 is true

...

else{

//code to be executed if all the conditions are false

Example:

PROGRAM TO CALCULATE THE GRADE OF THE


STUDENT ACCORDING TO THE SPECIFIED MARKS.
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 possible 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.

Syntax:
switch(expression){

case value1:

//code to be executed;

break; //optional

case value2:

//code to be executed;

break; //optional

......

default:

code to be executed if all cases are not matched;

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:

//SOME PART OF THE CODE;

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.

WHY USE LOOPS IN C LANGUAGE?


The looping simplifies the complex problems into the easy
ones. It enables us to alter the flow of the program so that
instead of writing the same code again and again, we can
repeat the same code for a finite number of times. For
example, if we need to print the first 10 natural numbers then,
instead of using the printf statement 10 times, we can print
inside a loop which runs up to 10 iterations.

ADVANTAGE OF LOOPS IN C

1) It provides code reusability.

2) Using loops, we do not need to write the same code again


and again.

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:

1. With switch case

2. With loop
Syntax:

//loop or switch case

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;

//some lines of the code which is to be skipped

You might also like