printf and scanf??

Pages: 12
Is that using namespace std; together with #include <iostream> can be use for printf and scanf as well? Or need #include<stdio> ? Thanks...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
	int a,b,c;


   cin>>a;
   cin>>b;
   c=a+b;
   cout<<"The value c is \n"<<c<<endl;

   _getch();
   return 0;
}
printf() & scanf are part of the "cstdio" header, the "iostream" header is used for std::cin and std::cout. If you do not want to prefix "std::" everytime then you would declare that you are using the std namespace in the global scope.

REFERENCE: http://www.cplusplus.com/reference/clibrary/cstdio/
http://www.cplusplus.com/reference/iostream/
Thanks...I tried this code in Visual Studio 2010 C++ but it cannot run, may I know which part I did wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
	int a,b,c;


   scanf("%u",a);
   scanf("%u",b);
   c=a+b;
   printf("The value c is \n", c);

   _getch();
   return 0;
}
"scanf()" takes chars as arguments to you have to pass in numbers by reference like in the documentation here: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

Also you forgot the "%u" in your "printf()" function.
Thanks~~
Problem solve, but I still not understand about header for printf and scanf because as u said above, the header for them is stdio.h...but then I tried to use only iostream, it works too, like below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	int a,b,c;


   scanf("%u",&a);
   scanf("%u",&b);
   c=a+b;
   printf("The value c is %u\n", c);

   _getch();
   return 0;
}


another question is, i need to put &a then only it can function, may i know what is reference means? why not i take the value of "a" only but need to refer it? TQ...
I saw many ppl said "arguments", wat's that means actually? TQ...
An argument is a piece of data you pass to a function.
Ok...thanks...can u explain about the condition above>?

Problem solve, but I still not understand about header for printf and scanf because as u said above, the header for them is stdio.h...but then I tried to use only iostream, it works too, like below...
As far as the headers go, the non-standard header conio.h is likely to bring in the stuff from stdio.h.

While some headers may include other headers, it's bad practice to depend on that happening and not include the headers you need. If you were to compile with another compiler, or even a different version of the compiler you're using, there are no guarantees the code would still work without modification.

You're using the wrong format strings for the code you wrote above. It should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
     int a,b,c;


     scanf("%d",&a);
     scanf("%d",&b);
     c=a+b;
     printf("The value c is %d\n", c);

     return 0;
}


%u would be used for a variable of type unsigned.

atjm88 wrote:
another question is, i need to put &a then only it can function, may i know what is reference means? why not i take the value of "a" only but need to refer it? TQ...


&a can be read "address of a." It evaluates to a pointer of type int* with the address of a as it's value. In this context it has nothing to do with references.

Last edited on
Thanks cire...

