The First C Program

getting started with C program
getting started with C program

getting started with C program Armed with the knowledge of the types of variables, constants, and keywords, the next logical step is to combine them to form instructions. However, instead, we will now write our first C program. After we have done this, we will see in detail the instructions he used.

Before you start with our first C program, remember the following rules that apply to all C programs:

  • Each instruction in a C program is written as a separate statement. Therefore, a complete C program will include a series of statements.
  • The declarations in a program must appear in the same order in which we want them to be executed; unless the logic of the problem requires a deliberate “jump” or transfer of control in a statement, which is not in order.
  • Empty spaces can be inserted between two words to improve the readability of the statement. However, blank spaces in a variable, constant, or keyword are not allowed.
  • All statements are written in lowercase letters.
  • C has no specific rules for the position in which a statement should be written. This is why it is often called free speech.
  • Each statement C must end with a. Thus; it acts as a statement terminator.

Let us now note our first program C. It would simply calculate the simple interest for a set of values ​​representing the principle,
the number of years and the interest rate.

/* Calculation of simple interest */

main( )
 int p, n ;
 float r, si ;
 p = 1000 ;
 n = 3 ;
 r = 8.5 ;
 /* formula for simple interest */
 si = p * n * r / 100 ;
 printf ( "%f" , si ) ; 

Now some useful tips about the program.

  • Program comments should be included in / * * /. For example, the first two statements in our program are comments.
  • Although comments are not required, it is good to start a program with a comment indicating the purpose of the program, its author and the date the program was written.
  • Any number of comments can be written anywhere in the program. For example, a comment may be written before the statement, after the statement or within the statement, as shown below:
/* _formula_ */ si = p * n * r / 100 ;
 si = p * n * r / 100 ; /* _formula_ */
 si = p * n * r / /* _formula_ */ 100 ; 

Sometimes it is not so obvious about what makes a particular statement in a program. At such times, it is worth mentioning the purpose of the statement (or set of statements) using a comment. E.g:

/* formula for simple interest */
 si = p * n * r / 100 ; 
  • Often, programmers seem to ignore writing comments. But when a team builds great software, well-commented code is almost essential for other team members to understand.
  • While many comments may not be required in this program, developers tend to use fewer comments than too many. An adequate number of comments can save you hours of trouble and offers when you try later to figure out what the program is doing.
  • Normal language rules do not apply to the text written in / * .. * /. Thus, we can enter this text in lowercase, uppercase or a combination. This is because the comments are given solely for the understanding of the programmer or the other programmers and are completely ignored by the compiler.
  • Comments cannot be nested. E.g,
* Cal of SI /* Author sam date 01/01/2020 */ */ 

is not valid.

  • A comment can be divided into several lines, as in,
/* This is
 a jazzy
 comment */ 

A comment can be divided into several lines, as in,

  • main () is a collective name given to a set of statements. This name must be primary (), it cannot be anything else. All statements belonging to the main () are enclosed in a pair of braces {} as shown below.
main( )
 statement 1 ;
 statement 2 ; 
statement 3 ;
  • Technically speaking main () is a function. Each function has a pair of parentheses () associated with it. We will discuss their functions and their operation in great detail in Chapter 5.
  • Any variable used in the program must be declared before using it. E.g,
nt p, n ;
float r, si ; 

Any statement C always ends with a;

float r, si ;
r = 8.5 ; 

In the statement,

si = p * n * r / 100 ; 
  • * and / are the arithmetic operators. The arithmetic operators available in C are +, -, * and /. C is very rich in operators.
  • There are approximately 45 operators available in C. Surprisingly, there is no exponential operator … a slip, which can be
    sorry about the fact that C was developed by a person, not a committee.
  • Once its value is calculated, it must be displayed on the screen. Unlike other languages, C does not contain any instructions to display the output on the screen. All screen output is done using ready-made library functions. Such a
    unction is printf (). I used it to display on the screen the value contained in and.
  • The general form of the printf () function is,
printf ( "<format string>", <list of variables> ) ; 

<format string> can contain,
%f for printing real values
%d for printing integer values
%c for printing character values 
  • In addition to format specifications, such as% f,% d and% c, the format string may also contain other characters. These characters are printed as they are when printf () is executed.
  • Here are some examples of using the printf () function:
printf ( "%f", si ) ;
printf ( "%d %d %f %f", p, n, r, si ) ;
printf ( "Simple interest = Rs. %f", si ) ;
printf ( "Prin = %d \nRate = %f", p, r ) ; 

The result of the last declaration would look like this …

Prin = 1000
Rate = 8.5 

What does “\ n” do in this statement? It’s called newline and takes the cursor to the next line. Therefore, you get the result
divided into two lines. “\ N” is one of several escape sequences available in C. These are discussed in detail in Chapter 11. At the moment, all we can say is that “\ n” comes in handy when we want to properly format the output on separated. lines.

printf () cannot print only the values ​​of variables, but can also print the result of an expression. An expression is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c – d are all valid expressions
The results of these expressions can be printed as shown below:

printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c – d ) ; 

Note that 3 and c are also valid expressions.

C Languages Topics

V.L.S.I Topics

Related Tech-News

Leave a Comment