Thursday, 15 April 2021
Student DataBase Using c
Strucature Notes : All in 1 page
Structure :
Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member.
The struct keyword is used to define the structure.
Saturday, 10 April 2021
Friday, 26 March 2021
Functions in C Programming
A function is a block of statements that performs a specific task. Let’s say you are writing a C program and you need to perform a same task in that program more than once.
Thursday, 4 March 2021
Lec 5: C if...else Statements all
C if Statement
The syntax of the if
statement in C programming is:
if (test expression)
{
// statements to be executed if the test expression is true
}
How if statement works?
The if
statement evaluates the test expression inside the parenthesis ()
.
- If the test expression is evaluated to true, statements inside the body of
if
are executed. - If the test expression is evaluated to false, statements inside the body of
if
are not executed
Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output 1
Enter an integer: -2 You entered -2. The if statement is easy.
When the user enters -2, the test expression number<0
is evaluated to true. Hence, You entered -2 is displayed on the screen.
Output 2
Enter an integer: 5 The if statement is easy.
When the user enters 5, the test expression number<0
is evaluated to false and the statement inside the body of if
is not executed
C if...else Statement
The if
statement may have an optional else
block. The syntax of the if..else
statement is:
if (test expression) {
// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is false
}
How if...else statement works?
If the test expression is evaluated to true,
- statements inside the body of
if
are executed. - statements inside the body of
else
are skipped from execution.
If the test expression is evaluated to false,
- statements inside the body of
else
are executed - statements inside the body of
if
are skipped from execution.
Example 2: if...else statement
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Output
Enter an integer: 7 7 is an odd integer.
When the user enters 7, the test expression number%2==0
is evaluated to false. Hence, the statement inside the body of else
is executed.
C if...else Ladder
The if...else
statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions and execute different statements.
Syntax of if...else Ladder
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Nested if...else
It is possible to include an if...else
statement inside the body of another if...else
statement.
Example 4: Nested if...else
This program given below relates two integers using either <
, >
and =
similar to the if...else
ladder's example. However, we will use a nested if...else
statement to solve this problem.
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
If the body of an if...else
statement has only one statement, you do not need to use brackets {}
.
For example, this code
if (a > b) {
print("Hello");
}
print("Hi");
is equivalent to
if (a > b)
print("Hello");
print("Hi");
Thursday, 18 February 2021
Lec3: Data Types in C
Data Types in C
C has various data types to store data in program. C program can store integer, decimal number, character(alphabets), string(words or sentence), list etc. using various data types.
1. Primitives (Primary) Data Types
These data types store fundamental data used in the C programming.
- 1.int
It is used to store integer values. C program compiled with GCC compiler (32-Bit) can store integers from -2147483648 to 2147483647. The size of int is compiler dependent. It takes 4 bytes in a 32-bit compiler such as GCC.
intmyIntegerValue = 100;
- char
- float
- double
It stores single character such as ‘a’, ‘Z’, ‘@’ etc. including number, symbol or special character. It takes 1 byte (8-bits) to store each character.
charmyCharacter = 'A';
Note: Every character has a corresponding ASCII value to it ranging from -128 to 127. Numbers as a character has their corresponding ASCII values too. For example, ‘1’ as char has ASCII value 49, ‘A’ has ASCII value 65.
It stores real numbers with precision upto 6 decimal places. It takes 4 bytes of memory and is also known as floating point number.
floatmyFloatingValue = 100.6543;
It stores real numbers with precision upto 15 decimal places. It takes 8 bytes of memory.
doublemyDoubleValue = 180.715586;
Modifiers in C
These are keywords in C to modify the default properties of int and char data types. There are 4 modifiers in C as follows.
- short
- long
- signed
- unsigned
It limits user to store small integer values from -32768 to 32767. It can be used only on intdata type.
shortintmyShortIntegerValue = 18;
It allows user to stores very large number (something like 9 Million Trillion) from -9223372036854775808 to 9223372036854775807. Syntax “long long” is used instead of “long int”.
longlongmyLongIntegerValue = 827337203685421584;
It is default modifier of int and char data type if no modifier is specified. It says that user can store negative and positive values.
signedintmyNegativeIntegerValue = -544;signedintmypositiveIntegerValue = 544;/* Both of the statements have same meaning even without "signed" modifier*/
When user intends to store only positive values in the given data type (int and char).
unsignedintmyIntegerValue = 486;
———————-
Suze of program:
Wednesday, 17 February 2021
Lec2_1: tokens, keywords, constant......
What are Tokens?
C language syntax specify rules for sequence of characters to be written in C language. In simple language it states how to form statements in a C language program - How should the line of code start, how it should end, where to use double quotes, where to use curly brackets etc.
The rule specify how the character sequence will be grouped together, to form tokens. A smallest individual unit in C program is known as C Token. Tokens are either keywords, identifier, constants, Variables or any symbol which has some meaning in C language. A C program can also be called as a collection of various tokens.
In the following program,
#include
int main()
{
printf("Hello,World");
return 0;
}
if we take any one statement:
printf("Hello,World");
Then the tokens in this statement are→ printf
, (
, "Hello,World"
, )
and ;
.
So C tokens are basically the building blocks of a C program.
————————————————————
What are Keywords in C?
Keywords are preserved words that have special meaning in C language. The meaning of C language keywords has already been described to the C compiler. These meaning cannot be changed. Thus, keywords cannot be used as Variables names because that would try to change the existing meaning of the keyword, which is not allowed.(Don't worry if you do not know what variables are, you will soon understand.) There are total 32 keywords in C language.
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
const | extern | return | union |
char | float | short | unsigned |
continue | for | signed | volatile |
default | goto | sizeof | void |
do | if | static | while |
———————————————
What are Identifiers?
In C language identifiers are the names given to variables, constants, functions and user-define data. These identifier are defined against a set of rules.
Rules for an Identifier
- An Identifier can only have alphanumeric characters(a-z , A-Z , 0-9) and underscore(
_
). - The first character of an identifier can only contain alphabet(a-z , A-Z) or underscore (
_
). - Identifiers are also case sensitive in C. For example name and Name are two different identifiers in C.
- Keywords are not allowed to be used as Identifiers.
- No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as Identifier.
When we declare a variable or any function in C language program, to use it we must provide a name to it, which identified it throughout the program, for example:
int myvariable = 10;
Here myvariable
is the name or identifier for the variable which stores the value 10 in it.
Variables in C Language
When we want to store any information(data) on our computer/laptop, we store it in the computer's memory space. Instead of remembering the complex address of that memory space where we have stored our data, our operating system provides us with an option to create folders, name them, so that it becomes easier for us to find it and access it.
Similarly, in C language, when we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it.
The naming of an address is known as variable. Variable is the name of memory location. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer can choose a meaningful variable name. Example : average, height, age, total etc.
Datatype of Variable
A variable in C language must be given a type, which defines what type of data the variable will hold.
It can be:
char
: Can hold/store a character in it.int
: Used to hold an integer.float
: Used to hold a float value.double
: Used to hold a double value.void
Rules to name a Variable
- Variable name must not start with a digit.
- Variable name can consist of alphabets, digits and special symbols like underscore
_
. - Blank or spaces are not allowed in variable name.
- Keywords are not allowed as variable name.
- Upper and lower case names are treated as different, as C is case-sensitive, so it is suggested to keep the variable names in lower case.
Declaring, Defining and Initializing a variable
Declaration of variables must be done before they are used in the program. Declaration does the following things.
- It tells the compiler what the variable name is.
- It specifies what type of data the variable will hold.
- Until the variable is defined the compiler doesn't have to worry about allocating memory space to the variable.
- Declaration is more like informing the compiler that there exist a variable with following datatype which is used in the program.
- A variable is declared using the
extern
keyword, outside themain()
function.
extern int a;
extern float b;
extern double c, d;
Defining a variable means the compiler has to now assign a storage to the variable because it will be used in the program. It is not necessary to declare a variable using extern
keyword, if you want to use it in your program. You can directly define a variable inside the main()
function and use it.
To define a function we must provide the datatype and the variable name. We can even define multiple variables of same datatype in a single line by using comma to separate them.
int a;
float b, c;
Initializing a variable means to provide it with a value. A variable can be initialized and defined in a single statement, like:
int a = 10;
Let's write a program in which we will use some variables.
#include <stdio.h>
// Variable declaration(optional)
extern int a, b;
extern int c;
int main () {
/* variable definition: */
int a, b;
/* actual initialization */
a = 7;
b = 14;
/* using addition operator */
c = a + b;
/* display the result */
printf("Sum is : %d \n", c);
return 0;
}
Sum is : 21
Difference between Variable and Identifier?
An Identifier is a name given to any variable, function, structure, pointer or any other entity in a programming language. While a variable, as we have just learned in this tutorial is a named memory location to store data which is used in the program.
Identifier | Variable |
---|---|
Identifier is the name given to a variable, function etc. | While, variable is used to name a memory location which stores data. |
An identifier can be a variable, but not all indentifiers are variables. | All variable names are identifiers. |
Example:
| Example:
|