Professional Documents
Culture Documents
Cse 101
Cse 101
CSE101-Computer Programming
C Program Development Environment
CSE101-Computer Programming
Overview of phases
CSE101-Computer Programming
Program is created in
Editor Disk the editor and stored
on disk.
Preprocessor program
Preprocessor Disk processes the code.
Loader
Loader puts program in memory.
Disk ..
..
..
Primary Memory
CPU CPU takes each
instruction and
executes it, possibly
.. storing new data
..
.. values as the program
executes.
CSE101-Computer Programming
CSE101-Computer Programming
Key points
▪ Preprocessing:
➢Generates expanded source code
➢Removal of comments
➢Expansion of included files(e.g. Header files)
▪ Compilation:
➢Detects syntax errors
➢Preprocessed code is translated to assembly instructions specific to the target
processor architecture.
➢The existence of this step allows for C code to contain inline assembly
instructions and for different assemblers to be used.
CSE101-Computer Programming
Key points
▪ Assembly:
➢This stage is used to translate the assembly instructions to object code
➢The output consists of actual instructions to be run by the target processor.
▪ Linking:
➢The result of this stage is the final executable program.
➢Mainly, all the programs written in C use library functions. These library functions are
pre-compiled, and the object code of these library files is stored with '.lib' (or '.a')
extension. The main working of the linker is to combine the object code of library files
with the object code of our program.
➢Sometimes the situation arises when our program refers to the functions defined in
other files; then linker plays a very important role in this. It links the object code of
these files to our program.
➢So, it links the object code of our program with the object code of the library files and
other files.
CSE101-Computer Programming
Polling Questions
Which of the following component will point out the syntax errors in the
code?
A. Linker
B. Compiler
C. Loader
D. Preprocessor
CSE101-Computer Programming
Extension of expanded source code file is
A. (.obj)
B. (.i)
C. (.s)
D. (.exe)
CSE101-Computer Programming
Which of the following component generates the final .exe file?
A. Linker
B. Loader
C. Assembler
D. Compiler
CSE101-Computer Programming
Role of preprocessor is to:
A. Detect syntax errors
B. Generate object code
C. Combine various object files and library files together
D. Include the code of header files at the point, where they are included to generate
expanded source code
CSE101-Computer Programming
Which of the following extension is valid for a file containing assembly code?
A. (.obj)
B. (.i)
C. (.s)
D. (.exe)
CSE101-Computer Programming
CSE101-Lec#2
Structured Programming Using Algorithm and Flowchart(or
Program development tools)
Outline
• Structured programming using
• Algorithm and
• Flowchart
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to
sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Step 1: Start
Step 2: Declare variables radius and area.
Step 3: Read value of radius.
Step 4: Apply the expression as specified below
and assign the result in area
area←3.14*radius*radius
Step 5: Display area
Step 6: Stop
Step 1: Start
Step 2: Declare three variables: num1,num2 and temp
Step 3: Read values of num1 and num2
Step 4: Assign the value of num1 in temp
temp ←num1
Step 5: Assign the value of num2 in num1
num1 ←num2
Step 6: Assign the value of temp in num2
num2 ←temp
Step 7: Display swapped values of num1 and num2
Step 8:Stop
A B C D …… X Y Z Characters
Programs
CSE 101 COMPUTER PROGRAMMING
Character Set
• The character set of C represents alphabet, digit or any symbol used
to represent information.
Types Character Set
Uppercase Alphabets A, B, C, … Y, Z
Lowercase Alphabets a, b, c, … y, z
Digits 0, 1, 2, 3, … 9
~‘!@#%^&*()_-+=|\{} []
Special Symbols
:;"'<>,.?/
White spaces Single space, tab, new line.
CSE 101 COMPUTER PROGRAMMING
Meaningfulness
• Let us look to some words
• saslc, enp, keib, rac, llab
• Rearrange
• Class, pen, bike, car, ball
ellipse square
circle ellipse
A) int number;
B) float rate;
C) int variable_count;
D) int $main;
do if static while
A) UpperCase letters
B) LowerCase letters
C) Digits
D) CamelCase letters
M2=34, G2=? 34 D
char in C
Data
Type
CSE 101 COMPUTER PROGRAMMING
List of Data Types
Type Size Minimal range
(bytes)
char 1 -128 to 127
unsigned char 1 0 to 255
int 2 or 4 -32768 to 32767
unsigned int 2 or 4 0 to 65535
short int 2 -32768 to 32767
unsigned short int 2 0 to 65535
long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float 4 3.4e-38 to 3.4e+38 with 6 digits of
precision
double 8 1.7e-308 to 1.7e+308 with 15 digits
of precision
long double 10 3.4e-4932 to 1.1e+4932 with 20
digits of precision
CSE 101 COMPUTER PROGRAMMING
Integer
• It is used to store positive and negative counting
numbers, as well as zero.
{...,-2,-1,0,1,2,...}
15 84 34 97
• The rational number written in red box of My-Car problem are the
float numbers.
3.14 94.2
A D A B C
15
%d
Program 90
%d
3.14
%d
15
%d
Program 94.2
3.14 %f
%f
84 %d %c A
34 %c D
%d
97 %d Program %c A
58 %d %c C
64 %d %c B
⮚Arithmetic Operators
These are binary operators i.e. expression requires two operands
Operato Description Example (a=4 and b=2)
r
+ Addition of two operands a+b=6
- Subtraction of two operands a–b=2
* Multiplication of two operands a*b=8
/ Division of two operands a/b=2
% Modulus gives the remainder a%b=0
after division of two operands
++count increments count by 1 and then uses its value as the value of the
expression. This is known a prefix operator.
count++ uses count as the value of the expression and then increments
count by 1. This is known as postfix operator.
> greater than, checks if the value of left operand is greater than (a > b) value is 0 (false).
the value of right operand, if yes then condition becomes true.
>= greater than or equal to, checks if the value of left operand is (a >= b) value is 0 (false).
greater than or equal to the value of right operand, if yes then
condition becomes true.
!= inequality, checks if the value of two operands is equal or not, (a != b) value is 1 (true).
if values are not equal then condition becomes true.
©LPU CSE101 C Programming
Relational Operator
Q: Age of Sam is 20 and age of Tom is 19.
Verify the relationship between their age.
Sol: age of Sam = S1 = 20
age of Tom = T1 = 19
S1 < T1 = 0 (false)
S1 > T1 = 1 (true)
So, Sam is elder than Tom.
S1 == T1 = 0 (false)
• Associativity tell us the order in which several operators with equal precedence are
computed or processed in two directions, either from left to right or vice-versa.
• Associativity tell us the order in which several operators with equal precedence are
computed or processed in two directions, either from left to right or vice-versa.
1. Wake up;
2. Get ready;
3. If you have enough time, then
eat breakfast;
©LPU CSE101 C Programming
4. Go to school.
Control Statements
• The C language programs until now follows a
sequential form of execution of statements.
• C language provides statements that can alter the
flow of a sequence of instructions. These statements
are called control statements.
• These statements help to jump from one part of the
program to another. The control transfer may be
conditional or unconditional.
Go!!!
Where
Class To Movie
Go?
Stop Stop
©LPU CSE101 C Programming
if statement
if (expression)
statement;
or
if (expression)
{
block of statements;
}
yes no
Clouds?
No rain
Raining
Grab
something to
eat along
MESSAG
E
DISPLAY
}
A. Nothing will be printed
B. Compile time error
C. Hello
D. Logical error
66.70
Grade D
or
78.00
Grade C C Programming
©LPU CSE101
Q1
#include <stdio.h>
int main() A. inside if
{ B. inside elseif
int x = 1; C. inside if
if (x > 0) inside elseif
printf("inside if\n");
D. Compile time error
else if (x > 0)
printf("inside elseif\n");
}
// Nested - if statement
// Will only be executed if statement above is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
Day= No Day=
Monday Sunday
Yes
case 2.5:
printf(“A line”); Floating point number
break; cannot be used
case 3 + 7.7:
printf(“A triangle”);
case 3 + 7.7: Floating point number
printf(“A triangle”); cannot be used and
break; same expression cannot
case count+5: be used
printf(“A pentagon”);
break;
} constant expression
should be used
©LPU CSE101 C Programming
#include<stdio.h>
int main()
{
int pt;
printf("Enter the number of nodes:");
Program to
scanf("%d", &pt);
switch(pt){ show switch
case 0:
printf("\nNo Geometry");
break;
statement in
case 1:
printf("\nA point");
break;
geometry
case 2:
printf("\nA line");
break;
case 3:
printf("\nA triangle");
break;
case 4:
printf("\nA rectangle");
break;
case 5:
printf("\nA pentagon");
break;
default:
printf("Invalid input");
break;
}
return 0;
}
• Body – a statement or a
block of statements that
will be repeated.
• Condition – is used to
control the iteration –
either to continue or stop
iterating.
#include <stdio.h>
int main()
{
int i = 0;
while (i = 0)
printf("True\n");
printf("False\n");
return 0;
}
A. True (infinite time)
B. True (1 time) False
C. False
D. Compiler dependent
A. 1 2 3 4 5
B. 6
C. 5
D. Compiler error
10 9 8 7 6 5 4 3 2 1
Do TEN push ups = for
count=1; count<=10;
count++
©LPU CSE101 C Programming
©LPU CSE101 C Programming
Q1
#include <stdio.h>
int main()
{
int i;
for (i = 1; i != 10; i += 2)
printf("Hello");
return 0;
}
A. Hello will be displayed 5 times
B. Hello will be displayed 4 times
C. Hello will be displayed infinite no. of times
D. Hello will be displayed 6 times
A. 1
B. 0
C. infinite loop
D. Nothing will be displayed
A. 1 6
B. 1 1
C. 6 1
D. 1 5
…
}
}
• The above loop will run for 100*50 iterations.
Enter a number
4
The tables from 1 to 4
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12CSE101
©LPU 16 20 24 28 32 36 40
C Programming
#include<stdio.h>
int main() Program to
{
int i,j;
display a
printf(“Displaying right angled triangle for 5 pattern.
rows”);
for(i=1 ; i<=5 ; i++) {
for(j=1 ; j<=i ; j++)
printf(“* ”);
printf(“\n”);
}
return 0;
}
5-32
©LPU CSE101 C Programming
do…while statement
do
{
Repeated_Actions;
} while (Condition);
10 9 8 7 6 5 4 3 2 1
B. In while loop
In while loop
10 9 8
10 8 6 4 2
}
return 0;
}
n=10;
A:
printf(“%d “, n);
n = n -1;
if (n>0)
goto A;
Output:
10 9 8 7 6 5 4 3 2 1
enter a number: 18
18 is even
A. int
B. char
C. long
D. All of the above
A. int
B. char
C. long
D. All of the above
(type) operand;
long double
double
float
long int
signed int
int
char
A. x = 107, z = 108.00
B. x = 107, z = 108.0000
C. x = 107, z = 108.000000
D. x = 108, z = 108.000000
A. sum = 2
B. sum = 1
C. sum = 0
D. sum = 3
a) int
b) long
c) float
d) double
A) *56543,*56543*
B) * 6543,*6543 *
C) *6543 ,* 6543*
D) *6543,*6543*
A) 654,654
B) 654.123456,654.123456
C) 654.123,654.12
D) 654.000,654.00
Example:
char c;
c = getchar();
©LPU CSE101 C Programming
#include<stdio.h>
void main()
{
char c;
printf(“enter a character”);
c=getchar();
printf(“c = %c ”,c);
}
Enter a character k
c = k
enter a character: r
r
A) A
B) B
C) S
D) Compiler error
getche();
Syntax
char str[length of string in number];
gets(str);
puts(str);
A. hello
B. No output
C. compiler error
#include <stdio.h>
void show();
int main()
{
show();
printf("hello");
}
void show()
{
printf(" World");
}
1. Hello World
2. World Hello
3. WorldHello
4. HelloWorld
#include <stdio.h>
void show();
int main()
{
show();
printf("hello");
}
void show()
{
printf("World");
main();
}
1. HelloWorld
2. WorldHello
3. World will be printed infinite times
4. Worldhello will be printed infinite time
CSE101-Lec#11
Function Call
Created By:
Amanpreet Kaur &
Sanjeev Kumar
©LPU CSE101 C Programming SME (CSE) LPU
Outline
• Function call
– Passing arguments by value
– Passing arguments by reference
void main()
{
int a;
a=show();
printf("%d", a);
}
int show()
{
return 15.5;
return 35;
}
1. 15.5
2. 15
3. 35
4. error
int main ()
{ /* main */
const float pi = 3.1415926;
printf("2.0 = %f\n", 2.0);
printf("pi = %f\n", pi);
printf("cos(pi) = %f\n", cos(pi));
printf("sin(pi) = %f\n", sin(pi));
printf("sqrt(2.0) = %f\n", sqrt(2.0));
printf("sqrt(2.0) / 2 = %f\n", sqrt(2.0) / 2);
} /* main */
Values returned
– For 5!, we write 5! = 5 * 4! 120
– Then for 4!, 4! = 4 * 3!
24
– Then for 3!, 3! = 3 * 2!
– Then for 2!, 2! = 2 * 1! 6
2
– Then for 1!, 1! = 1 * 0!
1
– And if its comes to 0,
– 0!=1
– Solve base case (1! = 0! = 1)
f( 3 )
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
#include<stdio.h>
int f(int n); a) 10
int main()
{
b) 80
int n=10; c) 30
printf("%d",f(n)); d) Error
}
int f(int n)
{
if(n>0)
return(n+f(n-2));
}
#include<stdio.h>
int f(int n); a) 10
int main()
{
b) 80
int n=10; c) 30
printf("%d",f(n)); d) Error
}
int f(int n)
{
if(n>0)
return(n+f(n-2));
}
#include<stdio.h>
int main()
{
printf("Hello");
main();
return 0;
}
a) Hello is printed once
b) Hello infinite number of times
c) Hello is not printed at all
d) 0 is returned
#include<stdio.h>
int main()
{
printf("Hello");
main();
return 0;
}
a) Hello is printed once
b) Hello infinite number of times
c) Hello is not printed at all
d) 0 is returned
avg=sum/n;
printf("\nAverage=%f\n",avg);
}
-------------------------------
//Smallest element in an array
main()
{
int i,n,min;
printf("Enter no of elements\n");
scanf("%d",&n);
int a[n];
printf("Enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Entered Array is-->\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
}
printf("\nSmallest element = %d\n",min);
}
------------------------------
//Largest element in an array
main()
{
int i,n,max;
printf("Enter no of elements\n");
scanf("%d",&n);
int a[n];
printf("Enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Entered Array is-->\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("\nLargest element = %d\n",max);
}
CSE101-Lec#15,16, 17
• What are Arrays?
• To declare an array
• To initialize an array
• To display address of the array
• Basic program examples of 1D array
• To pass an array to a function(By reference
and By value)
• Applications and Operations on 1D Array
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9]
c 79 87 94 82 67 98 87 81 74 91
scanf("%d",&a[i]);
}
printf("Enter integer value to search in array:");
scanf( "%d", &key );
// attempt to locate searchKey in array a
for ( i = 0; i < n; i++ )
{
if ( a[i] == key )
{
loc = i; // location of key is stored
break;
} // end if
} // end for
77 56 4 10 34 2 Original array
56 4 10 34 2 77 After pass 1
4 10 34 2 56 77 After pass 2
4 10 2 34 56 77 After pass 3
4 2 10 34 56 77 After pass 4
2 4 10 34 56 77 After pass 5
A. 5
B. 7
C. 4
D. 3
A. 15
B. 20
C. 25
D. 5
Column subscript
Array name
Row subscript
4
©LPU CSE101 C Programming
Memory representation of 2D-Array
• A 2D array’s elements are stored in continuous memory locations. It can
be represented in memory using any of the following two ways:
1. Column-Major Order
2. Row-Major Order
1. Column-Major Order:
In this method the elements are stored column wise, i.e. m elements of
first column are stored in first m locations, m elements of second column
are stored in next m locations and so on. E.g.
A 3 x 4 array will stored as below:
Pointers in C
y yptr y
5 500000 600000 600000 5
yPtr
Value of yptr
is the address
©LPU CSE101 C Programming
of y
Example Code
#include<stdio.h> This program
demonstrates
int main()
the use of the
{ pointer
int x=10,*p; operators: &
p=&x; and *
printf(“%u”,p);
printf(“\n%d”,*p);
}
int n=1;
int *ptr = (int *)malloc(n*sizeof(int));
*ptr=6;
printf("%d",*ptr);//6 is printed
printf("\n%d",ptr);//Printing address hold by pointer before deallocation
free(ptr);
printf("\n%d",ptr);//Same address will be printed(Dangling pointer)
//SOLUTION
ptr = NULL;//Pointer is now changed to NULL pointer
printf("\n%d",ptr);//0 will be printed
return 0;
}
©LPU CSE101 C Programming
Example-1-Passing pointer to a function(or call by reference)
A. 2 97
B. 2 2
C. Compile time error
D. Program will crash
©LPU CSE101 C Programming
Q10
What will be the output of the following C code?
#include <stdio.h>
void m(int *p, int *q)
{
p=q;
*p=8;
*q=7;
}
int main()
{
int a = 6, b = 5;
m(&a, &b);
printf("%d %d\n", a, b);
}
a) 8 7
b) 6 7
c) 6 5
d) 8 8
• Element b[3]
• Can be accessed by *(bPtr + 3)
• Where 3 is the offset. Called pointer/offset notation
• Can be accessed by bptr[3]
• Called pointer/subscript notation
• bPtr[3] same as b[3]
• Can be accessed by performing pointer arithmetic on the array
itself
*(b + 3)
• Array name itself is an address or pointer. It points to the first
element(0th element) of array.
• The arrays are accessed by pointers in same way as we access arrays
using array name.
• Consider an array b[5] and a pointer bPtr:
• bPtr[3] is same as b[3]
Example-Different notations with pointer to an array
#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5};
int *p=a;
// Different notations with pointer to an array for displaying second element
// Same terminology can be used to display any element
// All will display 2 on screen
printf("\n%d",*(p+1));
printf("\n%d",*(a+1));
printf("\n%d",p[1]);
printf("\n%d",1[p]);
printf("\n%d",1[a]);
return 0;
}
Pointer to an array with pointer arithmetic
#include<stdio.h>
int main()
{
int arr[]={1,2,3,4,5};
int i;
int *p;
p=arr;
printf("\n First value is:%d",*p);
p=p+1;
printf("\n Second value is:%d",*p);
*p=45;
p=p+2;
*p=-2;
printf("\n Modified array is:");
for(i=0;i<5;i++)
{
printf("\n%d",arr[i]);//We can also write i[arr]
}
p=arr;
*(p+1)=0;
*(p-1)=1;
printf("\n Modified array is:");
for(i=0;i<5;i++)
{
printf("\n%d",*(p+i));//We can also write *(i+arr)
Program example 1-WAP to read and display elements
of 1D array using pointer to an array
#include<stdio.h>
int main()
{
int i,n;
int a[10],*parr=a;
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",parr+i);
}
printf("\n Entered array elements are:");
for(i=0;i<n;i++)
{
printf("\t %d",*(parr+i));
}
return 0;
}
Program example 2-WAP to find the sum and mean of 1D array elements using pointer to an array
#include<stdio.h>
int main()
{
int i,n,arr[20],sum=0;
int *pn=&n,*parr=arr,*psum=∑
float mean=0.0,*pmean=&mean;
printf("\n Enter the number of elements in the array:");
scanf("%d",pn);
for(i=0;i<*pn;i++)
{
printf("\n Enter the number:");
scanf("%d",(parr+i));
}
for(i=0;i<*pn;i++)
{
*psum=*psum+*(arr+i);
}
*pmean=*psum/ *pn;
printf("\n The numbers you entered are:");
for(i=0;i<*pn;i++)
printf("\n%d",*(arr+i));
printf("\n The sum is:%d",*psum);
printf("\n The mean is:%f",*pmean);
return 0;
}
Pointer vs Array
1) the sizeof operator
sizeof(array) returns the amount of memory used by all elements in array
sizeof(pointer) only returns the amount of memory used by the pointer variable itself
2) the & operator
&array is an alias for &array[0] and returns the address of the first element in array
&pointer returns the address of pointer
3) a string literal initialization of a character array
char array[] = “abc” sets the first four elements in array to ‘a’, ‘b’, ‘c’, and ‘\0’
char *pointer = “abc” sets pointer to the address of the “abc” string (which may be stored in
read-only memory and thus unchangeable)
4) Pointer variable can be assigned a value whereas array variable cannot be.
int a[10];
int *p;
p=a; /*legal*/
a=p; /*illegal*/
5) Arithmetic on pointer variable is allowed.
p++; /*Legal*/
a++; /*illegal*/
Q1(Output)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a,i;
p++;
*(p+1)=29;
p=p+1;
*p=23;
p--;
*(p+0)=12;
p=a;
for(i=0;i<4;i++)
printf("%d ",*(p+i));
return 0;
}
A. 1 12 23 4
B. 1 29 23 12
C. 1 23 12 29
D. 1 23 3 4
Q2(Output)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p1=a,i;
int *p2=&a[2];
p2--;
*(p2-1)=90;
p1=p2;
*p1=100;
for(i=0;i<4;i++)
printf("%d ",a[i]);
return 0;
}
A. 1 90 100 4
B. 90 100 3 4
C. 90 2 100 4
D. 1 2 90 100
Q3(Output)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p1=a,*p2=&a[3];
p1++;
printf("\n%d %d",p2-p1,*p2-*p1);
return 0;
}
A. 2 2
B. 3 2
C. 3 3
D. 2 3
Q4(Output)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a;
*(p+1)=*(p+2);
printf("\n%d",a[2]);
return 0;
}
A. 3
B. 2
C. Compile time error
D. 0
Q5(Output)
#include<stdio.h>
int main()
{
int a[]={1,2,3,4};
int *p=a,x;
x=*p++;
printf("\n%d %d",x,*p);
return 0;
}
A. 1 2
B. 2 2
C. 1 1
D. Compile time error
CSE101-Lec#21
Dynamic memory management
Return value: void pointer (void *). void pointer (void *).
If the allocation succeeds, a If the allocation succeeds, a
pointer to the block of memory pointer to the block of memory
is returned. is returned.
©LPU CSE101 C Programming
realloc()
• Now suppose you've allocated a certain number of
bytes for an array but later find that you want to add
values to it. You could copy everything into a larger
array, which is inefficient, or you can allocate more
bytes using realloc(), without losing your data.
• realloc() takes two arguments.
1. The first is the pointer referencing the memory.
2. The second is the total number of bytes you want to
reallocate.
• Passing zero as the second argument is the equivalent
of calling free.
• Syntax:
void *realloc(pointerToObject, newsize);
A. int *
B. int **
C. void *
D. void **
A. int* malloc(int);
B. char* malloc(char);
C. unsigned int* malloc(unsigned int);
D. void* malloc(size_t);
Comparing strings
Determining the length of string
} //end main
Lovely
• So how to print:
Lovely Professional University
printf(“\nString is: ”)
puts(name); //to output const string
}//end main
Enter a string:
Lovely Professional University Output
String is:
Lovely Professional University
©LPU CSE101 C Programming
#include <stdio.h>
Program to print
void main() strings character
{
by character
char name[]={“Lovely Professional using loop.
University"}; //string char array
int i=0;
}//end main
A)I
B) INDIA
C) Compiler error
D) None of the above
}
return 0;
}
A)I
B) INDIA
C) I infinite times
D) None of the above
double atof( const char *nPtr ); Converts the string nPtr to double.
int atoi( const char *nPtr ); Converts the string nPtr to int.
long atol( const char *nPtr ); Converts the string nPtr to long int.
char * itoa(int value, char *str,int base); Converts the integer value to string
struct sname {
type var1;
type var2;
type var3;
.
.
type varN;
};
int main()
{
struct car myCar; //define struct variable
myCar.name = “Renault";
myCar.price = 500000;
myCar.seats = 2;
printf("%s %f %d \n”, myCar.name,
myCar.price, myCar.seats);
} //end main
Renault 500000 2
Array Structure
1. It is a collection of data items of same 1. It is a collection of data items of
data type. different data types.
2. It has declaration only. 2. It has declaration & definition.
printf("%ld",sizeof(s1));
}
1. 28
2. 26
3. 18
4. compiler error
Next Class: Structure,
functions and pointers
– myCarPtr->name is equivalent to
(*myCarPtr).name
a) *my_struct.b = 10;
b) (*my_struct).b = 10;
c) my_struct->b = 10;
d) Both *my_struct.b = 10; and (*my_struct).b = 10;
❑ So for union B, a 10 bytes (sizeof char str[10] = 10) memory block is shared among
the its item.
❑ SO the above program will print 4 and 10 as the sizeof union A and B.
Initializing and accessing union members
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}