23252Study-materials-of-BCA-semester-1-c-programming-keyworddata-types-Lecture-2
23252Study-materials-of-BCA-semester-1-c-programming-keyworddata-types-Lecture-2
Prepared By
Alok Haldar
Assistant professor
Department of Computer Science & BCA
Kharagpur College
Differences between Keyword and Identifier
Keyword Identifier
C Operators :
◦ Arithmetic Operators
◦ Relational Operators
◦ Shift Operators
◦ Logical Operators
◦ Bitwise Operators
◦ Ternary or Conditional Operators
◦ Assignment Operator
◦ Misc Operator
Precedence of Operators in C :
int value=10+20*10;
The value variable will contain 210 because * (multiplicative operator) is
evaluated before + (additive operator).
Comments in C :
#include<stdio.h>
int main()
{
//printing information
printf("Hello C");
return 0;
}
Output:
Hello C
Even you can place the comment after the statement. For example:
1. /*
2. code
3. to be commented
4. */
1. #include<stdio.h>
2. int main(){
3. /*printing information
4. Multi-Line Comment*/
5. printf("Hello C");
6. return 0;
7. }
C Format Specifier :
The Format specifier is a string used in the formatted input and output
functions. The format string determines the format of the input and
output. The format string always starts with a '%' character.
ASCII value in C :
We will create a program which will display the ascii value of the
character variable.
#include<stdio.h>
int main()
{
char ch;//variable declaration
printf("Enter a character");
scanf("%c",&ch);//user input
printf("\n The ascii value of the ch variable is : %d",ch);
return 0;
}
In the above code, the first user will give the character input, and the
input will get stored in the 'ch' variable. If we print the value of the 'ch'
variable by using %c format specifier, then it will display 'A' because we
have given the character input as 'A', and if we use the %d format
specifier then its ascii value will be displayed, i.e., 65.
Output
The above output shows that the user gave the input as 'A', and after
giving input, the ascii value of 'A' will get printed, i.e., 65.
Now, we will create a program which will display the ascii value of all the
characters.
#include<stdio.h>
int main()
{
int k;//variable declaration
for(int k=0;k<=255;k++)//for loop from 0-255
{
printf("\nThe ascii value of %c is %d", k,k);
}
return 0;
}
The above program will display the ascii value of all the characters. As we
know that ascii value of all the characters starts from 0 and ends at 255,
so we iterate the for loop from 0 to 255.
Now we will create the program which will sum the ascii value of a string.
#include<stdio.h>
int main()
{
int sum=0;//variable initialization
char name[20];//variable initialization
int i=0;//variable initialization
printf("Enter a name: ");
scanf("%s",name);
while(name[i]!='\0')//while loop
{
printf("\nThe ascii value of the character %c is %d",
name[i],name[i]);
sum=sum+name[i];
i++;
}
printf("\nSum of the ascii value of a stringis:%d",sum);
return0;
}
In the above code, we are taking user input as a string. After taking user
input, we execute the while loop which adds the ascii value of all the
characters of a string and stores it in a 'sum' variable.
Constants in C :
Constant Example
1. const keyword
2. #define preprocessor
1. C const keyword
#include<stdio.h>
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
Output:
#include<stdio.h>
int main(){
const float PI=3.14;
PI=4.5;
printf("The value of PI is: %f",PI);
return 0;
}
Output:
2) C #define preprocessor
Tokens in C
Classification of tokens in C
◦ Identifiers in C
◦ Strings in C
◦ Operators in C
◦ Constant in C
◦ Special Characters in C
Let us understand each token one by one.
Keywords in C
do if static while
Identifiers in C
Strings in C
char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a'
array.
char a[] = "javatpoint"; // The compiler allocates the memory at the run
time.
Operators in C
Unary Operator
◦ Arithmetic Operators
◦ Relational Operators
◦ Shift Operators
◦ Logical Operators
◦ Bitwise Operators
◦ Conditional Operators
◦ Assignment Operator
◦ Misc Operator
Constants in C
A constant is a value assigned to the variable which will remain the same
throughout the program, i.e., the constant value cannot be changed.
Constant Example
Some special characters are used in C, and they have a special meaning
which cannot be used for another purpose.
Programming Errors in C :
Errors are the problems or the faults that occur in the program, which
makes the behaviour of the program abnormal, and experienced
developers can also make these faults. Programming errors are also
known as the bugs or faults, and the process of removing these bugs is
known as debugging.
◦ Syntax error
◦ Run-time error
◦ Linker error
◦ Logical error
◦ Semantic error
Syntax error :
Syntax errors are also known as the compilation errors as they occurred at
the compilation time, or we can say that the syntax errors are thrown by
the compilers. These errors are mainly occurred due to the mistakes while
typing or do not follow the syntax of the specified programming language.
These mistakes are generally made by beginners only because they are
new to the language. These errors can be easily debugged or corrected.
For example:
#include<stdio.h>
int main()
{
a=10;
printf("The value of a is:%d",a);
return 0;
}
In the above output, we observe that the code throws the error that 'a' is
undeclared. This error is nothing but the syntax error only.
There can be another possibility in which the syntax error can exist, i.e., if
we make mistakes in the basic construct. Let's understand this scenario
through an example.
#include<stdio.h>
int main()
{
int a=2;
if(.)//syntax error
printf("a is greater than 1");
return 0;
}
In the above code, we put the (.) instead of condition in 'if', so this
generates the syntax error as shown in the below screenshot.
Run-time error :
Sometimes the errors exist during the execution-time even after the
successful compilation known as run-time errors. When the program is
running, and it is not able to perform the operation is the main cause of
the run-time error. The division by zero is the common example of the
run-time error. These errors are very difficult to find, as the compiler does
not point to these errors.
int main()
{
int a=2;
int b=2/0;
printf("The value of b is:%d",b);
return 0;
}
In the above output, we observe that the code shows the run-time error,
i.e., division by zero.