0% found this document useful (0 votes)
59 views

Arrays and Stringd

The document discusses various types of errors in C programming such as syntax errors, runtime errors, linker errors, logical errors, and semantic errors. It then provides examples of common errors like undeclared variables, using a single equal sign for equality checks, undeclared functions, missing semicolons after statements, overstepping array boundaries, incomplete loops, missing ampersand operators in scanf, and infinite loops. It also discusses special control statements like break and continue. Finally, it covers storage classes in C like automatic, external, static, and register variables and provides examples of each.

Uploaded by

umamaheshsjcet
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Arrays and Stringd

The document discusses various types of errors in C programming such as syntax errors, runtime errors, linker errors, logical errors, and semantic errors. It then provides examples of common errors like undeclared variables, using a single equal sign for equality checks, undeclared functions, missing semicolons after statements, overstepping array boundaries, incomplete loops, missing ampersand operators in scanf, and infinite loops. It also discusses special control statements like break and continue. Finally, it covers storage classes in C like automatic, external, static, and register variables and provides examples of each.

Uploaded by

umamaheshsjcet
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

Common errors in C programming

Types of errors in Programming

SYNTAX ERROR:

RUNTIME ERROR:

LINKER ERRORS:

LOGICAL ERRORS:

SEMANTIC ERRORS:
Example of Common Errors in C programming
a) Undeclared variables:
int main()
{
int b,c;
c=a+b;
printf("%d\n",c);
return 0;
}

b) Incomplete/Wrong usage of operators: Using a single equal sign to check your equality:

c) Undeclared functions: The function should be declared above the main function in c
programming.
include<stdio.h>
void sum(int count);
int main()
{
int count;
while(count<100)
{
printf("%d\n",count);
count++;
}
sum(count);
return 0;
}
void sum(int count)
{
printf("%d\n",count);

}
d) Termination of statements:

Semicolons don’t go after if statements, for loop and function definitions. Hence the right way of
writing the above program is

e) Overstepping array boundaries:


f) Incomplete loops

for(i=1;i<=10;i++)
sum1=sum1+i;
sum2=sum2+i*i;
printf(“%d%dn”,sum1,sum2);

Correct answer

for(i=1;i<=10;i++) {
sum1=sum1+i;
sum2=sum2+i*i;
}
printf(“%d%dn”,sum1,sum2);
g) Improper usage of "" and '' : "" is used for declaring a string whereas '' is used for
initializing a character.
if(response==YES)
Correct answer:
if(response==”YES”)

h) Forgetting/Ignoring the precedence of operators


if(value = product() >= 100)
tax=0.05*value;
Correct answer:
if((value = product()) >= 100)
tax=0.05*value;
i) Missing & operators in scanf operator

j) Infinite loops:

int k=3;
while (k==3)
{
Printf("%d", k);
}
The above program leads to an infinite loop and continuously prints the value of K. This infinite
loop can be avoided by writing this way.

int k=3;
while (k==3)
{
Printf("%d", k);
k++;
}
/*undeclared variables,using single sign to chk
equality,undeclared functions,
termination of statements,incomplete loops,missing &
operator,infinite loop,overstepping array boundaries*/
#include<stdio.h>
void undeclared();
void singleequalsign();
void semicolon_forloopend();
void incomplete_loops();

int sum(int count);


int main()
{
undeclared();
singleequalsign();
semicolon_forloopend();
incomplete_loops();
int count;
while(count<100)
{
printf("%d\n",count);
count++;
}
sum(count);
return 0;
}
void undeclared()
{
int a=2,b=10,c;
c=a+b;
printf("undeclared result=%d\n",c);

}
void singleequalsign()
{
int x=10,y=20;
if(x=y)
{
printf("single equal sign=%d\n",x);
}
}
int sum(int count)
{
printf("count=%d\n",count);
return 0;

}
void semicolon_forloopend()
{
int i;
for(i=1;i<=10;i++);
{
printf("semicolon_forloopend-%d\n",i);
}
}
void incomplete_loops()
{
int i,sum=0;
for(i=0;i<10;i++)
{

sum=sum+i;
sum++;
}
printf("incomplete_loop=%d\n",sum);

Special Control Statements: break and continue


The break statement terminates the innermost loop immediately when it's encountered. It's also
used to terminate the switch statement.

The continue statement skips the statements after it inside the loop for the iteration.

for (i=1;i<=10;++i)
{
if (i==3)
continue;
if (i==7)
break;
printf("%d ",i);
}

Output

12456
When i is equal to 3, the continue statement comes into effect and skips 3. When i is equal to 7,
the break statement comes into effect and terminates the for loop. To learn more, visit C break
and continue statement

#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==3)
continue;
if(i==7)
break;
printf("%d\t",i);
}

return 0;
}

Goto statement:

goto labelname;

break,continue,functioncall eliminate the need for goto statement .

labelname can be declared same like a variable.

The statement label must be followed by a colon.just like a case label in a switch.Like other
statements goto statement ends with a semicolon.

Goto statement is not considered a goof programming statement when overused.


It makes a program more difficult to read and maintain
#include<stdio.h>
int main()
{
int n;
printf("Enter a number:=");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even:
printf("\nThe number is EVEN");
odd:
printf("The number is ODD");
return 0;
}

Storage Classes:
Every variable in C programming has two properties: type and storage class.

Type refers to the data type of a variable. And, storage class determines the scope, visibility and
lifetime of a variable.

There are 4 types of storage class:

automatic
external
static
register
Local Variable
The variables declared inside a block are automatic or local variables. The local variables exist
only inside the block in which it is declared.
Let's take an example.

#include <stdio.h>

int main(void) {

for (int i = 0; i < 5; ++i) {


printf("C programming");
}

// Error: i is not declared at this point


printf("%d", i);
return 0;
}
When you run the above program, you will get an error undeclared identifier i. It's because i is
declared inside the for loop block. Outside of the block, it's undeclared.

Let's take another example.

int main() {
int n1; // n1 is a local variable to main()
}

void func() {
int n2; // n2 is a local variable to func()
}
In the above example, n1 is local to main() and n2 is local to func().

This means you cannot access the n1 variable inside func() as it only exists inside main().
Similarly, you cannot access the n2 variable inside main() as it only exists inside func().

Global Variable
Variables that are declared outside of all functions are known as external or global variables.
They are accessible from any function inside the program.

Example 1: Global Variable


#include <stdio.h>
void display();

int n = 5; // global variable

int main()
{
++n;
display();
return 0;
}

void display()
{
++n;
printf("n = %d", n);
}
Output

n=7
Suppose, a global variable is declared in file1. If you try to use that variable in a different file
file2, the compiler will complain. To solve this problem, keyword extern is used in file2 to
indicate that the external variable is declared in another file.

Register Variable
The register keyword is used to declare register variables. Register variables were supposed to be
faster than local variables.

However, modern compilers are very good at code optimization, and there is a rare chance that
using register variables will make your program faster.

Unless you are working on embedded systems where you know how to optimize code for the
given application, there is no use of register variables.

Static Variable
A static variable is declared by using the static keyword. For example;

static int i;
The value of a static variable persists until the end of the program.

Example 2: Static Variable


#include <stdio.h>
void display();

int main()
{
display();
display();
}
void display()
{
static int c = 1;
c += 5;
printf("%d ",c);
}
Output

6 11
During the first function call, the value of c is initialized to 1. Its value is increased by 5. Now,
the value of c is 6, which is printed on the screen.

During the second function call, c is not initialized to 1 again. It's because c is a static variable.
The value c is increased by 5. Now, its value will be 11, which is printed on the screen.

32 Keywords in the C language.

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while
Type casting in c:
What is Typecasting in C?
Typecasting is converting one data type into another one. It is also called as data conversion or type
conversion. It is one of the important concepts introduced in 'C' programming.

'C' programming provides two types of type casting operations:

Implicit type casting


Explicit type casting

Implicit type casting


Implicit type casting means conversion of data types without losing its original meaning. This type of
typecasting is essential when you want to change data types without changing the significance of the
values stored inside the variable.

Implicit type conversion happens automatically when a value is copied to its compatible data type.
During conversion, strict rules for type conversion are applied. If the operands are of two different data
types, then an operand having lower data type is automatically converted into a higher data type. This
type of type conversion can be seen in the following example.

