simple question on input

Dear CUDA People:

I have a set of code from the “Cuda by example” book:


  • Copyright 1993-2010 NVIDIA Corporation. All rights reserved.

  • NVIDIA Corporation and its licensors retain all intellectual property and

  • proprietary rights in and to this software and related documentation.

  • Any use, reproduction, disclosure, or distribution of this software

  • and related documentation without an express license agreement from

  • NVIDIA Corporation is strictly prohibited.

  • Please refer to the applicable NVIDIA end user license agreement (EULA)

  • associated with this source code for terms and conditions that govern

  • your use of this NVIDIA software.


#include “…/common/book.h”

global void add( int a, int b, int *c ) {

*c = a + b;


int main( void ) {

int c;

int *dev_c;

HANDLE_ERROR( cudaMalloc( (void**)&dev_c, sizeof(int) ) );

add<<<1,1>>>( 2, 7, dev_c );

HANDLE_ERROR( cudaMemcpy( &c, dev_c, sizeof(int),

                          cudaMemcpyDeviceToHost ) );

printf( "2 + 7 = %d\n", c );

HANDLE_ERROR( cudaFree( dev_c ) );

return 0;


which works fine.

My question is: what if I wanted to input my own two numbers from the command line, please? How would I go about doing that, please?



Well, this is a question that has few connection with cuda… Nonetheless, here is the common way of doing with very limited error checking (need a lot of improvement for being more robust):

#include <stdlib.h>

#include <stdio.h>

int main(int argc, char *argv[]) {

    if (argc != 3) {

        fprintf(stderr,"%s: Error: 2 parameters expected. Found %d\n", argv[0], argc);

        return 1;


    /* Warning: no proper error detection is made here */

    int a = atoi(argv[1]);

    int b = atoi(argv[2]);

See this for more explanations about argc ans argv.

This is perfect…just what I needed.

thanks so much!