0% found this document useful (0 votes)
3 views5 pages

Explanation

The document provides an overview of fundamental C programming concepts, including header files, variable declaration, arrays, decision-making, looping, user-defined functions, and the main() function. Each section includes examples and mini-questions to reinforce understanding. The content is structured to facilitate learning and application of C programming principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Explanation

The document provides an overview of fundamental C programming concepts, including header files, variable declaration, arrays, decision-making, looping, user-defined functions, and the main() function. Each section includes examples and mini-questions to reinforce understanding. The content is structured to facilitate learning and application of C programming principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Class-Discussion: C Programming Concepts

With Examples, Mini-Questions, and Hints

1. Header Files in C
What are header files?
Header files in C contain definitions of functions and macros that we can use in our
programs. Instead of writing common functions from scratch, we can #include header
files to use them.

Why do we include them?


- To reuse pre-defined functions. - To make the program modular and easy to read. - To
access libraries for math, input/output, memory, and time functions.

Common Header Files Used


• stdio.h: For standard input/output functions like printf(), scanf().

• stdlib.h: For memory allocation, random numbers, type conversions.

• time.h: To handle date and time, random seed initialization.

• math.h: For mathematical functions like sqrt(), pow(), sin(), etc.

// Example : Using header files


# include < stdio .h >
# include < stdlib .h >
# include < time .h >
# include < math .h >

int main () {
printf ( " Square root of 16 is : %.2 f \ n " , sqrt (16) ) ;
printf ( " Random number : % d \ n " , rand () ) ;
return 0;
}

Mini-Question 1: Write a program to generate a random number between 1 and


100. Hint: Use rand() with modulus and srand(time(0)) to change the sequence each
time.

2. Declaring Variables in C
Types of variables
Variables are storage locations in memory.

1
• int: Integer values (e.g., 5, -10).

• float: Decimal values (e.g., 3.14).

• double: Double precision floating point.

• char: Single character (e.g., ’A’).

// Example : Declaring variables


# include < stdio .h >
int main () {
int age = 25;
float pi = 3.14;
double g = 9.81;
char grade = ’A ’;

printf ( " Age = % d \ n " , age ) ;


printf ( " Pi = %.2 f \ n " , pi ) ;
printf ( " Gravity = %.2 lf \ n " , g ) ;
printf ( " Grade = % c \ n " , grade ) ;
return 0;
}

Mini-Question 2: Write a program to store your name’s first letter (char), age (int),
and height (float) and print them. Hint: Use %c, %d, %f in printf().

3. Arrays in C
1-D Arrays
A 1-D array stores multiple values of the same type in a single variable.
# include < stdio .h >
int main () {
int marks [5] = {90 , 85 , 88 , 92 , 76};
for ( int i =0; i <5; i ++) {
printf ( " marks [% d ] = % d \ n " , i , marks [ i ]) ;
}
return 0;
}

2-D Arrays
A 2-D array is like a table with rows and columns.
# include < stdio .h >
int main () {
int matrix [2][3] = {{1 ,2 ,3} ,{4 ,5 ,6}};
for ( int i =0; i <2; i ++) {
for ( int j =0; j <3; j ++) {

2
printf ( " % d " , matrix [ i ][ j ]) ;
}
printf ( " \ n " ) ;
}
return 0;
}

Mini-Question 3: Write a program to input 5 numbers into an array and print their
sum. Hint: Use a loop and a variable to keep adding.

4. Decision Making in C: if-else


Simple if-else
# include < stdio .h >
int main () {
int num = 10;
if ( num % 2 == 0) {
printf ( " Even \ n " ) ;
} else {
printf ( " Odd \ n " ) ;
}
return 0;
}

if-else-if ladder
# include < stdio .h >
int main () {
int marks = 72;
if ( marks >= 90)
printf ( " Grade A \ n " ) ;
else if ( marks >= 75)
printf ( " Grade B \ n " ) ;
else if ( marks >= 50)
printf ( " Grade C \ n " ) ;
else
printf ( " Fail \ n " ) ;
return 0;
}

Mini-Question 4: Write a program to input a number and check if it is positive,


negative, or zero. Hint: Use if, else if, else.

3
5. Looping in C
For Loop
Best used when the number of iterations is known.
# include < stdio .h >
int main () {
for ( int i =1; i <=5; i ++) {
printf ( " % d " , i ) ;
}
return 0;
}

While Loop
Best used when the number of iterations is not known beforehand.
# include < stdio .h >
int main () {
int i = 1;
while ( i <= 5) {
printf ( " % d " , i ) ;
i ++;
}
return 0;
}

Mini-Question 5: Write a program using a while loop to keep asking numbers


until the user enters 0. Print the sum of entered numbers. Hint: Use a variable to store
cumulative sum.

6. User-Defined Functions in C
Example: Factorial Function
# include < stdio .h >

// User - defined function


int factorial ( int n ) {
int fact = 1;
for ( int i =1; i <= n ; i ++) {
fact *= i ;
}
return fact ;
}

int main () {
int num = 5;
printf ( " Factorial of % d = % d \ n " , num , factorial ( num ) ) ;

4
return 0;
}

Example: Using Factorial to Compute nCx


# include < stdio .h >

// Factorial function
int factorial ( int n ) {
int fact = 1;
for ( int i =1; i <= n ; i ++) {
fact *= i ;
}
return fact ;
}

int main () {
int n = 5 , r = 2;
int nCr = factorial ( n ) / ( factorial ( r ) * factorial (n - r ) ) ;
printf ( " C (% d ,% d ) = % d \ n " , n , r , nCr ) ;
return 0;
}

Mini-Question 6: Write a function to compute the square of a number. Use it in


main() to compute a2 + b2 . Hint: Define a function returning int, call it twice.

7. The main() Function in C


Every C program starts execution from the main() function. Inside main(), we can call
other functions, write loops, use arrays, and make decisions.
# include < stdio .h >

int add ( int a , int b ) {


return a + b ;
}

int main () {
int x = 5 , y = 7;
int sum = add (x , y ) ; // calling user - defined function
printf ( " Sum = % d \ n " , sum ) ;
return 0;
}

Mini-Question 7: Write a user-defined function to compute the cube of a number


and call it inside main().

You might also like