Cse-It - Day1 C Programming
Cse-It - Day1 C Programming
C Programming
Enhancing Programming Skills-C Programming
Topic Highlights
S.No Topics Duration
(in hours)
Introduction to C Programming
Why C?
• Performance
– Execution Time
• Interfacing directly with hardware
• Higher level than Assembly Language
Where C is used?
• Embedded Systems
• System Programming
– Operating Systems
– Compilers
– Linkers
Enhancing Programming Skills-C Programming
C Standards
Structure of C Program
• Preprocessor Directives
– #include, #define
– Header Files
• Main function
• Comments
– Can be placed anywhere
Structure of C Program
#include <stdio.h>
int main()
{
/*This is main function*/
printf(“Hello World”);
return 0;
}
Enhancing Programming Skills-C Programming
Structure of C Program
Enhancing Programming Skills-C Programming
directive #include.
by ISO C.
Enhancing Programming Skills-C Programming
main() Function
Comments
/*This is comment*/
• C99 and upwards also supports
//This is comment
Enhancing Programming Skills-C Programming
Popular C Implementations
• GCC
• Visual Studio C++
• Clang
• Intel C
QUIZ
1. Who developed C?
1.C preprocessor
2.C compiler
3.Assembler
4.Linker
Enhancing Programming Skills-C Programming
E d it o r
So urc e C o d e Pr e pr oc e s s or He a d e r F ile s
C o mp i l e r
A s s e mb l e r
O b jec t C o d e
E x e c ut a b l e f i l e s
St a g e s of C o mp i l a t i o n a n d E x e c ut i o n
Enhancing Programming Skills-C Programming
C preprocessor
These special preprocessor statements libraries include the following.
• # include: The preprocessor adds the information about the object code
used in the body of the main function. These files are called header files.
C preprocessor
•C compiler: This program translates the C language source code
into the machine assembly language.
C Tokens
Token is the smallest element in the program , C tokens are
classified into five types
1. Identifier
2. Keywords
3. Constants
4. Operators
5. Special Symbols
Enhancing Programming Skills-C Programming
Keywords
Keywords are predefined words having predefined meaning. User
can use these keywords but cannot change their meaning. There 32
keywords in C
Identifier
Identifiers are user defined names, which are used to reference
variables, functions, labels, and various other user-defined elements.
Rules for Identifiers
Rule-1: Identifier name must start with alphabet or under score but
not digit.
Ex: total9 is valid identifier
9total is invalid identifier name
Enhancing Programming Skills-C Programming
Rule-2: Identifier name should not have any space or white space in
between of the characters or word in an identifier.
Ex: sum_of is valid identifier
Constant
An identifier whose value can not be changed though out the
program is known as constant.
In C constants are classified as
1. Integer Constant
2. Real constant
3. Character constant
4. String constant
Enhancing Programming Skills-C Programming
Constant
Constant
3. Character Constant: Anything placed within single quotes ‘ ‘ is
known as character constant.
ex: ‘a’, ’h’, ’k’
4. String Constant: Anything placed within double quotes “ “‘ is
known as string constant.
ex: “hi”, “hello”.
Enhancing Programming Skills-C Programming
Variables
An identifier whose value can be changed is known as Variable. A
variable usage in C invovles two main aspects
1. Declaring Variables
2. Initializing Variables
Enhancing Programming Skills-C Programming
Declaring Variables
A variable declaration is an intimation to the compiler about the
type of value you are going to store.
Syntax:
Data type
Data type specifies what type of value you are going to store in a variable. C
data types are as follows
1. Integer data type: Used to represents the numbers which do not have the
decimal point. The keyword “int” is used to represent this data type. It
occupies 2bytes in memory.
Ex: int a;
Here variable a can contain a value that does not decimal point.
2. Floating point data type: Used to represents the numbers which have the
decimal point. The keyword “float” is used to represent this data type. It
occupies 4 bytes in memory.
Ex: float b;
Here variable b can contain a value that contains decimal point.
Enhancing Programming Skills-C Programming
Data type
4.Double data type: it is also used to represent numbers with decimal point
similar to float . Float allows 6 digits after decimal point whereas double
allows 15 digits after decimal point. the keyword “double” is used to
represent this type of data.
Ex: double p;
Note: the int, float and char are known as primitive data types.
Enhancing Programming Skills-C Programming
unsigned short
or 2 bytes 0 to 65535
unsigned short int
Long
or 4 bytes -2147483648 to 2147483647 (2.1 billion)
long int
unsigned long
or 4 bytes 0 to 4294967295
unsigned long int
Short Int
Const Signed
Long
UnSigned
Char
Float
Double
Long
Enhancing Programming Skills-C Programming
Data type
-2n-1 to +2n-1 – 1
Where n represents the no of bits.
For example consider int data type
Size is 2bytes=16bits
-216-1 to +216-1 – 1
Enhancing Programming Skills-C Programming
Initializing Variables
Giving values at declaration itself is known as initialization.
Syntax:
Objective Questions
& Assignments
Enhancing Programming Skills-C Programming
#include <stdio.h>
void main()
{
int i=2,j=3,k=4;
printf(“%d”);
}
Output: 4
Enhancing Programming Skills-C Programming
#include <stdio.h>
void main()
{
int i=2,j=3,k=0;
int p;
p=(I,k,j)
printf(“%d”,j);
}
Output: 3
Enhancing Programming Skills-C Programming
Ans: C
Enhancing Programming Skills-C Programming
Ans: b
What will be output of the below code?
#include<stdio.h>
int x=40;
void main()
{
int x=20;
Printf(“%d”,x);
}
Ans: 20
Enhancing Programming Skills-C
Programming
What will be output of the below code?
#include<stdio.h>
void main()
{
int x=40;
{
int x=20;
printf(“%d”,x);
}
printf(“%d”,x);
}
Ans: 20 40
Enhancing Programming Skills-C
Programming
Assignments
1. Write a progression of data type sizes in c.
Estimated Time:10min
Estimated Time:10min
• Expressions
5. • Definition and Examples
Expressions • Statements
• Definition and Example
and
• Types of Statements
Operators • Symbolic Constants
Enhancing Programming Skills-C Programming
Expressions
Example:-
x = y; // Single data item
d = a * b + c; // combination of data items
Enhancing Programming Skills-C Programming
Statements
Types of Statements:-
Statements contd…
ii) Compound Statements – These statements enclosed within a pair of curly braces
{ }. They are also called as Statements Blocks.
Ex:-
{ {
pi = 3.14; l = 5, b = 10;
area = pi * r * r; rect = l * b;
} }
Enhancing Programming Skills-C Programming
Statements contd…
iii) Control Statements – These statements are also called as Looping Statements.
The program may require a group of instructions to be executed repeatedly until some
logical condition has been satisfied.
The detailed discussion will be dealt under the concept of Looping statements.
Symbolic Constants in ‘C’ – The name that substitutes for a sequence of characters,
that are always defined at the beginning of the program.
Syntax:- # define name text
Operators in
C
Enhancing Programming Skills-C Programming
Operators in ‘C’
Classifications
Definition And
Examples
Objective Interview
Questions Questions
Assignment and
Quiz
Enhancing Programming Skills-C Programming
Operators in ‘C’
Definition –
An operator specifies the operation to be applied to its operand.
Ex :-
- 14 % 3 = -2
- 14 % -3 = -2
14 % -3 = 2
b) Real Arithmetic
Ex :-
float x , y;
6.0 / 7.0 = 0.857
1.0 / 0.3 = 3.333
- 0. 66 / 4.50 = - 0.1466
Enhancing Programming Skills-C Programming
Ex :-
int a = 15;
float f = 10;
internally inside a / f is
calculated as 15 / 10.0 = 1.5
Enhancing Programming Skills-C Programming
Syntax:-
(datatype) expression ;
Ex :-
int a = 5;
float b = 10;
(int) b % a = 10 % 5 = 0
From the above example, the variable ‘b’ is converted to integer datatype
int a = 5; Output
Example : -
printf(“%d”, a ++); 5
printf(“%d”, ++a); 7
printf(“%d”, --a); 6
printf(“%d”, a--); 6
printf(“%d”, a); 5
Enhancing Programming Skills-C Programming
Shift operators
7 6 5 4 3 2 1 0
10 = 1 0 1 0
1 0 1 0 0 0
40 =
7 6 5 4 3 2 1 0
10 = 1 0 1 0
2= 1 0
It is a process in which a given bit pattern is transformed into another bit pattern by means of a logical
bitwise operator.
Special operators
Comma operator ,
sizeof ( ) operator – Returns the number of bytes occupied by the specified datatype.
Enhancing Programming Skills-C Programming
sizeof Operator
Returns the number of bytes occupied by the specified datatype.
right-to-left
++ -- Prefix increment/decrement
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Cast (convert value to temporary value of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this implementation
|| Logical OR left-to-right
= Assignment right-to-left
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
?
?
Objective
?
Questions
Enhancing Programming Skills-C Programming
2. main()
{
int a=30,b=40,x;
x=(a!=10) && (b=50);
printf(“x=%d”,x);
}
Enhancing Programming Skills-C Programming
3. main()
{
int a=100,b=200,c;
c=(a==100 || b > 200);
printf(“c=%d”,c);
}
4. main()
{
int x=11,y=6,z;
z=x==5|| y!=4;
printf(“z=%d”,z);
}
Enhancing Programming Skills-C Programming
5. main()
{
int x=10,y= -20;
x=!x;
y=!y;
printf(“x=%d y= %d”,x,y);
}
6. main()
{
int x=0,y=1;
y=!x;
x=!y;
printf(“x=%d y=%d”,x,y);
}
Enhancing Programming Skills-C Programming
7. main()
{
if(!3.14)
printf(“Hello”);
else
printf(“Fine”);
}
8. main()
{
int x=3,y=4,z=4;
printf(“Ans=%d”,(z>=y>=x?100:200));
}
Enhancing Programming Skills-C Programming
9. main()
{
int i= -4,j,num=10;
j=i% -3;
j=(j ? 0 : num*num);
printf(“j=%d”,j);
}
10. main()
{
int k=12,n=30;
k=(k>5 && n=4 ? 100 : 200);
printf(“k = %d”,k);
}
Enhancing Programming Skills-C Programming
11. main()
{
int c=0,d=5,e=10,a;
a=c>1 ? d>1 || e>1 ? 100 :200:300);
printf(“a=%d”,a);
}
12. main()
{
int x=10,y=20;
x=!x;
y=!x && !y);
printf(“x=%d y=%d”,x,y);
}
Enhancing Programming Skills-C Programming
?
?
Interview ?
Questions
Enhancing Programming Skills-C Programming
int x=40;
main()
{ Ans:- 20
int x=20;
printf(“\n%d”,x);
}
main()
{
static int a[20];
int i=0;
a[i]=i++; Ans:- 0 0 1
printf(“\n%d%d%d”,a[0],a[1],i);
}
Reason:- That’s what some compilers would give. But some may give
different answer. i.e., When a single expression causes the same object to
be modified or to be modified and then inspected the behavior is
undefined.
Enhancing Programming Skills-C Programming
int x=40;
main()
{ Ans:- 20
int x=20;
printf(“\n%d”,x);
}
A) y=(int) (x+0.5);
B) y=int(x+0.5);
C) y=(int)x+0.5; Ans:- A
D) Y=(int)((int)x+0.5);
Enhancing Programming Skills-C Programming
void main()
{
printf(“%d%d%d”,sizeof(3.14f,sizeof(3.14),sizeof(3.14l));
}
Ans:- 4 8 10
Enhancing Programming Skills-C Programming
void main()
{
int i=32,j=0x20,k,l,m;
k=i|j;
l=i&j;
m=k^l;
printf(“%d%d%d%d%d”,i,j,k,l,m);
}
Ans:- 32 32 32 32 0
Enhancing Programming Skills-C Programming
void main()
{
unsigned int m=32;
printf(“%x”,~m);
}
Ans:- f f d f
Enhancing Programming Skills-C Programming
void main()
{
unsigned int a=0xffff;
~a;
printf(“%x”,a);
}
Ans:- f f f f
Enhancing Programming Skills-C Programming
Ans:- f~=2;
Enhancing Programming Skills-C Programming
void main()
{
int i=2;
printf(“\n%d%d”,++i,++i);
}
void main()
{
int x=10,y=20,z=5,i;
i=x<y<z;
printf(“\n%d”,i);
}
Ans:- 1
Enhancing Programming Skills-C Programming
15. Can you suggest any other way of writing the following
expression such that 30 is used only once?
16. How come that the C standard says that the expression j=i++
* i++; is undefined, whereas, the expression j=i++ && i++ ; is
perfectly legal?
According to the C standard, an object’s stored value can
be modified only once between 2 sequence points
Enhancing Programming Skills-C Programming
void main()
{
int i= -3,j=2,k=0,m;
m= ++i && ++j || ++k;
printf(“\n%d%d%d%d”,i,j,k,m);
}
Ans:- -2 3 0 1
Enhancing Programming Skills-C Programming
void main()
{
int i= -3,j=2,k=0,m;
m= ++j && ++i || ++k;
printf(“\n%d%d%d%d”,i,j,k,m);
}
Ans:- -2 3 0 1
Enhancing Programming Skills-C Programming
void main()
{
int i= -3,j=2,k=0,m;
m= ++i && ++j && ++k;
printf(“\n%d%d%d%d”,i,j,k,m);
}
Ans:- -2 3 1 1
Enhancing Programming Skills-C Programming
void main()
{
int a=10,b;
a>=5 ? b=100 : b=200;
printf(“\n%d”,b);
}
?
?
Program ?
Assignment
Enhancing Programming Skills-C Programming
Duration:- 10 mins
Enhancing Programming Skills-C Programming
Duration:- 10 mins
Enhancing Programming Skills-C Programming
Duration:- 20 mins
Enhancing Programming Skills-C Programming
Duration:- 20 mins
Enhancing Programming Skills-C Programming
Input
Output
Operations in C
Enhancing Programming Skills-C Programming
The Input and Output operations in ‘C’ are classified as shown below:-
I/O Functions
where control string – specifies the field format in which data has to be entered.
float f1,f2;
scanf(“%f%f”, &f1,&f2);
void main()
{
How it happens???
char name[20];
printf(“\nEnter the name :”);
scanf(“%[^abcd]”, name); Reason:-
printf(“\n Name : %s”, name); The search set is inverted to
getch(); include all the characters even
} blank space except those inside
the square brackets
Enhancing Programming Skills-C Programming
control string – specifies the field format in which data has to be entered.
where control string – specifies the field format in which data has to be entered.
Assume n = 9876
9 8 7 6
printf(“%d”, n);
9 8 7 6
printf(“%6d”, n);
9 8 7 6
printf(“%2d”,n);
9 8 7 6
printf(“%-6d”,n);
0 0 9 8 7 6
printf(“%06d”,n);
Enhancing Programming Skills-C Programming
ii) fprintf() – This function is used to read the contents from the keyboard and writes to
the file.
control string – specifies the field format in which data has to be entered.
#include<stdio.h>
Ex:- Output:-
int main()
{ Enter character: a
char c; Character entered: a
printf("Enter character: ");
c = getc(stdin);
printf("Character entered: ");
putc(c, stdout);
return(0);
}
Enhancing Programming Skills-C Programming
Ex:- Output:-
#include<stdio.h>
void main()
{
char c; Enter character: a
printf("Enter character: "); // Assigns a to c
c = getchar();
}
Enhancing Programming Skills-C Programming
Ex:- Output:-
#include<stdio.h>
void main()
{
char s[15]; Enter the string:
printf("Enter the string: "); India Shines
gets(s);
}
Enhancing Programming Skills-C Programming
Syntax:-variableName = getch();
v) getche() – Reads the character from keyboard, but displays or echoes it onto the screen.
#include<stdio.h>
void main()
{
Ex:- char ch,ch1; Output:-
printf(“\n Enter 2 characters”);
ch=getch(); Enter 2 characters
ch1=getche(); a // no dispay
} b // displays
Enhancing Programming Skills-C Programming
#include<stdio.h>
Ex:- Output:-
void main()
{
FILE *fp;
fp=fopen("file1.txt","r"); 65
printf("%d",getw(fp)); // Returns the ASCII
fclose(fp); value of A that is already
} written into the file.
Enhancing Programming Skills-C Programming
Syntax:- putchar(variableName);
Ex:- Output:-
#include<stdio.h>
void main()
{
char c;
Enter character: a
printf("Enter character: ");
// Assigns a to c
c = getchar();
a //displays
putchar( c);
}
Enhancing Programming Skills-C Programming
#include<stdio.h>
Ex:- Output:-
void main()
{
char s[15];
printf("Enter the string: "); Enter the string:
gets(s); India Shines
puts(s); India Shines
}
Enhancing Programming Skills-C Programming
Syntax:-putch(characterVariable);
Ex:- Output:-
#include<stdio.h> Enter 2 characters
void main() a // no dispay
{ b // displays
char ch,ch1; a
printf(“\n Enter 2 characters”); b
ch=getch();
ch1=getche();
putch(ch);
putch(ch1);
}
Enhancing Programming Skills-C Programming
?
?
Objective
?
Questions
Enhancing Programming Skills-C Programming
Interview ?
Questions
& ?
Quiz
Enhancing Programming Skills-C Programming
void main()
{
printf(“%d”,sizeof(‘\n’));
}
void main()
{
printf(“%d%c”);
}
Ans:-
void main()
{
if (printf(“hello world”))
{ }
}
Enhancing Programming Skills-C Programming
void main()
{
printf(“c:\tc\bin”);
}
Ans:- c: in
Enhancing Programming Skills-C Programming
void main()
{
printf(“%05d,%5d,%-5d”,32,32,32);
}
?
?
Program ?
Assignment
Enhancing Programming Skills-C Programming
Duration:- 10 mins
Enhancing Programming Skills-C Programming
Duration:- 30 mins
Enhancing Programming Skills-C Programming
Duration:- 20 mins
Enhancing Programming Skills-C Programming
THANK YOU