NOS-125 - C Programming Quiz Review

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

How would you write the classic "Hello, World!" program in C?

#include <stdio.h> /* hello.c */ int main() { printf("Hello, world!\n"); return 0; }

What is Logstalgia and why might you want to use it?

. Logstalgia (also known as Apache Pong) is a website traffic visualization tool. The program reads the web server access.log and generates a graphic in an Atari pong-like game between the webserver and the specific HTTP page request. Requests appear as colored balls which travel across the screen to arrive at the requested file location. Any "Page not found" (404 errors) responses are marked by the ball slipping past the pong paddle. 1. Logstalgia is easily installable from source code.

What is the purpose of the configure command?

. The "./configure" script is used to configure the source code automatically. The primary job of the configure script is to detect information about the local system and configure the source code to work with it. This checks that you have the correct compilation components and libraries installed. Run the ./configure command now.

What are the steps to compile software from source code starting with downloading the source code itself?

??? Use wget to download the source code file. Untar the archive. Change into the directory Run the configure command Compile the source code with the make command. Install=sudo make install

What is the purpose of the stdio.h header file in a C program?

This C program starts with #include <stdio.h>. This line includes the "standard I/O library" into the program. The standard I/O library is used to read input from the keyboard (called "standard in"), write output to the screen(called "standard out"), process text files stored on the disk, and so on. It is an extremely useful library. C has a large number of standard libraries like stdio.

What are the steps in the compilation process?

To compiler the code, type, "gcc hello.c -o hello". This asks the gcc to compile hello.c and asks it to place the executable binary file it creates under the name "hello". The -o option is used to specify the name of the output file. The command compiles the hello.c file and stores the executable code in a file named hello. After successful compilation the C program is compiled and linked with the appropriate libraries. To execute it, run the program with a leading "./" which is essential.

How do you compile a program that needs to use an advanced math library?

you use the C numeric library functions by linking to math library to compute password entropy. When including math functions in a program, you must add the "-lm" option to the end of the gcc command line to force the linker to include the math library. Otherwise, the program will not compile. Recompile again including the option to link to the math (-lm) library functions. student@kali:~$ gcc entropy.c -o entropy -lm

What is an object file? A binary file? How do the two differ?

A compiler takes source code and translates it into an intermediate set of files called object code. These files are nearly ready to execute but may contain unresolved references to symbols and functions not included in the original source code file. These symbols and references are resolved through a process called linking, as each object file is linked together into an executable binary file A binary file is a type of file that contains data in a format that is not intended to be human-readable. It stores data in binary/machine readable/computer language. Files that can be digested by the computer and executed. To compile the code, type "gcc hello.c -o hello". This line invokes the C compiler called "gcc," asks it to compile hello.c and asks it to place the executable binary file it creates under the name "hello."

What is a compiler? What is the most common compiler in Linux?

A special program called a compiler is used to translate human readable text instruction into computer readable format that correspond to the same instructions. The C compiler used almost universally in the Linux environment is called gcc (GNU C Compiler), originally written by Richard Stallman. Compiling is the process of turning human-readable source code into machine-readable binary files that can be digested by the computer and executed. More specifically, a compiler takes source code and translates it into an intermediate set of files called object code.

What are the different data types in C? Approximately how much memory do they occupy when created?

Common data types in C: char (memory in bytes: 1 = %c), int (4 = %d), float (4 = %f), character strings (%s) Data type determines the size and layout of a variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. Each variable must have a specific data type.

How do you compile a program consisting of multiple source files in C?

Compile and link the two source code files together. The compiler separately compiles pv_main.c and pv_func.c. The object files are linked together, and the executable code is stored in the file pv_program. student@kali:~$ gcc pv_main.c pv_func.c -o pv_program

Who created the C programming language?

Developed at Bells Labs by Brian Kernighan and Dennis Ritchie between 1972 and 1973.

The make clean command?

Finally, most source code installations provide the option to clean up after themselves. Large programs often leave a substantial number of intermediate files lying about in their source directories, which can consume a significant amount of disk space. To make sure that these are cleaned up, or to clean up after a failed build of the program, try using the command "make clean." student@lubuntu:~/thc-hydra$ make clean

What are the different types of selection statements in C? What is the proper syntax for each?

If-else Selection Statement: if (condition) { Code to execute if condition is true }else{ Code to execute is false }

The make install command?

Now that the software is built and ready to run, the files can be copied to their final destinations. The "make install" command will copy the built program, and its libraries and documentation, to the correct locations. Usually, this directory is /usr/local/bin, the traditional location for locally built software. Some distributions prefer to use the /opt ("optional") software directory. Run "sudo make install" now

The make command?

