Basics of C Programming

From SHellium Wiki
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

This tutorial is a very quick and somewhat sloppy introduction to C. It may well aslo get you into bad programming practice, I suggest you read the book by the authors on the topic called : The C Programming Language hint: You can buy it but it has been found freely avalible on the net in PDF form. Learn to use google.

The C Programming Language is a general-purpose "high-level" programming language developed by Dennis Ritchie in order to implement the Unix Operating system for portability. C is one of the world's most popular programming language owing to its power, speed, and wide spread support community.

C is a compiled language and naturally you need a compiler. There are numerous vendors which distribute C compilers. The most popular among those are the GNU C Compiler, Borland C Compiler, Microsoft Visual C++ (It's a C compiler), Digital Mars C Compiler, and many more.

It's recommend the GNU C Compiler (MinGW in Windows). It is an excellent compiler, free and Open Source.

In addition you need a text editor such as Gedit, Kate, or Notepad++ or the famous *nix ones such as vim and emacs.

Contents

Your First Program

Start your IDE or text editor and write the following code in it:

//Comment
/*Multiple Line
 Comment*/
 
#include<stdio.h>
 
int main()
{
    printf("Hello, World!");
    return 0;
}

This is a very simple C program. You can save it as an ASCII text file, say myfirst.cpp.

Output

Hello, World!

Explanation

The first line in this code is a comment. Comments are inputs which a programmer puts in her code so that she could refer to them at a later date. Though comments are not needed in such a simple program, they are extremely useful when a program becomes complicated. Hell, you might not even remember how you coded the program after some time. Any text after the "//" is not parsed by the compiler till the line ends. The compiler moves to the next line. Another way to add comments is the traditional C way in which you can enclose your comments by "/* */" The advantage of this type of comment is that it can span multiple lines, which is useful if you want to add a paragraph as comment. Remember C does NOT support nested comments. You cannot comment like this:

/* /* Nested comment */ */

This is because C parses /* and looks for ending */. When it looks for the closing */, it finds the inner one and ends the comment and as a result the user will get an error in the program. The next line #include<stdio.h> will include a header file stdio.h from the include directory. It has definitions of various important functions, one of which we are using in the program. Now this part:

int main()
    {
    }

main() is the function which is executed with the execution of the program, specifically the code between { } in this main() function. The int part specifies the return data type of the function. Remember to always use the type int as the return value for main(). In case you don't understand what return data type means, don't worry. It will be discussed in detail later. You don't even need to write int. Even the following will function properly.

main()
    {
    }


Now let's discuss the code within the main() function.

printf() is a library function (a function which has been already defined) which will output text to the console. Consequently, **printf("Hello, World!"); ** will print the text "Hello, World!" to the console. You can type any ASCII text instead of "Hello World" here (currently only a single line. We'll see about multiple lines later in the chapter).

return 0; is the statement which will send a value of 0 as return value from the function main(). Note that '0' an integer is used, as specified in the data type of return value in a function.

NOTE: C is a case sensitive language. Here return is NOT same as RETURN or ReTuRn. Please keep that in mind.

The #include directive

You see that this line starts with a pound sign, #, which is followed by include. In C, #include forms a preprocessor directive that tells the C preprocessor to look for a file and place the contents of the file in the location where the #include directive indicates. Also in this line, you see that <stdio.h> follows #include. You may guess that the file the #include directive asks for is something called stdio.h. You are right. The < > in the #include directive have a special meaning. They tell the C preprocessor to look for the file within the < > in the directory for include files (which is defined already by the compiler). In case you want to include a header file in the current directory, you will have to use " " instead of < >.

The main() Function

As said earlier, main() is the function which is executed at start of a program. Now it is vital that a C program have a main() function since without the presence of the main() function, the program will not know where to start execution. It will compile properly but it won't be linked.

For example you create a program:

//file1.c
#include<stdio.h>
 
int maim() //see the typo
    {
    return 0;
    }

It will certainly compile.

gcc -c file1.c

However if you try to link it, it will generate an error.

gcc file1.c -o exe

Compiler Output

/usr/lib/gcc/x86_64-linux-gnu/4.2.3/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/tmp/ccmnG4fQ.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

OMG! Error!

When you finish writing a C program and start to compile it, you might get some error or warning messages. Just don't panic when you see error messages. It is possible that several errors might be generated as a result of a simple typo.

Have Fun.


Constants and Variables

As the name implies, constants are values whose value does not change, more technically their values do not change during a program execution. On the other hand, the values of Variables change during the execution of a program. Take for eg. in math, PI is a variable whose value is always constant, however an arbitary variable x can have any value. A variable or a constant always holds a single value. eg.

i = 1
j = -7

Expressions

An expression is a combination of constants, variables, and operators that are used to denote computations. For eg.

2 * 6 + (9-1)/4

Arithmetic Operators

As you've seen, an expression can contain symbols such as +, *, and /. In the C language, these symbols are called arithmetic operators. The most commonly used Operators in C is:

+ Addition Operator
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Modulus Operator


Statement

In the C language, a statement is a complete instruction, ending with a semicolon. In many cases, you can turn an expression into a statement by simply adding a semicolon at the end of the expression

i=2+3;
printf("Lambda");
Statement Blocks

A group of statements can form a statement block that starts with an opening brace ({) and ends with a closing brace (}). A statement block is treated as a single statement by the C compiler.

NOTE: Before you begin to code for a program, you should do some adequate planning before beginning typing code. It is better to plan ahead rather than being forced to make changes later.

A Slightly More Interesting Program

Now let us make a program that will calculate area of a circle. Before you code let's decide how we'll plan for this:

   * User is prompted to enter the radius of a circle
   * User enters the radius
   * The program calculates the area of the circle
   * The program displays the area of the circle.

Now let's rock.

Code

#include<stdio.h>
 
int main()
{
    float radius, area;
    #define PI 3.1415
 
    printf("Enter the radius for the Circle whose Area you wish to calculate: ");
    scanf("%f", &radius);
 
    area = PI * radius * radius;
 
    printf("The area of the circle is %f\n", area);
 
    return 0;
}

Output

Enter the radius for the Circle whose Area you wish to calculate: 2.1
The area of the circle is 13.854013

Explanation

We have already been familiar with the structure of program simple as this with the previous chapter so I'll go straight to the point.

float radius, area;

^ We declare two variables radius, and area using the keyword float. The float keyword will create variables of floating decimal types which will allocate 4 bytes in the memory. Similarly there are keywords such as int, long, char which are used to define variables of type integer, long integer, and characters respectively. Remember in C unlike many other programming languages, variables have to be explicitly defined. i.e. You just cannot start using a variable without defining it first.

#define PI 3.1415

^ Here a symbolic constant is defined using the preprocessor. As the program is compiled, the preprocessor will look for any appearance of "PI" in the code and replace it with "3.1415". An alternative to this is to create a constant variable, which exists as a variable during runtime, but which cannot be changed. Different types of variables will be discussed in more detail later.

printf("Enter the radius for the Circle whose Area you wish to calculate: ");

^ We are printing this text on the console. So that the user reads it. However this code will NOT prompt the user for input.

scanf("%f", &radius);


^ Now the function scanf() is used to prompt to prompt the user for input. Basically this statement takes input from the user and puts the user inputted value into the variable 'radius'. More on scanf() later.

area = PI * radius * radius;

^ This is essentially the most important statement of the program that will calculate the area of the circle. During compilation, PI is replaced by "3.1415" and during runtime, the calculation is performed with the current value of the variable "radius". It will calculate the value and will put that value to variable "area".

printf("The area of the circle is %f", area);

^ This will print the value of the area of the circle.


Variables and Constants in C

Variables

A variable in C is always explicitly defined. There is no generic keyword for defining variables like in many other languages, but there are data type keywords such as int, float, char, etc. For example you wish to create a integer variable, you can use the int keyword for creating an integer.

int var;
Integers

The "integer" variable type is used to store positive and negative integers. The number of bytes in an integer is system-dependent, with a minimum of two bytes and four and eight bytes among the possibilities, and so the largest and smallest integers which can be stored are also system dependent. For example, if an integer on a system takes up two bytes, then it can store value in the range of −32,768 to +32,767. The "int" keyword can be preceded by "unsigned" so that the integer will store only positive values. In the case of a two-byte integer, its possible values range from 0 to +65,535. An integer can also be preceded by "signed", but this is generally unnecessary, since integers are signed by default.

int var1; //var will store -ve as well as +ve values
unsigned int var2;e//only +ve values
signed int var3;  //same as var1
Long Integers

In case you want to store larger numbers, you can use the data types "long" and "long long". The sizes of these are also system-dependent, but a long integer must be at least four bytes and a long long integer must be at least eight bytes. It can also be assumed that a long integer is at least as long as an integer and that a long long integer is at least as long as a long integer.

Good programming practice dictates that a programmer never make assumptions about the length in bytes of these variables. If a program needs that information, the "sizeof()" function will return the size in bytes of its argument.

long var;
long long var;

The "unsigned" modifier can also be used with long integer types.

unsigned long var;

NOTE: Remember that integer data types only store integer values and NOT decimal values. In case you assign a decimal value to an integer variable it will cast to an integer values that is, the decimal part of the number is pruned.

Floating Point Decimals

The "float" variable type is used for storing floating point decimals. There are also "double" and "long double" types for storing numbers with higher precision and range. Please note you cannot use "unsigned" with floating point variables.

float var1;
double var2;
long double var3;
Characters

The "char" is the character data type and is used for storing alphanumeric characters and symbols. Each char takes only one byte and is capable of storing one character only. A variable of type char is internally treated as an integer with range of −128 to +127. It can also have type unsigned. When a variable is assigned as a character, it stores the ASCII code of the character.

char c1 = 'a';
char c2 = '9';
char c3 = 9;


All three statements are correct. However please note that the variables c2 and c3 are NOT the same. In case of c2 = '9', the variable c2 is assigned to character '9' and not the integer 9; internally c2 stores the value 57 which is the ASCII code for '9'. In case of c3=9, it stores the integer 9 and the character it exhibits is Horizontal Tab(character of Tab Key). Please see the ASCII Table.

Arrays

Arrays can effectively be thought of as a list or group of variables. Arrays can be made of any C type. for example

char c[50];

Creates an array of 50 characters. An array starts from element [0] and goes up to [n-1] of your definition e.g. Each element in the array is acessed by specifying the name of the array and then the element number in square brackets ([])

c[0] = 'A' /*valid*/
c[49] = 'B'/*valid*/
c[50]= 'C' /* this is invalid*/

NOTE: there is no length checking build in on arrays, so if the above invalid statment will actually try to write to a memory adress which may belong to another program. Depending on the OS this will probably be prevented and the program terminated.

Constants

As discussed above, constants do NOT change their value during a program execution. Constants can be defined in C as follows:

#define PI 3.1415

After you declare a constant, it's value does not change during program execution. You will be noting we are using the C preprocessor here. When a #define is used to create a constant, the preprocessor will replace each occurrence of the variable name by its value before sending it for compiling. Technically, #define does not create a variable whose value as constant but it appears as such to the programmer. To create a variable which exists during runtime, but which cannot be changed, use the keyword "const". Keep in mind though many antiquated compilers will not compile the keyword const. When using the const keyword it is possible to specify a data type for the variable, which does not occur when the preprocessor is used to define a symbolic constant. Usage:

const float PI = 3.1415;
const int ONE = 1;

In all these cases, the compiler will issue an error if you try to change the value of a constant variable. By convention, constant variables are always written in UPPER CASE, although it's not mandatory.

Reading and Writing to the Standard I/O

printf()

The standard library function "printf()" sends formatted output to the console. The simplest use of printf() is to print a fixed text string, by including the text in double quotes ("<text>") as an argument to the function, e.g.

printf("Text written here");

To print multi-line text requires a backslash to be inserted. This code

printf("This Code
is a multiline");

will not compile. With the backslash inserted

printf("This Code\
Is a multiline");

the output will be: This CodeIs a multiline.

To print the text on multiple lines, use the newline escape sequence `\n'. This code will print the text on two lines.

printf("This Code \n is a multiline");

There are many such escape sequences, such as '\t' for tab, '\\' for backslash('\'), and '\0' for an character with stored value zero.

Usage with Variables

To make use of the values of data as part of a print statement using printf(), it is necessary to insert formatting information into the string to be printed and to follow the string with the variables to be printed in the order that they are to be printed. To print something like "The Area of the Circle is <>" (where <> is the calculated area, we can call printf() like this:

printf("The Area of the Circle is %f", area);

The "%f" included in the string to be printed is the simplest format to print a floating point number.

The printf() function will recognize formatting for several different broad data types, such as:

%d Integer
%f Floating Point Decimals
%c Character
%p Pointer
%s String
%% The % Symbol

When there are multiple variables to be inserted into a message printed to the console, include a formatting command for each in the appropriate place and follow the string in the printf() command with the variables to be printed in the same order.

printf("The Area of the Circle is %f and radius was %f", area, radius);

Advanced Formatting

The printf() function allows for more detailed format specifications by allowing display flags, a field width, a precision , and a length modifier to be included in the basic format strings described above. Display flags control things like left- vs. right-justification and whether a sign will always be printed. The field width is the minimum number of characters to print and the precision indicates the number of digits past the decimal. The length modifier modifies basic data types to be printed as "short" or "long". More details and examples of this are described in Advanced printf Formatting.


The scanf() Function

For new C programmers the scanf function can be a large stumbling block for a few reasons. Firstly scanf does not have any inbuilt length checking. Ie it will get input up until a whitespace or newline character is found. This means

/*Old bad way! Don't use this!*/
char buffer[10];
scanf("%s",buffer);

will read as many characters as typed in into buffer even though buffer can only hold 10 characters. This will cause what is known as a buffer overflow or fence post error.

/*more work to follow on this*/ To remedy this use fgets() and sscanf().

fgets() is a function which takes three paramaters, its first paramater is a buffer to hold your string/array of characters in. The second paramter is the length of the array/string, and the final paramter is the input stream which you want to read from. If you want to read from the keyboard you can simply use 'stdin' as your input stream.

e.g.

char buffer[10];
fgets(buffer,10,stdin);

This will read at most 10 characters into buffer from stdin including the terminating '\0' (null byte).

sscanf() is a function which takes atleast 3 paramaters. the first paramater is the string you want to read from, the second is the format string which dictates how you want to store the data you are reading from the string and the rest of the paramaters are references to the variables you want to store the data in.

e.g.

/*Good new way! use this!
 get some input*/
int myInt;
char buffer[10];
fgets(buffer,10,stdin);
/*now lets try to put that input into an integer*/
sscanf(buffer, "%d", &myInt);

/* If you really want to use the bad way here it is. but be careful */ As depicted in the above example, the scanf() function is used in order to get user input.

scanf("%f", &radius);

^ The above statement will get user input and the value the user inputted from the standard input is put into the variable radius. The left part is the format string, while on the right you put the variables.

NOTE: Keep in mind not to forget to place the & in front of the variable is scanf(). Otherwise your program will probabaly show a Segmentation Fault or other Runtime Failure. Believe me, I have wasted hours banging my head on the failure of my program just for forgetting an &.

Just like printf(), scanf() can also get multiple inputs.

scanf("%f%d", &radius, &v1);

You can even put random string with the format string, however they are ignored. Do not forget to put the format specifier per variable though.

The getchar() Function

The getchar() function is used to read the character from the keyboard. Usage:

#include<stdio.h>
 
int main()
{
    char ch;
    ch = getchar();
    return 0;
}

Please Note it will read only ONE character.

Technically, getchar() is similar to getc(stdin)

The putchar() Function

The putchar() function is used to output a character into the standard console. Usage:

#include<stdio.h>
 
int main()
{
    char ch;
    ch = getchar();
    putchar(ch);
    return 0;
}

This will output the character input.

Technically putchar() is same as putch(ch, stdout)

Manipulating Data with Operators

You can think of operators in C that let you manipulate data. In fact, you've learned some operators, such as + (addition), - (subtraction), * (multiplication), / (division), and % (remainder).

  • Arithmetic Assignment Operator
  • Unary minus operators
  • Increment and decrement operators
  • Relational operators
  • Arithmetic Assignment Operator

In the C language, the = operator is called an assignment operator. eg. var = 12; Here the statement causes the value of the right-hand-operand to be assigned (or written) to the memory location of the left-hand-operand. Additionally, the assignment statement itself returns the same value that is assigned to the left-hand-operand. For example, the a = 5; statement writes the value of the right-hand operand (5) into the memory location of the integer variable a (which is the left-hand operand in this case).

NOTE: Don't confuse the assignment operator (=) with the relational operator, == (called the equal-to operator). The == operator will be introduced later. Combining Arithmetic Operators with the assignment operator

First consider this statement:

x = y + z;

It's simple. Isn't it? x is assigned a value of sum of y and z. Now consider another statement:

x = x + y;

Wait! Isn't this wrong? Would this would mathematically imply y = 0. No. In programming languages the '=' sign does not word as in equations in maths and as stated above it will write the value in right-hand operand in the left-hand operand. So in this case x's value would be overwritten with the sum of the previous value of x before this operation and y. In C you could shorten the above statement with:

x += y;

This is same as the statement above. This statement practically means add value of y to the existing value of x.

Similarly you can use the subtraction, multiplication, division, and remainder operator like this.

x += y;
x -= y;
x *= y;
x /= y;
x %= y;

There is also another short hand too. It is a very common scenario in programming to increment a number by 1. So we can use the statement:

x = x+1;

Taking cue from what we learnt just now:

x += 1;

This is work well too and with some typing changed. However there is even a more shorter notation too.

x++;

^ This statement will increment the existing value of x by 1.

Similarly you can decrease the value of a variable by 1:

x--;

Prefix and Postfix

You can also increment and decrement variables by:

++x
--x

Now the question comes to mind, what is the difference? Quite a lot actually. In case of earlier example of x++, the increment is called postfix. In this case the increment happens last after all other operations have been done and just before the assignment operator assigns the value. In case of ++x, th increment is called prefix and it happens before.

Program to demonstrate the difference
//This Program will demonstrate difference between prefix and postfix
 
#include<stdio.h>

int main()
{
   int a = 10;
   int b = 25;

   printf("Prefix = %d\n", ++a + b);
   a = 10;
   b = 25;
   printf("Postfix = %d\n", a++ + b);

   return 0;
}

Output:

Prefix = 36
Postfix = 35

What is happening is that in Prefix case it is incrementing with statement ++a and then adding to b to give the output of 36. In the postfix case, the original value of a is added to b giving the value 35 and then incrementing a.

For more information about programming in C, please refer to C Programming Topics.

Personal tools
Namespaces

Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox