C with GCC
You are here: Main Page >> GCC
GNU Compiler Collection (GCC) is open source compiler system supporting various languages like C, C++, Java etc. GCC was initially developed as C compiler but later extended for languages like C++, Fortran, Pascal. Various Unix and Linux flavors have accepted GCC as standard compiler. Also GCC is available for embedded platforms. This documentation assumes that the reader has fair knowledge of Linux commands, if not please see the Linux spoken tutorials.
These tutorials are created using GCC version 4.4.3.
The Spoken Tutorial Effort for C with GCC is being done by Anuvrat of Amity University and Thyagarajan Shanmugham of www.spoken-tutorial.org
- 1 Introduction
- 2 Basic Level
- 2.1 Hello World In C
- 2.2 Structure of a C Program
- 2.3 Variables and data types
- 2.3.1 What are Variables, how to initialize them
- 2.3.2 Declaring, Initializing, and Assigning Variables
- 2.3.3 Data Types
- 2.3.4 Modifiers
- 2.4 Operators in C
The GCC usage can be divided into following three levels Basic level, Intermediate level and Advanced level.
Basic level covers Introduction to Programming C language with gcc.This level is aimed for beginner level of audience.
Hello World In C
This episode is to introduce the environment, explaining the user about how to type the code, save and compiling it.
- Checking whether gcc is installed.
- Checking the version number.
- Creating the working directory
- Writing the first program to print the string "hello world in C".
- Saving the code in gedit.
- Compiling the code , narrating a.out.
- Inducing a typo, and explaining about the semantical error.
- Narrating the importance of -o output switch in gcc.
Structure of a C Program
This episode is to talk about the structure of the C program.Any c program is made of primarily two area's , The pre-processor area and the functional code area.
- The structure of C program, the preprocessor area and the functional code area
- What is the pre-processor
- Preprocessor area classification viz: The directive statements, The declarative statements and the conditional statements
- The directive statement - What #include meant to do
- The declarative statement - what #define meant to do
- The conditional statement - Ifdef - enddef and Ifndef-enddef area
- Global environmental variables like __win32__ and __posfix__
- The main() function- the functional code block
- User defined function belongs to the functional code area
Variables and data types
This episode talks about variables and various data types available in the C programming language.Basic data types in C are char,int,float and double.C language is not a strong data type checking language, if a variable is defined in one data type, it can be accessed in another data type because of the polymorphic behavior of the language.
What are Variables, how to initialize them
Like most programming languages, C is able to use and process named variables and their contents. Variables are simply names used to refer to some location in memory – a location that holds a value with which we are working.
It may help to think of variables as a placeholder for a value. You can think of a variable as being equivalent to its assigned value. So, if you have a variable i that is initialized (set equal) to 4, then it follows that i+1 will equal 5.
Since C is a relatively low-level programming language, before a C program can utilize memory to store a variable it must claim the memory needed to store the values for a variable. This is done by declaring variables. Declaring variables is the way in which a C program shows the number of variables it needs, what they are going to be named, and how much memory they will need.
Within the C programming language, when managing and working with variables, it is important to know the type of variables and the size of these types. Since C is a fairly low-level programming language, these aspects of its working can be hardware specific – that is, how the language is made to work on one type of machine can be different from how it is made to work on another.
All variables in C are typed. That is, every variable declared must be assigned as a certain type of variable.
Declaring, Initializing, and Assigning Variables
Here is an example of declaring an integer, which we've called some_number. (Note the semicolon at the end of the line; that is how your compiler separates one program statement from another.)
<source lang=c> int some_number; </source>
This statement means we're declaring some space for a variable called some_number, which will be used to store integer data. Note that we must specify the type of data that a variable will store. There are specific keywords to do this – we'll look at them in the next section.
Multiple variables can be declared with one statement, like this:
<source lang=c> int anumber, anothernumber, yetanothernumber; </source>
We can also declare and assign some content to a variable at the same time.
<source lang=c> int some_number=3; </source>
This is called initialization.
When we wish to store an integer, we use an integer type, usually int. ints have at least 16 bits. Many platforms have 32-bit ints. There is also long int, which is guaranteed to have at least 32 bits. short ints have at most 16 bits, just like regular ints. However, often int is made to be longer, so short int is used when conserving memory is important.
All these integer types are signed, meaning that they can hold both positive and negative values. There is an corresponding unsigned type that only holds nonnegative values. However, they can hold larger values in the same number of bits. Using the keyword unsigned yields an unsigned type. For example, unsigned int is a unsigned integer type of at least 16 bits. The keyword signed can also be used, but is redundant.
When we wish to store a single character or byte, we use a character type. The C name for this type is called char. chars always use one byte each. char can also be used to store integers small enough to fit in a byte.
When we wish to store a floating-point (non-integer) value, we can use the types float, double, and long double. These differ only in the precision and range that they provide. A float generally has 32 bits and a double 64. long double might be bigger, but it could just be the same as double.
Modifiers are used to restrict/enhance the data type capacity to hold the value, This will conserve the system resource such as memory, Modifiers are broadly classified into
Data Type Modifiers
Sign Data Type Modifiers
Sign modifiers can be classified into
signed example: <source lang=c> signed int a=10; </source>
unsigned example: <source lang=c> unsigned int b = 1000; </source>
Unsigned integers can only take non-negative values (positive or zero), while signed integers (default) can take both positive and negative values. The advantage of unsigned integers is to allow a greater range of positive values (e.g. 0 to +65535 for a 16-bit integer), whereas signed integers allow only up to half the same number as positive integers and the other half as negative integers (e.g. −32768 to +32767 for a 16-bit integer).(source :http://en./script/pedia.org//script//C_variable_types_and_declarations )
Size Data Type Modifiers
Size modifiers can be classified into
long example: <source lang=c> long int a = 30000; </source> This variable int a can hold a value anything between −2147483647 to +2147483647
short example: <source lang=c> short int b=104; </source>
this variable short int a can hold a value anything between -127 to 126
(Tabulation of various data types and modifiers are shown in /script/pedia, that needs to be incorporated).
Variable Type Modifiers
extern is used when a file needs to access a variable in another file that it may not have #included directly. Therefore, extern does not actually carve out space for a new variable, it just provides the compiler with sufficient information to access the remote variable.
volatile is a special type modifier which informs the compiler that the value of the variable may be changed by external entities other than the program itself. This is necessary for certain programs compiled with optimizations – if a variable were not defined volatile then the compiler may assume that certain operations involving the variable are safe to optimize away when in fact they aren't. volatile is particularly relevant when working with embedded systems (where a program may not have complete control of a variable) and multi-threaded applications.
auto is a modifier which specifies an "automatic" variable that is automatically created when in scope and destroyed when out of scope. If you think this sounds like pretty much what you've been doing all along when you declare a variable, you're right: all declared items within a block are implicitly "automatic". For this reason, the auto keyword is more like the answer to a trivia question than a useful modifier, and there are lots of very competent programmers that are unaware of its existence.
register is a hint to the compiler to attempt to optimize the storage of the given variable by storing it in a register of the computer's CPU when the program is run. Most optimizing compilers do this anyway, so use of this keyword is often unnecessary. In fact, ANSI C states that a compiler can ignore this keyword if it so desires – and many do.
a static variable is a variable that has been allocated statically — whose lifetime extends across the entire run of the program. This is in contrast to the more ephemeral automatic variables (local variables), whose storage is allocated and deallocated on the call stack; and in contrast to objects whose storage is dynamically allocated.
Operators in C
||Pre/Post increment|| The following is the example of pre-increment operator |
||pre/post decrement|| The following is the example of pre-decrement operator |
||Unary plus|| #include <stdio.h> |
||Unary minus|| #include <stdio.h> |
||Multiplication|| int a_variable=10; |
int b_variable= a_variable * 10;
||Division|| int a_variable=25; |
int b_variable= a_variable / 5;
||modulus (remainder)|| |
||Assignment by sum|
||Assignment by difference|
||Assignment by product|
||Assignment by quotient|
||Assignment by remainder|