2. C PROGRAMMING
• C Keywords and Identifier
• In this chapter you will learn about keywords; reserved words in C
programming that are part of the syntax. Also, you will learn about identifiers
and how to name them.Character set
• A character set is a set of alphabets, letters and some special characters that are
valid in C language.
3. C PROGRAMMING
• Alphabets
• Uppercase: A B C ................................... X Y Z
• Lowercase: a b c ...................................... x y z
• C accepts both lowercase and uppercase alphabets as variables and
functions.
• Digits
• 0 1 2 3 4 5 6 7 8 9
4. C PROGRAMMING
• Special Characters
• Special Characters in C Programming
• , < > . _
• ( ) ; $ :
• % [ ] # ?
• ' & { } "
• ^ ! * / |
• - ~ +
6. C PROGRAMMING
• C Keywords
• Keywords are predefined, reserved words used in programming that
have special meanings to the compiler. Keywords are part of the
syntax and they cannot be used as an identifier. For example:
Ex : int money;
7. C PROGRAMING
• Here, int is a keyword that indicates money is a variable of type int
(integer).
• As C is a case sensitive language, all keywords must be written in
lowercase. Here is a list of all keywords allowed in ANSI C.
8. C PROGRAMMING
• C Keywords
• auto doubleint struct
• break else long switch
• case enum register typedef
• char extern return union
• continue for signed void
• do if static while
• default goto sizeof volatile
• const float short unsigned
9. C PROGRAMMING
• C Identifiers
• Identifier refers to name given to entities such as variables, functions,
structures etc.
• Identifiers must be unique. They are created to give a unique name to
an entity to identify it during the execution of the program. For
example:
• int money;
• double accountBalance;
10. C PROGRAMMING
• Here, money and accountBalance are identifiers.
• Also remember, identifier names must be different from keywords.
You cannot use int as an identifier because int is a keyword.
11. C PROGRAMMING
• Rules for naming identifiers
• A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
• The first letter of an identifier should be either a letter or an
underscore.
• You cannot use keywords like int, while etc. as identifiers.
• There is no rule on how long an identifier can be. However, you may
run into problems in some compilers if the identifier is longer than 31
characters.
• You can choose any name as an identifier if you follow the above rule,
however, give meaningful names to identifiers that make sense.
12. C PROGAMMING
Data Types in C
A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
c data types :
Primary or Basic datatype
Derived datatype
Enumeration datatype
void dataype
13.
14. C PROGAMMING
• There are the following data types in C language.
• Basic Data Type : int, char, float, double
• Derived Data Type :array, pointer, structure, union
• Enumeration Data Type : enum
• Void Data Type : void
• Basic Data Types
• The basic data types are integer-based and floating-point based. C language
supports both signed and unsigned literals.
• The memory size of the basic data types may change according to 32 or 64-bit
operating system.
15. C PROGAMMING
• Let's see the basic data types. Its size is given according to 32-bit
architecture.
• Data Types Memory SizeRange
• char 1 byte −128 to 127
• signed char 1 byte −128 to 127
• unsigned char 1 byte 0 to 255
• short 2 byte −32,768 to 32,767
• signed short 2 byte −32,768 to 32,767
16. C PROGRAMMING
• int 2 byte −32,768 to 32,767
• signed int 2 byte −32,768 to 32,767
• unsigned int 2 byte 0 to 65,535
• short int 2 byte −32,768 to 32,767
• signed short int 2 byte −32,768 to 32,767
• unsigned short int 2 byte 0 to 65,535
• long int 4 byt -2,147,483,648 to 2,147,483,647
• signed long int 4 byte -2,147,483,648 to 2,147,483,647
• unsigned long int 4 byte 0 to 4,294,967,295
18. C PROGRAMING
• Variables in C
• A variable is a name of the memory location. It is used to store data.
Its value can be changed, and it can be reused many times.
• It is a way to represent memory location through symbol so that it
can be easily identified.
• Let's see the syntax to declare a variable:
• type variable_list;
19. C PROGAMMING
• Rules for defining variables
• A variable can have alphabets, digits, and underscore.
• A variable name can start with the alphabet, and underscore only. It
can't start with a digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword, e.g. int,
float, etc.
20. C PROGRAMMING
• Valid variable names:
• int a;
• int _ab;
• int a30;
• Invalid variable names:
• int 2;
• int a b;
• int long;
21. C PROGRAMMING
• Types of Variables i of variables in c:
• local variable
• global variablen C
• static variable
• automatic variable
• external variable
22. C PROGRAMMING
• Local Variable
• A variable that is declared inside the function or block is called a local
variable.
• It must be declared at the start of the block.
• void function1(){
• int x=10;//local variable
• }
• You must have to initialize the local variable before it is used.
23. C PROGRAMMING
• Global Variable
• A variable that is declared outside the function or block is called a global
variable. Any function can change the value of the global variable. It is
available to all the functions.
• It must be declared at the start of the block.
• int value=20;//global variable
• void function1(){
• int x=10;//local variable
• }
24. C PROGRAMMING
• Static Variable
• A variable that is declared with the static keyword is called static variable.
• It retains its value between multiple function calls.
• void function1(){
• int x=10;//local variable
• static int y=10;//static variable
• x=x+1;
• y=y+1;
• printf("%d,%d",x,y);
• }
25. C PROGRAMMING
• If you call this function many times, the local variable will print the
• same value for each function call, e.g, 11,11,11 and so on. But the
• static variable will print the incremented value in each function call,
• e.g. 11, 12, 13 and so on.
26. C PROGRAMMING
• Automatic Variable
• All variables in C that are declared inside the block, are automatic
variables by default. We can explicitly declare an automatic variable
using auto keyword.
• void main(){
• int x=10;//local variable (also automatic)
• auto int y=20;//automatic variable
• }
27. C PROGRAMMING
• External Variable
• We can share a variable in multiple C source files by using an external
variable. To declare an external variable, you need to use extern
keyword.
31. C PROGRAMMING
• The format specifiers :
• These are used in C for input and output purposes. Using this concept
• the compiler can understand that what type of data is in a variable
• during taking input using the scanf() function and printing using
• printf() function. Here is a list of format specifiers
32. C PROGRAMMING
• Format Specifier Type
• %c Character
• %d Signed integer
• %e or %E Scientific notation of floats
• %f Float values
• %g or %G Similar as %e or %E
• %hi Signed integer (short)
• %hu Unsigned Integer (short)
• %i Unsigned integer
• %l or %ld or %li Long
33. C PROGRAMMING
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
34. • #include <stdio.h>
• main() {
• char ch = 'B';
• printf("%cn", ch); //printing character data
• //print decimal or integer data with d and i
• int x = 45, y = 90;
• printf("%dn", x);
• printf("%in", y);
35. C PROGRAMMING
• float f = 12.67;
• printf("%fn", f); //print float value
• printf("%en", f); //print in scientific notation
• int a = 67;
• printf("%on", a); //print in octal format
• printf("%xn", a); //print in hex format
36. C PROGRAMMING
• char str[] = "Hello World";
• printf("%sn", str);
• printf("%20sn", str); //shift to the right 20 characters including the
string
• printf("%-20sn", str); //left align
• printf("%20.5sn", str); //shift to the right 20 characters including
the string, and print string up to 5 character
• printf("%-20.5sn", str); //left align and print string up to 5 character
37. C PROGRAMMING
• constants :
• Fixed value whose value cannot be changed entire execution of
programe.
• Integer constants :
• Decimal
• octal
• Hexa decimal
38. C PROGRAMMING
Floating point constants
Charecter constants
constants with in single cotations
Ex : ‘1’ , ‘A’ both uppercase and lowercase
39. C PROGRAMMING
• String constants
• collection of charectres with in double cotations .
• Ex : “robo”, “12345”
40. C PROGRAMMING
• Pre processor directive constants :
• it is not a part of compiler
• it is the process before compilation done by prprocessor ‘#’
• it is text substitution tool
41. C PROGRAMMING
• #define A 10
• int main()
• {
• int a= A;
• printf(“%d”,a);
• }
• A is macro name best is to write in capital letters
43. C PROGRAMMING
#define MAX(a,b) if(a>b)
printf(“%d is max”,a);
else
printf(“%d is max”,b);
printf(“%d”,MAX(5,6));
}
• #undef is for undefine macro Ex : #undef MAX
44. C PROGRAMMING
• Operators in c
• Types of operators based on operands
• 1)Unary
• 2)Binary
• 3)Ternary
• one operand is there in unary operators
• Two operands are there in binary operators
• Three operands are there in ternary operators
46. C PROGRAMMING
• ‘&’ address operator
• size of operator
• ‘-’ operator
• Ex : int main()
• {
• int a=10,b=5;
• c=a+(-b);}
• 10+(-5)=5
47. C PROGRAMMING
• ‘++’ incriment operator
• Ex : (post incriment)
int main(){
int x=10,y=11;
y=x++;
printf(“%d”,y);
printf(“%d”,x);
} //o/p : 10,11
48. C PROGRAMMING
• Ex : pre incriment operator :
int main(){
int a=10,b=11;
b=++a;
printf(“%d”,b);
printf(“%d”,a);
}
o/p : 11,11 //same as pre and post dicriment operator
49. C PROGRAMMING
• ‘!’ logical not operator
• true value as false and false value is true
51. C PROGRAMMING
• Ternary operator : (it require 3 operands)
• rules for ternary operator
• (condition)expressin1?expression2:expression3
• if condition is true expression 2 executed if not expression 3
executed
Ex : int a=10,b=11;
x=(a>b)?a:b//o/p 11
it is same as if -else statement
52. C PROGRAMMING
• Arithmetic operators : Example:
int mai(){
int a=10,b=11;
c=a+b;
printf(“%d”,c)
c=a-b,a*b,a/b,a%b;
printf(“%d%d%d%d”,c)
}
53.
54. C PROGRAMMING
• ‘=’ Assignment operator : Example :
rule : left side should be variable
a=b=c=d=10;
a+=1 (a=a+1)
a-=1 (a=a-1)
55. C PROGRAMMING
• Relational operators :
• it is used to comparision between two variables
and it will give result either 1 or 0
means true or false.
symbols : < , > , <= , >= , == , !=
58. C PROGRAMMING
• Dicission Control statements or
Conditional statements or Branching statements
they are
simple if , if - else , else - if ladder , nested if
switch statements
59. C PROGRAMMING
in conditional statements relational operators and logicl operators
play key role.
xample for simple if :
int a=0, b=5;
if(a<b)
{
printf(“a is small”);
}
60. C PROGRAMMING
• Example for if-else :
int main(){
int a=10,b=11;
printf(“show the result”);
if(a>b){
printf(“%d”,a);
else{
printf(“%d”,b);}}}
63. C PROGRAMMING
Example for ef-else ladder :
int x,y;
printf(“enter numbers”);
scanf(“%d%d”,&x,&y);
if(x>y){
printf(“ x is big”);}
else if(x<y){
printf(“y is small”);}
else{
printf(“both are same”);}
67. C PROGRAMMING
• Example for logical && operator :
int a=10,b=20,c=30;
if(a>b)&&(a>c){
printf(“a is large”);}
else if(b>a)&&(b>c){
printf(“b is large”);}
else{
printf(“c is large”);}
68. C PROGRAMMING
Switch statement :
switch allows expression to have integerbased evaluation while if
statements allows both integer and charecter based evaluations.
switch is multiple multyway decision making statement.
69. C PROGRAMMING
• syntax :
switch(condition/expression/constant value){
case lable1: statement;
break;
case labe2 : statement;
break;
default : statement;
break;}
70. C PROGRAMMING
int ch;
printf(“who is favorite hero”);
scanf(“%d”,&ch);
switch(ch){
case 1: printf(“nani”);
break;
case 2: printf(“mahesh babu”);
break;
default: printf(“invalid case”);
break;
}}
71. C PROGRAMMING
• Bit wise operators :
Bitwise operators are used to perform operations in bits.bitwise
operators works on binary numbers.
& bitwise AND
| bitwise OR
^ bitwise Ex-OR
~ compliment
<< left shift operator
>> right shift operator