Lab 3
Lab 3
Lab 3
Objectives
To Learn about compiler installation, basic C program, basic data types, basic input or output and the
use of variables.
Instructions
• Read, the text provided in bulleted form in different paragraphs, with proper attention.
• Perform all examples as shown.
• First complete the examples and tasks then try and test your own programs.
1. Programming
A programming language is a formal language designed to communicate instructions to a machine,
particularly a computer. Programming languages can be used to create programs that control the behavior
of a machine and/or to express algorithms precisely
1.2 Compiler
A compiler is a computer program (or set of programs) that transforms source code written in a
programming language (the source language) into another computer language (the target language, often
having a binary form known as object code). The most common reason for wanting to transform source
code is to create an executable program.
1.3 IDE
An Integrated Development Environment (IDE) is a software that provides comprehensive consolidated
environment to computer programmers to develop software applications. An IDE consists of various
components such as a code editor, build automation tools, debuggers, and sometimes visual designers. We
will use the Dev-C++ IDE to develop C programs. Dev-C++ is a free, open-source, lightweight IDE for C and
C++ programming that uses the MinGW (Minimalist GNU for Windows) port of the GCC compiler,
supporting efficient program development on the Windows platform.
1.3.1 Basic Components of an IDE
• Code Editor (CE):
A code editor is a specialized text editor designed for writing and editing the source code of computer
programs. It is an essential part of an IDE, providing features like syntax highlighting and code formatting
to make programming easier and more efficient. In Dev-C++, the built-in code editor supports both C and
C++ languages, allowing you to write, edit, and organize your code within a single interface.
• Build Automation Tools (BAT):
Build automation tools streamline the process of creating software builds. This involves compiling source
code into object code, linking libraries, and generating the final executable file. In Dev-C++, the build
automation process is handled using the MinGW compiler, which compiles and links your code into a
runnable program. These tools simplify the process, allowing for easy building and testing of software
without manual intervention.
• Debugger:
The debugger is a critical component of an IDE that assists in testing and identifying bugs in software. Dev-
C++ integrates with the GDB (GNU Debugger) to help developers test their code, set breakpoints, inspect
variables, and track down issues in complex applications. This makes debugging an efficient process,
allowing you to find and fix errors directly within the IDE.
1.3.2 Few Features of Dev-C++
• Syntax highlighting
• Code completion
• Debugging
• Code searching
• Project templates
• Single-platform compilation
1.10.1 Keywords
Keywords are the reserved words by the compiler. Every keyword has assigned a fixed special
meaning and therefore, cannot be used as a variable name. There are 32 keywords available in
C which have been listed in Figure 14.
1.10.2 Identifiers
Identifiers are the symbolic names of Arrays, functions, structures, variables etc. (all will be
covered in later sections). In simple words, identifiers are user defined names. There are
certain rules to be followed for using identifiers as a names of variables, arrays, functions etc.
• An identifier must start with an alphabet (a-z or A-Z).
• After the first alphabet, the remaining letters may be a sequence of alphabets and/or digits.
• digits (0 − 9), ’_’ and $ are allowed in identifiers
• An identifier can not start with digit.
• Constants only cannot be used as identifiers.
• identifiers, ABC, abc and AbC are all different.
• In C compiler, maximum length allowed for an identifier is 31 characters.
• An identifier should not be a C keyword.
Here we only cover basic/primitive data types. As shown in Figure. 15, the basic data types of
C language are Integer (int), character (char), floating point (float), and void. Brief explanation
of each data type is given below:
1. Integer (int)
C compilers offer short, long, signed and unsigned integer data types. Table. 3 and Table. 4
outline brief description of these data types.
2. Character (char)
C compilers offer signed and unsigned character data types. Table. 5 outlines brief description
of character data type.
1.10.4 Constants
Constant meaning in C is the value, which remains the same during the execution process of a
program. C language offers several types of constants, which have been explained below:
1. Numerical constants:
Numerical constants are of two types:
• Integer constants:
Integer constants are the constants, which are in whole numbers form (without decimal)
and require 2 to 4 bytes place in memory to store. These include decimal,
hexadecimal as well as octal numbers and can be found in both positive and negative
forms. Examples of integer constants are: 13 (decimal), 0171 (octal) and 0x 54
(Hexadecimal) etc.
• Real constants:
Real constants are also known as floating point constants. These are the numbers,
which contain decimal. Examples of real constants are: 2.1, 1.342, 30.03 or 3.7e-5 etc.
2. Character constants:
Character constants are also of two types:
• Single character constants:
Single character constants are based on single character only. If a single character,
single digit or a single special character is enclosed in pair of single quotation
marks(’ ’) , it represents a character constant. Examples of character constant are:
’A’, ’b’, ’4’, ’-’ etc.
• String constants:
When a sequence of characters is enclosed in double quotetion marks (" "), it represents
a string. A string can be a combination of all possible types of symbols.
Examples of strings are: "Pakistan", "123a", "333" etc.
Syntax of Constant
const data_type variable_name ;
Examples of Constant
const double PI = 3.14;
const int a = 2;
1.10.5 Variables
A variable is a container (storage area) to hold data. To specify the storage region, a distinctive
name should be provided to each variable. Variable names are just the symbolic representation
of a memory location. In the program, a variable’s value can be changed, hence the name of
the variable.
Syntax of Variable
data_type variable_name ;
Example of Variable
Note: A variable is declared, anywhere in the program. If it is declared outside main function
it is called global variable. Such variable can be used anywhere in the program. If it declared
inside the main function it is called local variable. Such variable cannot be used outside main
function.
When a variable is declared but not initialized with some value it stores a garbage value.
1.10.6 Programs Using Variables, User defined I/O operations, Format specifiers and Escape sequence
Note: sizeof() is an operator which tells about the size of data types depending upon on your
OS.
4) Create a C program to print formatted output as below. (Hint: use printf() field-width specifier)
5) Write a C program to take the user’s name, age and salary as input and display them using formatted output.
6) Write a C program to calculate the area of a rectangle. Take the length and width as input from the user.
7) Write a C program to take the user’s name and age as input and display them using formatted output.
8) Write a C program that prompts the user to input an integer, a floating-point number, and a character.
Assignment # 1
1) Write a C program to convert temperature from Celsius to Fahrenheit. Use the formula:
Fahrenheit = (Celsius * 9/5) + 32
2) Write a C program to calculate the volume of a cube. The volume of a cube is given by:
Volume = side * side * side
3) Write a C program to take three numbers as input and calculate their sum and average.
4) Write a C program to convert a given number of days into years, weeks, and days.
5) Study what is Steganography and create a program to generate below output using
ASCII codes