Arrays and Stringd
Arrays and Stringd
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
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”)
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();
}
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);
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;
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.
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.
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) {
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.
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.
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.
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.
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:
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,
#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:
int main()
{
int studmark1,studmark2.....studmark1000;
---
---
return 0;
}
*/
/*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.
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