Basics

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Birla Institute of Technology & Science Pilani, Hyderabad

Campus
Computer Programming (CS F111)
2021-22 Second semester
Lab1
Topics to be covered:
1. C Program structure and execution
2. Data Types
3. Reading input and printing output

1. C PROGRAM STRUCTURE AND EXECUTION FLOW


Let’s write a simple C program.
Step-1: Typing the program. Use the vi editor to create a file (for Unix
Env.).
$ vi firstprog.c
Step-2: Type the following program in the file, firstprog.c
#include<stdio.h> /* Include Header Files */
int main() /* main() is the entry point of the program */
{
printf ("Welcome to C Programming"); return 0;
}
Note:
1. In the above program, the statements that appear enclosed
in “/* */” are called comments.
2. Each instruction in a C program is written as a separate
statement, ending with a semicolon.

[Rest of the steps are for Unix Env.]


Step-3: Save and quit the file.
Step-4: Now, let’s run gcc compiler over this source code to create the
executable. $gcc firstprog.c -o exeFirstProg
In the above command:
✔ gcc – Invokes the GNU C compiler
✔ firstprog.c – Your C program file name (this contains your source
code)
✔ -o exeFirstProg – Instructs C compiler to create the executable with
the name exeFirstProg. If you don’t specify “-o” and the name of the
executable file, C compiler will create an output executable with the
name a.out.
Step-6: Finally, execute exeFirstProg (if no errors are displayed in the
above step). This step will execute exeFirstProg to display the final
output.

$./exeFirstProg

or
$./a.out (if during compilation the name of the output file was not given)

Data Types in C
✔ char: The most basic data type in C. It stores a single character and requires a
single byte of memory in almost all compilers.
✔ int: It is used to store an integer value.
✔ float: It is used to store decimal numbers (numbers with floating point value) with
single precision.
✔ double: It is used to store decimal numbers (numbers with floating point value)
with double precision.

Summary of Data Types


READING DATA FROM THE KEYBOARD
scanf function is used to accept the input from the user through keyboard. This
function is defined in stdio.h file, that’s why we have to include stdio.h file.

int scanf(“control string”, &variable1, &variable2 …


variableN);

Note: Missing the ampersand in scanf function is the most


common C Programming error because in this case compiler may
not produce any compile time error.

Control String:
It contains field specifications which direct the interpretation of input data. It consists
of conversion character % and type specifier. It is written in double quotes. Various
type specifiers are listed below.
For example:
int a; // variable is declared
scanf(“%d”,&a); // taking integer input into variable from
user

Writing on screen:
printf function is used to print output on console or monitor screen. It is similar to
scanf function, except it does not prefix with ampersand before the variable name.
Syntax of printf is as follows.

Syntax: printf(“<format string>”,<list of variables>);

Type the following program in a file called userinput.c


#include<stdio.h>
int main()
{
int a;
int b;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}

Exercises
1. Write a program to read a character and print the character using two format
specifiers %d and %c, and explain the output.

2. Write a program to print the minimum and maximum values of int and long int
using limits.h header file.

3. Write a program to define an integer variable and unsigned integer variable and
assign 2147483648 to both variables. Print both variables, Is there any difference
between both values? If yes, why?

4. Write a program to input a floating-point number and print it with 4 precision


values, i.e. 4 digits after the decimal point.

You might also like