C Notes

C
DSA
Software Engineering
Software Architecture
Operating System
Big Data
Data Mining and Warehousing
TOC
Ada
CPP
DBMS

All Topics (20)

  • 1. Why should we learn a programming language?
  • 2. Why are we starting our programming journey with C language?
  • 3. History of C Language
  • 4. Who created C language?
  • 5. Where was C language developed?
  • 6. When was C language developed?
  • 7. Why is C still popular even after many years?
  • 8. First C Program Example
  • 9. What is a Header File & Why Do We Use Header Files in C?
  • 10. What is stdio.h Header File in C?
  • 11. What is conio.h in C and Why Do We Use It?
  • 12. Why is # symbol used in C language? What is #include?
  • 13. What is a Function in C?
  • 14. What is Compilation of Code in C?
  • 15. Data Types in C (Size, Format Specifier & Range)
  • 16. Understanding ASCII in C Programming
  • 17. Understanding Float, Double, and Long Double in C
  • 18. What Are Operators in C?
  • 19. Type Casting vs Type Conversion in C/C++
  • 20. Type Promotion in C Programming

11. What is conio.h in C and Why Do We Use It?

In C programming, conio.h is a header file that stands for CONSOLE Input Output.

Here’s what that means:

  • Console – The window where your program displays output and interacts with the user. This could be the command prompt or terminal on your operating system.
  • Input/Outputconio.h provides functions to take input from the keyboard and control the console output, making your program more interactive and visually appealing.

 Note: The console window itself is not created by conio.h; it is generated by the operating system (like DOS or Windows). conio.h just gives you the tools to control it.

Important Functions of conio.h

  1. clrscr() – Clears the console window and removes all previous outputs.

     
    clrscr(); // Clear the screen
  2. textcolor(color) – Changes the font color of text in the console window.

     
    textcolor(RED); // Set text color to red
  3. getch() – Pauses the console window until a key is pressed.

     
    getch(); // Wait for user input
  4. gotoxy(x, y) – Moves the cursor to a specific position on the console.

     
    gotoxy(10, 5); // Move cursor to column 10, row 5

Why Use conio.h?

  • Better Control of Console Output: Change text colors, move the cursor, or clear the screen for a cleaner interface.
  • Pause Program Execution: Useful for letting the user see results before the console closes.
  • Interactive Programming: Makes your program visually appealing and easy to follow, especially for beginner projects or small games.

Example Program Using conio.h

#include <conio.h>
#include <stdio.h>

int main() {
  clrscr(); // Clear the screen
  textcolor(RED); // Change text color to red
  printf("Hello User!\n"); // Display output
  gotoxy(10, 5); // Move cursor to a specific position
  printf("Welcome to C Programming\n");
  getch(); // Wait for key press
  return 0;
}

12. Why is # symbol used in C language? What is #include?

In the C programming language, the symbol # is called a pound sign (also known as hash).

 Any line in a C program that begins with # is called a Preprocessor Directive.

 What are Preprocessor Directives?

Preprocessor directives are special instructions that are processed before compilation.

a. They are handled by a special program called the C Preprocessor, not by the compiler.
b. The preprocessor runs before the compiler and prepares the code for compilation.

 Role of the C Preprocessor

The preprocessor performs tasks like:

  • Including files
  • Defining macros
  • Conditional compilation
  • Removing comments

 It only processes lines that start with #.

 What is #include?

#include is a file inclusion directive.

 It tells the preprocessor to include the content of another file into the program.

Example:

#include <stdio.h>

a. This line includes the standard input-output library
b. It allows functions like printf() and scanf() to work

 Types of #include

  1. System File
 
#include <stdio.h>

 Searches in standard library folders

  1. User-defined File
 
#include "myfile.h"

 Searches in the current directory

 Popular Preprocessor Directives in C

1. File Inclusion

#include

 Used to include header files

2. Macro Definition

#define PI 3.14
 

 Defines a macro (constant or expression)

3. Macro Undefinition

#undef PI

 Removes a previously defined macro

4. Conditional Compilation

These directives control compilation based on conditions:

# if
# else
# elif
# ifdef
# ifndef
# endif

 Useful for:

  • Debugging
  • Platform-specific code
  • Feature toggling

 Why do we use Preprocessor Directives?

