0% found this document useful (0 votes)
97 views

SDF I - Lecture 4 Variables Keywords Datatypes

The document outlines a module on data types, operators, and control flow in C programming. It discusses data types in C including variables, constants, keywords, and format specifiers. It also covers C operators and control flow structures like if, else, while, for, and switch statements. The lecture outline provides details on C program structure, compilers, CodeBlocks IDE, and examples of variable declaration, constants, and format specifiers like %c, %s, %d.

Uploaded by

Adya Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

SDF I - Lecture 4 Variables Keywords Datatypes

The document outlines a module on data types, operators, and control flow in C programming. It discusses data types in C including variables, constants, keywords, and format specifiers. It also covers C operators and control flow structures like if, else, while, for, and switch statements. The lecture outline provides details on C program structure, compilers, CodeBlocks IDE, and examples of variable declaration, constants, and format specifiers like %c, %s, %d.

Uploaded by

Adya Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Software Development Fundamentals (SDF) – I

ODD 2020

Module 02:Data types, operators,


and Control Flow
Module Outline
 Data, variables and constants, data types in C programming
 Operators – binary, unary, ternary, operator precedence,
operations using different operators in C programming
 Control Flow- if, if-else, while, do-while, for, switch-case in
C programming
Lecture Outline
• C Programming – Introduction and General Structure
• C Compiler – Online and Offline
• Code Blocks – Download and Installation
• Code Blocks – Brief User Manual
• Data in C
• Variables in C
• Keywords in C
• Constant in C
• Data Types in C

3
C Programming – Introduction and General Structure
1. C is a structured programming language
2. C program consists functions (user defined functions and library functions) and variables
3. Functions contain instructions corresponding to the computations and operations to be performed,
whereas variables stores values and used during computations
4. main() in c programming is user defined as well as predefined. Because main() is programmer/user
implemented function, whose prototype is predefined in the compiler.
5. main() function is the entry and exit point of a C program & must be there in a C program

General Structure #include <stdio.h>


int main()
{
1. Include Header Files
int A, B;
2. main function/method printf("Enter an integer value");
3. Variable declaration scanf("%d", &A);
4. Body of the function/method B = A * 5;
5. Return printf("5 x %d is %d", B);
return 0;
}
C Compiler – Online and Offline
1. C is a high level language
2. Instructions are user’s friendly
3. Not understood by Computers
4. Need to convert into machine understandable
language, i.e. Machine Language
5. C compiler converts the program into M/c Language,
i.e. we need to compile the program for its execution
6. gcc is one of the C compilers
7. Many online C compilers are available to compile your
program
8. Code Blocks is one of the platforms (IDE) which
facilitate you to compile the program offline
Code Blocks – Download and Installation

codeblocks-20.03mingw-setup.exe

http://www.codeblocks.org/downloads/26
Code Blocks – Brief User Manual

1 3
Code Blocks – Brief User Manual

3 5
1. Source File, main
2. Code Block editor
having C program
6 3. Compile / Build
4. Compilation / Run
message display
2 block
1 5. Run / Execute
6. Output popup
window

4
Data
• Data is a piece of information that we used in programming to execute any program.

• Data stores the numbers, words, measurements, observations or just descriptions of


things.

• Data are characteristics or information, usually numerical or text.

• These data are used in the programming language to execute the programme.

9
Variables

•Variables are used to store the data.


•If you declare a variables in the programme means memory will be allocated
to that variable when program executes.
•These variable value can be changed during the programme execution.
•But if any variable is declared constant means that variable value can’t be
change.
•Each variable should be given a unique name that unique name will be used
throughout the programme.
10
• Variable names are just the symbolic representation of a memory location.

• Variables are stored in memory and each variable has an unique address in memory
region. For each variables memory is reserve in the computer.

• It is difficult to remember the unique address, thus for convenience, you can assign
a name to the variables.

• In variables you can store different type of data such as integer, string, float,
characters. All these data are of different sizes in computer.

11
Variable declaration syntax:

datatype variablename;

For example: int marks=95

Here marks is a variable of int type and the variable is assigned an


values 95.

Variable marks is stored in memory and variable marks have some


memory address and here marks has 1000 memory address. In the
memory these variables looks like:
marks 95
1000
12
Rules for Variable/Identifiers declaration
 Variable declaration contains only letters, digits and under score
characters,
example: marks, student_list

 Variable declaration must be begin with either a letter of the alphabet or


an underscore character.
 Variable can not be same as the keywords, example it can not be void,
int, extern, char etc.
 Variables names are case sensitive. For example amount, Amount and
AMOUNT all three are different variable names.
 Maximum length of the variable can be 31 characters.
 Special characters can’t be used in variable declaration for e.g. blank
