-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASICS
More file actions
79 lines (57 loc) · 3.48 KB
/
BASICS
File metadata and controls
79 lines (57 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
1] concepts of the C programming language : Basic sysntax , Variables,datatypes, constants,Operators, Strings, Conditional statements ,Loops, Arrays, Functions, structures, pointers .
2 ] #include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
#include <stdio.h> This line tells the compiler to include the Standard Input Output library. It gives you access to the printf function, which is used to display messages on the screen.
int main() { ... } This is the starting point of every C program. It’s where execution begins. Think of main() as the front door of your program—without it, nothing runs.
printf("Hello, World!"); This prints the text Hello, World! to the screen. It’s your way of telling the computer: “Show this message.”
return 0; This tells the operating system that the program finished successfully.
3] Preprocessor directives are special commands in C that start with the hash symbol (#) and are processed by the preprocessor (a text substitution tool) before the code is sent to the compiler. They are not part of the C language itself but help in managing code modularity, readability, and flexibility.
The main types of preprocessor directives are:
File Inclusion (#include): Inserts the contents of a specified file (usually a header file) into the current source file. This is crucial for using standard library functions like printf() and scanf().
#include <filename.h>: Searches in standard system directories.
#include "filename.h": Searches first in the current directory, then in standard system directories.
Macros (#define, #undef): Used for defining constants or code snippets that are replaced by their value or content throughout the program before compilation.
#define identifier replacement_text: Replaces identifier with replacement_text (e.g., #define PI 3.14).
#undef identifier: Removes a previously defined macro.
Function-like macros can also accept arguments, like #define SQUARE(x) ((x) * (x)).
Arguments = values you supply to a function or macro.
Conditional Compilation (#if, #ifdef, #ifndef, #else, #elif, #endif): Allows specific sections of code to be compiled or ignored based on certain conditions at compile time.
Conditional compilation allows selective inclusion/exclusion of code segments during preprocessing based on defined macros or conditions.
4] In C programming, code blocks are enclosed within curly braces { }. They are used to group multiple statements together and help define the structure of your program clearly
Code Blocks with Curly Braces:
#include <stdio.h> // Include the standard I/O library
int main() {
{
// Start of a block
printf("This is a block!"); // Print a message inside the block
// End of the block
}
// Code outside the block would run after this
return 0;
}
5] Printing Text:
To print plain text, just put it inside double quotes (" ") :
#include <stdio.h>
int main() {
printf("Hello, World!"); // Output: Hello, World!
return 0;
}
6] Printing Numbers and Characters
Use format specifiers inside the string to print different types of values:
Common Format Specifiers in C
Format Description Example Output
%d Integer (int) 42
%f Floating point (float) 3.14
%c Single character A
%% Percent symbol %
Example:
#include <stdio.h>
int main() {
printf("Integer: %d", 25); // Output: Integer: 25
printf("Float: %f", 3.14); // Output: Float: 3.140000
printf("Character: %c", 'A'); // Output: Character: A
return 0;
}