Professional Documents
Culture Documents
OBJECTIVE:
This lab will introduce basic concepts of C Language. At the end of this lab, you should be able to:
APPARATUS:
• Laptop\PC with following tools installed o Visual Studio Code with C/C++ and Code Runner
Extensions o C/C++ mingw-w64 tools for Windows 10
COMMENTS IN C:
A comment is an explanation or description of the source code of the program. It helps a developer explain
logic of the code and improves program readability. A simple comment can save a significant amount of
time otherwise wasted on having to re-understand the code. Comments can also be used to display
identifying information: the program name, the date written, the author, the purpose of program, and so
forth. At compilation time, comments are ignored by the compiler.
1) Multi-line Comments: A comment that starts with a slash asterisk /* and finishes with an asterisk
slash */ and you can place it anywhere in your code, on the same line or several lines. i.e., anything
between /* and */ will be considered as comment. These comments must be terminated otherwise all the
program will be turned into comments.
2) Single-line Comments: which uses a double slash // dedicated to comment single lines. i.e., anything
after // will be considered as comment till end of line.
/* This is an example of multi-line comments in C Language Program.
This type of comment can span over multiple lines */
// Example of single-line comment in C Language Program
1|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
The C preprocessor is exactly what its name implies. It is a program that processes our source program
before it is passed to the compiler. The preprocessor is a text substitution tool that modifies the source code
before it is compiled. This modification is done according to the preprocessor directives that are included in
the source files. The directives are easily distinguished from normal programming code in that they all start
with a hash sign (#). They must always appear as the first non-whitespace character on a line and do not
need to end with a semicolon. These preprocessor directives extend only across a single line of code. As
soon as a newline character is found the preprocessor directive is considered to end.
The preprocessor directives provide the ability for the inclusion of header files, macro expressions,
conditional compilation, and line control etc. Here we discuss only two important preprocessor directives:
The #include directive inserts the contents of a file into the current source file. Its most common use is to
include header files (.h), both user-defined and library ones. Library header files are enclosed between angle
brackets (<>). This tells the preprocessor to search for the header in the default directory where it is
configured to look for standard header files.
Header files that you create for your own program are enclosed within double quotes (""). The preprocessor
will then search for the file in the same directory as the current file. In case the header is not found there, the
preprocessor will then search among the standard header files.
#include <fileName> /* search library directory */
#include "fileName" /* search current, then default */
Syntax for #include Directive
#include <stdio.h> /* search library directory */
#include "myfile.h" /* search current, then default */
Examples of #include Directive
The double quoted form can also be used to specify an absolute or relative path to the file.
Object-like Macros
An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like
because it looks like a data object in code that uses it. They are most used to give symbolic names to numeric
constants. To define object-like macros we can use #define directive. Its format is:
#define identifier replacement
2|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
By convention, constants should be named in uppercase letters with each word separated by an underscore.
That way, they are easy to spot when reading the source code. Wherever, DELAY is found as a token, it is
replaced by with 20. (see following example)
#define DELAY 20
Macro Example for a Numeric Value
Example 2.1: Use of an Object-like Macro
/* Example_2_1.c: Use of an Object-like Macro
----------------------------------------------------------------------------
This example program demonstrates the definition and usage of an Object-like
Macro in a C Language Program.
----------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 19-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */
#include<stdio.h> // include library file
#define PI 3.141593 // macro definition
int main()
{ printf("The value of Pi is %f", PI); // display the macro
value
Return
0;
}
Undefine Macro
A #define directive should not be used to directly override a previously defined macro. Doing so will give a
compiler warning unless the macro definitions are the same. To redefine an existing macro, it first needs to
be undefined using the #undef directive. Attempting to undefine a macro that is not currently defined will
not generate a warning.
#define DELAY 20
#undef DELAY #define
DELAY 10
Example of Redefining a Macro
3|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
Predefined Macros
Macro Description
__FILE__ The name and path of current file
__LINE__ The current line number
__DATE__ The compilation date in MM DD YYYY format
__TIME__ The compilation time in HH:MM:SS format
__func__ The name of the current function. Added in C99
__STDC__ Defined as 1 if the compiler complies with the
ANSI C standard.
There are several macros that are predefined by the compiler. To distinguish them from other macros, their
names begin and end with two underscores. The standard macros that all ANSI C-
compliant compilers include are listed in the following table. A common use for predefined macros is to
provide debugging information.
Example 2.2: Use of Pre-defined Macros
/* Example_2_2.c: Use of Pre-defined Macros
-----------------------------------------------------------------------------
This example program illustrates the use of Pre-defined Macros in C Language.
It will display current function, line, file name and its compilation time.
-----------------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 19-Sep-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */
#include<stdio.h>
int main()
{
// display pre-defined Macros values printf("This statement is in
function %s at line %d", __func__, __LINE__); printf("\nCompilation time
of %s is %s", __FILE__, __TIME__);
return 0;
}
4|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
Although macros can be powerful, they tend to make the code more difficult to read and debug. Macros
should therefore only be used when they are necessary and should always be kept short. C code such as
constant variables, enums, and inline functions can often accomplish the same goal more efficiently and
safely than #define directives can.
TASK 2.1: Pre-defined Macros [1 point]
Write a C language program that displays all the predefined macro
values on screen for this file.
The value of __FILE__ macro is Task_2_1.c
The value of __LINE__ macro is 16
The value of __DATE__ macro is Sep 19 2021
Sample Output
The value of __TIME__ macro is 16:11:20
The value of __func__ macro is main
The value of __STDC__ macro is 1
DISPLAYING OUTPUT IN C:
As you know, the printf function, available in stdio.h file, can be used to print values and variables to the
standard output stream. This is done by embedding format specifiers into the string where the value is to be
printed. Each specifier must be matched by a corresponding argument to printf of the correct type, as seen
in the following example
printf("This statement is in function %s at line %d", __func__, __LINE__);
Example of Format Specifiers for printf Function
Pre-defined macro __func__ is a string and __LINE__ is an integer value therefor we have embedded format
specifiers %s and %d to the positions in string where we want to print their values. We will see the complete
list of these format specifiers in our next lab.
5|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
int main()
{ int num1, num2; // define two int
variables
printf("Enter 1st number >> "); scanf("%d",
&num1); // take 1st input number
printf("Enter 2nd number >> "); scanf("%d", &num2);
// take 2nd input number
printf("The sum of %d and %d is %d", num1, num2, num1 +
num2); return 0;
}
6|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
7|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
Write a C program that a number as input from user and displays it with
a double quote on it.
Enter any number you like >> 25
Sample Output
You entered "25"
Create a Visual Studio Code Workspace, Lab_2 and c files (Task_2_1.c to Task_2_8.c) for individual tasks
and add them to Lab_2 workspace. You need to submit all these files on Google Classroom as part of this
lab evaluation.
|Page ComputerProgramming-ILab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
|Page ComputerProgramming-ILab