space, #, $, *, semicolon, comma etc.
13
Constants in C
• If you want to define a variable whose value can not be change during program
execution, you can use the const keyword just before the variable declaration.
• This will create a constant variable .
syntax:
const datatype constant_name

14
• For example,
const double root_two = 1.414;

• We have added keyword const to make the variable constant. Here root_two, is a
constant variable. Its value can’t be change.

• When you try to change root_two from 1.414 to 2.9 it will give an error.
root_two= 2.9; // error= constant value can’t change.

• You can also define a constant using the #define pre-processor directive.

15
Keywords in C
• There are certain words that are reserved in C.
• These words are known as keywords and each keyword have some
meaning.
• Keywords are always written in lowercase.
• There are 32 keywords available in C which are given below:
auto extern sizeof break static case
for struct goto switch const if
typedef enum signed default
int union long continue
unsigned do register void
double return volatile else
short while float char
16
Format Specifiers
• Format specifiers are used with printf() statement for printing the data that are
stored in variable.

• To print the data stored in variable you need to use the format specifier.

• Format specifiers start with a percentage % operator and followed by a special


character for identifying the type of data.
Format Specifier Description
%d int
%c char
%f float
%lf double
%s string 17
Character format specifier : %c String printing : %s

#include <stdio.h>
#include <stdio.h>
int main()
int main()
{
{
char a[] = “JIIT";
char ch = 'A';
printf("%s\n", a);
printf("%c\n", ch);
return 0;
return 0;
}
}

Output: Output: JIIT


A
Integer format specifier : %d, %i

#include <stdio.h>
int main()
{
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", x);
printf("%u\n", x);
return 0;
}

45
45
45
Difference between %d and %i format specifier in C
When you are printing using the printf function, there is no specific difference between the %i and %d
format specifiers. But both format specifiers behave differently with scanf function.

The %d format specifier takes the integer number as decimal but the %i format specifier takes the integer
number as decimal, hexadecimal or octal type. it means the %i automatically identified the base of the input
integer number.
#include <stdio.h>
int main()
{
int data1, data2, data3;
printf("Enter value in decimal format:");
scanf("%d",&data1);
printf("data1 = %i\n\n", data1);
printf("Enter value in hexadecimal format:");
scanf("%i",&data2);
printf("data2 = %i\n\n", data2);
printf("Enter value in octal format:");
scanf("%i",&data3);
printf("data2 = %i\n\n", data3);
return 0;
}
Integer format specifier : %d, %i Floating-point format specifier : %f, %e or %E

#include <stdio.h>
#include <stdio.h>
int main()
int main()
{
{
float a = 12.67;
int x = 45, y = 90;
printf("%f\n", a);
printf("%d\n", x);
printf("%e\n", a);
printf("%i\n", x);
return 0;
printf("%u\n", x);
}
return 0;
}

Output:
45 12.670000
45 12.67000+e01
45
Use of special elements with %f Format specifiers (octal number): %o

#include <stdio.h>
int main() #include <stdio.h>
{ int main()
float data = 6.276240; {
printf("%f\n", data); int data = 65;
printf("%0.2f\n", data); printf("%o\n", data);
printf("%0.4f\n", data); return 0;
return 0; }
}

Output:
6.276240
6.28 Output: 101
6.2762
Use of special elements with %s

#include <stdio.h>
int main()
{ • A minus(-) sign tells left alignment.
char str[] = "Welcometojiit128"; • A number after % specifies the minimum field width to be
printf("%s\n", str); printed if the characters are less than the size of width the
printf("%3s\n", str); remaining space is filled with space and if it is greater than it
printf("%20s\n", str); printed as it is without truncation.
printf("%-20s\n", str); • A period( . ) symbol separate field width with the precision.
printf("%.3s\n", str); • Number after . tells that the maximum number of character
printf("%8.3s\n", str); to be printed
return 0;
}

Output
Welcometojiit128
Welcometojiit128
Welcometojiit128
Welcometojiit128
Wel
Wel
Data Types in C
• C supports different types of data such as integer, character, float and string etc.

• C provides various types of data-types which allow the programmer to select the
appropriate type for the variable to set its value.

• Data types are used to define the type of variable when it declared.

• Data types are used to define the return type of function.

• Data types are used to define the types of parameter expected by a function.

• C supports many data types out of which the basic types are:

void, int, float , double and char.


24
Derived and User Defined data
types
• The derived data types are built using one or more primary data types.
• Example: arrays, functions, structures, pointers.
• In C, a user is allowed to define a data types as per the requirement. These data-
types are referred as user-defined data types.
• Examples of user-defined data-types include structure, union, enum.
Four Basic Data Types
• In C, there are 4 basic data types:
char,
int,
Float
Double
• 'char' is used to store any single character
• ‘int' is used to store integer value
• 'float’ is used for storing single precision floating point number
• 'double' is used for storing double precision floating point number.

