Posts

Showing posts with the label introduction

How to print Hello World! in C language

How to print Hello World! As when learning any new language, the place to start is with the classic "Hello World!" program: #include <stdio.h> int main() {     printf("Hello, World!\n");     return 0; } Let's break down the code to understand each line: #include <stdio.h> The function used for generating output is defined in stdio.h. In order to use the printf function, we need to first include the required file, also called a header file. int main() The main() function is the entry point to a program. Curly brackets { } indicate the beginning and end of a function (also called a code block). The statements inside the brackets determine what the function does when executed. The printf function is used to generate output: printf(); Here, we pass the text "Hello World!" to it. The \n escape sequence outputs a newline character. Escape sequences always begin with a backslash \. The semicolon ; indicates the end of the...

Variables in C Programming Language

Variables in C : A variable is a name for an area in memory. The name of a variable (also called the identifier) must begin with either a letter or an underscore and can be composed of letters, digits, and the underscore character. Variable naming conventions differ, however using lowercase letters with an underscore to separate words is common (snake_case). Variables must also be declared as a data type before they are used. The value for a declared variable is changed with an assignment statement. For example, the following statements declare an integer variable my_var and then assigns it the value 42: int my_var; my_var = 42; You can also declare and initialize (assign an initial value) a variable in a single statement: int my_var = 42; Let's define variables of different types, do a simple math operation, and output the results: #include <stdio.h> int main() {     int a, b;     float salary = 56.23;     char letter = 'Z'...

Data type in C Programming Language

Data Types in C : C supports the following basic data types: int: integer, a whole number. float: floating point, a number with a fractional part. double: double-precision floating point value. char: single character. The amount of storage required for each of these types varies by platform. C has a built-in sizeof operator that gives the memory requirements for a particular data type. For example: #include <stdio.h> int main() {     printf("int: %ld \n", sizeof(int));     printf("float: %ld \n", sizeof(float));     printf("double: %ld \n", sizeof(double));     printf("char: %ld \n", sizeof(char));     return 0; } The program output displays the corresponding size in bytes for each data type. The printf statements in this program have two arguments. The first is the output string with a format specifier (%ld), while the next argument returns the sizeof value. In the final output, the %ld (for long decimal) ...

Constants in C Programming Language

Constants in C : A constant stores a value that cannot be changed from its initial assignment. By using constants with meaningful names, code is easier to read and understand. To distinguish constants from variables, a common practice is to use uppercase identifiers. One way to define a constant is by using the const keyword in a variable declaration: #include <stdio.h> int main() {     const double PI = 3.14;     printf("%f", PI);     return 0; } The value of PI cannot be changed during program execution. For example, another assignment statement, such as PI = 3.141 will generate an error. Another way to define a constant is with the #define preprocessor directive. The #define directive uses macros for defining constant values. For example: #include <stdio.h> #define PI 3.14 int main() {     printf("%f", PI);     return 0; } Before compilation, the preprocessor replaces every macro identifier i...