#include<stdio.h>
int main(){
int a=10; //initializing variable of short data type
double b; //declaring int variable
b=a; //implicit type casting
printf("%d\n",a);
printf("%ld\n",b);
}
Output

10
10.
Converting Character to Int
Consider the example of adding a character decoded in ASCII with an integer:

#include <stdio.h>
main() {
int number = 1;
char character = 'k'; /*ASCII value is 107 */
int sum;
sum = number + character;
printf("Value of sum : %d\n", sum );
}
Output:

Value of sum : 108


Here, compiler has done an integer promotion by converting the value of 'k' to ASCII before performing
the actual addition operation.
Explicit type casting
In implicit type conversion, the data type is converted automatically. There are some scenarios in which
we may have to force type conversion. Suppose we have a variable div that stores the division of two
operands which are declared as an int data type.

int result, var1=10, var2=3;


result=var1/var2;
In this case, after the division performed on variables var1 and var2 the result stored in the variable
"result" will be in an integer format. Whenever this happens, the value stored in the variable "result"
loses its meaning because it does not consider the fraction part which is normally obtained in the
division of two numbers.

To force the type conversion in such situations, we use explicit type casting.

It requires a type casting operator. The general syntax for type casting operations is as follows:

(type-name) expression
Here,

The type name is the standard 'C' language data type.


An expression can be a constant, a variable or an actual expression.
Let us write a program to demonstrate implementation of explicit type-casting in 'C'.

#include<stdio.h>
int main()
{
double a = 1.2;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of a is %lf\n", a);
printf("Value of b is %d\n",b);
return 0;
}
Output:

Value of a is 1.200000
Value of b is 2
ARRAYS AND STRINGS:

Learning Objectives:

1. Understand what an array is?


2. Learn about 1-dimensional Array, their declarations,
initialization, ways to access individual array elements,
representation of array elements in memory and other possible
operations.
3. Learn about 2-dimensional arrays, Initialization of sized and
unsized two-dimensional arrays, accessing elements in such
arrays, and how this kind of arrays can be used.
4. Learn about 1-dimensional strings and the way they are
declared, initialized, manipulated, inputted and displayed
5. Know about array of strings, its declaration, initialization,
other operations, manipulations and uses.
6. Get a brief idea of three dimensional arrays or even larger
ones.
Why arrays?

//we have a list of 1000 student's marks of an integer type.


/*if using the basic data type (int) we will declare something like the following

int main()
{
int studmark1,studmark2.....studmark1000;
---
---
return 0;
}
*/

Ordinary variable and array


/*by using array we just do like this int studmark[1000]; this will reserve 1000
contigious memory locations for storing the students marks*/

/*ordinary variable: capable of holding only one value at a time. If there is large amount
of similar data to be handled, then using a different variable for each data item would make the
job unwieldy, tedious and confusing.*/

/*instead, on combining all this similar data into an array ,the whole task of organising
and manipulating data would become easier and more efiicient*/

Note:
scalar variable or single variable whose stored value is an atomic type.
Contrast to scalar is Aggregate type,which is referred as both a structured type and data structure,is
any type whose values can be decomposed and are related by some defined structure.

Derived data type is array

2nd Example for why array:


A program that can print input in reverse order?

ARRAY
An array is a collection of data of the same type (and therefore, the same size) stored in
consecutive memory cells under one name.
Arrays are quite adjustable, just like a bag; you can carry 10 books a day or 15 the other day.
Suppose you aren't aware of the number of values a user or a system is going to input, and then

you'll definitely need an array with a maximum limiting size


Real time Applications:

2D Arrays, generally called Matrices are mainly used in Image processing


It is used in Speech Processing where each speech signal is an array of Signal Amplitudes

Searching and sorting techniques.

- An entire array is declared all at once.


- Each individual element in the array can be referenced by indexing. Indexed means the array
elements are numbered and always start at 0.
In C, an element of an array (i.e., an individual data item) is referred to by specifying the array
name followed by one or more subscripts, with each subscript enclosed in square brackets
Types of Arrays:

You might also like