1. Is #include <cstdio> same as #include <stdio.h> ?
2. Why many of u here do not use getch () inside program? getch () use to stop the display screen, so that we can see the answer of "c" right? Or ur compiler will not exit once "c" is appear? (I'm using Borland C++ and Ms Visual Studio 2010 C++)...for Borland & Visual, if no getch (), it will exit once "c" is appear...
3. %d is what?
4.
&a can be read "address of a." It evaluates to a pointer of type int* with the address of a as it's value. In this context it has nothing to do with references.
Not really understand about this...
1. Is #include <cstdio> same as #include <stdio.h> ?


No. Including stdio.h pollutes the global namespace. Including cstdio keeps stuff in the std::namespace that belongs there.

2. Why many of u here do not use getch () inside program? getch () use to stop the display screen, so that we can see the answer of "c" right? Or ur compiler will not exit once "c" is appear? (I'm using Borland C++ and Ms Visual Studio 2010 C++)...for Borland & Visual, if no getch (), it will exit once "c" is appear...


In VSC++ 2010, if you run the program from the debug menu with "Start Without Debugging" it will keep the window open for you. That menu option isn't available by default unless you go into the tools menu, and set your Settings to Expert Settings. For Borland, I don't know if there's anyway to keep it open.

%d is what?


If you search the site for scanf, I'm sure you'll figure it out.

Thanks...
I get to know from website that Start Debugging means that we can check through every single line inside our program what's the output of each right? And for the Start without Debugging, the program will just give us the final result is it? So may I know how to use that Start Debugging to check my program error STEP by STEP?

I cannot find that Expert Settings in VSC++ 2010 u mean...instead go to Customize to add that button "Start Without Debugging", is that the way too? TQ...
VS 2010 already has "start without debugging" in menu, without making any setting.
The function's printf and scanf are from "C" and not from "C++". Because "C" code works in "C++" they can be used. Here is a little program that print out the size of different variables in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
console mode. You can add scanf if you wand to convert a variable to a string. Good luck.

#include <stdio.h>
#include <conio.h>

void main()
{
char      a;
short int b;
int       c;
long      d;
float     e;
double    f;
char     *g;
char      h[20];
char      i[20];

   // Show the size of variable on current computer
    printf("Char      = %d bytes\r\n", sizeof(a));
    printf("Short Int = %d bytes\r\n", sizeof(b));
    printf("Int       = %d bytes\r\n", sizeof(c));
    printf("Long      = %d bytes\r\n", sizeof(d));
    printf("Float     = %d bytes\r\n", sizeof(e));
    printf("Double    = %d bytes\r\n", sizeof(f));
    printf("Pointer   = %d bytes\r\n", sizeof(g));
    printf("Array     = %d bytes\r\n", sizeof(h));
    printf("Address   = %p\r\n", sizeof(i));
   // Quit on keypress
    getch();
} 
Forgot to say... If you are dealing with microcontrollers and not PC's use "C". "C++" will only make your life miserable in controller world. The good thing is you can always test your code in console mode on a PC. If You are in PC world, learn to program in Win32 GUI. That's where you make all the windows and all that stuff.
Good luck.
Perman.
Here is a little simple program written in Win32 GUI. It create a window wit a button that can be clicked and will bring up a text string on the window. I used the strcpy function to copy a string so I see no reason why you can't use scanf too. Printf is only for console windows. Check it out an create you own stuff with it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <windows.h>
#include <string.h>

// Make global definitions

UINT Timer_ID;
HWND hWnd;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

#define ID_BUTTON   102

static int   iXpos;
static int   iYpos;
       char  szMessage[100];

static char gszClassName[]  = "My Program";
static HINSTANCE ghInstance = NULL;

/*****************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

        WNDCLASSEX WndClass;
        MSG Msg;

        ghInstance = hInstance;

        WndClass.cbSize        = sizeof(WNDCLASSEX);
        WndClass.style         = NULL;
        WndClass.lpfnWndProc   = WndProc;
        WndClass.cbClsExtra    = 0;
        WndClass.cbWndExtra    = 0;
        WndClass.hInstance     = ghInstance;
        WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        WndClass.lpszMenuName  = NULL;
        WndClass.lpszClassName = gszClassName;
        WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

        if(!RegisterClassEx(&WndClass)) {
                MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
                return 0;
        }

        hWnd = CreateWindowEx(
                WS_EX_STATICEDGE,
                gszClassName,
                " My Program",
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, CW_USEDEFAULT,
                320, 240,
                NULL, NULL,
                ghInstance,
                NULL);


        if(hWnd == NULL) {
                MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);
                return 0;
        }

        strcpy(szMessage, "Welcome to My Window");
        iXpos = 70;
        iYpos = 10;
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);

        while(GetMessage(&Msg, NULL, 0, 0)) {
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
        }
        return Msg.wParam;
}
/*****************************************************************************/
class WriteText
{
public:

    HDC hdc;
    PAINTSTRUCT ppaint;

    WriteText(int _Xpos, int _Ypos, char *_szMessage)
    {
        hdc = BeginPaint(hWnd, &ppaint);
        // Here you can set the color of the string
        //SetTextColor(hdc, RGB(0,0,0));
        TextOut(hdc, _Xpos, _Ypos, _szMessage, strlen(_szMessage));
        EndPaint(hWnd, &ppaint);
    }
};

/*****************************************************************************/
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {

        HWND hWndButton;
        switch(Message) {
                case WM_PAINT:
                        WriteText(iXpos, iYpos, szMessage);
                        break;

        		case WM_TIMER:
                        break;

                case WM_CLOSE:
                        DestroyWindow(hWnd);
                        break;

                case WM_DESTROY:
                        PostQuitMessage(0);
                        break;

                case WM_CREATE:{
                        hWndButton = CreateWindowEx(0,          /* more or ''extended'' styles */
                        TEXT("BUTTON"),                         /* GUI ''class'' to create */
                        TEXT("Run "),                           /* GUI caption */
                        WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,   /* control styles separated by | */
                        260,                                    /* LEFT POSITION (Position from left) */
                        185,                                    /* TOP POSITION  (Position from Top) */
                        47,                                     /* WIDTH OF CONTROL */
                        25,                                     /* HEIGHT OF CONTROL */
                        hWnd,                                   /* Parent window handle */
                        (HMENU)ID_BUTTON,                       /* control''s ID for WM_COMMAND */
                        ghInstance,                             /* application instance */
                        NULL);
                        break;
                }

                case WM_COMMAND:{
                        if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)){
                            int iMID;
                            iMID = LOWORD(wParam);
                            switch(iMID){
                               // Button clicked
                                case ID_BUTTON:{
                                    // Notice this is a "C" call in string .h
                                    strcpy(szMessage, "*** This is my program ***");
                                    //wsprintf(szMessage."*** This is my program ***");
                                    iXpos = 10;
                                    iYpos = 40;
                                    // Write string in window by sending message WM_PAINT
                                    InvalidateRect(hWnd, NULL, NULL);
                                    break;
                                }
                                default:
                                break;
                            }
                        }
                        break;
                }
                default:
                return DefWindowProc(hWnd, Message, wParam, lParam);
        }
        return 0;
}
Last edited on
Hi, Perman, actually I'm going to do about Image Processing on car plate recognition system stuff using VSC++ and openCV, is that ok?

If that is fine, which one have to use, MFC, Win32, CLR and so on...Coz inside VSC++, there are a lot of this things but I don't know which one to use, quite confuse, can u give some idea? TQ...
Forgot to ask, for the above program(long one), why I can't run it...Im using VSC++ in Win32, dun noe why...
Good question. Code is written in Borland C++ IDE ver 5.03 and should work fine under MSVC too. Did you open a new project and copied and past the file in as the main file? Does it generate errors or what is going on? The code is the simplest you can make in Win32 GUI programming. Make sure your project is set up to Win32 GUI application. One window and one button. Could not be much simpler. Let me know what happen. This is a good start file.
You can change the strcpy() "C" function to wsprinf(). Then you can take out include file <string.h>
Last edited on
atjm88.

To answer your question about which one to use I can't tell you. I'm I have been working in Win32 GUI and don't know about the other stuff. Latest I'm going to start to work on Qt workframe which is a overhead for C++. It should make life easier to create a GUI. We will see. Anyhow I'm downloading Visual Studio 10 Express so I can figure out why the program won't run under that compiler. I will keep you posted.
Per.
Pages: 12