My First C Program

Page content

Characters

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

• a digit from 0 to 9;

• an uppercase letter from A to Z;

• a lowercase letter from a to z;

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

The following are commonly used terms:

letter – one of a to z or A to Z

lowercase letter – one of a to z

uppercase letter – one of A to Z

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

special character – any symbol except a letter or a digit

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

alphabetic – used to refer to a letter

numeric – used to refer to a digit

alphanumeric – used to refer to a letter or a digit

Characters are the basic building blocks used in writing programs;

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;

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

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

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

Welcome to BrightHub

One solution is the following

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

The statement

#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