a. To make code reusable
b. To simplify complex code
c. To improve readability
d. To control compilation

13. What is a Function in C?

A function in C language is a named block of code that performs a specific task.

 It consists of:

  • A function name
  • A pair of parentheses ()
  • A set of instructions enclosed in curly braces {}

Example:

void greet() {
printf("Hello!");
}

a. Functions help in breaking a program into smaller, manageable parts
b. They improve code readability and reusability

 What is the main() Function in C?

 Every C program must have a main() function.

 a. It is the starting point of program execution
b. The Operating System (OS) always begins execution from main()

 Importance of main() Function

  •  It acts like the entry point of the program
  •  Execution of the program always starts from main()
  •  Without main(), the program cannot run

 Think of main() as the engine of a car 
Just like a car starts with an engine, a C program starts with main().

 Structure of main() Function

int main() {
// code statements
return 0;
}

int → return type
return 0; → indicates successful execution

 Can We Compile a C Program Without main()?

   Yes, we can compile it, but…

a.  Compilation is done by the compiler
b.  The compiler checks syntax from top to bottom
c. It does not require main() to compile

 Can We Run a Program Without main()?

No, we cannot run it ❌

a.  Execution is handled by the Operating System (OS)
b.  The OS always starts execution from the main() function
c.  If main() is missing → program will not run

 Key Difference (Important )

Process Needs main()?
Compilation ❌ No
Execution ✅ Yes

 

14. What is Compilation of Code in C?

Compilation is the process of converting source code (written in C language) into machine code (binary code).

a)  Source code → Human-readable
b)  Machine code → Computer-readable (0s and 1s)

 Why is Compilation Important?

 Computers (CPU) do not understand C language directly.

a)  They only understand binary language (0 and 1)
b)  So, we must convert our program into machine code

 This conversion process is called Compilation

 What is a Compiler?

 A compiler is a software that performs compilation.

a>  It converts the entire program at once
b>  It checks for syntax errors
c>  It generates machine code (object/executable file)

 Compilation Process (Step by Step)

Source Code (C Program)
        ↓
Compiler
        ↓
Machine Code (Binary)
        ↓
Execution (by OS)

 Example

Source Code:

#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}

After Compilation:

 Converted into binary like:

01010100 11010101 10101010 ...

 This binary code is what the computer executes

 Key Points (Exam Ready )

  • Compilation = Source Code → Machine Code
  • Done by a Compiler
  • Computers understand only binary (0 & 1)
  • It checks for errors before execution
  • It is required before running a program

 Simple Definition (1 Line)

Compilation is the process of converting C source code into machine code using a compiler.

15. Data Types in C (Size, Format Specifier & Range)

Integer Data Types

Data Type Size (Bytes) Format Specifier Range
int / Signed int 2 Bytes (TC) / 4 Bytes (Modern) %d -32768 to 32767 (TC)
unsigned int 2 / 4 Bytes %u 0 to 65535
long int 4 Bytes %ld -2147483648 to 2147483647
unsigned long int 4 Bytes %lu 0 to 4294967295

 Character Data Types

Data Type Size (Bytes) Format Specifier Range
char / Signed char 1 Byte %c -128 to 127
unsigned char 1 Byte %c 0 to 255

 How Values Are Stored in Memory?

 Every value stored in RAM is converted into binary form (0s and 1s).

Example:

  • Decimal value: 25
  • Binary value: 11001

 So internally:

25 → 11001 (Binary)

 Computers store and process only binary data

 Integer Storage Example

 
int x = 25;

 Stored in memory as:

11001 (Binary form of 25)

 Character Storage & ASCII Value

 Characters are stored using ASCII (American Standard Code for Information Interchange)

Example:

  • 'A' → ASCII = 65
  • 'B' → ASCII = 66

 So:

 
'A' → 65 → Binary → Stored in memory
 

 Important Points

  • Every value in RAM is stored in binary form
  • Characters are stored using ASCII codes
  • ASCII converts characters → numbers → binary

 Program: Display Character and its ASCII Value

 Correct & Clean Code:

#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("Character is: %c\n", ch);
printf("Its ASCII value is: %d", ch);
return 0;
}

Sample Output

Enter a character: B
Character is: B
Its ASCII value is: 66
 
Page 3 of 4