Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword or section
Like this

Table Of Contents

0 of .
Results for:
No results containing your search query
P. 1
c Programming

c Programming

Ratings: (0)|Views: 12,247|Likes:
Published by ezgibulut

More info:

Published by: ezgibulut on May 05, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, DOC, TXT or read online from Scribd
See More
See less





To Cut a Long Story Short...
C is a high-level programming language developedby Dennis Ritchie in 1972. He named it C becausethere was an existing programming language calledB.Other programming languages, such as C++, Perl,Java and JavaScript, all have one common ancestor:C.Programs are written in C. Computers don'tunderstand C - the code needs to becompiledandturned into machine (binary) code before theprogram can be executed. Binary code is the lowestlevel language and can be thought of as consistingof 1's and 0's.To learn C you mustPRACTICE!!!! Reading is notenough.You should also get a C compiler and write and testyour own programs - I learnt best from my (many)careless mistakes. There are many free compilersavailable on the net. For freeware, shareware anddemo programs, try performing a search at:
So why should you learn C? Well, It opens the doorsto other languages like C++, Java and evenJavaScript. Flash ActionScript follows on nicely fromJavaScript too.
The Basics
The C code you write is called theSYNTAX.Syntax is a mixture of:C keywords like
.Constants and variables.Operators like
(arithmetic "addition"),
(logical "or") and
(the "address of"operator).Note that C isCASE SENSITIVE! For example, wordslike cat, Cat, cAt and CAT are all considered differentfrom one another.Also, the amount of "white space" you use in a Cprogram does not affect the way it's compiled. Useextra spaces to make your programs more readable -indentation of code is very common. Obviously, youcan NOT put spaces or line breaks in the middle of keywords like this:
str uct
At home, you may use a Borland C++ compiler.Nowadays, Microsoft's Visual C++ 6.0 is popular. This is a powerful package and is suitable for peoplewho are interested in software engineering.In general, C++ packages are fine for compiling C programs - the compiler executed depends on theextension of your source file(s).Whilst at university, you may also use UNIX-based compiler (gcc ie). The command for compiling is of theform:
cc world.c -o world
, where
ran the gcc compiler. The filename after
is the file to becompiled, which is "world.c" in this case, the filename after
is the output file, which is "world". Theoutput filename is the word you type to run the program.
Commenting Your Code
You can add comments to your code by enclosingyour remarks within
. However, nestedcomments aren't allowed.A few properties of comments:• They can be used to inform the personviewing the code what the code does. This ishelpful when you revisit the code at a laterdate.• The compiler ignores all the comments.Hence, commenting does not affect theefficiency of the program.• You can use
to comment outsections of code when it comes to finding
/* Comments spanning several */ /* lines can be commented*/ /* out like this!*/ 
/* But this is asimpler way of doing it! */  // These are C++// style comments// and should NOT // be used with C!! /* /* NESTED COMMENTS ARE ILLEGAL!! */ */ 
errors, instead of deletion.
Creating Executable Programs
There are several tasks that need to be performed before you can run a program: coding, compiling andlinking.1. You have to write thesource codein C. You declare which header files you want to include withinthe source code. The source code must be saved with the extension .c.2. Then you run thecompiler, which translates the source code into machine, or binary code, whichthe computer can understand. Computers do NOT understand C!3. Sometimes the source code is still lacking some parts, so after going through the compiler, thecode is passed through aLINKER. This basically "links" the source code to other library or objectfiles so that the final binary code is produced. Don't worry, I'll explain compiling and linking ingreater detail in a later section.
Hello World
Your First Program
Let's write and compile your first program!Enter or copy the following code into your favoritetext editor (e.g. Notepad, Emacs, SimpleText etc.)and save it with the .c extension. These MUST besaved in text only format - try and avoid wordprocessors like MS Word.
is also very good. Plus it's free - make sureyou download it!!
#include <stdio.h> int main() {printf("Hello World!\n");return 0;}
When saving in Notepad you should surround a filename with quote marks, forexample, "world.c". This ensures that the filename will be saved with the correctextension, rather than world.c.txt, which is incorrect.
If a line starts with a hash, denoted by
, it tells thecompiler that a command should be sent to theCPREPROCESSOR. The C preprocessor is a programthat is run when you compile.
is one of the many C preprocessor commands you'll use.Basically, when the preprocessor finds
itlooks for the file specified and replaces
withthe contents of that file. In a way, this makes thecode more readable and easier to maintain if youneeded to use common library functions. Morepreprocessor commands in the later sections.....
Header Files
Header files have the extension .h and the fullfilename follows from the
directive.They contain declarations to certain functions thatyou may or may not have used in your program.For example, the
file is required if you haveused functions like
in yourprogram. More about these two functions in theStandard Input and Output section.There are two ways to include a header file:
#include "stdio.h"
#include <stdio.h>
If you use the double quote marks, it means that thedirectory you're currently in, will be searched for firstfor the header file, before any other directories aresearched.If you use the square brackets, directories other thanthe one you're currently in, will be searched for theheader file. 
AFUNCTIONcan be thought of as a group of instructions that are evaluated when the function isCALLED.If a function returns nothing, its return type is of type
- i.e. nothing is returned.The
function is special, as it returns an integer
All C programs must have a
function. You canonly have one, but you can place it anywhere withinthe code.The program always start with the
functionand ends when the end of 
is reached.Functions return a value too - this will be explainedlater.by default, which is why you'll see me write
at the end of the program. Zero is usually returnedto indicate error-free function termination.Another way to terminate a program is to use the
function - there's an example later on.
Constants and Variables
The Main Idea
Variables are like containers in your computer'smemory - you can store values in them and retrieveor modify them when necessary.Constants are like variables, but once a valued isstored (i.e. the constant isINITIALIZED), its valuecannot be changed.
Naming Variables
There are several rules that you must follow when naming variables:
Variable names....ExampleCANNOT start with a number2iCAN contain a number elsewhereh2oCANNOT contain any arithmetic operators...r*s+t... or any other punctuation marks...#@x%£!!a... but may contain or begin with an underscore_height_CANNOT be a C keywordstructCANNOT contain a spaceim stupidCAN be of mixed casesXSquared
There's a quiz on naming variables later...
Some Terminology
EXPRESSIONSconsist of a mixture of constants,variables and operators (more about operatorslater). They return values. Here are some examplesof expressions:
/* a constant */ 
/* a variable */ 
x + 17
/* a variable plus a constant */ 
STATEMENTSare instructions and are terminatedwith a semicolon,
. Statements consist of a mixtureof expressions, operators, function calls and variouskeywords. Here are some examples of statements:
x = 1 + 8;printf("We will learn printf soon!\n");int x, y, z;
/* more on "int" later */ 
STATEMENT BLOCKS, on the other hand, cancontain a group of statements. The C compilercompiles the statement block as if it was just onestatement. To declare a statement block youenclose your statements between curly braces.This example has a statement block in an if-elsestatement - don't worry if you can't understand thecode just yet. Everything in each statement block issomewhat merged into a single statement. Thereare 2 statement blocks here:
if(x==10) {
/* block 1 */  
printf("x equals 10\n");x = 11;printf("Now x equals 11\n");x = x + 1;printf("Now x equals 12\n");}
/* end of block 1 */ 
else {
/* block 2 */  
printf("x not equal to 10\n");printf("Good bye!\n");}
/* end of block 2 */ 
Types of Constants

Activity (7)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Miles Pruitt liked this
EzKeezE liked this
Rajesh Verma liked this
ankithns102 liked this
user1230 liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->