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

C Programing: by Mohammed Tajuddin

C programming keywords include reserved words like int, float, etc. Identifiers are names given to variables and functions and have specific naming rules. The basic data types in C are integer, floating point, character, and void. Variables are used to store and manipulate data in a program and have different scopes like local, global, static, and external. Format specifiers are used in input/output functions like scanf and printf to specify the type of data. Constants represent fixed values that cannot change during program execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

C Programing: by Mohammed Tajuddin

C programming keywords include reserved words like int, float, etc. Identifiers are names given to variables and functions and have specific naming rules. The basic data types in C are integer, floating point, character, and void. Variables are used to store and manipulate data in a program and have different scopes like local, global, static, and external. Format specifiers are used in input/output functions like scanf and printf to specify the type of data. Constants represent fixed values that cannot change during program execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 75

C PROGRAMING

BY MOHAMMED TAJUDDIN
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.
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
• 0123456789
C PROGRAMMING
• Special Characters
• Special Characters in C Programming
• ,< > . _
• () ; $ :
• % [ ] # ?
• '& { } "
• ^ ! * / |
• -\ ~ +
CPROGRAMMING

White space Characters

Blank space, newline, horizontal tab, carriage return and form feed.
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;
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.
C PROGRAMMING

• C Keywords
• auto double int struct
• break else long switch
• case enum register typedef
• char extern return union
• continuefor signed void
• do if staticwhile
• default goto sizeof volatile
• const float shortunsigned
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;
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.
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.
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
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.
C PROGAMMING
• Let's see the basic data types. Its size is given according to 32-bit
architecture.

• Data Types Memory Size Range


• 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
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
C PROGAMMING
• float 4 byte
• double 8 byte
• long double 10 byte
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;
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.
C PROGRAMMING
• Valid variable names:

• int a;
• int _ab;
• int a30;
• Invalid variable names:

• int 2;
• int a b;
• int long;
C PROGRAMMING
• Types of Variables i of variables in c:

• local variable
• global variablen C
• static variable
• automatic variable
• external variable
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.
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
•}
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);
•}
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.
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
•}
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.
C PROGRAMMING
• extern int x=10;//external variable (also global)
• program1.c
• #include "myfile.h"
• #include <stdio.h>
• void printValue(){
• printf("Global variable: %d", global_variable);
•}
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
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
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
• #include <stdio.h>
• main() {
• char ch = 'B';
• printf("%c\n", ch); //printing character data
• //print decimal or integer data with d and i
• int x = 45, y = 90;
• printf("%d\n", x);
• printf("%i\n", y);
C PROGRAMMING
• float f = 12.67;
• printf("%f\n", f); //print float value
• printf("%e\n", f); //print in scientific notation
• int a = 67;
• printf("%o\n", a); //print in octal format
• printf("%x\n", a); //print in hex format
C PROGRAMMING
• char str[] = "Hello World";
• printf("%s\n", str);
• printf("%20s\n", str); //shift to the right 20 characters including the
string
• printf("%-20s\n", str); //left align
• printf("%20.5s\n", str); //shift to the right 20 characters including
the string, and print string up to 5 character
• printf("%-20.5s\n", str); //left align and print string up to 5 character
C PROGRAMMING
• constants :
• Fixed value whose value cannot be changed entire execution of
programe.
• Integer constants :
• Decimal
• octal
• Hexa decimal
C PROGRAMMING
Floating point constants
Charecter constants
constants with in single cotations
Ex : ‘1’ , ‘A’ both uppercase and lowercase
C PROGRAMMING
• String constants

• collection of charectres with in double cotations .

• Ex : “robo”, “12345”
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


C PROGRAMMING
• #define A 10
• int main()
•{
• int a= A;
• printf(“%d”,a);
•}
• A is macro name best is to write in capital letters
C PROGRAMMING
#define MUL(a,b) a*b
int main()
{
int a=2;
int b=3;
printf(“%d”,MUL(a,b));
}
• it work as a function
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
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
C PROGRAMMING
• Unary operators :
• ‘-’ minus operator
• ‘++’ incriment operator
• 1)pre incriment operator
• 2)post incriment operator
• ‘--’ dicriment operator
• 1)pre dicriment operator
• 2)post dicriment operator
C PROGRAMMING
• ‘&’ address operator
• size of operator
• ‘-’ operator
• Ex : int main()
•{
• int a=10,b=5;
• c=a+(-b);}
• 10+(-5)=5
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
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
C PROGRAMMING
• ‘!’ logical not operator

• true value as false and false value is true


C PROGRAMMING
• Binary operators : (it has two operands)
• 1)Arithmetic (+, -, /, *, %)
• 2)Relational (<, >, <=, >=)
• 3)Logical (&&(AND), II(OR))
• 4)Bit wise(&, I, <<, >>, ^ (XOR), ~(bit wise nagation))
• 5)Equality (==, !=)
• 6)Comma
• 7)Assignment
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
C PROGRAMMING
• Arithmetic operators : Example:
int main(){
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)
}
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)
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 : < , > , <= , >= , == , !=
C PROGRAMMING
• Logical operators :
To compare 3 variables
symbols :
&& , || ,!
&& true true true, || true false true,
! false false true
C PROGRAMMING
• Dicission Control statements or
Conditional statements or Branching statements
they are
simple if , if - else , else - if ladder , nested if
switch statements
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”);
}
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);}}}
C PROGRAMMING
• if-else syntax :
if(condition)
{
true statement
}
else
{
false statement}
C PROGRAMMING
• else-if syntax :
if(condition){
true statement}
else if(condition){
true statement}
else{
false statement
}
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”);}
C PROGRAMMING
• Nested if statement sytax :
if(condition)
{
if(condition)
{
}
else
}
Example for nested if statement :
int x,y,z;
printf(“enter numbers”);
scanf(“%d%d%d”,&x,&y,&z);
if(x>y){
if(x>z){
printf(“x is big”);
}
C PROGRAMMING
else{
printf(“z is big”);
}
else{
if(y>z){
printf(“y is big”);}
else{
printf(“z isbig”);}
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”);}
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.


C PROGRAMMING
• syntax :
switch(condition/expression/constant value){
case lable1: statement;
break;
case labe2 : statement;
break;
default : statement;
break;}
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;
}}
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
C PROGRAMMING
Bitwise AND( & ) :
Example : a&b
a=101 ; b=110

101 Truth table(AND)


110
100 0 00
0 10
1 00
1 11
0=false ; 1=true
C PROGRAMMING
Bitwise ‘|’ (OR) : Truth Table
Example : a | b 0 00
a = 101 ; b = 110 0 11
101 1 01
110 1 11
111 1= true ; 0 = false
• Exclusive OR ‘^’ (or) Bitwise X - OR : Truth Table
a^b 0 00
a = 101 ; b = 110 0 11
101 1 01
110 1 10
011
C PROGRAMMING
Compliment ‘~’ :
a = 101
~a=010
Left shift operator ‘<<’ :
a = 10<<2
1010 ‘<<’ is 1010
Right shift operator ‘>>’ :
a = 10 ‘>>’ 2
1010 ‘>>’ 0010

You might also like