This class outlines the easy steps to write your first C program. You will study what the basic elements that make it work are.
Learn to Write Your First C Program
1. C Program Coding Snippet
Below is the C program coding snippet which prints “Hello Computer” on the console. You can use any textual content editor to write this code. But we advocate and use Codeblocks to create, compile, and execute any C program.
#include<stdio.h> int main() { printf("Hello Computer"); return(0); }
2. basic structure of c program
We will now see what each particular person line in the above code means.
#include<stdio.h>
In the large mind of C, there are some pre-outlined directions to assist customers to execute instructions quicker. One instance of that is the “printf” instruction, which we’ll see quickly. There are particular libraries in C that include data of those directions. Therefore, “#include<stdio.h>” tells the pc to embrace the knowledge of those pre-outlined directions to be able to use them in your packages. “stdio” stands for “standard input-output.”
Note: You can observe what occurs if you do not write this line. It will present an error that the perform “printf” is not outlined. It is as a result of the compiler does not know what it means until you present the header file “#include<stdio.h>” which has its definition.
int function() in C
Every C program has a set of directions to be executed. We often write them contained in the physique of the primary() perform. It is the entry-level routine for all packages to start execution. From the primary perform, we name all different capabilities to carry out a set of duties. So, there has to be a “main” performance in a C program.
In C programming, the primary at all times returns an integer kind of worth. The “int” earlier than the primary represents the kind of worth returned.
A C program also can have consumer-outlined capabilities which we’ll cowl in the later tutorials. However, in C programming, utilizing the “main” is obligatory.
Note: You might have observed that after each performance, we give parentheses (). It is as a result of while calling, this system can pass a set of values or parameters to it. They additionally ship again information utilizing the “return” assertion. If there is nothing to return, then we might specify the 0 as a return worth or not use it in any respect.
printf() in c programming
It is a pre-outlined performance in C that tells the pc to show the textual content as it is. It helps the compiler to differentiate between command and precise textual content supposed for printing. The textual content contained in the parentheses ought to seem as it is on the display screen
Note: In the printf() perform, precise textual content to be displayed ought to be enclosed in double inverted commas “_.” Something like printf(“Hello Computer”).
getch() in c programming
It is a C enter perform which makes this system wait till the consumer presses a key.
return(0)
It represents the return code of the primary() perform. A non-zero worth means error whereas zero signifies this system will get executed efficiently.
In our instance, we return a zero worth which evaluates to success.
Curly braces in C
The curly brackets allow us to create a number of items of code in a C program.
They outline the boundaries of a performance such as the primary() right here and contain directions that may get executed in the C program.
A perform can have a number of nested blocks of code delineating utilizing the curly brackets. Each such part has entry to variables from the dad or mum block, increase spotify plays however, any of its variables will not be seen from outdoors.
We’ll present an in-depth description of the variable scope in the subsequent lessons.
3. Execute Your First C Program
Now that we’ve efficiently understood what each line means. Let us kindly this code on our newly setup IDE and Run it.
When you type this code and run it, you must see one thing like this.
Now click on on the higher inexperienced triangle, and you must see a display screen like this after just a few seconds.
Congratulations, You have efficiently written, compiled, and run a C program.
Related Posts
- How to compare dates in java|algorithm with source code
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order
- Source link