You are on page 1of 19

GAYATRI VIDYA PARISHAD COLLEGE OF ENGINEERING

(AUTONOMOUS)

AFFILIATED TO JNTU- KAKINADA

MADHURAWADA, VISAKHAPATNAM

R-2019 Regulations
Problem Solving Lab Using C
STEPS TO CREATE A WORKSPACE IN VISUAL STUDIO CODE

 Create a New Folder(with your roll number or name) on your computer.

 Open Visual Studio Code either from start button or by double clicking its icon on
desktop

or
 Click on File menu of Visual Studio Code and select "add folder to workspace"
 Select the Folder created earlier (step1)

 Now click on “add” button to add it to the workspace

 Now the folder you have added will appear in the workspace.
 Now right click added folder from workspace and select new file

 Give a new file name with an extension of .c (for example sample.c) in the blank text box
and click enter
 Now an empty file with sample.c will be opened on the right side of the workspace.

 Type the program in the space provided.

 Save the program by pressing ctrl+s from the keyboard.

Steps to Execute a C Program:


 Open Terminal menu and click on New Terminal.

 Now a command prompt will open with your workspace directory at the bottom of the
editor.
 Type gcc filename.c to execute the program
 Type .\a to run your executable file and to check the output.
STRUCTURE OF A C PROGRAM
C Program structure is divided in to several sections.

Documentation Section
Link Section
Global Declaration Section
main()
{
Local Declarations
Program statements and Expressions
}
User Defined Functions

For Example:

#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}

Documentations (Documentation Section)

The Documentation Section consists of a set of comment lines giving the name of the program and
other details. The Link Section provides instructions to the compiler to link functions from the system
library.

Preprocessor Statements (Link Section)

C Preprocessor directives: Before a C program is compiled in a compiler, source code is processed by


aprogram called preprocessor. This process is calledpreprocessing. Commands used in preprocessor
are called preprocessor directives and they begin with “#” symbol.

Global Declarations (Definition Section)

Global variables hold their values throughout the lifetime of your program and they can be accessed
inside any of the functions defined for the program. A global variable can be accessed by any
function. That is, a global variable is available for use throughout your entire program after its
declaration.

The main() function

In C, the "main" function is treated the same as everyfunction, it has a return type (and in some cases
accepts inputs via parameters). The only difference is that the main function is "called" by the
operating system when the user runs the program.

Local Declarations

A scope in any programming is a region of the program where a defined variable can have its
existence and beyond that variable it cannot be accessed. There are three places where variables can
be declared in C programming language − Inside a function or a block which is called local variables.
Program Statements & Expressions

An expression statement consists of an optional expression followed by a semicolon (;). If the


expression is present, the statement may have a value. If no expression is present, the statement is
often called the null statement.

The printf function calls are expressions, so statements such as printf ("Hello World!\n"); are
expression statements.

User Defined Functions

A function is a block of code that performs a specific task. Callows you to define functions according
to your need. These functions are known as user-defined functions.
WEEK-1
Program1: C program to display hello world message

Aim:
The main objective of this program is to make familiar with printf() function and to print a
text on a console (monitor) in multiple ways using printf(). You need to use the below syntax
to write output on console.

Pre-Requisite:

printf() function:
printf() is a predefined function in "stdio.h" header file. stdio stands for Standard Input
Output. Using printf(), we can print data or a user defined message onto the standard output
console that is a monitor.

Syntax:
printf(“user defined message”); // example: printf(“Welcome to GVPCE”);

Commonly used escape sequences in printf are:


 \n (Newline)
 \t (Tab Space)
 \” (Double Quotes)
 \f (New Page)
 \b (Backspace)
 \r (Carriage Return)

Example:
Printf(“Hello\nWorld”);
Output:
Hello
World
Test Cases:
This program requires you to print “Hello World!!!” text in multiple ways by using above
specified escape sequences.
Input Format:
You do not need to read any input in this challenge.

Expected Output Format:


The argument for printf() must be Hello World!!!”. The program has to be tested for the
escape sequences that include new line (\n), tab space (\t), back space (\b), carriage return
(\r), Hello World!!! with in double quotes (\”).

S.No. Input Expected Output Actual Output Remarks


Program2: C program to scan all data type variables as input and
print it as output.
Aim:
The main objective of this program to make familiar with scanf() function and all the
primitive data types in C language.

Pre-Requisites:
scanf() function:
The scanf() function allows you to accept input (character, string, numeric data) from
standard input console that is a key board.
Syntax:
scanf(“format specifiers”,&var1,&var2……); // example: scanf(“%d %f”,&a,&b);
Here the format specifier tells the compiler that it is storing a value into the variable which is
of a particular data type. The following are the list of format specifiers used to read values for
the variables of primitive data types.
Data Type Key word Format Specifier
integer int %d
float float %f
character char %c

& used in the scanf function is an operator, called address operator, which is used to store the
value read from keyboard. It is used to access the memory location of the variable.
Note: &var1 in above syntax denotes the memory address of variable var1.

Datatypes in C:
Each variable in C has an associated data type. Each data type requires different amounts of
memory and has some specific operations which can be performed over it.
Different data types also have different ranges upto which they can store numbers. These
ranges may vary from compiler to compiler. Below is list of ranges along with the memory
requirement and format specifiers on 32 bit gcc compiler.
In general, the range of a particular data type for signed and unsigned variables can be
defined as follows:
For Unsigned variables, the range is 0 to 2n-1
For Signed variables, the range is -2n-1 to 2n-1-1
Note: To know the size of a variable of a particular data type, use sizeof() function.

MEMORY FORMAT
DATA TYPE RANGE
(BYTES) SPECIFIER

short int 2 -32,768 to 32,767 %hd


MEMORY FORMAT
DATA TYPE RANGE
(BYTES) SPECIFIER

unsigned short int 2 0 to 65,535 %hu

unsigned int 4 0 to 4,294,967,295 %u

int 4 -2,147,483,648 to 2,147,483,647 %d

long int 4 -2,147,483,648 to 2,147,483,647 %ld

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long long 0 to


8 %llu
int 18,446,744,073,709,551,615

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 1.2E-38 to 3.4E+38 %f

double 8 2.3E-308 to 1.7E+308 %lf

long double 12 -(2^95) to (2^95)-1 %Lf

Variable:
A variable in C language is a name given to a storage area that our programs can manipulate.
Each variable in C has a specific data type. Every variable declaration in a c program will end
with a semi colon.
Syntax to declare variable/ variables:
To declare a variable of a single data type, use the following syntax.
Data type variablename; // Example: int num1; or float total; or char ch;
To declare multiple variables of a single data type, use the following syntax.
Data type variablename1, variablename2,……,variablenameN; // Example: int num1,
num2, num3; or float total, average;
This program requires you to read all data type variables using scanf function and print them
onto the output screen using printf function
Test Cases:
Input Format:

Provide the input using keyboard in a sequence of character(X), integer(12) and real value(12.34)
in a Single line or Multiple line.
Expected Output Format:
First line: The char value read is X
Second line: The integer value read is 12 // Six Spaces before integer value without
using \t
Third line: The float value read is 12.34

S.No. Input Expected Output Actual Output Remarks


Program3: C program to perform arithmetic operations like +,-
,*,/,% on two input variables.

Aim:
The main objective of this program to make familiar with all arithmetic operators in C
language.

Pre-Requisites:
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division and modulus division on numerical values.

The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 7 and variable B holds 2 then

Operator Description Example

+: Addition Adds two operands. A+B=9

−: Subtraction Subtracts second operand from the first. A−B=5

*: Multiplies both operands. A * B =


Multiplication 14

/: Division Divides numerator by de-numerator. A/B=3

%: Modulus Modulus Operator gives remainder after an integer A%B=1


division.

fflush() function
fflush() is a predefined function in "stdio.h" header file. It is typically used for output stream
only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console
(in case of stdout)

Syntax:
fflush (stdin); // on execution the output buffer will be cleared

Formatted printf() syntax:


Printf(“Format Specifiers”, List of variables);
Formats for various outputs

SNO FORMAT EXPLANATION


1 %wd w is width in integer and d is conversion specification
w is width in integer, c specifies number of digits after decimal point
2 %w.cf
and f specifies conversion specification
w is width for total characters, c is used for displaying leading
3 %w.cs
blanks and s specifies conversion specification

Test Cases:
Input Format:

Provide the input using keyboard in a sequence of two integers or two real values or two
characters in a Single line or multiple lines.
Example:
12 3
Expected Output Format:
The addition is 15
The difference is 9
The product is 36
The quotient is 4
The remainder is 0

S.No. Input Expected Output Actual Output Remarks


Program4: C program to perform temperature conversions from
Centigrade to Fahrenheit and vice versa.

Aim:
The main objective of this program to make familiar with all expression evaluation and type
casting in C language.

Pre-Requisites:
Expression:
An expression is a sequence of operands and operators that reduces to a single value. For
example, the expression, 10+5 reduces to the value of 15.
Syntax:
Variable=expression
All variables used in the expression must be declared and assigned values before evaluation is
attempted. Examples of expressions are:
x= a + b * c;
x= (a + b) * c;
Expressions are evaluated based on operator precedence and associativity rules when an
expression contains more than one operator.
x=5+3*2; // output: x=11
x= (5+3)*2; // output: x=16

Type casting:
A type cast is basically a conversion from one type to another. There are two types of type
conversion
1. Implicit Type conversion:
Also known as „automatic type conversion‟. Done by the compiler on its own.
Generally takes place when in an expression more than one data type is present. In
such condition type conversion (type promotion) takes place to avoid lose of data.
ex: int x;
float y;
// x is implicitly converted to float
float z = x + 1.0;
2. Explicit Type conversion:
This process is also called type casting and it is user defined. Here the user can type
cast the result to make it of a particular data type.
The syntax in C:
(Type) expression
ex: double x = 1.2;
// Explicit conversion from double to int
int sum = (int) x + 1;

Problem Analysis:
This problem can be solved by using below formula.
Formula for temperature conversion is
(0°C × 9/5) + 32 = 32°F

Test Cases:
Input Format:

Provide the input using keyboard in a sequence of two values (oC and oF) in a Single
line or Multiple line.
Ex: 100oC
100oF
Expected Output Format:
The temperature in Fahrenheit is 212.000000 F
The temperature in Celsius is 37.777780 C

S.No. Input Expected Output Actual Output Remarks

You might also like