Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Commonly Asked C/c++ Questions

Recommended Posts

Commonly Asked C/C++ Questions


--------------------------------------------------------------------------------


Here are some answers to commonly asked C/C++ questions on the forum. If you are new to the forum, please read this thread as your question may already be answered here. This guide is divided into many sections:

0. Frequently Asked Newbie Questions (how do I pause output, compile my program etc.)

1. Converting types

2. Formatting Output

3. String manipulation (copy, concatenate, replace etc.)

4. File Manipulation (File size, dir contents, remove files etc.)

5. Secure Programming Practices for Newbies.


This guide is a work in progress, so feel free to PM me with your suggestions

In general, people on this forum generally use the following:

On Unix-like OS (Linux, FreeBSD, OpenBSD, NetBSD, Darwin etc.):

gcc

tendra (this is a relatively unknown compiler though ).


On Windows:

Bloodshed Dev C++ (which uses gcc as the backend)

Visual C++

Borland C++ and Borland C++ Builder



0.2 How do I compile my program?

On unix-like systems:

gcc -o exename myfile.c

This will compile myfile.c to produce an executable file called exename. You can then run exename by typing ./exename


On Windows systems, different compilers have different keys to compile a program.

Bloodshed/Dev C++: Ctrl-F9 compiles, Ctrl-F10 executes.

Visual C++: F7 compiles, F5 executes.

Borland C++ Builder: Ctrl-F9 compiles, F9 executes.

Turbo-C: F9 compiles, Ctrl-F9 executes.



0.3 I cannot see my C compiler output. The window is closing too quickly for me.

This is a common question that many newbie programmers on Windows have. See http://forums.xisto.com/no_longer_exists/ for many solutions.


0.4 What are some handy C/C++ links?

http://c-faq.com/

http://forums.xisto.com/no_longer_exists/


0.5 Why does my scanf/fscanf/sscanf stop working?

Most C input is provided in a stream. That is, it is a series of characters made available one at a time. The scanf() function family are format-sensitive functions; they not only collect the characters for you, but attempt to convert them to a type (such as an integer) that you specify. They have great difficulty converting ZyGH4 to a meaningful number so they fail. The conversion attempt is governed by format specifiers that YOU provide. Since these may not match the input actually encountered, the family returns a value indicating the number of items successfully scanned AND assigned. If this value is zero, you have nothing. If this value is EOF, there was an end-of-file or other error. If you don't examine the return, how will you know? If an error occurs it will not be automatically cleared. Operations on the stream will continue to return an error until clearerr(), fseek(), fsetpos(), or rewind() is called. This means that a loop that is designed to pause for input will loop indefinitely.


The characters that f/s/scanf attempt to convert as one value are all the characters up to the first whitespace character (space, tab, newline) or up to the specified field width, or up to the first character that cannot be converted. (Note: The [ and c format directives are not whitespace delimited, but we won't consider them for the explanation here). If a character conflicts with the format specification, the function terminates and the character is left in the stream as if it had not been read. You probably will not expect it to be there to serve as input for your next call, so your input will not behave as you expect.


Example of proper usage:

int status;

status = scanf ("%s%d\n", name, &number);


Check the value of 'status' after the scanf() call. If it is not what you expect (two, in this case), you didn't get all your fields. If it is EOF, your stream is broken and will remain so until you clear the error.


0.6 Why does getline() not work correctly with Visual C++ (VC++)? Why do I have to type Enter twice for getline to process my input line?

If you're reading this, you've probably noticed that getline() is not functioning the way your book says it should -- you need to hit <enter> twice for the program to read your input. For example:


Code:

#include <iostream>

#include <string>


using namespace std;


int main()

{

string name;

cout<<"Enter a name:\n";

getline(cin, name);

cout<<name<<endl;


return 0;

}

.................next day

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.