drronakpanchal.files.wordpress.com  · web viewall the data types of the variables are upgraded to...

12
1. Explain conversion function in detail. A type cast is basically a conversion from one type to another. There are two types of type conversion: Implicit Type Conversion: Done by the compiler on its own, without any external trigger from the user. 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. All the data types of the variables are upgraded to the data type of the variable with largest data type It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float). int x = 10; // integer x char y = 'a'; // character c // y implicitly converted to int. ASCII // value of 'a' is 97

Upload: others

Post on 09-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

1. Explain conversion function in detail.A type cast is basically a conversion from one type to another. There are two types of type conversion:Implicit Type Conversion:

Done by the compiler on its own, without any external trigger from the user.

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.

All the data types of the variables are upgraded to the data type of the variable with largest data type

It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).

int x = 10; // integer x

char y = 'a'; // character c

// y implicitly converted to int. ASCII

// value of 'a' is 97

x = x + y;

// x is implicitly converted to float

float z = x + 1.0;

printf("x = %d, z = %f", x, z)

Page 2: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

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

Type indicated the data type to which the final result is converted.

double x = 1.2;

// Explicit conversion from double to int

int sum = (int)x + 1;

printf("sum = %d", sum)

2. Write a short note on debugging.Debugging is the routine process of locating and removing computer program bugs, errors or abnormalities, which is methodically handled by software programmers via debugging tools. Debugging

Page 3: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

checks, detects and corrects errors or bugs to allow proper program operation according to set specifications.Debugging is also known as debug

Bug: In IT, a bug refers to an error, fault or flaw in any computer program or a hardware system. A bug produces unexpected results or causes a system to behave unexpectedly. In short it is any behavior or result that a program or system gets but it was not designed to do.

Bug fix: A bug fix is a change to a system or product designed to handle a programming bug/glitch. Many different types of programming bugs that create errors with system implementation may require specific bug fixes that are successfully resolved by a development or other IT team.

Bug tracking : is a process used by quality assurance personnel and programmers to keep track of software problems and resolutions. A bug tracking system is normally put in place to store information about reported bugs. This type of issue-tracking system provides a clear, centralized overview of development requests and their corresponding state

Exception handling : is a mechanism in which a programming construct is used to consistently trap, intercept and handle the error occurred during application execution

Quality assurance (QA) is the process of verifying whether a product meets required specifications and customer expectations. QA is a process-driven approach that facilitates and defines goals regarding product design, development and production. QA's primary goal is tracking and resolving deficiencies prior to product release

3. Storage ClassStorage Classes in C Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program

C language uses 4 storage classes, namely

Page 4: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

1. auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto variable was declared. However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables resides. They are assigned a garbage value by default whenever they are declared.

2. extern: Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program

3 static: This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.

4. register: This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is not available, these are then stored in the memory only. Usually few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers.

1. Short notes on types of error.

Error is an illegal operation performed by the user which results in abnormal working of the program.

Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors

Page 5: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

should be removed before compiling and executing.The most common errors can be broadly classified as follows.

a. Syntax errors :  Errors that occur when you violate the rules of writing C/C++ syntax are known as syntax errors. This compiler error indicates something that must be fixed before the code can be compiled.

b. Run-time Errors : Errors which occur during program execution(run-time) after successful compilation are called run-time errors. One of the most common run-time error is division by zero also known as Division error. These types of error are hard to find as the compiler doesn’t point to the line at which the error occurs.

c. Logical Errors : On compilation and execution of a program, desired output is not obtained when certain input values are given. These types of errors which provide incorrect output but appears to be error free are called logical errors. These are one of the most common errors done by beginners of programming.

d. Linker Errors : hese error occurs when after compilation we link the different object files with main’s object using Ctrl+F9 key(RUN). These are errors generated when the executable of the program cannot be generated. This may be due to wrong function prototyping, incorrect header files. One of the most common linker error is writing Main() instead of main().

e. Semantic errors :  This error occurs when the statements written in the program are not meaningful to the compiler.

1. Explain getchar( ), gets( ) and getch( ) function with example.getchar is a function in C programming language that reads a single character from the standard input stream stdin, regardless of what it is, and returns it to the program. It is specified in ANSI-C and is the most basic input function in C. It is included in the stdio.h header file

#include <stdio.h>

int main(void)

{

char str[10090];

int ch, n = 0;

Page 6: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

while ((ch = getchar()) != EOF && n < 1000) {

str[n] = ch;

++n;

}

for (int i = 0; i < n; ++i)

putchar(str[i]);

putchar('\n'); /* trailing '\n' needed in Standard C */

return 0;

gets() Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached

// C program to illustrate

// gets()

#include <stdio.h>

#define MAX 15

int main()

{

char buf[MAX];

printf("Enter a string: ");

gets(buf);

printf("string is: %s\n", buf);

return 0;

}

Page 7: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

getch function prompts a user to press a character and that character isn't printed on screen, getch header file is conio.h. This function is not a part of standard C library

/* Function getch in C example */

#include<stdio.h>

#include<conio.h>

main()

{

printf("Waiting for a character to be pressed from the keyboard to exit.\n");

getch();

return 0;

2. Forward and backward jumpGoto Statement in CThe Goto statement in C Programming is used to alter the flow of a program. When the compiler reaches the goto statement then it will jump unconditionally ( both forward and backward ) to the location specified in the goto statement (we called it as label)

Unlike, the Break and Continue Statement, goto statement in C doesn’t require any If Statements to perform

Page 8: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

/* Goto Statement in C Programming example */

#include <stdio.h>

int main()

{

int Totalmarks;

printf(" \n Please Enter your Subject Marks \n ");

scanf("%d", & Totalmarks);

if(Totalmarks >= 50)

goto Pass;

else

goto Fail;

Pass:

printf(" \n Congratulation! You made it \n");

Fail:

printf(" \n Better Luck Next Time \n");

Page 9: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

return 0;

3. Explain enum data type in detail.Enumeration (or enum) in CEnumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain

enum State {Working = 1, Failed = 0};

The keyword ‘enum’ is used to declare new enumeration types in C and C++. Following is an example of enum declaration

// An example program to demonstrate working

// of enum in C

#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()

{

enum week day;

day = Wed;

Page 10: drronakpanchal.files.wordpress.com  · Web viewAll the data types of the variables are upgraded to the data type of the variable with largest data type. It is possible for implicit

printf("%d",day);

return 0;

}

Output:

2