Back Next

Programming in C

Lesson 1


Course support : You can ask your course questions in Learnem support forums .

Please support us by visiting our support forums and asking your questions and answering questions of others.

1-1 Introduction

This course is a quick tutorial on C Programming language. We assume that you are familiar with at least one of famous operating systems.

For this course you can use the following compilers or Programming Environments.

- cc in Unix or gcc in Linux operating systems
- Borland C or Turbo C in DOS operating system
- Visual C++ (create a simple win32 console application.) in Windows
- C++Builder (create a console application using console app wizard) in windows

We suggest a simple environment like Unix, Linux or DOS. Windows compilers are very complex for newbie programmers. 


1-2 Your first C program

Let's write our first C program.

#include <stdio.h>
main()
{
printf("Hello World!\n");
}

First step for running this program is to make a text file containing above code. Be sure that the file is a pure text file. You must save the text file with .c extension.

Then you must compile the source code. The result of compile step is an executable file that you can run it.

To compile program under most of Unix operating systems you will use the command:

$ cc test.c

and under linux: 

$ gcc test.c

The resulting executable file is a.out file. To run this executable you must type:

$./a.out

Program output must appear on your screen.

Hello World!

To compile the source code in other environments, you must use their Development Environment. You must consult your compiler User's Guide for this. 

If you have problem working with your compiler you may ask your problem in our support forums. To reach our forums, visit:

http://www.learnem.com/forums/


1-3 Details of Test program

#include <stdio.h> 

Tells C compiler to include the file "stdio.h" in this point of your C program before starting compile step. This "include file" contains several definitions , declarations etc.

main()

C program consist of one or more functions. Functions are building blocks of C programs. main() function is different from other functions by that it is the start point of program execution. Our program contains only function while complicated programs may contain thousands.



Opening brace marks the start of a block. Closing brace will mark its end. This one marks main () function start

printf("Hello world!");

This line of code prints the statement between quotation marks on your output screen. \n tells program to start a new line in output screen. 

Each command line in C ends with ";" character. Control statements are exceptions. You will soon be able to 
determine when you must use ; to end a line of code.

}

closes main() function. This program contains only one function while complicated programs may contain several functions.


1-4 Data types and variables

C uses several data types of data. These include characters, integer numbers and float numbers.

In C language you must declare a variable before you can use it.

Declaring a variable to be an integer or a character for example will let computer to allocate memory space for 
storing and interpreting data properly.


1-5 Naming a variable 

It's better that you use meaningful names for your variables even if this causes them to became long names. Also take this in mind that C is case sensitive. A variable named "COUNTER" is different from a variable named "counter".

Functions and commands are all case sensitive in C Programming language. You can use letters, digits and underscore _ character to make your variable names. Variable names can be up to 31 characters in ANSI C language.

The declaration of variables must take place just after the opening brace of a block. For example we can declare variables for main() function as below code:

main()
{
int count;
float sum,area;
.
.
.
}

First character in a variable name must be a letter or an underscore character. It cannot be a C programming 
language-reserved word (i.e. Commands and pre defined function names etc)

An example for using variables comes below:

#include<stdio.h>
main()
{
int sum;
sum=12;
sum=sum+5; 
printf("Sum is %d",sum);
}

General form for declaring a variable is:

Type name;

The line sum=sum+5; means: Increase value of sum by 5. We can also write this as sum+=5; in C programming language.

printf function will print the following:  

Sum is 17

In fact %d is the placeholder for integer variable value that it's name comes after double quotes.


Common data types are:

int : integer
long : long integer
float : float number
double : long float
char : character


Other placeholders are:

%d : decimal integer
%ld : decimal long integer
%s : string or character array
%f : float number
%e : double ( long float)


printf () function used in this example contains two sections. First section is a string enclosed in double quotes. It is called a format string. It determines output format for printf function. Second section is "variable list" section.

We include placeholders for each variable listed in variable list to determine it's output place in final output text of printf function.


1-6 Control characters

As you saw in previous examples \n control character makes a new line in output. Other control characters are:

\n New line
\t tab 
\r carriage return
\f form feed
\v vertical tab


1-7 Multiple functions

Look at this example:

#include<stdio.h>
main()
{
printf("I am going inside test function now\n");
test();
printf("\nNow I am back from test function\n");
}

test()
{
int a,b;
b=a+100;
printf("a is %d and b is %d",a,b);
}


In this example we have written an additional function. We have called this function from inside main function. When we call the function, program continues inside test () function and after it reached end of it, control returns just after test () function call in main ()

You see declaring a function and calling it, is an easy task. 

Just pay attention that we used ";" when we called the function but not when we were declaring it.

We finish this lesson here. Now try to do lesson exercises and know the point that you will not learn anything if you do not do programming exercises.


Exercises:

Course support: 
Paid students must send exercises to their tutor. Tutor will return corrected exercise to the student. Others can ask their  questions in Support forums in our web site.

http://www.learnem.com/forums/


1- What is the exact output result of this code.

#include <stdio.h>

main()
{
printf("Hi\nthere\nWhat is the output\n?");
}

2- Write a program that declares two floating numbers. Initialize them with float values. Then print 
their sum and multiplication in two separate lines.

3- Write the output result of multiple function example in this lesson.

4- Why these variable names are not valid ? 

test$var
my counter
9count
float

============================================================ 

Copyright Notice :

© 2000,2001 Siamak Sarmady and Learnem Group . All rights reserved. This text is licensed to be used on ProgrammingUni website. Any kind of reproduction, redistribution in any form is strictly prohibited without written permission of the writer. 

Back Next