Psuc Manual
Psuc Manual
Psuc Manual
(AUTONOMOUS)
MADHURAWADA, VISAKHAPATNAM
R-2019 Regulations
Problem Solving Lab Using C
STEPS TO CREATE A WORKSPACE IN VISUAL STUDIO CODE
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 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.
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;
}
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.
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.
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
The printf function calls are expressions, so statements such as printf ("Hello World!\n"); are
expression statements.
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”);
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.
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
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
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
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
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
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