26
Char Data type
• Char type variable store single character.

• Size of char type variable is 1 or 2 byte depends on operating system


configuration.

• The range of char is -128 to 127.

• All character have a numeric code associated with them, so while storing a
character variable its numeric value gets stored.

• The set of numeric code is called as “ASCII”, American Standard Code for
Information Interchange.
27
Source: http://www.haghish.com/statistics/stata-blog/stata programming/ascii_characters.php 28
Char data type
• Characters (char) – To store a character into a char variable, you must enclose it
with SINGLE QUOTE marks i.e. by ‘ ’.
• The double quotes are reserved for string (a collection of character).
• You can assign a char variable as an integer i.e. its integer value.
‘a’, ‘z’ , ‘A’ , ‘Z’ , ‘?’, ‘@’, ‘0’, ‘9’
Declaration of char variable:
char c; OR char c, k, l;

Initialization of char variable:


char a=‘x’;
Declaration and initializing can be done on separate lines.
char c;
c = ‘a’; 29
Printing value of char variable

• printf( “%c”, a);


Output: a
This causes the “ %c” to be replaced by value of character variable a.

• printf(“\n %c”, a);


Output: (blank space for new line)
a
“\n” is a new line character which will print the value of variable on newline.

30
Program using character constants
#include<stdio.h>
int main()
{
char p, q, r, s; /* declare char variables*/
char m=‘e'; /* declare char variable*/
p=‘W'; /* initialize the rest */
q='e'; /* q=e is incorrect */
r='l'; /* s=“c” is incorrect*/
s=‘c’;
t=‘o’;
u= 109 /* the ASCII value of m is 109 */
printf("%c%c%c%c%c%c%c", p, q, r, s, t, u, m);
return 0;
} Output: Welcome

31
Integer Data Type
• Integer data types are allows to store numeric value in variables.
• Integer data type are represented as int.
• Size of int type variable is 2 or 4 byte depends on operating system
configuration.
• If you try to store float value in integer variable then integer part will be stored
in variable and the decimal part is ignored and the value assigned is rounded
down from the actual value.
• Also assigning a character constant to an int variable assigns the ASCII value.

32
• Declaration on Integer variable:
int student_id;
• Here, student_id is a variable of type int.
• You can declare multiple variables at once in C programming. For example,
int student_id, marks;
• Input through scanf() and printing through printf() of Integer variable will be
done by %d.
• The size of int is usually 2 bytes (32 bit OS). And, it can take 2^32 distinct
states from -2147483648 to 2147483647

33
Program to print different variables.
#include<stdio.h>
main()
{ int a,b,c,d,e;
a=5;
b=8.2;
c=5.3; Output:
d='A'; a=5
e= 5.3 +8.2; b=8.2
printf("\n a=%d",a); c=5.3
printf("\n b=%d",b); d=65
printf("\n c=%d",c); e=13
printf("\n d=%d",d); b+c=13
printf("\n e=%d",e);
printf(“ \n b+c=%d”,b+c);
return 0;
}
34
 FLOATING POINT DATA TYPE:
• Floating point data type consists of 2 types. They are,
float
double
1. FLOAT:
• Float data type allows a variable to store decimal values.

• Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.

• We can use up-to 6 digits after decimal using float data type.

• For example, 10.456789 can be stored in a variable using float data type.
35
 FLOATING POINT DATA TYPE:

• Floats are relatively easy to use but problems tend to occur when performing division
In general:
An int divided by an int returns an int.
An int divided by a float returns a float.
A float divided by an int returns a float.
A float divided by a float returns a float.
Declaration:
float f=3.4;

36
2. DOUBLE:

• Double data type is also same as float data type which allows up-to 15 digits after decimal.
• The range for double datatype is from 1E–37 to 1E+37.
• Declaration of double variable:
double price;

What is the difference between float and double:

• The size of float (single precision float data type) is 4 bytes. and the size of double (double
precision float data type) is 8 bytes.
• Input using scanf() and printing using printf() of float data type will be done by %f.

37
int and float data types
#include<stdio.h>
main( )
{ Output:
float x=8.0; a=8.000000
float y= 8.00 + 7.00; b=15.000000
printf(“x=%f “,x); Here output is till 6 digit after decimal.
printf(“y=%f “,y); If you want to print till 3 digit the write %.3f
return 0; inplace of %f.
} Changing the statement:
printf(“x=%.3f" ,x);
printf(“y=%.3f" ,y);
Output:
a=8.000
b=15.000
38
• http://www.haghish.com/statistics/stata-blog/stata programming/ascii_characters.php
• https://www.programiz.com/c-programming/c-data-types
• https://www.w3schools.in/c-tutorial/format-specifiers/

39

You might also like