Writing Your First C Program
To write your first C program, you need to follow these steps: 1. Open a text editor or an IDE (Integrated Development Environment) like CodeBlocks or Visual Studio Code. 2. Create a new file and name it "first.c" (you can use any other name as well, but make sure it ends with ".c"). 3. Copy and paste the following code:
first.c
copy
#include <stdio.h> // This is a header file
int main(){ // This is the main function
int a = 5; // This is a data type and initialize value
printf("Hello World"); // This is a predefined function
return 0;
}
4. Save the file and compile it using CodeBlocks or Visual Studio Code. 5. After successful compilation, you can run the program and see the output "Hello World" on your screen.
Basic Syntax and Rules of C Language
Every C program follows some basic syntax and rules that you should know before starting to write a program. These include: 1. Each instruction in a C program is written as a separate statement. 2. The statements in a program must appear in the same order in which we wish them to be executed. 3. Blank spaces may be inserted between two words to improve the readability of the statement. 4. All statements should be in lowercase letters. 5. Every C statement must end with a semicolon (;). Thus, ; acts as a statement terminator. 6. A C statement can be written anywhere in a given line. That's why it is often called a free-form language. 7. Usually, each line contains one statement. However, you can write multiple statements in one line, provided each statement is terminated with a semicolon (;).
Comments in C
Comments are used to give some message to the programmer and others. They are not executed by the compiler and are only used to increase the readability of the program. There are two types of comments in C: Single-line comments syntax: "//"
copy
// This is a single-line comment
Multi-line comments enclosed within "/* */"
copy
/*
This is a multi-line comment
It can span over multiple lines
*/
Variables in C
A variable is a name of the memory location used to store data. In C, variables are declared before they are used, and each variable has a specific data type associated with it. Some common data types in C include: 1. int: for integer values 2. float: for floating-point values 3. double: for double-precision floating-point values 4. char: for character values To declare a variable, you need to specify its data type, followed by its name. For example:
copy
int a;
float b;
char c;
You can also assign an initial value to a variable while declaring it. For example:
copy
int a = 10;
float b = 3.14;
char c = 'A';
What is Keyword in C
In C programming language, a keyword is a reserved word that has a specific meaning and purpose in the language. These keywords are part of the syntax of the language and are used to define the structure, behavior, and functionality of a program.
Some examples of keywords in C include:
1. int: used to declare a variable of type integer 2. float: used to declare a variable of type floating-point number 3. if: used to define a conditional statement 4. while: used to create a loop that executes as long as a certain condition is met 5. break: used to exit a loop or switch statement 6. return: used to exit a function and return a value
It is important to note that because these words are reserved, they cannot be used as variable names or identifiers in a program. Attempting to use a keyword as a variable name will result in a syntax error.
There are only 32 keywords available in C
copy
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while