Once configure script has done its job, the application is formerly built with the "make" command. This runs a series of tasks defined in a "Makefile" to build the finished program from its source code. Source code tarballs usually doesn't include a finished Makefile. Instead it comes with a template called "Makefile.in" and the configure script produces a customized Makefile specific to the system. Run the make command now This is the actual compilation process occurring. This step may take some time, depending on how big the program is and the speed of the computer. If all goes as it should, the binary executable is finished and ready to run after make has done its job. Now, the final step is to install the program

What is OpenGL and why would you want to use it?

The Open Graphics Library (OpenGL) graphics library is a cross language (programming language independent), cross-platform (platform independent) set of functions for rendering 2D and 3D graphics(using of polygons to represent image). OpenGL models an object via a set of geometric primitives such as points, lines and polygons (See elephant images above). The OpenGL Utilities Toolkit (GLUT) is an OpenGL extension is designed to be independent of the windowing system or operating system. GLUT is needed to interact with the operating system (such as creating a window, handling key and mouse inputs). It also provides more building models (such as sphere and torus). GLUT commands start with a prefix of "glut" (e.g., glutCreatewindow, glutMouseFunc).

What is the importance of the main function in a C program?

The line int main() declares the "main" function. Every C program must havea function named main somewhere in the code. At run time, program execution starts at the first line of the main function.

How do you print the values of variables in C?

The printf function is used to print the value of the variable. It uses the same format specifiers as scanf as well. Address operator however is not used when printing the value of the variable. Example: printf("Scanning port: %d\n", port_number);

What is the purpose of a return value in a C program? What does "return 0;" mean?

The return 0; line causes the function to return an error code of 0 (no error) to the shell that started execution. This line is optional as any successful program execution will return this value to the shell upon successful program completion.

What are the steps to compile a program from source code?

When you obtain the source code file(s) you compile them to generate an executable binary that's specific to the OS and hardware platform. Practically all source code in Linux comes in a compressed archive file known as "tarball" (Because it was created with the "tar" command) and have the suffix ".tgz" or ".tar.gz.". In Linux, this will also have a name and a version number, something like "source_code-1.2.3.tar.gz." The next step is to unpack the downloaded source code archive. Depending on the type of archive downloaded, the command for unpacking it may differ slightly. The tar (tape archiver) command is used for working with file archives. Typical command switches are "x" to extract files, "v" for verbose mode so you can tell what's going on, "f" indicating there will be a filename to follow. If the file name ends in ".tar" that is all you need, but usually the tar file has been compressed with another utility. Files ending in ".gz" w student@lubuntu:~$ tar zxvf thc-hydra.tar.gz Once unpacked, the next step is to change into the newly created source code directory. For example, if the archive name was source_code-1.2.3.tar.gz, change into the directory named source_code-1.2.3. ls -ld thc-hydra-9.5 Create a text file with the .c extension and write your C program in it Open terminal/command prompt Navigate to directory where source code is Compile it! gcc hello.c -o hello Run the executable

What are the different types of looping statements in C? What is the proper syntax for each?

While, do-while, and for In programming, an infinite loop is a sequence of code which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over (CTRL +C) For: int main() { for(initialization; condition; increment) { code } return 0; } While: while(condition){ code } Do-While: do { code to execute } while (condition);

How do you obtain user input in a C program? What is the significance of a format specifier, like %d? What role does the "&" play in obtaining user input? What is likely to happen if the & is left off?

You use the scanf function to accept user input. The scanf function requires a format specifier to indicate the type of data to be enter (e.g., character, integer, float, string, etc. The %d is the int data type) and the address of a variable in memory used to store the value of the variable using the address ("&") operator. You must put the & in front of the variable used in scanf. When you forget it the program will almost always crash when you run it. Forgetting to prepend the ampersand to the variable name with scanf is a common programming error in C which results in a "Segmentation fault" message upon program execution. Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that does not belong to the program.

What is the general command format for compiling a program with gcc?

gcc source_file.c -o output_file.c

What is the purpose of a Makefile? What is its relationship to the make command?

you use the "make" command and a "Makefile" to create a multi-source code C project. The make" program was designed as a system for making compiled code. The make command automatically determines which pieces of a large program need to be compiled, and issues commands to compile them. A "Makefile" describes the relationships among files and provides compile commands. Executable(s) are updated from object files, which are compiled from sources files. The make command is a build automation tool that automatically builds executable programs and libraries from source code by reading files called "Makefiles." A Makefile consists of rules defined by targets, dependencies, and commands. Each rule begins with a target, followed by a colon and its dependencies, and commands to build the target are placed on the lines below, prefixed with a TAB character. The standard convention for the name of a Makefile in most projects is simply "Makefile".


Kaugnay na mga set ng pag-aaral

Language Learning Materials Development

View Set

Consideration and Promissory Estoppel

View Set

International Marketing Test 1 (Ch. 1-7)

View Set

Synonyms & Antonyms [ Match, Write]

View Set

Physical Geography URSP 204 Study Set

View Set