Advertisement
Tech

My First C Program

In this article, we explain what characters are - the basic building blocks of any program. We then show how to write our first C program, a variation of the classic “Hello World” program.

By Noel Kalicharan
Desk Tech
Reading time 4 min read
Word count 752
Linux Computing Linux commands
My First C Program
Advertisement
Quick Take

In this article, we explain what characters are - the basic building blocks of any program. We then show how to write our first C program, a variation of the classic “Hello World” program.

On this page

Characters

In computer terminology, we use the term character to refer to any one of the following:

• a digit from 0 to 9;

Advertisement

• an uppercase letter from A to Z;

• a lowercase letter from a to z;

Advertisement

• a special symbol like (, ), $, =, <, >, +, -, /, *, etc.

The following are commonly used terms:

Advertisement

letter – one of a to z or A to Z

lowercase letter – one of a to z

Advertisement

uppercase letter – one of A to Z

digit – one of 0,1,2,3,4,5,6,7,8,9

Advertisement

special character – any symbol except a letter or a digit

e.g. +, <, >, $, &, *, /, =

Advertisement

alphabetic – used to refer to a letter

numeric – used to refer to a digit

Advertisement

alphanumeric – used to refer to a letter or a digit

Characters are the basic building blocks used in writing programs;

Advertisement

we put characters together to form variables and constants;

we put variables, constants and special characters to form expressions such as (a + 2.5) * c;

Advertisement

we add special words such as if, else and while to form statements such as

if (a > 0) b = a + 2;

Advertisement

and we put statements together to form programs.

Welcome to C Programming

We take a quick peek at the C programming language by writing a program to print, on the screen, the message

Advertisement

Welcome to BrightHub

One solution is the following

Advertisement

#include <stdio.h> main() { printf(“Welcome to BrightHub”); }

The statement

Advertisement

#include <stdio.h>

is called a compiler directive. This simply means that it provides information the compiler needs to compile your program. In C, input/output instructions are provided by means of standard functions stored in a standard library. These functions use variable (and other) declarations stored in a special header file called stdio.h. Any program which uses an input/output instruction (such as printf) must inform the compiler to include the declarations in the file stdio.h with the program. If this is not done, the compiler will not know how to interpret the input/output statements used in the program.

A C program consists of one or more functions (or, subprograms), one of which must be called main. Our solution consists of just one function so it must be called main. The (round) brackets after main are necessary because, in C, a function name is followed by a list of arguments, enclosed in brackets. If there are no arguments, the brackets must still be present. Here, main has no arguments so the brackets alone are present.

Every function has a section called the body of the function. The body is where the work of the function is performed. The left and right braces, { and }, are used to define the start and end, respectively, of the body. In C, one or more statements enclosed by { and } is called a block or compound statement.

The body of main contains one statement:

printf(“Welcome to BrightHub”);

printf is a standard output function which, in this example, takes one argument, a string constant “Welcome to BrightHub”. Note that, as with all functions, the argument is enclosed in round brackets. The semicolon is used to indicate the end of the statement. We say the semicolon terminates the statement. When executed, this statement will print

Welcome to BrightHub

on the ‘standard output’. For now, take this to mean the screen.

References

Reference text: C Programming - A Beginner’s Course

Related programming references

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement