Posts Tagged ‘C’
Setup and Use GCC on Windows in 5 Easy Steps
Do you want to be able to compile C, C++, Ada and Fortran programs on Windows without losing time wrestling with a bulk, resource-intensive IDE? You can! The solution is to port the ultra-popular, lightweight GCC (GNU Compiler Collection) often used in *nix systems to Windows via MinGW + Cygwin.
MinGW is the minimalist GNU implementation for Windows.
1. Install Cygwin, which provides many Linux commands/libraries to Windows
3. Make sure that MinGW was added to your PATH by typing PATH in a terminal
If not, you can add it through Window’s GUI interface (just type PATH or Environment in Start on Win 7 or 8 and it will come right up as a search result).
4. In a terminal (cmd or PowerShell) go to the directory containing the code you want to compile, i.e.
5. Type the compile command:
g++ hackr.c -o hackr.exe
where -o is indicating your output file.
This also works with compiling multiple files:
g++ hackr.c econometrics.c hadoop.c overflow.c -o hackeconometrics.exe
This should take at least an hour or 2 less time than installing Microsoft’s Visual Studio (though the latter has it’s virtues) or a similar bulky IDE and will give you fewer headaches by avoiding complicating your life with a million unnecessary options and confusing attempts to “help” you build a simple project.

Software Sec: C / C++ Buffer overflows and Robert Morris
- over-read or over-write
- could be during iteration (running off the end), or direct access (pointer arithmetic)
- this is a general definition; some people use more specific definitions of differing types of buffer overflows
A buffer overflow is a bug that affects low-level code, typically C and C++ with significant sec implications
- dump/steal information
- corrupt information
- run code (payload)
- OS Kernels
- embedded systems
- HPC servers
C: Program Structure
A programmer uses comments to document the code. Text that has been “commented” is ignored by the compiler. The largest contributor of the total cost of software is code maintenance, not program development. Adequately commented code can drastically reduce the lifecycle cost of a program. Often, even the programmer who wrote the code forgets details when editing code several months later.
C supports two different formats of comments. The first type starts with a slash asterisk and ends with an asterisk slash; everything in between is ignored by the compiler. There are many implementation of this format used by programmers. A typical program will have a header comment to provide details regarding the entire program as well as comments inserted throughout the program to clarify the programmer’s intent. This is the hello program with comments added.
/******************************************* * Filename: hello.c * Programmer: Bob Adams * Date: June 8, 1998 * * Description: * Displays a hello message on screen to user ********************************************/ /* preprocessor directive */ #include <stdio.c> int main(void) { printf("Hello"); /* display hello on screen */ return 0; /* indicates success to OS */ }
OUTPUT
Hello
The newer C99 standard implemented the C++ syntax for comments using the double slash //. Everything after the double slash until the end of the line is considered a comment and ignored by the compiler. Below is the program using the newer style of comments. Both styles can be used, sometimes one style is more clear than the other within the code.
//************************************************ // Filename: hello.c // Programmer: Bob Adams // Date: June 8, 1998 // // Description: // Displays a hello message on screen to user //************************************************ // preprocessor directive #include <stdio.c> int main(void) { printf("Hello"); // display hello on screen return 0; // indicates success to OS }
OUTPUT
Hello Source: C Primer Plus, Berkeley C programming class notes
C: 3 basic steps in using C compilers
- Creating a project (or makefile) to identify the directories and files to be compiled
- Creating a new C program or adding an existing C program to the project
- Compiling, linking and executing your program
C: use getch() or a breakpoint to view output
#include <stdio.h> int main(void) { printf("Hello"); return 0; }
OUTPUT
Hello
Save the program with a filename of “hello.c”. The program then needs to be compiled, linked and executed. Many compilers allow you to do this in one step but some require a multi-step process. Depending upon the compiler used, you may or may not see a display window with the message “Hello” appear and then disappear. There are several solutions to the disappearing window. The first solution is to insert a command to stop the program before it exits, which would keep the display window open. The two most popular methods are the following:
Option 1: Insert a getch() statement.
#include <stdio.h> int main( void ) { printf("Hello"); getch(); return 0; }
OUTPUT
Hello
Option 2: Insert a system(“pause”) statement
#include <stdio.h> int main( void ) { printf("Hello"); system("pause"); return 0; }
OUTPUT
Hello
C: C v. C++, Object Orientation
C++ graphs object oriented programming* tools to the C language
C: Virtues and Shortcoming of C
Virtues
- Powerful control structures
- Fast (and efficient, like an assembly language)
- Compact code (small programs)
- Portable (moreso than other languages)
Shortcomings
- Use of pointers — errors are hard to trace
- Can be difficult to follow
C: Dennis Ritchie, history of C
C was created by Dennis Ritchie of Bell Labs in 1972, while Ritchie worked with Ken Thompson on designing Unix.
C: When to use C v. C++
You pick C when
In all other cases you should pick C++.
Source: |