C Programming Fundamentals: Structure, Variables, and I/O
1. C Program Structure and Components
Input Section
- Input Devices: Used to enter data into the computer system.
- Converts human-understandable input into computer-controllable data.
- The CPU accepts information from the user through these devices.
- Examples: Mouse, Keyboard, Touch screen, Joystick.
Output Section
- Output Devices: Used to send information from the computer to the outside world.
- Converts data stored as 1s and 0s into human-understandable information.
- Examples: Monitor, Printer, Plotter, Speakers.
Central Processing Unit (CPU)
- Arithmetic Logic Unit (ALU): Performs arithmetic calculations (add, subtract, multiply, compare) and logical decisions.
- Control Unit (CU): Manages all operations, including reading instructions and data from memory.
- Primary Memory: Also known as main memory; the CPU directly stores and retrieves information from it.
Secondary Memory
Also called auxiliary or external memory, it is used for permanent data storage.
- Can be modified easily and stores large amounts of data (Terabytes).
- Examples: Hard disk, CD, DVD, Pen drive.
C Programming Terminology
- Token: Individual words and punctuation marks in a passage of text (e.g., keywords, identifiers, constants, strings, operators).
- Keyword: Reserved words with predefined meanings (e.g.,
int,float,if,else,while,return). - Identifier: Names given to program elements like variables, functions, or arrays (e.g.,
sum,total_marks). - Variable: A named memory location for storing data that can change during execution (e.g.,
int a = 10;). - Constant: Fixed values that do not change during execution (e.g.,
10,3.14,'A'). - Compiler: System software that translates high-level source code into machine code at once and reports errors.
2. Rules for Defining Variables
- Identifiers consist of 1 to 31 characters (alphabets, digits, or underscores) without spaces.
- C is case-sensitive;
NUMandnumare treated as different variables. - Variable names cannot be reserved keywords (e.g.,
int,float).
3. Type Conversion in C
Type conversion (or Type Casting) is the process of converting one data type to another.
Implicit Type Conversion
Performed automatically by the compiler when necessary.
int a = 20;
double b = 20.5;
printf("%lf", a + b); // Output: 40.500000Explicit Type Conversion
■ These conversions are done explicitly by users using the predefined functions.
double a = 4.5, b = 4.6, c = 4.9; int result = (int)da + (int)db + (int)dc; printf(“result = %d”, result);
Output:12
Scanf(); scanf() is a function defined in stdio.h file.
Scanf() is used to take input from user. Syntax:
Scanf(“format specified”,&variable);
Example: int a;
Scanf(“%d”,&a);
Printf(); printf() is a function defined in stdio.h file.
It displays output on standard output mostly monitor.
Message and value of variable can be printed.
gets(); is used to read a string(including space)from the keyword. syntax: gets(string_name); example:
Char name[20];
gets(name);
puts(); is used to display a string on the screen.
It automatically Move the cursor to the next line.
syntax:puts(string_name); example:
Char name[ ]
Puts(name);
getch(); is used to read a single character from the keyword without displaying it on the screen. It is commonly used to pause the program. syntax:getch();
Example:#include
getch();
putch(); prints one character at a time to the console.
syntax: putch(character); example:#include
